org.el 959 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2016 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. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  85. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  86. ;;
  87. ;; In Org buffers, the value of `outline-regexp' is that of
  88. ;; `org-outline-regexp'. The only function still directly relying on
  89. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  90. ;; job when `orgstruct-mode' is active.
  91. (defvar org-outline-regexp "\\*+ "
  92. "Regexp to match Org headlines.")
  93. (defvar org-outline-regexp-bol "^\\*+ "
  94. "Regexp to match Org headlines.
  95. This is similar to `org-outline-regexp' but additionally makes
  96. sure that we are at the beginning of the line.")
  97. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  98. "Matches a headline, putting stars and text into groups.
  99. Stars are put in group 1 and the trimmed body in group 2.")
  100. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  101. (unless (boundp 'calendar-view-holidays-initially-flag)
  102. (org-defvaralias 'calendar-view-holidays-initially-flag
  103. 'view-calendar-holidays-initially))
  104. (unless (boundp 'calendar-view-diary-initially-flag)
  105. (org-defvaralias 'calendar-view-diary-initially-flag
  106. 'view-diary-entries-initially))
  107. (unless (boundp 'diary-fancy-buffer)
  108. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  109. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  112. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  113. (declare-function org-agenda-redo "org-agenda" (&optional all))
  114. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  115. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  116. (declare-function org-beamer-mode "ox-beamer" ())
  117. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  118. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  119. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  120. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  121. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  122. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  123. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  124. (declare-function org-clock-update-time-maybe "org-clock" ())
  125. (declare-function org-clocktable-shift "org-clock" (dir n))
  126. (declare-function org-element-at-point "org-element" ())
  127. (declare-function org-element-cache-refresh "org-element" (pos))
  128. (declare-function org-element-cache-reset "org-element" (&optional all))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-context "org-element" (&optional element))
  131. (declare-function org-element-copy "org-element" (datum))
  132. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  133. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  134. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  135. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element" (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element-type "org-element" (element))
  140. (declare-function org-element-update-syntax "org-element" ())
  141. (declare-function org-id-find-id-file "org-id" (id))
  142. (declare-function org-id-get-create "org-id" (&optional force))
  143. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  144. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  145. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  146. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  147. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  148. (declare-function org-table-align "org-table" ())
  149. (declare-function org-table-begin "org-table" (&optional table-type))
  150. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  151. (declare-function org-table-blank-field "org-table" ())
  152. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  153. (declare-function org-table-edit-field "org-table" (arg))
  154. (declare-function org-table-end "org-table" (&optional table-type))
  155. (declare-function org-table-end-of-field "org-table" (&optional n))
  156. (declare-function org-table-insert-row "org-table" (&optional arg))
  157. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  158. (declare-function org-table-maybe-eval-formula "org-table" ())
  159. (declare-function org-table-maybe-recalculate-line "org-table" ())
  160. (declare-function org-table-next-row "org-table" ())
  161. (declare-function org-table-paste-rectangle "org-table" ())
  162. (declare-function org-table-wrap-region "org-table" (arg))
  163. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  164. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  165. (declare-function orgtbl-mode "org-table" (&optional arg))
  166. (defsubst org-uniquify (list)
  167. "Non-destructively remove duplicate elements from LIST."
  168. (let ((res (copy-sequence list))) (delete-dups res)))
  169. (defsubst org-get-at-bol (property)
  170. "Get text property PROPERTY at the beginning of line."
  171. (get-text-property (point-at-bol) property))
  172. (defsubst org-trim (s)
  173. "Remove whitespace at the beginning and the end of string S."
  174. (replace-regexp-in-string
  175. "\\`[ \t\n\r]+" ""
  176. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  177. ;; load languages based on value of `org-babel-load-languages'
  178. (defvar org-babel-load-languages)
  179. ;;;###autoload
  180. (defun org-babel-do-load-languages (sym value)
  181. "Load the languages defined in `org-babel-load-languages'."
  182. (set-default sym value)
  183. (mapc (lambda (pair)
  184. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  185. (if active
  186. (progn
  187. (require (intern (concat "ob-" lang))))
  188. (progn
  189. (funcall 'fmakunbound
  190. (intern (concat "org-babel-execute:" lang)))
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-expand-body:" lang)))))))
  193. org-babel-load-languages))
  194. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  195. ;;;###autoload
  196. (defun org-babel-load-file (file &optional compile)
  197. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  198. This function exports the source code using `org-babel-tangle'
  199. and then loads the resulting file using `load-file'. With prefix
  200. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  201. file to byte-code before it is loaded."
  202. (interactive "fFile to load: \nP")
  203. (let* ((age (lambda (file)
  204. (float-time
  205. (time-subtract (current-time)
  206. (nth 5 (or (file-attributes (file-truename file))
  207. (file-attributes file)))))))
  208. (base-name (file-name-sans-extension file))
  209. (exported-file (concat base-name ".el")))
  210. ;; tangle if the org-mode file is newer than the elisp file
  211. (unless (and (file-exists-p exported-file)
  212. (> (funcall age file) (funcall age exported-file)))
  213. ;; Tangle-file traversal returns reversed list of tangled files
  214. ;; and we want to evaluate the first target.
  215. (setq exported-file
  216. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  217. (message "%s %s"
  218. (if compile
  219. (progn (byte-compile-file exported-file 'load)
  220. "Compiled and loaded")
  221. (progn (load-file exported-file) "Loaded"))
  222. exported-file)))
  223. (defcustom org-babel-load-languages '((emacs-lisp . t))
  224. "Languages which can be evaluated in Org-mode buffers.
  225. This list can be used to load support for any of the languages
  226. below, note that each language will depend on a different set of
  227. system executables and/or Emacs modes. When a language is
  228. \"loaded\", then code blocks in that language can be evaluated
  229. with `org-babel-execute-src-block' bound by default to C-c
  230. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  231. be set to remove code block evaluation from the C-c C-c
  232. keybinding. By default only Emacs Lisp (which has no
  233. requirements) is loaded."
  234. :group 'org-babel
  235. :set 'org-babel-do-load-languages
  236. :version "24.1"
  237. :type '(alist :tag "Babel Languages"
  238. :key-type
  239. (choice
  240. (const :tag "Awk" awk)
  241. (const :tag "C" C)
  242. (const :tag "R" R)
  243. (const :tag "Asymptote" asymptote)
  244. (const :tag "Calc" calc)
  245. (const :tag "Clojure" clojure)
  246. (const :tag "CSS" css)
  247. (const :tag "Ditaa" ditaa)
  248. (const :tag "Dot" dot)
  249. (const :tag "Emacs Lisp" emacs-lisp)
  250. (const :tag "Forth" forth)
  251. (const :tag "Fortran" fortran)
  252. (const :tag "Gnuplot" gnuplot)
  253. (const :tag "Haskell" haskell)
  254. (const :tag "IO" io)
  255. (const :tag "J" J)
  256. (const :tag "Java" java)
  257. (const :tag "Javascript" js)
  258. (const :tag "LaTeX" latex)
  259. (const :tag "Ledger" ledger)
  260. (const :tag "Lilypond" lilypond)
  261. (const :tag "Lisp" lisp)
  262. (const :tag "Makefile" makefile)
  263. (const :tag "Maxima" maxima)
  264. (const :tag "Matlab" matlab)
  265. (const :tag "Mscgen" mscgen)
  266. (const :tag "Ocaml" ocaml)
  267. (const :tag "Octave" octave)
  268. (const :tag "Org" org)
  269. (const :tag "Perl" perl)
  270. (const :tag "Pico Lisp" picolisp)
  271. (const :tag "PlantUML" plantuml)
  272. (const :tag "Python" python)
  273. (const :tag "Ruby" ruby)
  274. (const :tag "Sass" sass)
  275. (const :tag "Scala" scala)
  276. (const :tag "Scheme" scheme)
  277. (const :tag "Screen" screen)
  278. (const :tag "Shell Script" shell)
  279. (const :tag "Shen" shen)
  280. (const :tag "Sql" sql)
  281. (const :tag "Sqlite" sqlite)
  282. (const :tag "ebnf2ps" ebnf2ps))
  283. :value-type (boolean :tag "Activate" :value t)))
  284. ;;;; Customization variables
  285. (defcustom org-clone-delete-id nil
  286. "Remove ID property of clones of a subtree.
  287. When non-nil, clones of a subtree don't inherit the ID property.
  288. Otherwise they inherit the ID property with a new unique
  289. identifier."
  290. :type 'boolean
  291. :version "24.1"
  292. :group 'org-id)
  293. ;;; Version
  294. (org-check-version)
  295. ;;;###autoload
  296. (defun org-version (&optional here full message)
  297. "Show the org-mode version.
  298. Interactively, or when MESSAGE is non-nil, show it in echo area.
  299. With prefix argument, or when HERE is non-nil, insert it at point.
  300. In non-interactive uses, a reduced version string is output unless
  301. FULL is given."
  302. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  303. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  304. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  305. (load-suffixes (list ".el"))
  306. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  307. (org-trash (or
  308. (and (fboundp 'org-release) (fboundp 'org-git-version))
  309. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  310. (load-suffixes save-load-suffixes)
  311. (org-version (org-release))
  312. (git-version (org-git-version))
  313. (version (format "Org-mode version %s (%s @ %s)"
  314. org-version
  315. git-version
  316. (if org-install-dir
  317. (if (string= org-dir org-install-dir)
  318. org-install-dir
  319. (concat "mixed installation! " org-install-dir " and " org-dir))
  320. "org-loaddefs.el can not be found!")))
  321. (version1 (if full version org-version)))
  322. (when here (insert version1))
  323. (when message (message "%s" version1))
  324. version1))
  325. (defconst org-version (org-version))
  326. ;;; Syntax Constants
  327. ;;;; Block
  328. (defconst org-block-regexp
  329. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  330. "Regular expression for hiding blocks.")
  331. (defconst org-dblock-start-re
  332. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  333. "Matches the start line of a dynamic block, with parameters.")
  334. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  335. "Matches the end of a dynamic block.")
  336. ;;;; Clock and Planning
  337. (defconst org-clock-string "CLOCK:"
  338. "String used as prefix for timestamps clocking work hours on an item.")
  339. (defvar org-closed-string "CLOSED:"
  340. "String used as the prefix for timestamps logging closing a TODO entry.")
  341. (defvar org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].")
  346. (defvar org-scheduled-string "SCHEDULED:"
  347. "String to mark scheduled TODO entries.
  348. A schedule is this string, followed by a time stamp. Should be a word,
  349. terminated by a colon. You can insert a schedule keyword and
  350. a timestamp with \\[org-schedule].")
  351. (defconst org-ds-keyword-length
  352. (+ 2
  353. (apply #'max
  354. (mapcar #'length
  355. (list org-deadline-string org-scheduled-string
  356. org-clock-string org-closed-string))))
  357. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  358. (defconst org-planning-line-re
  359. (concat "^[ \t]*"
  360. (regexp-opt
  361. (list org-closed-string org-deadline-string org-scheduled-string)
  362. t))
  363. "Matches a line with planning info.
  364. Matched keyword is in group 1.")
  365. (defconst org-clock-line-re
  366. (concat "^[ \t]*" org-clock-string)
  367. "Matches a line with clock info.")
  368. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  369. "Matches the DEADLINE keyword.")
  370. (defconst org-deadline-time-regexp
  371. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  372. "Matches the DEADLINE keyword together with a time stamp.")
  373. (defconst org-deadline-time-hour-regexp
  374. (concat "\\<" org-deadline-string
  375. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  376. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  377. (defconst org-deadline-line-regexp
  378. (concat "\\<\\(" org-deadline-string "\\).*")
  379. "Matches the DEADLINE keyword and the rest of the line.")
  380. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  381. "Matches the SCHEDULED keyword.")
  382. (defconst org-scheduled-time-regexp
  383. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  384. "Matches the SCHEDULED keyword together with a time stamp.")
  385. (defconst org-scheduled-time-hour-regexp
  386. (concat "\\<" org-scheduled-string
  387. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  388. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  389. (defconst org-closed-time-regexp
  390. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  391. "Matches the CLOSED keyword together with a time stamp.")
  392. (defconst org-keyword-time-regexp
  393. (concat "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string
  396. org-clock-string)
  397. t)
  398. " *[[<]\\([^]>]+\\)[]>]")
  399. "Matches any of the 4 keywords, together with the time stamp.")
  400. (defconst org-keyword-time-not-clock-regexp
  401. (concat
  402. "\\<"
  403. (regexp-opt
  404. (list org-scheduled-string org-deadline-string org-closed-string) t)
  405. " *[[<]\\([^]>]+\\)[]>]")
  406. "Matches any of the 3 keywords, together with the time stamp.")
  407. (defconst org-maybe-keyword-time-regexp
  408. (concat "\\(\\<"
  409. (regexp-opt
  410. (list org-scheduled-string org-deadline-string org-closed-string
  411. org-clock-string)
  412. t)
  413. "\\)?"
  414. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  415. "\\|"
  416. "<%%([^\r\n>]*>\\)")
  417. "Matches a timestamp, possibly preceded by a keyword.")
  418. (defconst org-all-time-keywords
  419. (mapcar (lambda (w) (substring w 0 -1))
  420. (list org-scheduled-string org-deadline-string
  421. org-clock-string org-closed-string))
  422. "List of time keywords.")
  423. ;;;; Drawer
  424. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  425. "Matches first or last line of a hidden block.
  426. Group 1 contains drawer's name or \"END\".")
  427. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  428. "Regular expression matching the first line of a property drawer.")
  429. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  430. "Regular expression matching the last line of a property drawer.")
  431. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  432. "Regular expression matching the first line of a clock drawer.")
  433. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  434. "Regular expression matching the last line of a clock drawer.")
  435. (defconst org-property-drawer-re
  436. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  437. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  438. "[ \t]*:END:[ \t]*$")
  439. "Matches an entire property drawer.")
  440. (defconst org-clock-drawer-re
  441. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  442. org-clock-drawer-end-re "\\)\n?")
  443. "Matches an entire clock drawer.")
  444. ;;;; Headline
  445. (defconst org-heading-keyword-regexp-format
  446. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  447. "Printf format for a regexp matching a headline with some keyword.
  448. This regexp will match the headline of any node which has the
  449. exact keyword that is put into the format. The keyword isn't in
  450. any group by default, but the stars and the body are.")
  451. (defconst org-heading-keyword-maybe-regexp-format
  452. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  453. "Printf format for a regexp matching a headline, possibly with some keyword.
  454. This regexp can match any headline with the specified keyword, or
  455. without a keyword. The keyword isn't in any group by default,
  456. but the stars and the body are.")
  457. (defconst org-archive-tag "ARCHIVE"
  458. "The tag that marks a subtree as archived.
  459. An archived subtree does not open during visibility cycling, and does
  460. not contribute to the agenda listings.")
  461. (defconst org-comment-string "COMMENT"
  462. "Entries starting with this keyword will never be exported.
  463. An entry can be toggled between COMMENT and normal with
  464. \\[org-toggle-comment].")
  465. ;;;; LaTeX Environments and Fragments
  466. (defconst org-latex-regexps
  467. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  468. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  469. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  470. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  471. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  472. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  473. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  474. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  475. "Regular expressions for matching embedded LaTeX.")
  476. ;;;; Node Property
  477. (defconst org-effort-property "Effort"
  478. "The property that is being used to keep track of effort estimates.
  479. Effort estimates given in this property need to have the format H:MM.")
  480. ;;;; Table
  481. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  482. "Detect an org-type or table-type table.")
  483. (defconst org-table-line-regexp "^[ \t]*|"
  484. "Detect an org-type table line.")
  485. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  486. "Detect an org-type table line.")
  487. (defconst org-table-hline-regexp "^[ \t]*|-"
  488. "Detect an org-type table hline.")
  489. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  490. "Detect a table-type table hline.")
  491. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  492. "Detect the first line outside a table when searching from within it.
  493. This works for both table types.")
  494. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  495. "Detect a #+TBLFM line.")
  496. ;;;; Timestamp
  497. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  498. "Regular expression for fast time stamp matching.")
  499. (defconst org-ts-regexp-inactive
  500. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  501. "Regular expression for fast inactive time stamp matching.")
  502. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  503. "Regular expression for fast time stamp matching.")
  504. (defconst org-ts-regexp0
  505. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  506. "Regular expression matching time strings for analysis.
  507. This one does not require the space after the date, so it can be used
  508. on a string that terminates immediately after the date.")
  509. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  510. "Regular expression matching time strings for analysis.")
  511. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  512. "Regular expression matching time stamps, with groups.")
  513. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  514. "Regular expression matching time stamps (also [..]), with groups.")
  515. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  516. "Regular expression matching a time stamp range.")
  517. (defconst org-tr-regexp-both
  518. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  519. "Regular expression matching a time stamp range.")
  520. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  521. org-ts-regexp "\\)?")
  522. "Regular expression matching a time stamp or time stamp range.")
  523. (defconst org-tsr-regexp-both
  524. (concat org-ts-regexp-both "\\(--?-?"
  525. org-ts-regexp-both "\\)?")
  526. "Regular expression matching a time stamp or time stamp range.
  527. The time stamps may be either active or inactive.")
  528. (defconst org-repeat-re
  529. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  530. "Regular expression for specifying repeated events.
  531. After a match, group 1 contains the repeat expression.")
  532. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  533. "Formats for `format-time-string' which are used for time stamps.")
  534. ;;; The custom variables
  535. (defgroup org nil
  536. "Outline-based notes management and organizer."
  537. :tag "Org"
  538. :group 'outlines
  539. :group 'calendar)
  540. (defcustom org-mode-hook nil
  541. "Mode hook for Org-mode, run after the mode was turned on."
  542. :group 'org
  543. :type 'hook)
  544. (defcustom org-load-hook nil
  545. "Hook that is run after org.el has been loaded."
  546. :group 'org
  547. :type 'hook)
  548. (defcustom org-log-buffer-setup-hook nil
  549. "Hook that is run after an Org log buffer is created."
  550. :group 'org
  551. :version "24.1"
  552. :type 'hook)
  553. (defvar org-modules) ; defined below
  554. (defvar org-modules-loaded nil
  555. "Have the modules been loaded already?")
  556. (defun org-load-modules-maybe (&optional force)
  557. "Load all extensions listed in `org-modules'."
  558. (when (or force (not org-modules-loaded))
  559. (mapc (lambda (ext)
  560. (condition-case nil (require ext)
  561. (error (message "Problems while trying to load feature `%s'" ext))))
  562. org-modules)
  563. (setq org-modules-loaded t)))
  564. (defun org-set-modules (var value)
  565. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  566. (set var value)
  567. (when (featurep 'org)
  568. (org-load-modules-maybe 'force)
  569. (org-element-cache-reset 'all)))
  570. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  571. "Modules that should always be loaded together with org.el.
  572. If a description starts with <C>, the file is not part of Emacs
  573. and loading it will require that you have downloaded and properly
  574. installed the Org mode distribution.
  575. You can also use this system to load external packages (i.e. neither Org
  576. core modules, nor modules from the CONTRIB directory). Just add symbols
  577. to the end of the list. If the package is called org-xyz.el, then you need
  578. to add the symbol `xyz', and the package must have a call to:
  579. (provide \\='org-xyz)
  580. For export specific modules, see also `org-export-backends'."
  581. :group 'org
  582. :set 'org-set-modules
  583. :version "24.4"
  584. :package-version '(Org . "8.0")
  585. :type
  586. '(set :greedy t
  587. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  588. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  589. (const :tag " crypt: Encryption of subtrees" org-crypt)
  590. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  591. (const :tag " docview: Links to doc-view buffers" org-docview)
  592. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  593. (const :tag " habit: Track your consistency with habits" org-habit)
  594. (const :tag " id: Global IDs for identifying entries" org-id)
  595. (const :tag " info: Links to Info nodes" org-info)
  596. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  597. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  598. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  599. (const :tag " mouse: Additional mouse support" org-mouse)
  600. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  601. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  602. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  603. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  604. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  605. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  606. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  607. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  608. (const :tag "C collector: Collect properties into tables" org-collector)
  609. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  610. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  611. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  612. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  613. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  614. (const :tag "C eval: Include command output as text" org-eval)
  615. (const :tag "C eww: Store link to url of eww" org-eww)
  616. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  617. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  618. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  619. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  620. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  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. \\<org-mode-map>\
  739. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  740. start selecting a region, or enlarge regions started in this way.
  741. In Org-mode, in special contexts, these same keys are used for
  742. other purposes, important enough to compete with shift selection.
  743. Org tries to balance these needs by supporting `shift-select-mode'
  744. outside these special contexts, under control of this variable.
  745. The default of this variable is nil, to avoid confusing behavior. Shifted
  746. cursor keys will then execute Org commands in the following contexts:
  747. - on a headline, changing TODO state (left/right) and priority (up/down)
  748. - on a time stamp, changing the time
  749. - in a plain list item, changing the bullet type
  750. - in a property definition line, switching between allowed values
  751. - in the BEGIN line of a clock table (changing the time block).
  752. Outside these contexts, the commands will throw an error.
  753. When this variable is t and the cursor is not in a special
  754. context, Org-mode will support shift-selection for making and
  755. enlarging regions. To make this more effective, the bullet
  756. cycling will no longer happen anywhere in an item line, but only
  757. if the cursor is exactly on the bullet.
  758. If you set this variable to the symbol `always', then the keys
  759. will not be special in headlines, property lines, and item lines,
  760. to make shift selection work there as well. If this is what you
  761. want, you can use the following alternative commands:
  762. `\\[org-todo]' and `\\[org-priority]' \
  763. to change TODO state and priority,
  764. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  765. can be used to switch TODO sets,
  766. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  767. and properties can be edited by hand or in column view.
  768. However, when the cursor is on a timestamp, shift-cursor commands
  769. will still edit the time stamp - this is just too good to give up.
  770. XEmacs user should have this variable set to nil, because
  771. `shift-select-mode' is in Emacs 23 or later only."
  772. :group 'org
  773. :type '(choice
  774. (const :tag "Never" nil)
  775. (const :tag "When outside special context" t)
  776. (const :tag "Everywhere except timestamps" always)))
  777. (defcustom org-loop-over-headlines-in-active-region nil
  778. "Shall some commands act upon headlines in the active region?
  779. When set to t, some commands will be performed in all headlines
  780. within the active region.
  781. When set to `start-level', some commands will be performed in all
  782. headlines within the active region, provided that these headlines
  783. are of the same level than the first one.
  784. When set to a string, those commands will be performed on the
  785. matching headlines within the active region. Such string must be
  786. a tags/property/todo match as it is used in the agenda tags view.
  787. The list of commands is: `org-schedule', `org-deadline',
  788. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  789. `org-archive-to-archive-sibling'. The archiving commands skip
  790. already archived entries."
  791. :type '(choice (const :tag "Don't loop" nil)
  792. (const :tag "All headlines in active region" t)
  793. (const :tag "In active region, headlines at the same level than the first one" start-level)
  794. (string :tag "Tags/Property/Todo matcher"))
  795. :version "24.1"
  796. :group 'org-todo
  797. :group 'org-archive)
  798. (defgroup org-startup nil
  799. "Options concerning startup of Org-mode."
  800. :tag "Org Startup"
  801. :group 'org)
  802. (defcustom org-startup-folded t
  803. "Non-nil means entering Org-mode will switch to OVERVIEW.
  804. This can also be configured on a per-file basis by adding one of
  805. the following lines anywhere in the buffer:
  806. #+STARTUP: fold (or `overview', this is equivalent)
  807. #+STARTUP: nofold (or `showall', this is equivalent)
  808. #+STARTUP: content
  809. #+STARTUP: showeverything
  810. By default, this option is ignored when Org opens agenda files
  811. for the first time. If you want the agenda to honor the startup
  812. option, set `org-agenda-inhibit-startup' to nil."
  813. :group 'org-startup
  814. :type '(choice
  815. (const :tag "nofold: show all" nil)
  816. (const :tag "fold: overview" t)
  817. (const :tag "content: all headlines" content)
  818. (const :tag "show everything, even drawers" showeverything)))
  819. (defcustom org-startup-truncated t
  820. "Non-nil means entering Org-mode will set `truncate-lines'.
  821. This is useful since some lines containing links can be very long and
  822. uninteresting. Also tables look terrible when wrapped."
  823. :group 'org-startup
  824. :type 'boolean)
  825. (defcustom org-startup-indented nil
  826. "Non-nil means turn on `org-indent-mode' on startup.
  827. This can also be configured on a per-file basis by adding one of
  828. the following lines anywhere in the buffer:
  829. #+STARTUP: indent
  830. #+STARTUP: noindent"
  831. :group 'org-structure
  832. :type '(choice
  833. (const :tag "Not" nil)
  834. (const :tag "Globally (slow on startup in large files)" t)))
  835. (defcustom org-use-sub-superscripts t
  836. "Non-nil means interpret \"_\" and \"^\" for display.
  837. If you want to control how Org exports those characters, see
  838. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  839. used to be an alias for `org-export-with-sub-superscripts' in
  840. Org <8.0, it is not anymore.
  841. When this option is turned on, you can use TeX-like syntax for
  842. sub- and superscripts within the buffer. Several characters after
  843. \"_\" or \"^\" will be considered as a single item - so grouping
  844. with {} is normally not needed. For example, the following things
  845. will be parsed as single sub- or superscripts:
  846. 10^24 or 10^tau several digits will be considered 1 item.
  847. 10^-12 or 10^-tau a leading sign with digits or a word
  848. x^2-y^3 will be read as x^2 - y^3, because items are
  849. terminated by almost any nonword/nondigit char.
  850. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  851. Still, ambiguity is possible. So when in doubt, use {} to enclose
  852. the sub/superscript. If you set this variable to the symbol `{}',
  853. the braces are *required* in order to trigger interpretations as
  854. sub/superscript. This can be helpful in documents that need \"_\"
  855. frequently in plain text."
  856. :group 'org-startup
  857. :version "24.4"
  858. :package-version '(Org . "8.0")
  859. :type '(choice
  860. (const :tag "Always interpret" t)
  861. (const :tag "Only with braces" {})
  862. (const :tag "Never interpret" nil)))
  863. (defcustom org-startup-with-beamer-mode nil
  864. "Non-nil means turn on `org-beamer-mode' on startup.
  865. This can also be configured on a per-file basis by adding one of
  866. the following lines anywhere in the buffer:
  867. #+STARTUP: beamer"
  868. :group 'org-startup
  869. :version "24.1"
  870. :type 'boolean)
  871. (defcustom org-startup-align-all-tables nil
  872. "Non-nil means align all tables when visiting a file.
  873. This is useful when the column width in tables is forced with <N> cookies
  874. in table fields. Such tables will look correct only after the first re-align.
  875. This can also be configured on a per-file basis by adding one of
  876. the following lines anywhere in the buffer:
  877. #+STARTUP: align
  878. #+STARTUP: noalign"
  879. :group 'org-startup
  880. :type 'boolean)
  881. (defcustom org-startup-with-inline-images nil
  882. "Non-nil means show inline images when loading a new Org file.
  883. This can also be configured on a per-file basis by adding one of
  884. the following lines anywhere in the buffer:
  885. #+STARTUP: inlineimages
  886. #+STARTUP: noinlineimages"
  887. :group 'org-startup
  888. :version "24.1"
  889. :type 'boolean)
  890. (defcustom org-startup-with-latex-preview nil
  891. "Non-nil means preview LaTeX fragments when loading a new Org file.
  892. This can also be configured on a per-file basis by adding one of
  893. the following lines anywhere in the buffer:
  894. #+STARTUP: latexpreview
  895. #+STARTUP: nolatexpreview"
  896. :group 'org-startup
  897. :version "24.4"
  898. :package-version '(Org . "8.0")
  899. :type 'boolean)
  900. (defcustom org-insert-mode-line-in-empty-file nil
  901. "Non-nil means insert the first line setting Org-mode in empty files.
  902. When the function `org-mode' is called interactively in an empty file, this
  903. normally means that the file name does not automatically trigger Org-mode.
  904. To ensure that the file will always be in Org-mode in the future, a
  905. line enforcing Org-mode will be inserted into the buffer, if this option
  906. has been set."
  907. :group 'org-startup
  908. :type 'boolean)
  909. (defcustom org-replace-disputed-keys nil
  910. "Non-nil means use alternative key bindings for some keys.
  911. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  912. These keys are also used by other packages like shift-selection-mode'
  913. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  914. If you want to use Org-mode together with one of these other modes,
  915. or more generally if you would like to move some Org-mode commands to
  916. other keys, set this variable and configure the keys with the variable
  917. `org-disputed-keys'.
  918. This option is only relevant at load-time of Org-mode, and must be set
  919. *before* org.el is loaded. Changing it requires a restart of Emacs to
  920. become effective."
  921. :group 'org-startup
  922. :type 'boolean)
  923. (defcustom org-use-extra-keys nil
  924. "Non-nil means use extra key sequence definitions for certain commands.
  925. This happens automatically if you run XEmacs or if `window-system'
  926. is nil. This variable lets you do the same manually. You must
  927. set it before loading org.
  928. Example: on Carbon Emacs 22 running graphically, with an external
  929. keyboard on a Powerbook, the default way of setting M-left might
  930. not work for either Alt or ESC. Setting this variable will make
  931. it work for ESC."
  932. :group 'org-startup
  933. :type 'boolean)
  934. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  935. (defcustom org-disputed-keys
  936. '(([(shift up)] . [(meta p)])
  937. ([(shift down)] . [(meta n)])
  938. ([(shift left)] . [(meta -)])
  939. ([(shift right)] . [(meta +)])
  940. ([(control shift right)] . [(meta shift +)])
  941. ([(control shift left)] . [(meta shift -)]))
  942. "Keys for which Org-mode and other modes compete.
  943. This is an alist, cars are the default keys, second element specifies
  944. the alternative to use when `org-replace-disputed-keys' is t.
  945. Keys can be specified in any syntax supported by `define-key'.
  946. The value of this option takes effect only at Org-mode's startup,
  947. therefore you'll have to restart Emacs to apply it after changing."
  948. :group 'org-startup
  949. :type 'alist)
  950. (defun org-key (key)
  951. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  952. Or return the original if not disputed.
  953. Also apply the translations defined in `org-xemacs-key-equivalents'."
  954. (when org-replace-disputed-keys
  955. (let* ((nkey (key-description key))
  956. (x (org-find-if (lambda (x)
  957. (equal (key-description (car x)) nkey))
  958. org-disputed-keys)))
  959. (setq key (if x (cdr x) key))))
  960. (when (featurep 'xemacs)
  961. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  962. key)
  963. (defun org-find-if (predicate seq)
  964. (catch 'exit
  965. (while seq
  966. (if (funcall predicate (car seq))
  967. (throw 'exit (car seq))
  968. (pop seq)))))
  969. (defun org-defkey (keymap key def)
  970. "Define a key, possibly translated, as returned by `org-key'."
  971. (define-key keymap (org-key key) def))
  972. (defcustom org-ellipsis nil
  973. "The ellipsis to use in the Org-mode outline.
  974. When nil, just use the standard three dots.
  975. When a string, use that string instead.
  976. When a face, use the standard 3 dots, but with the specified face.
  977. The change affects only Org-mode (which will then use its own display table).
  978. Changing this requires executing \\[org-mode] in a buffer to become
  979. effective."
  980. :group 'org-startup
  981. :type '(choice (const :tag "Default" nil)
  982. (face :tag "Face" :value org-warning)
  983. (string :tag "String" :value "...#")))
  984. (defvar org-display-table nil
  985. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  986. (defgroup org-keywords nil
  987. "Keywords in Org-mode."
  988. :tag "Org Keywords"
  989. :group 'org)
  990. (defcustom org-closed-keep-when-no-todo nil
  991. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  992. :group 'org-todo
  993. :group 'org-keywords
  994. :version "24.4"
  995. :package-version '(Org . "8.0")
  996. :type 'boolean)
  997. (defgroup org-structure nil
  998. "Options concerning the general structure of Org-mode files."
  999. :tag "Org Structure"
  1000. :group 'org)
  1001. (defgroup org-reveal-location nil
  1002. "Options about how to make context of a location visible."
  1003. :tag "Org Reveal Location"
  1004. :group 'org-structure)
  1005. (defcustom org-show-context-detail '((isearch . lineage)
  1006. (bookmark-jump . lineage)
  1007. (default . ancestors))
  1008. "Alist between context and visibility span when revealing a location.
  1009. \\<org-mode-map>Some actions may move point into invisible
  1010. locations. As a consequence, Org always expose a neighborhood
  1011. around point. How much is shown depends on the initial action,
  1012. or context. Valid contexts are
  1013. agenda when exposing an entry from the agenda
  1014. org-goto when using the command `org-goto' (\\[org-goto])
  1015. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1016. tags-tree when constructing a sparse tree based on tags matches
  1017. link-search when exposing search matches associated with a link
  1018. mark-goto when exposing the jump goal of a mark
  1019. bookmark-jump when exposing a bookmark location
  1020. isearch when exiting from an incremental search
  1021. default default for all contexts not set explicitly
  1022. Allowed visibility spans are
  1023. minimal show current headline; if point is not on headline,
  1024. also show entry
  1025. local show current headline, entry and next headline
  1026. ancestors show current headline and its direct ancestors; if
  1027. point is not on headline, also show entry
  1028. lineage show current headline, its direct ancestors and all
  1029. their children; if point is not on headline, also show
  1030. entry and first child
  1031. tree show current headline, its direct ancestors and all
  1032. their children; if point is not on headline, also show
  1033. entry and all children
  1034. canonical show current headline, its direct ancestors along with
  1035. their entries and children; if point is not located on
  1036. the headline, also show current entry and all children
  1037. As special cases, a nil or t value means show all contexts in
  1038. `minimal' or `canonical' view, respectively.
  1039. Some views can make displayed information very compact, but also
  1040. make it harder to edit the location of the match. In such
  1041. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1042. more context."
  1043. :group 'org-reveal-location
  1044. :version "25.1"
  1045. :package-version '(Org . "8.3")
  1046. :type '(choice
  1047. (const :tag "Canonical" t)
  1048. (const :tag "Minimal" nil)
  1049. (repeat :greedy t :tag "Individual contexts"
  1050. (cons
  1051. (choice :tag "Context"
  1052. (const agenda)
  1053. (const org-goto)
  1054. (const occur-tree)
  1055. (const tags-tree)
  1056. (const link-search)
  1057. (const mark-goto)
  1058. (const bookmark-jump)
  1059. (const isearch)
  1060. (const default))
  1061. (choice :tag "Detail level"
  1062. (const minimal)
  1063. (const local)
  1064. (const ancestors)
  1065. (const lineage)
  1066. (const tree)
  1067. (const canonical))))))
  1068. (defcustom org-indirect-buffer-display 'other-window
  1069. "How should indirect tree buffers be displayed?
  1070. This applies to indirect buffers created with the commands
  1071. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1072. Valid values are:
  1073. current-window Display in the current window
  1074. other-window Just display in another window.
  1075. dedicated-frame Create one new frame, and re-use it each time.
  1076. new-frame Make a new frame each time. Note that in this case
  1077. previously-made indirect buffers are kept, and you need to
  1078. kill these buffers yourself."
  1079. :group 'org-structure
  1080. :group 'org-agenda-windows
  1081. :type '(choice
  1082. (const :tag "In current window" current-window)
  1083. (const :tag "In current frame, other window" other-window)
  1084. (const :tag "Each time a new frame" new-frame)
  1085. (const :tag "One dedicated frame" dedicated-frame)))
  1086. (defcustom org-use-speed-commands nil
  1087. "Non-nil means activate single letter commands at beginning of a headline.
  1088. This may also be a function to test for appropriate locations where speed
  1089. commands should be active.
  1090. For example, to activate speed commands when the point is on any
  1091. star at the beginning of the headline, you can do this:
  1092. (setq org-use-speed-commands
  1093. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1094. :group 'org-structure
  1095. :type '(choice
  1096. (const :tag "Never" nil)
  1097. (const :tag "At beginning of headline stars" t)
  1098. (function)))
  1099. (defcustom org-speed-commands-user nil
  1100. "Alist of additional speed commands.
  1101. This list will be checked before `org-speed-commands-default'
  1102. when the variable `org-use-speed-commands' is non-nil
  1103. and when the cursor is at the beginning of a headline.
  1104. The car if each entry is a string with a single letter, which must
  1105. be assigned to `self-insert-command' in the global map.
  1106. The cdr is either a command to be called interactively, a function
  1107. to be called, or a form to be evaluated.
  1108. An entry that is just a list with a single string will be interpreted
  1109. as a descriptive headline that will be added when listing the speed
  1110. commands in the Help buffer using the `?' speed command."
  1111. :group 'org-structure
  1112. :type '(repeat :value ("k" . ignore)
  1113. (choice :value ("k" . ignore)
  1114. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1115. (cons :tag "Letter and Command"
  1116. (string :tag "Command letter")
  1117. (choice
  1118. (function)
  1119. (sexp))))))
  1120. (defcustom org-bookmark-names-plist
  1121. '(:last-capture "org-capture-last-stored"
  1122. :last-refile "org-refile-last-stored"
  1123. :last-capture-marker "org-capture-last-stored-marker")
  1124. "Names for bookmarks automatically set by some Org commands.
  1125. This can provide strings as names for a number of bookmarks Org sets
  1126. automatically. The following keys are currently implemented:
  1127. :last-capture
  1128. :last-capture-marker
  1129. :last-refile
  1130. When a key does not show up in the property list, the corresponding bookmark
  1131. is not set."
  1132. :group 'org-structure
  1133. :type 'plist)
  1134. (defgroup org-cycle nil
  1135. "Options concerning visibility cycling in Org-mode."
  1136. :tag "Org Cycle"
  1137. :group 'org-structure)
  1138. (defcustom org-cycle-skip-children-state-if-no-children t
  1139. "Non-nil means skip CHILDREN state in entries that don't have any."
  1140. :group 'org-cycle
  1141. :type 'boolean)
  1142. (defcustom org-cycle-max-level nil
  1143. "Maximum level which should still be subject to visibility cycling.
  1144. Levels higher than this will, for cycling, be treated as text, not a headline.
  1145. When `org-odd-levels-only' is set, a value of N in this variable actually
  1146. means 2N-1 stars as the limiting headline.
  1147. When nil, cycle all levels.
  1148. Note that the limiting level of cycling is also influenced by
  1149. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1150. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1151. than its value."
  1152. :group 'org-cycle
  1153. :type '(choice
  1154. (const :tag "No limit" nil)
  1155. (integer :tag "Maximum level")))
  1156. (defcustom org-hide-block-startup nil
  1157. "Non-nil means entering Org-mode will fold all blocks.
  1158. This can also be set in on a per-file basis with
  1159. #+STARTUP: hideblocks
  1160. #+STARTUP: showblocks"
  1161. :group 'org-startup
  1162. :group 'org-cycle
  1163. :type 'boolean)
  1164. (defcustom org-cycle-global-at-bob nil
  1165. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1166. This makes it possible to do global cycling without having to use S-TAB or
  1167. \\[universal-argument] TAB. For this special case to work, the first line
  1168. of the buffer must not be a headline -- it may be empty or some other text.
  1169. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1170. sure the cursor stays at the beginning of the buffer. When this option is
  1171. nil, don't do anything special at the beginning of the buffer."
  1172. :group 'org-cycle
  1173. :type 'boolean)
  1174. (defcustom org-cycle-level-after-item/entry-creation t
  1175. "Non-nil means cycle entry level or item indentation in new empty entries.
  1176. When the cursor is at the end of an empty headline, i.e., with only stars
  1177. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1178. and then all possible ancestor states, before returning to the original state.
  1179. This makes data entry extremely fast: M-RET to create a new headline,
  1180. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1181. When the cursor is at the end of an empty plain list item, one TAB will
  1182. make it a subitem, two or more tabs will back up to make this an item
  1183. higher up in the item hierarchy."
  1184. :group 'org-cycle
  1185. :type 'boolean)
  1186. (defcustom org-cycle-emulate-tab t
  1187. "Where should `org-cycle' emulate TAB.
  1188. nil Never
  1189. white Only in completely white lines
  1190. whitestart Only at the beginning of lines, before the first non-white char
  1191. t Everywhere except in headlines
  1192. exc-hl-bol Everywhere except at the start of a headline
  1193. If TAB is used in a place where it does not emulate TAB, the current subtree
  1194. visibility is cycled."
  1195. :group 'org-cycle
  1196. :type '(choice (const :tag "Never" nil)
  1197. (const :tag "Only in completely white lines" white)
  1198. (const :tag "Before first char in a line" whitestart)
  1199. (const :tag "Everywhere except in headlines" t)
  1200. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1201. (defcustom org-cycle-separator-lines 2
  1202. "Number of empty lines needed to keep an empty line between collapsed trees.
  1203. If you leave an empty line between the end of a subtree and the following
  1204. headline, this empty line is hidden when the subtree is folded.
  1205. Org-mode will leave (exactly) one empty line visible if the number of
  1206. empty lines is equal or larger to the number given in this variable.
  1207. So the default 2 means at least 2 empty lines after the end of a subtree
  1208. are needed to produce free space between a collapsed subtree and the
  1209. following headline.
  1210. If the number is negative, and the number of empty lines is at least -N,
  1211. all empty lines are shown.
  1212. Special case: when 0, never leave empty lines in collapsed view."
  1213. :group 'org-cycle
  1214. :type 'integer)
  1215. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1216. (defcustom org-pre-cycle-hook nil
  1217. "Hook that is run before visibility cycling is happening.
  1218. The function(s) in this hook must accept a single argument which indicates
  1219. the new state that will be set right after running this hook. The
  1220. argument is a symbol. Before a global state change, it can have the values
  1221. `overview', `content', or `all'. Before a local state change, it can have
  1222. the values `folded', `children', or `subtree'."
  1223. :group 'org-cycle
  1224. :type 'hook)
  1225. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1226. org-cycle-hide-drawers
  1227. org-cycle-show-empty-lines
  1228. org-optimize-window-after-visibility-change)
  1229. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1230. The function(s) in this hook must accept a single argument which indicates
  1231. the new state that was set by the most recent `org-cycle' command. The
  1232. argument is a symbol. After a global state change, it can have the values
  1233. `overview', `contents', or `all'. After a local state change, it can have
  1234. the values `folded', `children', or `subtree'."
  1235. :group 'org-cycle
  1236. :type 'hook
  1237. :version "25.1"
  1238. :package-version '(Org . "8.3"))
  1239. (defgroup org-edit-structure nil
  1240. "Options concerning structure editing in Org-mode."
  1241. :tag "Org Edit Structure"
  1242. :group 'org-structure)
  1243. (defcustom org-odd-levels-only nil
  1244. "Non-nil means skip even levels and only use odd levels for the outline.
  1245. This has the effect that two stars are being added/taken away in
  1246. promotion/demotion commands. It also influences how levels are
  1247. handled by the exporters.
  1248. Changing it requires restart of `font-lock-mode' to become effective
  1249. for fontification also in regions already fontified.
  1250. You may also set this on a per-file basis by adding one of the following
  1251. lines to the buffer:
  1252. #+STARTUP: odd
  1253. #+STARTUP: oddeven"
  1254. :group 'org-edit-structure
  1255. :group 'org-appearance
  1256. :type 'boolean)
  1257. (defcustom org-adapt-indentation t
  1258. "Non-nil means adapt indentation to outline node level.
  1259. When this variable is set, Org assumes that you write outlines by
  1260. indenting text in each node to align with the headline (after the
  1261. stars). The following issues are influenced by this variable:
  1262. - The indentation is increased by one space in a demotion
  1263. command, and decreased by one in a promotion command. However,
  1264. in the latter case, if shifting some line in the entry body
  1265. would alter document structure (e.g., insert a new headline),
  1266. indentation is not changed at all.
  1267. - Property drawers and planning information is inserted indented
  1268. when this variable is set. When nil, they will not be indented.
  1269. - TAB indents a line relative to current level. The lines below
  1270. a headline will be indented when this variable is set.
  1271. Note that this is all about true indentation, by adding and
  1272. removing space characters. See also `org-indent.el' which does
  1273. level-dependent indentation in a virtual way, i.e. at display
  1274. time in Emacs."
  1275. :group 'org-edit-structure
  1276. :type 'boolean)
  1277. (defcustom org-special-ctrl-a/e nil
  1278. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1279. When t, `C-a' will bring back the cursor to the beginning of the
  1280. headline text, i.e. after the stars and after a possible TODO
  1281. keyword. In an item, this will be the position after bullet and
  1282. check-box, if any. When the cursor is already at that position,
  1283. another `C-a' will bring it to the beginning of the line.
  1284. `C-e' will jump to the end of the headline, ignoring the presence
  1285. of tags in the headline. A second `C-e' will then jump to the
  1286. true end of the line, after any tags. This also means that, when
  1287. this variable is non-nil, `C-e' also will never jump beyond the
  1288. end of the heading of a folded section, i.e. not after the
  1289. ellipses.
  1290. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1291. normally, going to the true line boundary first. Only a directly
  1292. following, identical keypress will bring the cursor to the
  1293. special positions.
  1294. This may also be a cons cell where the behavior for `C-a' and
  1295. `C-e' is set separately."
  1296. :group 'org-edit-structure
  1297. :type '(choice
  1298. (const :tag "off" nil)
  1299. (const :tag "on: after stars/bullet and before tags first" t)
  1300. (const :tag "reversed: true line boundary first" reversed)
  1301. (cons :tag "Set C-a and C-e separately"
  1302. (choice :tag "Special C-a"
  1303. (const :tag "off" nil)
  1304. (const :tag "on: after stars/bullet first" t)
  1305. (const :tag "reversed: before stars/bullet first" reversed))
  1306. (choice :tag "Special C-e"
  1307. (const :tag "off" nil)
  1308. (const :tag "on: before tags first" t)
  1309. (const :tag "reversed: after tags first" reversed)))))
  1310. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1311. (defcustom org-special-ctrl-k nil
  1312. "Non-nil means `C-k' will behave specially in headlines.
  1313. When nil, `C-k' will call the default `kill-line' command.
  1314. When t, the following will happen while the cursor is in the headline:
  1315. - When the cursor is at the beginning of a headline, kill the entire
  1316. line and possible the folded subtree below the line.
  1317. - When in the middle of the headline text, kill the headline up to the tags.
  1318. - When after the headline text, kill the tags."
  1319. :group 'org-edit-structure
  1320. :type 'boolean)
  1321. (defcustom org-ctrl-k-protect-subtree nil
  1322. "Non-nil means, do not delete a hidden subtree with C-k.
  1323. When set to the symbol `error', simply throw an error when C-k is
  1324. used to kill (part-of) a headline that has hidden text behind it.
  1325. Any other non-nil value will result in a query to the user, if it is
  1326. OK to kill that hidden subtree. When nil, kill without remorse."
  1327. :group 'org-edit-structure
  1328. :version "24.1"
  1329. :type '(choice
  1330. (const :tag "Do not protect hidden subtrees" nil)
  1331. (const :tag "Protect hidden subtrees with a security query" t)
  1332. (const :tag "Never kill a hidden subtree with C-k" error)))
  1333. (defcustom org-special-ctrl-o t
  1334. "Non-nil means, make `C-o' insert a row in tables."
  1335. :group 'org-edit-structure
  1336. :type 'boolean)
  1337. (defcustom org-catch-invisible-edits nil
  1338. "Check if in invisible region before inserting or deleting a character.
  1339. Valid values are:
  1340. nil Do not check, so just do invisible edits.
  1341. error Throw an error and do nothing.
  1342. show Make point visible, and do the requested edit.
  1343. show-and-error Make point visible, then throw an error and abort the edit.
  1344. smart Make point visible, and do insertion/deletion if it is
  1345. adjacent to visible text and the change feels predictable.
  1346. Never delete a previously invisible character or add in the
  1347. middle or right after an invisible region. Basically, this
  1348. allows insertion and backward-delete right before ellipses.
  1349. FIXME: maybe in this case we should not even show?"
  1350. :group 'org-edit-structure
  1351. :version "24.1"
  1352. :type '(choice
  1353. (const :tag "Do not check" nil)
  1354. (const :tag "Throw error when trying to edit" error)
  1355. (const :tag "Unhide, but do not do the edit" show-and-error)
  1356. (const :tag "Show invisible part and do the edit" show)
  1357. (const :tag "Be smart and do the right thing" smart)))
  1358. (defcustom org-yank-folded-subtrees t
  1359. "Non-nil means when yanking subtrees, fold them.
  1360. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1361. it starts with a heading and all other headings in it are either children
  1362. or siblings, then fold all the subtrees. However, do this only if no
  1363. text after the yank would be swallowed into a folded tree by this action."
  1364. :group 'org-edit-structure
  1365. :type 'boolean)
  1366. (defcustom org-yank-adjusted-subtrees nil
  1367. "Non-nil means when yanking subtrees, adjust the level.
  1368. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1369. this function for details."
  1370. :group 'org-edit-structure
  1371. :type 'boolean)
  1372. (defcustom org-M-RET-may-split-line '((default . t))
  1373. "Non-nil means M-RET will split the line at the cursor position.
  1374. When nil, it will go to the end of the line before making a
  1375. new line.
  1376. You may also set this option in a different way for different
  1377. contexts. Valid contexts are:
  1378. headline when creating a new headline
  1379. item when creating a new item
  1380. table in a table field
  1381. default the value to be used for all contexts not explicitly
  1382. customized"
  1383. :group 'org-structure
  1384. :group 'org-table
  1385. :type '(choice
  1386. (const :tag "Always" t)
  1387. (const :tag "Never" nil)
  1388. (repeat :greedy t :tag "Individual contexts"
  1389. (cons
  1390. (choice :tag "Context"
  1391. (const headline)
  1392. (const item)
  1393. (const table)
  1394. (const default))
  1395. (boolean)))))
  1396. (defcustom org-insert-heading-respect-content nil
  1397. "Non-nil means insert new headings after the current subtree.
  1398. When nil, the new heading is created directly after the current line.
  1399. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1400. this variable on for the duration of the command."
  1401. :group 'org-structure
  1402. :type 'boolean)
  1403. (defcustom org-blank-before-new-entry '((heading . auto)
  1404. (plain-list-item . auto))
  1405. "Should `org-insert-heading' leave a blank line before new heading/item?
  1406. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1407. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1408. which case Org will look at the surrounding headings/items and try to
  1409. make an intelligent decision whether to insert a blank line or not.
  1410. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1411. the setting here is ignored and no empty line is inserted to avoid breaking
  1412. the list structure."
  1413. :group 'org-edit-structure
  1414. :type '(list
  1415. (cons (const heading)
  1416. (choice (const :tag "Never" nil)
  1417. (const :tag "Always" t)
  1418. (const :tag "Auto" auto)))
  1419. (cons (const plain-list-item)
  1420. (choice (const :tag "Never" nil)
  1421. (const :tag "Always" t)
  1422. (const :tag "Auto" auto)))))
  1423. (defcustom org-insert-heading-hook nil
  1424. "Hook being run after inserting a new heading."
  1425. :group 'org-edit-structure
  1426. :type 'hook)
  1427. (defcustom org-enable-fixed-width-editor t
  1428. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1429. This currently only means they are never auto-wrapped.
  1430. When nil, such lines will be treated like ordinary lines."
  1431. :group 'org-edit-structure
  1432. :type 'boolean)
  1433. (defcustom org-goto-auto-isearch t
  1434. "Non-nil means typing characters in `org-goto' starts incremental search.
  1435. When nil, you can use these keybindings to navigate the buffer:
  1436. q Quit the org-goto interface
  1437. n Go to the next visible heading
  1438. p Go to the previous visible heading
  1439. f Go one heading forward on same level
  1440. b Go one heading backward on same level
  1441. u Go one heading up"
  1442. :group 'org-edit-structure
  1443. :type 'boolean)
  1444. (defgroup org-sparse-trees nil
  1445. "Options concerning sparse trees in Org-mode."
  1446. :tag "Org Sparse Trees"
  1447. :group 'org-structure)
  1448. (defcustom org-highlight-sparse-tree-matches t
  1449. "Non-nil means highlight all matches that define a sparse tree.
  1450. The highlights will automatically disappear the next time the buffer is
  1451. changed by an edit command."
  1452. :group 'org-sparse-trees
  1453. :type 'boolean)
  1454. (defcustom org-remove-highlights-with-change t
  1455. "Non-nil means any change to the buffer will remove temporary highlights.
  1456. \\<org-mode-map>\
  1457. Such highlights are created by `org-occur' and `org-clock-display'.
  1458. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1459. to get rid of the highlights.
  1460. The highlights created by `org-toggle-latex-fragment' always need
  1461. `\\[org-toggle-latex-fragment]' to be removed."
  1462. :group 'org-sparse-trees
  1463. :group 'org-time
  1464. :type 'boolean)
  1465. (defcustom org-occur-hook '(org-first-headline-recenter)
  1466. "Hook that is run after `org-occur' has constructed a sparse tree.
  1467. This can be used to recenter the window to show as much of the structure
  1468. as possible."
  1469. :group 'org-sparse-trees
  1470. :type 'hook)
  1471. (defgroup org-imenu-and-speedbar nil
  1472. "Options concerning imenu and speedbar in Org-mode."
  1473. :tag "Org Imenu and Speedbar"
  1474. :group 'org-structure)
  1475. (defcustom org-imenu-depth 2
  1476. "The maximum level for Imenu access to Org-mode headlines.
  1477. This also applied for speedbar access."
  1478. :group 'org-imenu-and-speedbar
  1479. :type 'integer)
  1480. (defgroup org-table nil
  1481. "Options concerning tables in Org-mode."
  1482. :tag "Org Table"
  1483. :group 'org)
  1484. (defcustom org-enable-table-editor 'optimized
  1485. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1486. When nil, such lines will be treated like ordinary lines.
  1487. When equal to the symbol `optimized', the table editor will be optimized to
  1488. do the following:
  1489. - Automatic overwrite mode in front of whitespace in table fields.
  1490. This makes the structure of the table stay in tact as long as the edited
  1491. field does not exceed the column width.
  1492. - Minimize the number of realigns. Normally, the table is aligned each time
  1493. TAB or RET are pressed to move to another field. With optimization this
  1494. happens only if changes to a field might have changed the column width.
  1495. Optimization requires replacing the functions `self-insert-command',
  1496. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1497. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1498. very good at guessing when a re-align will be necessary, but you can always
  1499. force one with \\[org-ctrl-c-ctrl-c].
  1500. If you would like to use the optimized version in Org-mode, but the
  1501. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1502. This variable can be used to turn on and off the table editor during a session,
  1503. but in order to toggle optimization, a restart is required.
  1504. See also the variable `org-table-auto-blank-field'."
  1505. :group 'org-table
  1506. :type '(choice
  1507. (const :tag "off" nil)
  1508. (const :tag "on" t)
  1509. (const :tag "on, optimized" optimized)))
  1510. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1511. (version<= emacs-version "24.1"))
  1512. "Non-nil means cluster self-insert commands for undo when possible.
  1513. If this is set, then, like in the Emacs command loop, 20 consecutive
  1514. characters will be undone together.
  1515. This is configurable, because there is some impact on typing performance."
  1516. :group 'org-table
  1517. :type 'boolean)
  1518. (defcustom org-table-tab-recognizes-table.el t
  1519. "Non-nil means TAB will automatically notice a table.el table.
  1520. When it sees such a table, it moves point into it and - if necessary -
  1521. calls `table-recognize-table'."
  1522. :group 'org-table-editing
  1523. :type 'boolean)
  1524. (defgroup org-link nil
  1525. "Options concerning links in Org-mode."
  1526. :tag "Org Link"
  1527. :group 'org)
  1528. (defvar org-link-abbrev-alist-local nil
  1529. "Buffer-local version of `org-link-abbrev-alist', which see.
  1530. The value of this is taken from the #+LINK lines.")
  1531. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1532. (defcustom org-link-abbrev-alist nil
  1533. "Alist of link abbreviations.
  1534. The car of each element is a string, to be replaced at the start of a link.
  1535. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1536. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1537. [[linkkey:tag][description]]
  1538. The `linkkey' must be a word word, starting with a letter, followed
  1539. by letters, numbers, `-' or `_'.
  1540. If REPLACE is a string, the tag will simply be appended to create the link.
  1541. If the string contains \"%s\", the tag will be inserted there. If the string
  1542. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1543. at that point (see the function `url-hexify-string'). If the string contains
  1544. the specifier \"%(my-function)\", then the custom function `my-function' will
  1545. be invoked: this function takes the tag as its only argument and must return
  1546. a string.
  1547. REPLACE may also be a function that will be called with the tag as the
  1548. only argument to create the link, which should be returned as a string.
  1549. See the manual for examples."
  1550. :group 'org-link
  1551. :type '(repeat
  1552. (cons
  1553. (string :tag "Protocol")
  1554. (choice
  1555. (string :tag "Format")
  1556. (function)))))
  1557. (defcustom org-descriptive-links t
  1558. "Non-nil means Org will display descriptive links.
  1559. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1560. \"Org Website\", hiding the link itself and just displaying its
  1561. description. When set to nil, Org will display the full links
  1562. literally.
  1563. You can interactively set the value of this variable by calling
  1564. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1565. :group 'org-link
  1566. :type 'boolean)
  1567. (defcustom org-link-file-path-type 'adaptive
  1568. "How the path name in file links should be stored.
  1569. Valid values are:
  1570. relative Relative to the current directory, i.e. the directory of the file
  1571. into which the link is being inserted.
  1572. absolute Absolute path, if possible with ~ for home directory.
  1573. noabbrev Absolute path, no abbreviation of home directory.
  1574. adaptive Use relative path for files in the current directory and sub-
  1575. directories of it. For other files, use an absolute path."
  1576. :group 'org-link
  1577. :type '(choice
  1578. (const relative)
  1579. (const absolute)
  1580. (const noabbrev)
  1581. (const adaptive)))
  1582. (defvaralias 'org-activate-links 'org-highlight-links)
  1583. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1584. "Types of links that should be highlighted in Org-mode files.
  1585. This is a list of symbols, each one of them leading to the
  1586. highlighting of a certain link type.
  1587. You can still open links that are not highlighted.
  1588. In principle, it does not hurt to turn on highlighting for all
  1589. link types. There may be a small gain when turning off unused
  1590. link types. The types are:
  1591. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1592. angle Links in angular brackets that may contain whitespace like
  1593. <bbdb:Carsten Dominik>.
  1594. plain Plain links in normal text, no whitespace, like http://google.com.
  1595. radio Text that is matched by a radio target, see manual for details.
  1596. tag Tag settings in a headline (link to tag search).
  1597. date Time stamps (link to calendar).
  1598. footnote Footnote labels.
  1599. If you set this variable during an Emacs session, use `org-mode-restart'
  1600. in the Org buffer so that the change takes effect."
  1601. :group 'org-link
  1602. :group 'org-appearance
  1603. :type '(set :greedy t
  1604. (const :tag "Double bracket links" bracket)
  1605. (const :tag "Angular bracket links" angle)
  1606. (const :tag "Plain text links" plain)
  1607. (const :tag "Radio target matches" radio)
  1608. (const :tag "Tags" tag)
  1609. (const :tag "Timestamps" date)
  1610. (const :tag "Footnotes" footnote)))
  1611. (defcustom org-make-link-description-function nil
  1612. "Function to use for generating link descriptions from links.
  1613. When nil, the link location will be used. This function must take
  1614. two parameters: the first one is the link, the second one is the
  1615. description generated by `org-insert-link'. The function should
  1616. return the description to use."
  1617. :group 'org-link
  1618. :type '(choice (const nil) (function)))
  1619. (defgroup org-link-store nil
  1620. "Options concerning storing links in Org-mode."
  1621. :tag "Org Store Link"
  1622. :group 'org-link)
  1623. (defcustom org-url-hexify-p t
  1624. "When non-nil, hexify URL when creating a link."
  1625. :type 'boolean
  1626. :version "24.3"
  1627. :group 'org-link-store)
  1628. (defcustom org-email-link-description-format "Email %c: %.30s"
  1629. "Format of the description part of a link to an email or usenet message.
  1630. The following %-escapes will be replaced by corresponding information:
  1631. %F full \"From\" field
  1632. %f name, taken from \"From\" field, address if no name
  1633. %T full \"To\" field
  1634. %t first name in \"To\" field, address if no name
  1635. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1636. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1637. %s subject
  1638. %d date
  1639. %m message-id.
  1640. You may use normal field width specification between the % and the letter.
  1641. This is for example useful to limit the length of the subject.
  1642. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1643. :group 'org-link-store
  1644. :type 'string)
  1645. (defcustom org-from-is-user-regexp
  1646. (let (r1 r2)
  1647. (when (and user-mail-address (not (string= user-mail-address "")))
  1648. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1649. (when (and user-full-name (not (string= user-full-name "")))
  1650. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1651. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1652. "Regexp matched against the \"From:\" header of an email or usenet message.
  1653. It should match if the message is from the user him/herself."
  1654. :group 'org-link-store
  1655. :type 'regexp)
  1656. (defcustom org-context-in-file-links t
  1657. "Non-nil means file links from `org-store-link' contain context.
  1658. A search string will be added to the file name with :: as separator and
  1659. used to find the context when the link is activated by the command
  1660. `org-open-at-point'. When this option is t, the entire active region
  1661. will be placed in the search string of the file link. If set to a
  1662. positive integer, only the first n lines of context will be stored.
  1663. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1664. negates this setting for the duration of the command."
  1665. :group 'org-link-store
  1666. :type '(choice boolean integer))
  1667. (defcustom org-keep-stored-link-after-insertion nil
  1668. "Non-nil means keep link in list for entire session.
  1669. The command `org-store-link' adds a link pointing to the current
  1670. location to an internal list. These links accumulate during a session.
  1671. The command `org-insert-link' can be used to insert links into any
  1672. Org-mode file (offering completion for all stored links). When this
  1673. option is nil, every link which has been inserted once using \\[org-insert-link]
  1674. will be removed from the list, to make completing the unused links
  1675. more efficient."
  1676. :group 'org-link-store
  1677. :type 'boolean)
  1678. (defgroup org-link-follow nil
  1679. "Options concerning following links in Org-mode."
  1680. :tag "Org Follow Link"
  1681. :group 'org-link)
  1682. (defcustom org-link-translation-function nil
  1683. "Function to translate links with different syntax to Org syntax.
  1684. This can be used to translate links created for example by the Planner
  1685. or emacs-wiki packages to Org syntax.
  1686. The function must accept two parameters, a TYPE containing the link
  1687. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1688. which is everything after the link protocol. It should return a cons
  1689. with possibly modified values of type and path.
  1690. Org contains a function for this, so if you set this variable to
  1691. `org-translate-link-from-planner', you should be able follow many
  1692. links created by planner."
  1693. :group 'org-link-follow
  1694. :type '(choice (const nil) (function)))
  1695. (defcustom org-follow-link-hook nil
  1696. "Hook that is run after a link has been followed."
  1697. :group 'org-link-follow
  1698. :type 'hook)
  1699. (defcustom org-tab-follows-link nil
  1700. "Non-nil means on links TAB will follow the link.
  1701. Needs to be set before org.el is loaded.
  1702. This really should not be used, it does not make sense, and the
  1703. implementation is bad."
  1704. :group 'org-link-follow
  1705. :type 'boolean)
  1706. (defcustom org-return-follows-link nil
  1707. "Non-nil means on links RET will follow the link.
  1708. In tables, the special behavior of RET has precedence."
  1709. :group 'org-link-follow
  1710. :type 'boolean)
  1711. (defcustom org-mouse-1-follows-link
  1712. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1713. "Non-nil means mouse-1 on a link will follow the link.
  1714. A longer mouse click will still set point. Does not work on XEmacs.
  1715. Needs to be set before org.el is loaded."
  1716. :group 'org-link-follow
  1717. :version "24.4"
  1718. :package-version '(Org . "8.3")
  1719. :type '(choice
  1720. (const :tag "A double click follows the link" double)
  1721. (const :tag "Unconditionally follow the link with mouse-1" t)
  1722. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1723. (defcustom org-mark-ring-length 4
  1724. "Number of different positions to be recorded in the ring.
  1725. Changing this requires a restart of Emacs to work correctly."
  1726. :group 'org-link-follow
  1727. :type 'integer)
  1728. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1729. "Non-nil means internal links in Org files must exactly match a headline.
  1730. When nil, the link search tries to match a phrase with all words
  1731. in the search text."
  1732. :group 'org-link-follow
  1733. :version "24.1"
  1734. :type '(choice
  1735. (const :tag "Use fuzzy text search" nil)
  1736. (const :tag "Match only exact headline" t)
  1737. (const :tag "Match exact headline or query to create it"
  1738. query-to-create)))
  1739. (defcustom org-link-frame-setup
  1740. '((vm . vm-visit-folder-other-frame)
  1741. (vm-imap . vm-visit-imap-folder-other-frame)
  1742. (gnus . org-gnus-no-new-news)
  1743. (file . find-file-other-window)
  1744. (wl . wl-other-frame))
  1745. "Setup the frame configuration for following links.
  1746. When following a link with Emacs, it may often be useful to display
  1747. this link in another window or frame. This variable can be used to
  1748. set this up for the different types of links.
  1749. For VM, use any of
  1750. `vm-visit-folder'
  1751. `vm-visit-folder-other-window'
  1752. `vm-visit-folder-other-frame'
  1753. For Gnus, use any of
  1754. `gnus'
  1755. `gnus-other-frame'
  1756. `org-gnus-no-new-news'
  1757. For FILE, use any of
  1758. `find-file'
  1759. `find-file-other-window'
  1760. `find-file-other-frame'
  1761. For Wanderlust use any of
  1762. `wl'
  1763. `wl-other-frame'
  1764. For the calendar, use the variable `calendar-setup'.
  1765. For BBDB, it is currently only possible to display the matches in
  1766. another window."
  1767. :group 'org-link-follow
  1768. :type '(list
  1769. (cons (const vm)
  1770. (choice
  1771. (const vm-visit-folder)
  1772. (const vm-visit-folder-other-window)
  1773. (const vm-visit-folder-other-frame)))
  1774. (cons (const vm-imap)
  1775. (choice
  1776. (const vm-visit-imap-folder)
  1777. (const vm-visit-imap-folder-other-window)
  1778. (const vm-visit-imap-folder-other-frame)))
  1779. (cons (const gnus)
  1780. (choice
  1781. (const gnus)
  1782. (const gnus-other-frame)
  1783. (const org-gnus-no-new-news)))
  1784. (cons (const file)
  1785. (choice
  1786. (const find-file)
  1787. (const find-file-other-window)
  1788. (const find-file-other-frame)))
  1789. (cons (const wl)
  1790. (choice
  1791. (const wl)
  1792. (const wl-other-frame)))))
  1793. (defcustom org-display-internal-link-with-indirect-buffer nil
  1794. "Non-nil means use indirect buffer to display infile links.
  1795. Activating internal links (from one location in a file to another location
  1796. in the same file) normally just jumps to the location. When the link is
  1797. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1798. is displayed in
  1799. another window. When this option is set, the other window actually displays
  1800. an indirect buffer clone of the current buffer, to avoid any visibility
  1801. changes to the current buffer."
  1802. :group 'org-link-follow
  1803. :type 'boolean)
  1804. (defcustom org-open-non-existing-files nil
  1805. "Non-nil means `org-open-file' will open non-existing files.
  1806. When nil, an error will be generated.
  1807. This variable applies only to external applications because they
  1808. might choke on non-existing files. If the link is to a file that
  1809. will be opened in Emacs, the variable is ignored."
  1810. :group 'org-link-follow
  1811. :type 'boolean)
  1812. (defcustom org-open-directory-means-index-dot-org nil
  1813. "Non-nil means a link to a directory really means to index.org.
  1814. When nil, following a directory link will run dired or open a finder/explorer
  1815. window on that directory."
  1816. :group 'org-link-follow
  1817. :type 'boolean)
  1818. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1819. "Non-nil means ask for confirmation before executing shell links.
  1820. Shell links can be dangerous: just think about a link
  1821. [[shell:rm -rf ~/*][Google Search]]
  1822. This link would show up in your Org-mode document as \"Google Search\",
  1823. but really it would remove your entire home directory.
  1824. Therefore we advise against setting this variable to nil.
  1825. Just change it to `y-or-n-p' if you want to confirm with a
  1826. single keystroke rather than having to type \"yes\"."
  1827. :group 'org-link-follow
  1828. :type '(choice
  1829. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1830. (const :tag "with y-or-n (faster)" y-or-n-p)
  1831. (const :tag "no confirmation (dangerous)" nil)))
  1832. (put 'org-confirm-shell-link-function
  1833. 'safe-local-variable
  1834. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1835. (defcustom org-confirm-shell-link-not-regexp ""
  1836. "A regexp to skip confirmation for shell links."
  1837. :group 'org-link-follow
  1838. :version "24.1"
  1839. :type 'regexp)
  1840. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1841. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1842. Elisp links can be dangerous: just think about a link
  1843. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1844. This link would show up in your Org-mode document as \"Google Search\",
  1845. but really it would remove your entire home directory.
  1846. Therefore we advise against setting this variable to nil.
  1847. Just change it to `y-or-n-p' if you want to confirm with a
  1848. single keystroke rather than having to type \"yes\"."
  1849. :group 'org-link-follow
  1850. :type '(choice
  1851. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1852. (const :tag "with y-or-n (faster)" y-or-n-p)
  1853. (const :tag "no confirmation (dangerous)" nil)))
  1854. (put 'org-confirm-shell-link-function
  1855. 'safe-local-variable
  1856. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1857. (defcustom org-confirm-elisp-link-not-regexp ""
  1858. "A regexp to skip confirmation for Elisp links."
  1859. :group 'org-link-follow
  1860. :version "24.1"
  1861. :type 'regexp)
  1862. (defconst org-file-apps-defaults-gnu
  1863. '((remote . emacs)
  1864. (system . mailcap)
  1865. (t . mailcap))
  1866. "Default file applications on a UNIX or GNU/Linux system.
  1867. See `org-file-apps'.")
  1868. (defconst org-file-apps-defaults-macosx
  1869. '((remote . emacs)
  1870. (t . "open %s")
  1871. (system . "open %s")
  1872. ("ps.gz" . "gv %s")
  1873. ("eps.gz" . "gv %s")
  1874. ("dvi" . "xdvi %s")
  1875. ("fig" . "xfig %s"))
  1876. "Default file applications on a MacOS X system.
  1877. The system \"open\" is known as a default, but we use X11 applications
  1878. for some files for which the OS does not have a good default.
  1879. See `org-file-apps'.")
  1880. (defconst org-file-apps-defaults-windowsnt
  1881. (list
  1882. '(remote . emacs)
  1883. (cons t
  1884. (list (if (featurep 'xemacs)
  1885. 'mswindows-shell-execute
  1886. 'w32-shell-execute)
  1887. "open" 'file))
  1888. (cons 'system
  1889. (list (if (featurep 'xemacs)
  1890. 'mswindows-shell-execute
  1891. 'w32-shell-execute)
  1892. "open" 'file)))
  1893. "Default file applications on a Windows NT system.
  1894. The system \"open\" is used for most files.
  1895. See `org-file-apps'.")
  1896. (defcustom org-file-apps
  1897. '((auto-mode . emacs)
  1898. ("\\.mm\\'" . default)
  1899. ("\\.x?html?\\'" . default)
  1900. ("\\.pdf\\'" . default))
  1901. "External applications for opening `file:path' items in a document.
  1902. Org-mode uses system defaults for different file types, but
  1903. you can use this variable to set the application for a given file
  1904. extension. The entries in this list are cons cells where the car identifies
  1905. files and the cdr the corresponding command. Possible values for the
  1906. file identifier are
  1907. \"string\" A string as a file identifier can be interpreted in different
  1908. ways, depending on its contents:
  1909. - Alphanumeric characters only:
  1910. Match links with this file extension.
  1911. Example: (\"pdf\" . \"evince %s\")
  1912. to open PDFs with evince.
  1913. - Regular expression: Match links where the
  1914. filename matches the regexp. If you want to
  1915. use groups here, use shy groups.
  1916. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1917. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1918. to open *.html and *.xhtml with firefox.
  1919. - Regular expression which contains (non-shy) groups:
  1920. Match links where the whole link, including \"::\", and
  1921. anything after that, matches the regexp.
  1922. In a custom command string, %1, %2, etc. are replaced with
  1923. the parts of the link that were matched by the groups.
  1924. For backwards compatibility, if a command string is given
  1925. that does not use any of the group matches, this case is
  1926. handled identically to the second one (i.e. match against
  1927. file name only).
  1928. In a custom lisp form, you can access the group matches with
  1929. (match-string n link).
  1930. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1931. to open [[file:document.pdf::5]] with evince at page 5.
  1932. `directory' Matches a directory
  1933. `remote' Matches a remote file, accessible through tramp or efs.
  1934. Remote files most likely should be visited through Emacs
  1935. because external applications cannot handle such paths.
  1936. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1937. so all files Emacs knows how to handle. Using this with
  1938. command `emacs' will open most files in Emacs. Beware that this
  1939. will also open html files inside Emacs, unless you add
  1940. (\"html\" . default) to the list as well.
  1941. t Default for files not matched by any of the other options.
  1942. `system' The system command to open files, like `open' on Windows
  1943. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1944. that will be selected if you call `C-c C-o' with a double
  1945. \\[universal-argument] \\[universal-argument] prefix.
  1946. Possible values for the command are:
  1947. `emacs' The file will be visited by the current Emacs process.
  1948. `default' Use the default application for this file type, which is the
  1949. association for t in the list, most likely in the system-specific
  1950. part.
  1951. This can be used to overrule an unwanted setting in the
  1952. system-specific variable.
  1953. `system' Use the system command for opening files, like \"open\".
  1954. This command is specified by the entry whose car is `system'.
  1955. Most likely, the system-specific version of this variable
  1956. does define this command, but you can overrule/replace it
  1957. here.
  1958. string A command to be executed by a shell; %s will be replaced
  1959. by the path to the file.
  1960. sexp A Lisp form which will be evaluated. The file path will
  1961. be available in the Lisp variable `file'.
  1962. For more examples, see the system specific constants
  1963. `org-file-apps-defaults-macosx'
  1964. `org-file-apps-defaults-windowsnt'
  1965. `org-file-apps-defaults-gnu'."
  1966. :group 'org-link-follow
  1967. :type '(repeat
  1968. (cons (choice :value ""
  1969. (string :tag "Extension")
  1970. (const :tag "System command to open files" system)
  1971. (const :tag "Default for unrecognized files" t)
  1972. (const :tag "Remote file" remote)
  1973. (const :tag "Links to a directory" directory)
  1974. (const :tag "Any files that have Emacs modes"
  1975. auto-mode))
  1976. (choice :value ""
  1977. (const :tag "Visit with Emacs" emacs)
  1978. (const :tag "Use default" default)
  1979. (const :tag "Use the system command" system)
  1980. (string :tag "Command")
  1981. (sexp :tag "Lisp form")))))
  1982. (defcustom org-doi-server-url "http://dx.doi.org/"
  1983. "The URL of the DOI server."
  1984. :type 'string
  1985. :version "24.3"
  1986. :group 'org-link-follow)
  1987. (defgroup org-refile nil
  1988. "Options concerning refiling entries in Org-mode."
  1989. :tag "Org Refile"
  1990. :group 'org)
  1991. (defcustom org-directory "~/org"
  1992. "Directory with Org files.
  1993. This is just a default location to look for Org files. There is no need
  1994. at all to put your files into this directory. It is used in the
  1995. following situations:
  1996. 1. When a capture template specifies a target file that is not an
  1997. absolute path. The path will then be interpreted relative to
  1998. `org-directory'
  1999. 2. When the value of variable `org-agenda-files' is a single file, any
  2000. relative paths in this file will be taken as relative to
  2001. `org-directory'."
  2002. :group 'org-refile
  2003. :group 'org-capture
  2004. :type 'directory)
  2005. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2006. "Default target for storing notes.
  2007. Used as a fall back file for org-capture.el, for templates that
  2008. do not specify a target file."
  2009. :group 'org-refile
  2010. :group 'org-capture
  2011. :type 'file)
  2012. (defcustom org-goto-interface 'outline
  2013. "The default interface to be used for `org-goto'.
  2014. Allowed values are:
  2015. outline The interface shows an outline of the relevant file
  2016. and the correct heading is found by moving through
  2017. the outline or by searching with incremental search.
  2018. outline-path-completion Headlines in the current buffer are offered via
  2019. completion. This is the interface also used by
  2020. the refile command."
  2021. :group 'org-refile
  2022. :type '(choice
  2023. (const :tag "Outline" outline)
  2024. (const :tag "Outline-path-completion" outline-path-completion)))
  2025. (defcustom org-goto-max-level 5
  2026. "Maximum target level when running `org-goto' with refile interface."
  2027. :group 'org-refile
  2028. :type 'integer)
  2029. (defcustom org-reverse-note-order nil
  2030. "Non-nil means store new notes at the beginning of a file or entry.
  2031. When nil, new notes will be filed to the end of a file or entry.
  2032. This can also be a list with cons cells of regular expressions that
  2033. are matched against file names, and values."
  2034. :group 'org-capture
  2035. :group 'org-refile
  2036. :type '(choice
  2037. (const :tag "Reverse always" t)
  2038. (const :tag "Reverse never" nil)
  2039. (repeat :tag "By file name regexp"
  2040. (cons regexp boolean))))
  2041. (defcustom org-log-refile nil
  2042. "Information to record when a task is refiled.
  2043. Possible values are:
  2044. nil Don't add anything
  2045. time Add a time stamp to the task
  2046. note Prompt for a note and add it with template `org-log-note-headings'
  2047. This option can also be set with on a per-file-basis with
  2048. #+STARTUP: nologrefile
  2049. #+STARTUP: logrefile
  2050. #+STARTUP: lognoterefile
  2051. You can have local logging settings for a subtree by setting the LOGGING
  2052. property to one or more of these keywords.
  2053. When bulk-refiling from the agenda, the value `note' is forbidden and
  2054. will temporarily be changed to `time'."
  2055. :group 'org-refile
  2056. :group 'org-progress
  2057. :version "24.1"
  2058. :type '(choice
  2059. (const :tag "No logging" nil)
  2060. (const :tag "Record timestamp" time)
  2061. (const :tag "Record timestamp with note." note)))
  2062. (defcustom org-refile-targets nil
  2063. "Targets for refiling entries with \\[org-refile].
  2064. This is a list of cons cells. Each cell contains:
  2065. - a specification of the files to be considered, either a list of files,
  2066. or a symbol whose function or variable value will be used to retrieve
  2067. a file name or a list of file names. If you use `org-agenda-files' for
  2068. that, all agenda files will be scanned for targets. Nil means consider
  2069. headings in the current buffer.
  2070. - A specification of how to find candidate refile targets. This may be
  2071. any of:
  2072. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2073. This tag has to be present in all target headlines, inheritance will
  2074. not be considered.
  2075. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2076. todo keyword.
  2077. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2078. headlines that are refiling targets.
  2079. - a cons cell (:level . N). Any headline of level N is considered a target.
  2080. Note that, when `org-odd-levels-only' is set, level corresponds to
  2081. order in hierarchy, not to the number of stars.
  2082. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2083. Note that, when `org-odd-levels-only' is set, level corresponds to
  2084. order in hierarchy, not to the number of stars.
  2085. Each element of this list generates a set of possible targets.
  2086. The union of these sets is presented (with completion) to
  2087. the user by `org-refile'.
  2088. You can set the variable `org-refile-target-verify-function' to a function
  2089. to verify each headline found by the simple criteria above.
  2090. When this variable is nil, all top-level headlines in the current buffer
  2091. are used, equivalent to the value `((nil . (:level . 1))'."
  2092. :group 'org-refile
  2093. :type '(repeat
  2094. (cons
  2095. (choice :value org-agenda-files
  2096. (const :tag "All agenda files" org-agenda-files)
  2097. (const :tag "Current buffer" nil)
  2098. (function) (variable) (file))
  2099. (choice :tag "Identify target headline by"
  2100. (cons :tag "Specific tag" (const :value :tag) (string))
  2101. (cons :tag "TODO keyword" (const :value :todo) (string))
  2102. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2103. (cons :tag "Level number" (const :value :level) (integer))
  2104. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2105. (defcustom org-refile-target-verify-function nil
  2106. "Function to verify if the headline at point should be a refile target.
  2107. The function will be called without arguments, with point at the
  2108. beginning of the headline. It should return t and leave point
  2109. where it is if the headline is a valid target for refiling.
  2110. If the target should not be selected, the function must return nil.
  2111. In addition to this, it may move point to a place from where the search
  2112. should be continued. For example, the function may decide that the entire
  2113. subtree of the current entry should be excluded and move point to the end
  2114. of the subtree."
  2115. :group 'org-refile
  2116. :type '(choice
  2117. (const nil)
  2118. (function)))
  2119. (defcustom org-refile-use-cache nil
  2120. "Non-nil means cache refile targets to speed up the process.
  2121. \\<org-mode-map>\
  2122. The cache for a particular file will be updated automatically when
  2123. the buffer has been killed, or when any of the marker used for flagging
  2124. refile targets no longer points at a live buffer.
  2125. If you have added new entries to a buffer that might themselves be targets,
  2126. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2127. if you find that easier, \
  2128. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2129. \\[org-refile]'."
  2130. :group 'org-refile
  2131. :version "24.1"
  2132. :type 'boolean)
  2133. (defcustom org-refile-use-outline-path nil
  2134. "Non-nil means provide refile targets as paths.
  2135. So a level 3 headline will be available as level1/level2/level3.
  2136. When the value is `file', also include the file name (without directory)
  2137. into the path. In this case, you can also stop the completion after
  2138. the file name, to get entries inserted as top level in the file.
  2139. When `full-file-path', include the full file path."
  2140. :group 'org-refile
  2141. :type '(choice
  2142. (const :tag "Not" nil)
  2143. (const :tag "Yes" t)
  2144. (const :tag "Start with file name" file)
  2145. (const :tag "Start with full file path" full-file-path)))
  2146. (defcustom org-outline-path-complete-in-steps t
  2147. "Non-nil means complete the outline path in hierarchical steps.
  2148. When Org-mode uses the refile interface to select an outline path
  2149. \(see variable `org-refile-use-outline-path'), the completion of
  2150. the path can be done in a single go, or it can be done in steps down
  2151. the headline hierarchy. Going in steps is probably the best if you
  2152. do not use a special completion package like `ido' or `icicles'.
  2153. However, when using these packages, going in one step can be very
  2154. fast, while still showing the whole path to the entry."
  2155. :group 'org-refile
  2156. :type 'boolean)
  2157. (defcustom org-refile-allow-creating-parent-nodes nil
  2158. "Non-nil means allow the creation of new nodes as refile targets.
  2159. New nodes are then created by adding \"/new node name\" to the completion
  2160. of an existing node. When the value of this variable is `confirm',
  2161. new node creation must be confirmed by the user (recommended).
  2162. When nil, the completion must match an existing entry.
  2163. Note that, if the new heading is not seen by the criteria
  2164. listed in `org-refile-targets', multiple instances of the same
  2165. heading would be created by trying again to file under the new
  2166. heading."
  2167. :group 'org-refile
  2168. :type '(choice
  2169. (const :tag "Never" nil)
  2170. (const :tag "Always" t)
  2171. (const :tag "Prompt for confirmation" confirm)))
  2172. (defcustom org-refile-active-region-within-subtree nil
  2173. "Non-nil means also refile active region within a subtree.
  2174. By default `org-refile' doesn't allow refiling regions if they
  2175. don't contain a set of subtrees, but it might be convenient to
  2176. do so sometimes: in that case, the first line of the region is
  2177. converted to a headline before refiling."
  2178. :group 'org-refile
  2179. :version "24.1"
  2180. :type 'boolean)
  2181. (defgroup org-todo nil
  2182. "Options concerning TODO items in Org-mode."
  2183. :tag "Org TODO"
  2184. :group 'org)
  2185. (defgroup org-progress nil
  2186. "Options concerning Progress logging in Org-mode."
  2187. :tag "Org Progress"
  2188. :group 'org-time)
  2189. (defvar org-todo-interpretation-widgets
  2190. '((:tag "Sequence (cycling hits every state)" sequence)
  2191. (:tag "Type (cycling directly to DONE)" type))
  2192. "The available interpretation symbols for customizing `org-todo-keywords'.
  2193. Interested libraries should add to this list.")
  2194. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2195. "List of TODO entry keyword sequences and their interpretation.
  2196. \\<org-mode-map>This is a list of sequences.
  2197. Each sequence starts with a symbol, either `sequence' or `type',
  2198. indicating if the keywords should be interpreted as a sequence of
  2199. action steps, or as different types of TODO items. The first
  2200. keywords are states requiring action - these states will select a headline
  2201. for inclusion into the global TODO list Org-mode produces. If one of
  2202. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2203. signify that no further action is necessary. If \"|\" is not found,
  2204. the last keyword is treated as the only DONE state of the sequence.
  2205. The command \\[org-todo] cycles an entry through these states, and one
  2206. additional state where no keyword is present. For details about this
  2207. cycling, see the manual.
  2208. TODO keywords and interpretation can also be set on a per-file basis with
  2209. the special #+SEQ_TODO and #+TYP_TODO lines.
  2210. Each keyword can optionally specify a character for fast state selection
  2211. \(in combination with the variable `org-use-fast-todo-selection')
  2212. and specifiers for state change logging, using the same syntax that
  2213. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2214. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2215. indicates to record a time stamp each time this state is selected.
  2216. Each keyword may also specify if a timestamp or a note should be
  2217. recorded when entering or leaving the state, by adding additional
  2218. characters in the parenthesis after the keyword. This looks like this:
  2219. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2220. record only the time of the state change. With X and Y being either
  2221. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2222. Y when leaving the state if and only if the *target* state does not
  2223. define X. You may omit any of the fast-selection key or X or /Y,
  2224. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2225. For backward compatibility, this variable may also be just a list
  2226. of keywords. In this case the interpretation (sequence or type) will be
  2227. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2228. :group 'org-todo
  2229. :group 'org-keywords
  2230. :type '(choice
  2231. (repeat :tag "Old syntax, just keywords"
  2232. (string :tag "Keyword"))
  2233. (repeat :tag "New syntax"
  2234. (cons
  2235. (choice
  2236. :tag "Interpretation"
  2237. ;;Quick and dirty way to see
  2238. ;;`org-todo-interpretations'. This takes the
  2239. ;;place of item arguments
  2240. :convert-widget
  2241. (lambda (widget)
  2242. (widget-put widget
  2243. :args (mapcar
  2244. (lambda (x)
  2245. (widget-convert
  2246. (cons 'const x)))
  2247. org-todo-interpretation-widgets))
  2248. widget))
  2249. (repeat
  2250. (string :tag "Keyword"))))))
  2251. (defvar org-todo-keywords-1 nil
  2252. "All TODO and DONE keywords active in a buffer.")
  2253. (make-variable-buffer-local 'org-todo-keywords-1)
  2254. (defvar org-todo-keywords-for-agenda nil)
  2255. (defvar org-done-keywords-for-agenda nil)
  2256. (defvar org-todo-keyword-alist-for-agenda nil)
  2257. (defvar org-tag-alist-for-agenda nil
  2258. "Alist of all tags from all agenda files.")
  2259. (defvar org-tag-groups-alist-for-agenda nil
  2260. "Alist of all groups tags from all current agenda files.")
  2261. (defvar org-tag-groups-alist nil)
  2262. (make-variable-buffer-local 'org-tag-groups-alist)
  2263. (defvar org-agenda-contributing-files nil)
  2264. (defvar org-not-done-keywords nil)
  2265. (make-variable-buffer-local 'org-not-done-keywords)
  2266. (defvar org-done-keywords nil)
  2267. (make-variable-buffer-local 'org-done-keywords)
  2268. (defvar org-todo-heads nil)
  2269. (make-variable-buffer-local 'org-todo-heads)
  2270. (defvar org-todo-sets nil)
  2271. (make-variable-buffer-local 'org-todo-sets)
  2272. (defvar org-todo-log-states nil)
  2273. (make-variable-buffer-local 'org-todo-log-states)
  2274. (defvar org-todo-kwd-alist nil)
  2275. (make-variable-buffer-local 'org-todo-kwd-alist)
  2276. (defvar org-todo-key-alist nil)
  2277. (make-variable-buffer-local 'org-todo-key-alist)
  2278. (defvar org-todo-key-trigger nil)
  2279. (make-variable-buffer-local 'org-todo-key-trigger)
  2280. (defcustom org-todo-interpretation 'sequence
  2281. "Controls how TODO keywords are interpreted.
  2282. This variable is in principle obsolete and is only used for
  2283. backward compatibility, if the interpretation of todo keywords is
  2284. not given already in `org-todo-keywords'. See that variable for
  2285. more information."
  2286. :group 'org-todo
  2287. :group 'org-keywords
  2288. :type '(choice (const sequence)
  2289. (const type)))
  2290. (defcustom org-use-fast-todo-selection t
  2291. "\\<org-mode-map>\
  2292. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2293. This variable describes if and under what circumstances the cycling
  2294. mechanism for TODO keywords will be replaced by a single-key, direct
  2295. selection scheme.
  2296. When nil, fast selection is never used.
  2297. When the symbol `prefix', it will be used when `org-todo' is called
  2298. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2299. in an Org-mode buffer, and
  2300. `\\[universal-argument] t' in an agenda buffer.
  2301. When t, fast selection is used by default. In this case, the prefix
  2302. argument forces cycling instead.
  2303. In all cases, the special interface is only used if access keys have
  2304. actually been assigned by the user, i.e. if keywords in the configuration
  2305. are followed by a letter in parenthesis, like TODO(t)."
  2306. :group 'org-todo
  2307. :type '(choice
  2308. (const :tag "Never" nil)
  2309. (const :tag "By default" t)
  2310. (const :tag "Only with C-u C-c C-t" prefix)))
  2311. (defcustom org-provide-todo-statistics t
  2312. "Non-nil means update todo statistics after insert and toggle.
  2313. ALL-HEADLINES means update todo statistics by including headlines
  2314. with no TODO keyword as well, counting them as not done.
  2315. A list of TODO keywords means the same, but skip keywords that are
  2316. not in this list.
  2317. When set to a list of two lists, the first list contains keywords
  2318. to consider as TODO keywords, the second list contains keywords
  2319. to consider as DONE keywords.
  2320. When this is set, todo statistics is updated in the parent of the
  2321. current entry each time a todo state is changed."
  2322. :group 'org-todo
  2323. :type '(choice
  2324. (const :tag "Yes, only for TODO entries" t)
  2325. (const :tag "Yes, including all entries" all-headlines)
  2326. (repeat :tag "Yes, for TODOs in this list"
  2327. (string :tag "TODO keyword"))
  2328. (list :tag "Yes, for TODOs and DONEs in these lists"
  2329. (repeat (string :tag "TODO keyword"))
  2330. (repeat (string :tag "DONE keyword")))
  2331. (other :tag "No TODO statistics" nil)))
  2332. (defcustom org-hierarchical-todo-statistics t
  2333. "Non-nil means TODO statistics covers just direct children.
  2334. When nil, all entries in the subtree are considered.
  2335. This has only an effect if `org-provide-todo-statistics' is set.
  2336. To set this to nil for only a single subtree, use a COOKIE_DATA
  2337. property and include the word \"recursive\" into the value."
  2338. :group 'org-todo
  2339. :type 'boolean)
  2340. (defcustom org-after-todo-state-change-hook nil
  2341. "Hook which is run after the state of a TODO item was changed.
  2342. The new state (a string with a TODO keyword, or nil) is available in the
  2343. Lisp variable `org-state'."
  2344. :group 'org-todo
  2345. :type 'hook)
  2346. (defvar org-blocker-hook nil
  2347. "Hook for functions that are allowed to block a state change.
  2348. Functions in this hook should not modify the buffer.
  2349. Each function gets as its single argument a property list,
  2350. see `org-trigger-hook' for more information about this list.
  2351. If any of the functions in this hook returns nil, the state change
  2352. is blocked.")
  2353. (defvar org-trigger-hook nil
  2354. "Hook for functions that are triggered by a state change.
  2355. Each function gets as its single argument a property list with at
  2356. least the following elements:
  2357. (:type type-of-change :position pos-at-entry-start
  2358. :from old-state :to new-state)
  2359. Depending on the type, more properties may be present.
  2360. This mechanism is currently implemented for:
  2361. TODO state changes
  2362. ------------------
  2363. :type todo-state-change
  2364. :from previous state (keyword as a string), or nil, or a symbol
  2365. `todo' or `done', to indicate the general type of state.
  2366. :to new state, like in :from")
  2367. (defcustom org-enforce-todo-dependencies nil
  2368. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2369. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2370. be blocked if any prior sibling is not yet done.
  2371. Finally, if the parent is blocked because of ordered siblings of its own,
  2372. the child will also be blocked."
  2373. :set (lambda (var val)
  2374. (set var val)
  2375. (if val
  2376. (add-hook 'org-blocker-hook
  2377. 'org-block-todo-from-children-or-siblings-or-parent)
  2378. (remove-hook 'org-blocker-hook
  2379. 'org-block-todo-from-children-or-siblings-or-parent)))
  2380. :group 'org-todo
  2381. :type 'boolean)
  2382. (defcustom org-enforce-todo-checkbox-dependencies nil
  2383. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2384. When this is nil, checkboxes have no influence on switching TODO states.
  2385. When non-nil, you first need to check off all check boxes before the TODO
  2386. entry can be switched to DONE.
  2387. This variable needs to be set before org.el is loaded, and you need to
  2388. restart Emacs after a change to make the change effective. The only way
  2389. to change is while Emacs is running is through the customize interface."
  2390. :set (lambda (var val)
  2391. (set var val)
  2392. (if val
  2393. (add-hook 'org-blocker-hook
  2394. 'org-block-todo-from-checkboxes)
  2395. (remove-hook 'org-blocker-hook
  2396. 'org-block-todo-from-checkboxes)))
  2397. :group 'org-todo
  2398. :type 'boolean)
  2399. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2400. "Non-nil means inserting a TODO heading is treated as state change.
  2401. So when the command \\[org-insert-todo-heading] is used, state change
  2402. logging will apply if appropriate. When nil, the new TODO item will
  2403. be inserted directly, and no logging will take place."
  2404. :group 'org-todo
  2405. :type 'boolean)
  2406. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2407. "Non-nil means switching TODO states with S-cursor counts as state change.
  2408. This is the default behavior. However, setting this to nil allows a
  2409. convenient way to select a TODO state and bypass any logging associated
  2410. with that."
  2411. :group 'org-todo
  2412. :type 'boolean)
  2413. (defcustom org-todo-state-tags-triggers nil
  2414. "Tag changes that should be triggered by TODO state changes.
  2415. This is a list. Each entry is
  2416. (state-change (tag . flag) .......)
  2417. State-change can be a string with a state, and empty string to indicate the
  2418. state that has no TODO keyword, or it can be one of the symbols `todo'
  2419. or `done', meaning any not-done or done state, respectively."
  2420. :group 'org-todo
  2421. :group 'org-tags
  2422. :type '(repeat
  2423. (cons (choice :tag "When changing to"
  2424. (const :tag "Not-done state" todo)
  2425. (const :tag "Done state" done)
  2426. (string :tag "State"))
  2427. (repeat
  2428. (cons :tag "Tag action"
  2429. (string :tag "Tag")
  2430. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2431. (defcustom org-log-done nil
  2432. "Information to record when a task moves to the DONE state.
  2433. Possible values are:
  2434. nil Don't add anything, just change the keyword
  2435. time Add a time stamp to the task
  2436. note Prompt for a note and add it with template `org-log-note-headings'
  2437. This option can also be set with on a per-file-basis with
  2438. #+STARTUP: nologdone
  2439. #+STARTUP: logdone
  2440. #+STARTUP: lognotedone
  2441. You can have local logging settings for a subtree by setting the LOGGING
  2442. property to one or more of these keywords."
  2443. :group 'org-todo
  2444. :group 'org-progress
  2445. :type '(choice
  2446. (const :tag "No logging" nil)
  2447. (const :tag "Record CLOSED timestamp" time)
  2448. (const :tag "Record CLOSED timestamp with note." note)))
  2449. ;; Normalize old uses of org-log-done.
  2450. (cond
  2451. ((eq org-log-done t) (setq org-log-done 'time))
  2452. ((and (listp org-log-done) (memq 'done org-log-done))
  2453. (setq org-log-done 'note)))
  2454. (defcustom org-log-reschedule nil
  2455. "Information to record when the scheduling date of a tasks is modified.
  2456. Possible values are:
  2457. nil Don't add anything, just change the date
  2458. time Add a time stamp to the task
  2459. note Prompt for a note and add it with template `org-log-note-headings'
  2460. This option can also be set with on a per-file-basis with
  2461. #+STARTUP: nologreschedule
  2462. #+STARTUP: logreschedule
  2463. #+STARTUP: lognotereschedule"
  2464. :group 'org-todo
  2465. :group 'org-progress
  2466. :type '(choice
  2467. (const :tag "No logging" nil)
  2468. (const :tag "Record timestamp" time)
  2469. (const :tag "Record timestamp with note." note)))
  2470. (defcustom org-log-redeadline nil
  2471. "Information to record when the deadline date of a tasks is modified.
  2472. Possible values are:
  2473. nil Don't add anything, just change the date
  2474. time Add a time stamp to the task
  2475. note Prompt for a note and add it with template `org-log-note-headings'
  2476. This option can also be set with on a per-file-basis with
  2477. #+STARTUP: nologredeadline
  2478. #+STARTUP: logredeadline
  2479. #+STARTUP: lognoteredeadline
  2480. You can have local logging settings for a subtree by setting the LOGGING
  2481. property to one or more of these keywords."
  2482. :group 'org-todo
  2483. :group 'org-progress
  2484. :type '(choice
  2485. (const :tag "No logging" nil)
  2486. (const :tag "Record timestamp" time)
  2487. (const :tag "Record timestamp with note." note)))
  2488. (defcustom org-log-note-clock-out nil
  2489. "Non-nil means record a note when clocking out of an item.
  2490. This can also be configured on a per-file basis by adding one of
  2491. the following lines anywhere in the buffer:
  2492. #+STARTUP: lognoteclock-out
  2493. #+STARTUP: nolognoteclock-out"
  2494. :group 'org-todo
  2495. :group 'org-progress
  2496. :type 'boolean)
  2497. (defcustom org-log-done-with-time t
  2498. "Non-nil means the CLOSED time stamp will contain date and time.
  2499. When nil, only the date will be recorded."
  2500. :group 'org-progress
  2501. :type 'boolean)
  2502. (defcustom org-log-note-headings
  2503. '((done . "CLOSING NOTE %t")
  2504. (state . "State %-12s from %-12S %t")
  2505. (note . "Note taken on %t")
  2506. (reschedule . "Rescheduled from %S on %t")
  2507. (delschedule . "Not scheduled, was %S on %t")
  2508. (redeadline . "New deadline from %S on %t")
  2509. (deldeadline . "Removed deadline, was %S on %t")
  2510. (refile . "Refiled on %t")
  2511. (clock-out . ""))
  2512. "Headings for notes added to entries.
  2513. The value is an alist, with the car being a symbol indicating the
  2514. note context, and the cdr is the heading to be used. The heading
  2515. may also be the empty string. The following placeholders can be
  2516. used:
  2517. %t a time stamp.
  2518. %T an active time stamp instead the default inactive one
  2519. %d a short-format time stamp.
  2520. %D an active short-format time stamp.
  2521. %s the new TODO state or time stamp (inactive), in double quotes.
  2522. %S the old TODO state or time stamp (inactive), in double quotes.
  2523. %u the user name.
  2524. %U full user name.
  2525. In fact, it is not a good idea to change the `state' entry,
  2526. because Agenda Log mode depends on the format of these entries."
  2527. :group 'org-todo
  2528. :group 'org-progress
  2529. :type '(list :greedy t
  2530. (cons (const :tag "Heading when closing an item" done) string)
  2531. (cons (const :tag
  2532. "Heading when changing todo state (todo sequence only)"
  2533. state) string)
  2534. (cons (const :tag "Heading when just taking a note" note) string)
  2535. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2536. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2537. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2538. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2539. (cons (const :tag "Heading when refiling" refile) string)
  2540. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2541. (unless (assq 'note org-log-note-headings)
  2542. (push '(note . "%t") org-log-note-headings))
  2543. (defcustom org-log-into-drawer nil
  2544. "Non-nil means insert state change notes and time stamps into a drawer.
  2545. When nil, state changes notes will be inserted after the headline and
  2546. any scheduling and clock lines, but not inside a drawer.
  2547. The value of this variable should be the name of the drawer to use.
  2548. LOGBOOK is proposed as the default drawer for this purpose, you can
  2549. also set this to a string to define the drawer of your choice.
  2550. A value of t is also allowed, representing \"LOGBOOK\".
  2551. A value of t or nil can also be set with on a per-file-basis with
  2552. #+STARTUP: logdrawer
  2553. #+STARTUP: nologdrawer
  2554. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2555. will be ignored.
  2556. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2557. a subtree.
  2558. Do not check directly this variable in a Lisp program. Call
  2559. function `org-log-into-drawer' instead."
  2560. :group 'org-todo
  2561. :group 'org-progress
  2562. :type '(choice
  2563. (const :tag "Not into a drawer" nil)
  2564. (const :tag "LOGBOOK" t)
  2565. (string :tag "Other")))
  2566. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2567. (defun org-log-into-drawer ()
  2568. "Name of the log drawer, as a string, or nil.
  2569. This is the value of `org-log-into-drawer'. However, if the
  2570. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2571. be used instead of the default value."
  2572. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2573. (cond ((equal p "nil") nil)
  2574. ((equal p "t") "LOGBOOK")
  2575. ((stringp p) p)
  2576. (p "LOGBOOK")
  2577. ((stringp org-log-into-drawer) org-log-into-drawer)
  2578. (org-log-into-drawer "LOGBOOK"))))
  2579. (defcustom org-log-state-notes-insert-after-drawers nil
  2580. "Non-nil means insert state change notes after any drawers in entry.
  2581. Only the drawers that *immediately* follow the headline and the
  2582. deadline/scheduled line are skipped.
  2583. When nil, insert notes right after the heading and perhaps the line
  2584. with deadline/scheduling if present.
  2585. This variable will have no effect if `org-log-into-drawer' is
  2586. set."
  2587. :group 'org-todo
  2588. :group 'org-progress
  2589. :type 'boolean)
  2590. (defcustom org-log-states-order-reversed t
  2591. "Non-nil means the latest state note will be directly after heading.
  2592. When nil, the state change notes will be ordered according to time.
  2593. This option can also be set with on a per-file-basis with
  2594. #+STARTUP: logstatesreversed
  2595. #+STARTUP: nologstatesreversed"
  2596. :group 'org-todo
  2597. :group 'org-progress
  2598. :type 'boolean)
  2599. (defcustom org-todo-repeat-to-state nil
  2600. "The TODO state to which a repeater should return the repeating task.
  2601. By default this is the first task in a TODO sequence, or the previous state
  2602. in a TODO_TYP set. But you can specify another task here.
  2603. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2604. :group 'org-todo
  2605. :version "24.1"
  2606. :type '(choice (const :tag "Head of sequence" nil)
  2607. (string :tag "Specific state")))
  2608. (defcustom org-log-repeat 'time
  2609. "Non-nil means record moving through the DONE state when triggering repeat.
  2610. An auto-repeating task is immediately switched back to TODO when
  2611. marked DONE. If you are not logging state changes (by adding \"@\"
  2612. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2613. record a closing note, there will be no record of the task moving
  2614. through DONE. This variable forces taking a note anyway.
  2615. nil Don't force a record
  2616. time Record a time stamp
  2617. note Prompt for a note and add it with template `org-log-note-headings'
  2618. This option can also be set with on a per-file-basis with
  2619. #+STARTUP: nologrepeat
  2620. #+STARTUP: logrepeat
  2621. #+STARTUP: lognoterepeat
  2622. You can have local logging settings for a subtree by setting the LOGGING
  2623. property to one or more of these keywords."
  2624. :group 'org-todo
  2625. :group 'org-progress
  2626. :type '(choice
  2627. (const :tag "Don't force a record" nil)
  2628. (const :tag "Force recording the DONE state" time)
  2629. (const :tag "Force recording a note with the DONE state" note)))
  2630. (defgroup org-priorities nil
  2631. "Priorities in Org-mode."
  2632. :tag "Org Priorities"
  2633. :group 'org-todo)
  2634. (defcustom org-enable-priority-commands t
  2635. "Non-nil means priority commands are active.
  2636. When nil, these commands will be disabled, so that you never accidentally
  2637. set a priority."
  2638. :group 'org-priorities
  2639. :type 'boolean)
  2640. (defcustom org-highest-priority ?A
  2641. "The highest priority of TODO items. A character like ?A, ?B etc.
  2642. Must have a smaller ASCII number than `org-lowest-priority'."
  2643. :group 'org-priorities
  2644. :type 'character)
  2645. (defcustom org-lowest-priority ?C
  2646. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2647. Must have a larger ASCII number than `org-highest-priority'."
  2648. :group 'org-priorities
  2649. :type 'character)
  2650. (defcustom org-default-priority ?B
  2651. "The default priority of TODO items.
  2652. This is the priority an item gets if no explicit priority is given.
  2653. When starting to cycle on an empty priority the first step in the cycle
  2654. depends on `org-priority-start-cycle-with-default'. The resulting first
  2655. step priority must not exceed the range from `org-highest-priority' to
  2656. `org-lowest-priority' which means that `org-default-priority' has to be
  2657. in this range exclusive or inclusive the range boundaries. Else the
  2658. first step refuses to set the default and the second will fall back
  2659. to (depending on the command used) the highest or lowest priority."
  2660. :group 'org-priorities
  2661. :type 'character)
  2662. (defcustom org-priority-start-cycle-with-default t
  2663. "Non-nil means start with default priority when starting to cycle.
  2664. When this is nil, the first step in the cycle will be (depending on the
  2665. command used) one higher or lower than the default priority.
  2666. See also `org-default-priority'."
  2667. :group 'org-priorities
  2668. :type 'boolean)
  2669. (defcustom org-get-priority-function nil
  2670. "Function to extract the priority from a string.
  2671. The string is normally the headline. If this is nil Org computes the
  2672. priority from the priority cookie like [#A] in the headline. It returns
  2673. an integer, increasing by 1000 for each priority level.
  2674. The user can set a different function here, which should take a string
  2675. as an argument and return the numeric priority."
  2676. :group 'org-priorities
  2677. :version "24.1"
  2678. :type '(choice
  2679. (const nil)
  2680. (function)))
  2681. (defgroup org-time nil
  2682. "Options concerning time stamps and deadlines in Org-mode."
  2683. :tag "Org Time"
  2684. :group 'org)
  2685. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2686. "Number of minutes to round time stamps to.
  2687. \\<org-mode-map>\
  2688. These are two values, the first applies when first creating a time stamp.
  2689. The second applies when changing it with the commands `S-up' and `S-down'.
  2690. When changing the time stamp, this means that it will change in steps
  2691. of N minutes, as given by the second value.
  2692. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2693. numbers should be factors of 60, so for example 5, 10, 15.
  2694. When this is larger than 1, you can still force an exact time stamp by using
  2695. a double prefix argument to a time stamp command like \
  2696. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2697. and by using a prefix arg to `S-up/down' to specify the exact number
  2698. of minutes to shift."
  2699. :group 'org-time
  2700. :get (lambda (var) ; Make sure both elements are there
  2701. (if (integerp (default-value var))
  2702. (list (default-value var) 5)
  2703. (default-value var)))
  2704. :type '(list
  2705. (integer :tag "when inserting times")
  2706. (integer :tag "when modifying times")))
  2707. ;; Normalize old customizations of this variable.
  2708. (when (integerp org-time-stamp-rounding-minutes)
  2709. (setq org-time-stamp-rounding-minutes
  2710. (list org-time-stamp-rounding-minutes
  2711. org-time-stamp-rounding-minutes)))
  2712. (defcustom org-display-custom-times nil
  2713. "Non-nil means overlay custom formats over all time stamps.
  2714. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2715. To turn this on on a per-file basis, insert anywhere in the file:
  2716. #+STARTUP: customtime"
  2717. :group 'org-time
  2718. :set 'set-default
  2719. :type 'sexp)
  2720. (make-variable-buffer-local 'org-display-custom-times)
  2721. (defcustom org-time-stamp-custom-formats
  2722. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2723. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2724. These are overlaid over the default ISO format if the variable
  2725. `org-display-custom-times' is set. Time like %H:%M should be at the
  2726. end of the second format. The custom formats are also honored by export
  2727. commands, if custom time display is turned on at the time of export."
  2728. :group 'org-time
  2729. :type 'sexp)
  2730. (defun org-time-stamp-format (&optional long inactive)
  2731. "Get the right format for a time string."
  2732. (let ((f (if long (cdr org-time-stamp-formats)
  2733. (car org-time-stamp-formats))))
  2734. (if inactive
  2735. (concat "[" (substring f 1 -1) "]")
  2736. f)))
  2737. (defcustom org-time-clocksum-format
  2738. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2739. "The format string used when creating CLOCKSUM lines.
  2740. This is also used when Org mode generates a time duration.
  2741. The value can be a single format string containing two
  2742. %-sequences, which will be filled with the number of hours and
  2743. minutes in that order.
  2744. Alternatively, the value can be a plist associating any of the
  2745. keys :years, :months, :weeks, :days, :hours or :minutes with
  2746. format strings. The time duration is formatted using only the
  2747. time components that are needed and concatenating the results.
  2748. If a time unit in absent, it falls back to the next smallest
  2749. unit.
  2750. The keys :require-years, :require-months, :require-days,
  2751. :require-weeks, :require-hours, :require-minutes are also
  2752. meaningful. A non-nil value for these keys indicates that the
  2753. corresponding time component should always be included, even if
  2754. its value is 0.
  2755. For example,
  2756. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2757. :require-minutes t)
  2758. means durations longer than a day will be expressed in days,
  2759. hours and minutes, and durations less than a day will always be
  2760. expressed in hours and minutes (even for durations less than an
  2761. hour).
  2762. The value
  2763. (:days \"%dd\" :minutes \"%dm\")
  2764. means durations longer than a day will be expressed in days and
  2765. minutes, and durations less than a day will be expressed entirely
  2766. in minutes (even for durations longer than an hour)."
  2767. :group 'org-time
  2768. :group 'org-clock
  2769. :version "24.4"
  2770. :package-version '(Org . "8.0")
  2771. :type '(choice (string :tag "Format string")
  2772. (set :tag "Plist"
  2773. (group :inline t (const :tag "Years" :years)
  2774. (string :tag "Format string"))
  2775. (group :inline t
  2776. (const :tag "Always show years" :require-years)
  2777. (const t))
  2778. (group :inline t (const :tag "Months" :months)
  2779. (string :tag "Format string"))
  2780. (group :inline t
  2781. (const :tag "Always show months" :require-months)
  2782. (const t))
  2783. (group :inline t (const :tag "Weeks" :weeks)
  2784. (string :tag "Format string"))
  2785. (group :inline t
  2786. (const :tag "Always show weeks" :require-weeks)
  2787. (const t))
  2788. (group :inline t (const :tag "Days" :days)
  2789. (string :tag "Format string"))
  2790. (group :inline t
  2791. (const :tag "Always show days" :require-days)
  2792. (const t))
  2793. (group :inline t (const :tag "Hours" :hours)
  2794. (string :tag "Format string"))
  2795. (group :inline t
  2796. (const :tag "Always show hours" :require-hours)
  2797. (const t))
  2798. (group :inline t (const :tag "Minutes" :minutes)
  2799. (string :tag "Format string"))
  2800. (group :inline t
  2801. (const :tag "Always show minutes" :require-minutes)
  2802. (const t)))))
  2803. (defcustom org-time-clocksum-use-fractional nil
  2804. "When non-nil, \\[org-clock-display] uses fractional times.
  2805. See `org-time-clocksum-format' for more on time clock formats."
  2806. :group 'org-time
  2807. :group 'org-clock
  2808. :version "24.3"
  2809. :type 'boolean)
  2810. (defcustom org-time-clocksum-use-effort-durations nil
  2811. "When non-nil, \\[org-clock-display] uses effort durations.
  2812. E.g. by default, one day is considered to be a 8 hours effort,
  2813. so a task that has been clocked for 16 hours will be displayed
  2814. as during 2 days in the clock display or in the clocktable.
  2815. See `org-effort-durations' on how to set effort durations
  2816. and `org-time-clocksum-format' for more on time clock formats."
  2817. :group 'org-time
  2818. :group 'org-clock
  2819. :version "24.4"
  2820. :package-version '(Org . "8.0")
  2821. :type 'boolean)
  2822. (defcustom org-time-clocksum-fractional-format "%.2f"
  2823. "The format string used when creating CLOCKSUM lines,
  2824. or when Org mode generates a time duration, if
  2825. `org-time-clocksum-use-fractional' is enabled.
  2826. The value can be a single format string containing one
  2827. %-sequence, which will be filled with the number of hours as
  2828. a float.
  2829. Alternatively, the value can be a plist associating any of the
  2830. keys :years, :months, :weeks, :days, :hours or :minutes with
  2831. a format string. The time duration is formatted using the
  2832. largest time unit which gives a non-zero integer part. If all
  2833. specified formats have zero integer part, the smallest time unit
  2834. is used."
  2835. :group 'org-time
  2836. :type '(choice (string :tag "Format string")
  2837. (set (group :inline t (const :tag "Years" :years)
  2838. (string :tag "Format string"))
  2839. (group :inline t (const :tag "Months" :months)
  2840. (string :tag "Format string"))
  2841. (group :inline t (const :tag "Weeks" :weeks)
  2842. (string :tag "Format string"))
  2843. (group :inline t (const :tag "Days" :days)
  2844. (string :tag "Format string"))
  2845. (group :inline t (const :tag "Hours" :hours)
  2846. (string :tag "Format string"))
  2847. (group :inline t (const :tag "Minutes" :minutes)
  2848. (string :tag "Format string")))))
  2849. (defcustom org-deadline-warning-days 14
  2850. "Number of days before expiration during which a deadline becomes active.
  2851. This variable governs the display in sparse trees and in the agenda.
  2852. When 0 or negative, it means use this number (the absolute value of it)
  2853. even if a deadline has a different individual lead time specified.
  2854. Custom commands can set this variable in the options section."
  2855. :group 'org-time
  2856. :group 'org-agenda-daily/weekly
  2857. :type 'integer)
  2858. (defcustom org-scheduled-delay-days 0
  2859. "Number of days before a scheduled item becomes active.
  2860. This variable governs the display in sparse trees and in the agenda.
  2861. The default value (i.e. 0) means: don't delay scheduled item.
  2862. When negative, it means use this number (the absolute value of it)
  2863. even if a scheduled item has a different individual delay time
  2864. specified.
  2865. Custom commands can set this variable in the options section."
  2866. :group 'org-time
  2867. :group 'org-agenda-daily/weekly
  2868. :version "24.4"
  2869. :package-version '(Org . "8.0")
  2870. :type 'integer)
  2871. (defcustom org-read-date-prefer-future t
  2872. "Non-nil means assume future for incomplete date input from user.
  2873. This affects the following situations:
  2874. 1. The user gives a month but not a year.
  2875. For example, if it is April and you enter \"feb 2\", this will be read
  2876. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2877. 2. The user gives a day, but no month.
  2878. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2879. read this as the third of *next* month. However, if you enter \"17\",
  2880. it will be considered as *this* month.
  2881. If you set this variable to the symbol `time', then also the following
  2882. will work:
  2883. 3. If the user gives a time.
  2884. If the time is before now, it will be interpreted as tomorrow.
  2885. Currently none of this works for ISO week specifications.
  2886. When this option is nil, the current day, month and year will always be
  2887. used as defaults.
  2888. See also `org-agenda-jump-prefer-future'."
  2889. :group 'org-time
  2890. :type '(choice
  2891. (const :tag "Never" nil)
  2892. (const :tag "Check month and day" t)
  2893. (const :tag "Check month, day, and time" time)))
  2894. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2895. "Should the agenda jump command prefer the future for incomplete dates?
  2896. The default is to do the same as configured in `org-read-date-prefer-future'.
  2897. But you can also set a deviating value here.
  2898. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2899. :group 'org-agenda
  2900. :group 'org-time
  2901. :version "24.1"
  2902. :type '(choice
  2903. (const :tag "Use org-read-date-prefer-future"
  2904. org-read-date-prefer-future)
  2905. (const :tag "Never" nil)
  2906. (const :tag "Always" t)))
  2907. (defcustom org-read-date-force-compatible-dates t
  2908. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2909. Depending on the system Emacs is running on, certain dates cannot
  2910. be represented with the type used internally to represent time.
  2911. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2912. correctly. Some systems allow for earlier dates, some for later,
  2913. some for both. One way to find out it to insert any date into an
  2914. Org buffer, putting the cursor on the year and hitting S-up and
  2915. S-down to test the range.
  2916. When this variable is set to t, the date/time prompt will not let
  2917. you specify dates outside the 1970-2037 range, so it is certain that
  2918. these dates will work in whatever version of Emacs you are
  2919. running, and also that you can move a file from one Emacs implementation
  2920. to another. WHenever Org is forcing the year for you, it will display
  2921. a message and beep.
  2922. When this variable is nil, Org will check if the date is
  2923. representable in the specific Emacs implementation you are using.
  2924. If not, it will force a year, usually the current year, and beep
  2925. to remind you. Currently this setting is not recommended because
  2926. the likelihood that you will open your Org files in an Emacs that
  2927. has limited date range is not negligible.
  2928. A workaround for this problem is to use diary sexp dates for time
  2929. stamps outside of this range."
  2930. :group 'org-time
  2931. :version "24.1"
  2932. :type 'boolean)
  2933. (defcustom org-read-date-display-live t
  2934. "Non-nil means display current interpretation of date prompt live.
  2935. This display will be in an overlay, in the minibuffer."
  2936. :group 'org-time
  2937. :type 'boolean)
  2938. (defcustom org-read-date-popup-calendar t
  2939. "Non-nil means pop up a calendar when prompting for a date.
  2940. In the calendar, the date can be selected with mouse-1. However, the
  2941. minibuffer will also be active, and you can simply enter the date as well.
  2942. When nil, only the minibuffer will be available."
  2943. :group 'org-time
  2944. :type 'boolean)
  2945. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2946. 'org-read-date-popup-calendar)
  2947. (make-obsolete-variable
  2948. 'org-read-date-minibuffer-setup-hook
  2949. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2950. (defcustom org-read-date-minibuffer-setup-hook nil
  2951. "Hook to be used to set up keys for the date/time interface.
  2952. Add key definitions to `minibuffer-local-map', which will be a
  2953. temporary copy.
  2954. WARNING: This option is obsolete, you should use
  2955. `org-read-date-minibuffer-local-map' to set up keys."
  2956. :group 'org-time
  2957. :type 'hook)
  2958. (defcustom org-extend-today-until 0
  2959. "The hour when your day really ends. Must be an integer.
  2960. This has influence for the following applications:
  2961. - When switching the agenda to \"today\". It it is still earlier than
  2962. the time given here, the day recognized as TODAY is actually yesterday.
  2963. - When a date is read from the user and it is still before the time given
  2964. here, the current date and time will be assumed to be yesterday, 23:59.
  2965. Also, timestamps inserted in capture templates follow this rule.
  2966. IMPORTANT: This is a feature whose implementation is and likely will
  2967. remain incomplete. Really, it is only here because past midnight seems to
  2968. be the favorite working time of John Wiegley :-)"
  2969. :group 'org-time
  2970. :type 'integer)
  2971. (defcustom org-use-effective-time nil
  2972. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2973. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2974. \"effective time\" of any timestamps between midnight and 8am will be
  2975. 23:59 of the previous day."
  2976. :group 'org-time
  2977. :version "24.1"
  2978. :type 'boolean)
  2979. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2980. "When non-nil, use the last clock out time for `org-todo'.
  2981. Note that this option has precedence over the combined use of
  2982. `org-use-effective-time' and `org-extend-today-until'."
  2983. :group 'org-time
  2984. :version "24.4"
  2985. :package-version '(Org . "8.0")
  2986. :type 'boolean)
  2987. (defcustom org-edit-timestamp-down-means-later nil
  2988. "Non-nil means S-down will increase the time in a time stamp.
  2989. When nil, S-up will increase."
  2990. :group 'org-time
  2991. :type 'boolean)
  2992. (defcustom org-calendar-follow-timestamp-change t
  2993. "Non-nil means make the calendar window follow timestamp changes.
  2994. When a timestamp is modified and the calendar window is visible, it will be
  2995. moved to the new date."
  2996. :group 'org-time
  2997. :type 'boolean)
  2998. (defgroup org-tags nil
  2999. "Options concerning tags in Org-mode."
  3000. :tag "Org Tags"
  3001. :group 'org)
  3002. (defcustom org-tag-alist nil
  3003. "List of tags allowed in Org-mode files.
  3004. When this list is nil, Org-mode will base TAG input on what is already in the
  3005. buffer.
  3006. The value of this variable is an alist, the car of each entry must be a
  3007. keyword as a string, the cdr may be a character that is used to select
  3008. that tag through the fast-tag-selection interface.
  3009. See the manual for details."
  3010. :group 'org-tags
  3011. :type '(repeat
  3012. (choice
  3013. (cons (string :tag "Tag name")
  3014. (character :tag "Access char"))
  3015. (list :tag "Start radio group"
  3016. (const :startgroup)
  3017. (option (string :tag "Group description")))
  3018. (list :tag "Start tag group, non distinct"
  3019. (const :startgrouptag)
  3020. (option (string :tag "Group description")))
  3021. (list :tag "Group tags delimiter"
  3022. (const :grouptags))
  3023. (list :tag "End radio group"
  3024. (const :endgroup)
  3025. (option (string :tag "Group description")))
  3026. (list :tag "End tag group, non distinct"
  3027. (const :endgrouptag)
  3028. (option (string :tag "Group description")))
  3029. (const :tag "New line" (:newline)))))
  3030. (defcustom org-tag-persistent-alist nil
  3031. "List of tags that will always appear in all Org-mode files.
  3032. This is in addition to any in buffer settings or customizations
  3033. of `org-tag-alist'.
  3034. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3035. The value of this variable is an alist, the car of each entry must be a
  3036. keyword as a string, the cdr may be a character that is used to select
  3037. that tag through the fast-tag-selection interface.
  3038. See the manual for details.
  3039. To disable these tags on a per-file basis, insert anywhere in the file:
  3040. #+STARTUP: noptag"
  3041. :group 'org-tags
  3042. :type '(repeat
  3043. (choice
  3044. (cons (string :tag "Tag name")
  3045. (character :tag "Access char"))
  3046. (const :tag "Start radio group" (:startgroup))
  3047. (const :tag "Group tags delimiter" (:grouptags))
  3048. (const :tag "End radio group" (:endgroup))
  3049. (const :tag "New line" (:newline)))))
  3050. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3051. "If non-nil, always offer completion for all tags of all agenda files.
  3052. Instead of customizing this variable directly, you might want to
  3053. set it locally for capture buffers, because there no list of
  3054. tags in that file can be created dynamically (there are none).
  3055. (add-hook \\='org-capture-mode-hook
  3056. (lambda ()
  3057. (set (make-local-variable
  3058. \\='org-complete-tags-always-offer-all-agenda-tags)
  3059. t)))"
  3060. :group 'org-tags
  3061. :version "24.1"
  3062. :type 'boolean)
  3063. (defvar org-file-tags nil
  3064. "List of tags that can be inherited by all entries in the file.
  3065. The tags will be inherited if the variable `org-use-tag-inheritance'
  3066. says they should be.
  3067. This variable is populated from #+FILETAGS lines.")
  3068. (defcustom org-use-fast-tag-selection 'auto
  3069. "Non-nil means use fast tag selection scheme.
  3070. This is a special interface to select and deselect tags with single keys.
  3071. When nil, fast selection is never used.
  3072. When the symbol `auto', fast selection is used if and only if selection
  3073. characters for tags have been configured, either through the variable
  3074. `org-tag-alist' or through a #+TAGS line in the buffer.
  3075. When t, fast selection is always used and selection keys are assigned
  3076. automatically if necessary."
  3077. :group 'org-tags
  3078. :type '(choice
  3079. (const :tag "Always" t)
  3080. (const :tag "Never" nil)
  3081. (const :tag "When selection characters are configured" auto)))
  3082. (defcustom org-fast-tag-selection-single-key nil
  3083. "Non-nil means fast tag selection exits after first change.
  3084. When nil, you have to press RET to exit it.
  3085. During fast tag selection, you can toggle this flag with `C-c'.
  3086. This variable can also have the value `expert'. In this case, the window
  3087. displaying the tags menu is not even shown, until you press C-c again."
  3088. :group 'org-tags
  3089. :type '(choice
  3090. (const :tag "No" nil)
  3091. (const :tag "Yes" t)
  3092. (const :tag "Expert" expert)))
  3093. (defvar org-fast-tag-selection-include-todo nil
  3094. "Non-nil means fast tags selection interface will also offer TODO states.
  3095. This is an undocumented feature, you should not rely on it.")
  3096. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3097. "The column to which tags should be indented in a headline.
  3098. If this number is positive, it specifies the column. If it is negative,
  3099. it means that the tags should be flushright to that column. For example,
  3100. -80 works well for a normal 80 character screen.
  3101. When 0, place tags directly after headline text, with only one space in
  3102. between."
  3103. :group 'org-tags
  3104. :type 'integer)
  3105. (defcustom org-auto-align-tags t
  3106. "Non-nil keeps tags aligned when modifying headlines.
  3107. Some operations (i.e. demoting) change the length of a headline and
  3108. therefore shift the tags around. With this option turned on, after
  3109. each such operation the tags are again aligned to `org-tags-column'."
  3110. :group 'org-tags
  3111. :type 'boolean)
  3112. (defcustom org-use-tag-inheritance t
  3113. "Non-nil means tags in levels apply also for sublevels.
  3114. When nil, only the tags directly given in a specific line apply there.
  3115. This may also be a list of tags that should be inherited, or a regexp that
  3116. matches tags that should be inherited. Additional control is possible
  3117. with the variable `org-tags-exclude-from-inheritance' which gives an
  3118. explicit list of tags to be excluded from inheritance, even if the value of
  3119. `org-use-tag-inheritance' would select it for inheritance.
  3120. If this option is t, a match early-on in a tree can lead to a large
  3121. number of matches in the subtree when constructing the agenda or creating
  3122. a sparse tree. If you only want to see the first match in a tree during
  3123. a search, check out the variable `org-tags-match-list-sublevels'."
  3124. :group 'org-tags
  3125. :type '(choice
  3126. (const :tag "Not" nil)
  3127. (const :tag "Always" t)
  3128. (repeat :tag "Specific tags" (string :tag "Tag"))
  3129. (regexp :tag "Tags matched by regexp")))
  3130. (defcustom org-tags-exclude-from-inheritance nil
  3131. "List of tags that should never be inherited.
  3132. This is a way to exclude a few tags from inheritance. For way to do
  3133. the opposite, to actively allow inheritance for selected tags,
  3134. see the variable `org-use-tag-inheritance'."
  3135. :group 'org-tags
  3136. :type '(repeat (string :tag "Tag")))
  3137. (defun org-tag-inherit-p (tag)
  3138. "Check if TAG is one that should be inherited."
  3139. (cond
  3140. ((member tag org-tags-exclude-from-inheritance) nil)
  3141. ((eq org-use-tag-inheritance t) t)
  3142. ((not org-use-tag-inheritance) nil)
  3143. ((stringp org-use-tag-inheritance)
  3144. (string-match org-use-tag-inheritance tag))
  3145. ((listp org-use-tag-inheritance)
  3146. (member tag org-use-tag-inheritance))
  3147. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3148. (defcustom org-tags-match-list-sublevels t
  3149. "Non-nil means list also sublevels of headlines matching a search.
  3150. This variable applies to tags/property searches, and also to stuck
  3151. projects because this search is based on a tags match as well.
  3152. When set to the symbol `indented', sublevels are indented with
  3153. leading dots.
  3154. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3155. the sublevels of a headline matching a tag search often also match
  3156. the same search. Listing all of them can create very long lists.
  3157. Setting this variable to nil causes subtrees of a match to be skipped.
  3158. This variable is semi-obsolete and probably should always be true. It
  3159. is better to limit inheritance to certain tags using the variables
  3160. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3161. :group 'org-tags
  3162. :type '(choice
  3163. (const :tag "No, don't list them" nil)
  3164. (const :tag "Yes, do list them" t)
  3165. (const :tag "List them, indented with leading dots" indented)))
  3166. (defcustom org-tags-sort-function nil
  3167. "When set, tags are sorted using this function as a comparator."
  3168. :group 'org-tags
  3169. :type '(choice
  3170. (const :tag "No sorting" nil)
  3171. (const :tag "Alphabetical" string<)
  3172. (const :tag "Reverse alphabetical" string>)
  3173. (function :tag "Custom function" nil)))
  3174. (defvar org-tags-history nil
  3175. "History of minibuffer reads for tags.")
  3176. (defvar org-last-tags-completion-table nil
  3177. "The last used completion table for tags.")
  3178. (defvar org-after-tags-change-hook nil
  3179. "Hook that is run after the tags in a line have changed.")
  3180. (defgroup org-properties nil
  3181. "Options concerning properties in Org-mode."
  3182. :tag "Org Properties"
  3183. :group 'org)
  3184. (defcustom org-property-format "%-10s %s"
  3185. "How property key/value pairs should be formatted by `indent-line'.
  3186. When `indent-line' hits a property definition, it will format the line
  3187. according to this format, mainly to make sure that the values are
  3188. lined-up with respect to each other."
  3189. :group 'org-properties
  3190. :type 'string)
  3191. (defcustom org-properties-postprocess-alist nil
  3192. "Alist of properties and functions to adjust inserted values.
  3193. Elements of this alist must be of the form
  3194. ([string] [function])
  3195. where [string] must be a property name and [function] must be a
  3196. lambda expression: this lambda expression must take one argument,
  3197. the value to adjust, and return the new value as a string.
  3198. For example, this element will allow the property \"Remaining\"
  3199. to be updated wrt the relation between the \"Effort\" property
  3200. and the clock summary:
  3201. ((\"Remaining\" (lambda(value)
  3202. (let ((clocksum (org-clock-sum-current-item))
  3203. (effort (org-duration-string-to-minutes
  3204. (org-entry-get (point) \"Effort\"))))
  3205. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3206. :group 'org-properties
  3207. :version "24.1"
  3208. :type '(alist :key-type (string :tag "Property")
  3209. :value-type (function :tag "Function")))
  3210. (defcustom org-use-property-inheritance nil
  3211. "Non-nil means properties apply also for sublevels.
  3212. This setting is chiefly used during property searches. Turning it on can
  3213. cause significant overhead when doing a search, which is why it is not
  3214. on by default.
  3215. When nil, only the properties directly given in the current entry count.
  3216. When t, every property is inherited. The value may also be a list of
  3217. properties that should have inheritance, or a regular expression matching
  3218. properties that should be inherited.
  3219. However, note that some special properties use inheritance under special
  3220. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3221. and the properties ending in \"_ALL\" when they are used as descriptor
  3222. for valid values of a property.
  3223. Note for programmers:
  3224. When querying an entry with `org-entry-get', you can control if inheritance
  3225. should be used. By default, `org-entry-get' looks only at the local
  3226. properties. You can request inheritance by setting the inherit argument
  3227. to t (to force inheritance) or to `selective' (to respect the setting
  3228. in this variable)."
  3229. :group 'org-properties
  3230. :type '(choice
  3231. (const :tag "Not" nil)
  3232. (const :tag "Always" t)
  3233. (repeat :tag "Specific properties" (string :tag "Property"))
  3234. (regexp :tag "Properties matched by regexp")))
  3235. (defun org-property-inherit-p (property)
  3236. "Check if PROPERTY is one that should be inherited."
  3237. (cond
  3238. ((eq org-use-property-inheritance t) t)
  3239. ((not org-use-property-inheritance) nil)
  3240. ((stringp org-use-property-inheritance)
  3241. (string-match org-use-property-inheritance property))
  3242. ((listp org-use-property-inheritance)
  3243. (member property org-use-property-inheritance))
  3244. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3245. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3246. "The default column format, if no other format has been defined.
  3247. This variable can be set on the per-file basis by inserting a line
  3248. #+COLUMNS: %25ITEM ....."
  3249. :group 'org-properties
  3250. :type 'string)
  3251. (defcustom org-columns-ellipses ".."
  3252. "The ellipses to be used when a field in column view is truncated.
  3253. When this is the empty string, as many characters as possible are shown,
  3254. but then there will be no visual indication that the field has been truncated.
  3255. When this is a string of length N, the last N characters of a truncated
  3256. field are replaced by this string. If the column is narrower than the
  3257. ellipses string, only part of the ellipses string will be shown."
  3258. :group 'org-properties
  3259. :type 'string)
  3260. (defcustom org-columns-modify-value-for-display-function nil
  3261. "Function that modifies values for display in column view.
  3262. For example, it can be used to cut out a certain part from a time stamp.
  3263. The function must take 2 arguments:
  3264. column-title The title of the column (*not* the property name)
  3265. value The value that should be modified.
  3266. The function should return the value that should be displayed,
  3267. or nil if the normal value should be used."
  3268. :group 'org-properties
  3269. :type '(choice (const nil) (function)))
  3270. (defconst org-global-properties-fixed
  3271. '(("VISIBILITY_ALL" . "folded children content all")
  3272. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3273. "List of property/value pairs that can be inherited by any entry.
  3274. These are fixed values, for the preset properties. The user variable
  3275. that can be used to add to this list is `org-global-properties'.
  3276. The entries in this list are cons cells where the car is a property
  3277. name and cdr is a string with the value. If the value represents
  3278. multiple items like an \"_ALL\" property, separate the items by
  3279. spaces.")
  3280. (defcustom org-global-properties nil
  3281. "List of property/value pairs that can be inherited by any entry.
  3282. This list will be combined with the constant `org-global-properties-fixed'.
  3283. The entries in this list are cons cells where the car is a property
  3284. name and cdr is a string with the value.
  3285. You can set buffer-local values for the same purpose in the variable
  3286. `org-file-properties' this by adding lines like
  3287. #+PROPERTY: NAME VALUE"
  3288. :group 'org-properties
  3289. :type '(repeat
  3290. (cons (string :tag "Property")
  3291. (string :tag "Value"))))
  3292. (defvar org-file-properties nil
  3293. "List of property/value pairs that can be inherited by any entry.
  3294. Valid for the current buffer.
  3295. This variable is populated from #+PROPERTY lines.")
  3296. (make-variable-buffer-local 'org-file-properties)
  3297. (defgroup org-agenda nil
  3298. "Options concerning agenda views in Org-mode."
  3299. :tag "Org Agenda"
  3300. :group 'org)
  3301. (defvar org-category nil
  3302. "Variable used by org files to set a category for agenda display.
  3303. Such files should use a file variable to set it, for example
  3304. # -*- mode: org; org-category: \"ELisp\"
  3305. or contain a special line
  3306. #+CATEGORY: ELisp
  3307. If the file does not specify a category, then file's base name
  3308. is used instead.")
  3309. (make-variable-buffer-local 'org-category)
  3310. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3311. (defcustom org-agenda-files nil
  3312. "The files to be used for agenda display.
  3313. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3314. \\[org-remove-file]. You can also use customize to edit the list.
  3315. If an entry is a directory, all files in that directory that are matched by
  3316. `org-agenda-file-regexp' will be part of the file list.
  3317. If the value of the variable is not a list but a single file name, then
  3318. the list of agenda files is actually stored and maintained in that file, one
  3319. agenda file per line. In this file paths can be given relative to
  3320. `org-directory'. Tilde expansion and environment variable substitution
  3321. are also made."
  3322. :group 'org-agenda
  3323. :type '(choice
  3324. (repeat :tag "List of files and directories" file)
  3325. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3326. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3327. "Regular expression to match files for `org-agenda-files'.
  3328. If any element in the list in that variable contains a directory instead
  3329. of a normal file, all files in that directory that are matched by this
  3330. regular expression will be included."
  3331. :group 'org-agenda
  3332. :type 'regexp)
  3333. (defcustom org-agenda-text-search-extra-files nil
  3334. "List of extra files to be searched by text search commands.
  3335. These files will be searched in addition to the agenda files by the
  3336. commands `org-search-view' (`\\[org-agenda] s') \
  3337. and `org-occur-in-agenda-files'.
  3338. Note that these files will only be searched for text search commands,
  3339. not for the other agenda views like todo lists, tag searches or the weekly
  3340. agenda. This variable is intended to list notes and possibly archive files
  3341. that should also be searched by these two commands.
  3342. In fact, if the first element in the list is the symbol `agenda-archives',
  3343. then all archive files of all agenda files will be added to the search
  3344. scope."
  3345. :group 'org-agenda
  3346. :type '(set :greedy t
  3347. (const :tag "Agenda Archives" agenda-archives)
  3348. (repeat :inline t (file))))
  3349. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3350. 'org-agenda-text-search-extra-files)
  3351. (defcustom org-agenda-skip-unavailable-files nil
  3352. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3353. A nil value means to remove them, after a query, from the list."
  3354. :group 'org-agenda
  3355. :type 'boolean)
  3356. (defcustom org-calendar-to-agenda-key [?c]
  3357. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3358. The command `org-calendar-goto-agenda' will be bound to this key. The
  3359. default is the character `c' because then `c' can be used to switch back and
  3360. forth between agenda and calendar."
  3361. :group 'org-agenda
  3362. :type 'sexp)
  3363. (defcustom org-calendar-insert-diary-entry-key [?i]
  3364. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3365. This option is irrelevant until `org-agenda-diary-file' has been configured
  3366. to point to an Org-mode file. When that is the case, the command
  3367. `org-agenda-diary-entry' will be bound to the key given here, by default
  3368. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3369. if you want to continue doing this, you need to change this to a different
  3370. key."
  3371. :group 'org-agenda
  3372. :type 'sexp)
  3373. (defcustom org-agenda-diary-file 'diary-file
  3374. "File to which to add new entries with the `i' key in agenda and calendar.
  3375. When this is the symbol `diary-file', the functionality in the Emacs
  3376. calendar will be used to add entries to the `diary-file'. But when this
  3377. points to a file, `org-agenda-diary-entry' will be used instead."
  3378. :group 'org-agenda
  3379. :type '(choice
  3380. (const :tag "The standard Emacs diary file" diary-file)
  3381. (file :tag "Special Org file diary entries")))
  3382. (eval-after-load "calendar"
  3383. '(progn
  3384. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3385. 'org-calendar-goto-agenda)
  3386. (add-hook 'calendar-mode-hook
  3387. (lambda ()
  3388. (unless (eq org-agenda-diary-file 'diary-file)
  3389. (define-key calendar-mode-map
  3390. org-calendar-insert-diary-entry-key
  3391. 'org-agenda-diary-entry))))))
  3392. (defgroup org-latex nil
  3393. "Options for embedding LaTeX code into Org-mode."
  3394. :tag "Org LaTeX"
  3395. :group 'org)
  3396. (defcustom org-format-latex-options
  3397. '(:foreground default :background default :scale 1.0
  3398. :html-foreground "Black" :html-background "Transparent"
  3399. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3400. "Options for creating images from LaTeX fragments.
  3401. This is a property list with the following properties:
  3402. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3403. `default' means use the foreground of the default face.
  3404. `auto' means use the foreground from the text face.
  3405. :background the background color, or \"Transparent\".
  3406. `default' means use the background of the default face.
  3407. `auto' means use the background from the text face.
  3408. :scale a scaling factor for the size of the images, to get more pixels
  3409. :html-foreground, :html-background, :html-scale
  3410. the same numbers for HTML export.
  3411. :matchers a list indicating which matchers should be used to
  3412. find LaTeX fragments. Valid members of this list are:
  3413. \"begin\" find environments
  3414. \"$1\" find single characters surrounded by $.$
  3415. \"$\" find math expressions surrounded by $...$
  3416. \"$$\" find math expressions surrounded by $$....$$
  3417. \"\\(\" find math expressions surrounded by \\(...\\)
  3418. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3419. :group 'org-latex
  3420. :type 'plist)
  3421. (defcustom org-format-latex-signal-error t
  3422. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3423. When nil, just push out a message."
  3424. :group 'org-latex
  3425. :version "24.1"
  3426. :type 'boolean)
  3427. (defcustom org-latex-to-mathml-jar-file nil
  3428. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3429. Use this to specify additional executable file say a jar file.
  3430. When using MathToWeb as the converter, specify the full-path to
  3431. your mathtoweb.jar file."
  3432. :group 'org-latex
  3433. :version "24.1"
  3434. :type '(choice
  3435. (const :tag "None" nil)
  3436. (file :tag "JAR file" :must-match t)))
  3437. (defcustom org-latex-to-mathml-convert-command nil
  3438. "Command to convert LaTeX fragments to MathML.
  3439. Replace format-specifiers in the command as noted below and use
  3440. `shell-command' to convert LaTeX to MathML.
  3441. %j: Executable file in fully expanded form as specified by
  3442. `org-latex-to-mathml-jar-file'.
  3443. %I: Input LaTeX file in fully expanded form.
  3444. %i: The latex fragment to be converted.
  3445. %o: Output MathML file.
  3446. This command is used by `org-create-math-formula'.
  3447. When using MathToWeb as the converter, set this option to
  3448. \"java -jar %j -unicode -force -df %o %I\".
  3449. When using LaTeXML set this option to
  3450. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3451. :group 'org-latex
  3452. :version "24.1"
  3453. :type '(choice
  3454. (const :tag "None" nil)
  3455. (string :tag "\nShell command")))
  3456. (defcustom org-latex-create-formula-image-program 'dvipng
  3457. "Program to convert LaTeX fragments with.
  3458. dvipng Process the LaTeX fragments to dvi file, then convert
  3459. dvi files to png files using dvipng.
  3460. This will also include processing of non-math environments.
  3461. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3462. to convert pdf files to png files"
  3463. :group 'org-latex
  3464. :version "24.1"
  3465. :type '(choice
  3466. (const :tag "dvipng" dvipng)
  3467. (const :tag "imagemagick" imagemagick)))
  3468. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3469. "Path to store latex preview images.
  3470. A relative path here creates many directories relative to the
  3471. processed org files paths. An absolute path puts all preview
  3472. images at the same place."
  3473. :group 'org-latex
  3474. :version "24.3"
  3475. :type 'string)
  3476. (defun org-format-latex-mathml-available-p ()
  3477. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3478. (save-match-data
  3479. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3480. org-latex-to-mathml-convert-command)
  3481. (let ((executable (car (split-string
  3482. org-latex-to-mathml-convert-command))))
  3483. (when (executable-find executable)
  3484. (if (string-match
  3485. "%j" org-latex-to-mathml-convert-command)
  3486. (file-readable-p org-latex-to-mathml-jar-file)
  3487. t))))))
  3488. (defcustom org-format-latex-header "\\documentclass{article}
  3489. \\usepackage[usenames]{color}
  3490. \[PACKAGES]
  3491. \[DEFAULT-PACKAGES]
  3492. \\pagestyle{empty} % do not remove
  3493. % The settings below are copied from fullpage.sty
  3494. \\setlength{\\textwidth}{\\paperwidth}
  3495. \\addtolength{\\textwidth}{-3cm}
  3496. \\setlength{\\oddsidemargin}{1.5cm}
  3497. \\addtolength{\\oddsidemargin}{-2.54cm}
  3498. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3499. \\setlength{\\textheight}{\\paperheight}
  3500. \\addtolength{\\textheight}{-\\headheight}
  3501. \\addtolength{\\textheight}{-\\headsep}
  3502. \\addtolength{\\textheight}{-\\footskip}
  3503. \\addtolength{\\textheight}{-3cm}
  3504. \\setlength{\\topmargin}{1.5cm}
  3505. \\addtolength{\\topmargin}{-2.54cm}"
  3506. "The document header used for processing LaTeX fragments.
  3507. It is imperative that this header make sure that no page number
  3508. appears on the page. The package defined in the variables
  3509. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3510. will either replace the placeholder \"[PACKAGES]\" in this
  3511. header, or they will be appended."
  3512. :group 'org-latex
  3513. :type 'string)
  3514. (defun org-set-packages-alist (var val)
  3515. "Set the packages alist and make sure it has 3 elements per entry."
  3516. (set var (mapcar (lambda (x)
  3517. (if (and (consp x) (= (length x) 2))
  3518. (list (car x) (nth 1 x) t)
  3519. x))
  3520. val)))
  3521. (defun org-get-packages-alist (var)
  3522. "Get the packages alist and make sure it has 3 elements per entry."
  3523. (mapcar (lambda (x)
  3524. (if (and (consp x) (= (length x) 2))
  3525. (list (car x) (nth 1 x) t)
  3526. x))
  3527. (default-value var)))
  3528. (defcustom org-latex-default-packages-alist
  3529. '(("AUTO" "inputenc" t)
  3530. ("T1" "fontenc" t)
  3531. ("" "fixltx2e" nil)
  3532. ("" "graphicx" t)
  3533. ("" "grffile" t)
  3534. ("" "longtable" nil)
  3535. ("" "wrapfig" nil)
  3536. ("" "rotating" nil)
  3537. ("normalem" "ulem" t)
  3538. ("" "amsmath" t)
  3539. ("" "textcomp" t)
  3540. ("" "amssymb" t)
  3541. ("" "capt-of" nil)
  3542. ("" "hyperref" nil))
  3543. "Alist of default packages to be inserted in the header.
  3544. Change this only if one of the packages here causes an
  3545. incompatibility with another package you are using.
  3546. The packages in this list are needed by one part or another of
  3547. Org mode to function properly:
  3548. - inputenc, fontenc: for basic font and character selection
  3549. - fixltx2e: Important patches of LaTeX itself
  3550. - graphicx: for including images
  3551. - grffile: allow periods and spaces in graphics file names
  3552. - longtable: For multipage tables
  3553. - wrapfig: for figure placement
  3554. - rotating: for sideways figures and tables
  3555. - ulem: for underline and strike-through
  3556. - amsmath: for subscript and superscript and math environments
  3557. - textcomp, amssymb: for various symbols used
  3558. for interpreting the entities in `org-entities'. You can skip
  3559. some of these packages if you don't use any of their symbols.
  3560. - capt-of: for captions outside of floats
  3561. - hyperref: for cross references
  3562. Therefore you should not modify this variable unless you know
  3563. what you are doing. The one reason to change it anyway is that
  3564. you might be loading some other package that conflicts with one
  3565. of the default packages. Each element is either a cell or
  3566. a string.
  3567. A cell is of the format
  3568. (\"options\" \"package\" SNIPPET-FLAG)
  3569. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3570. when compiling LaTeX snippets into images for inclusion into
  3571. non-LaTeX output.
  3572. A string will be inserted as-is in the header of the document."
  3573. :group 'org-latex
  3574. :group 'org-export-latex
  3575. :set 'org-set-packages-alist
  3576. :get 'org-get-packages-alist
  3577. :version "25.1"
  3578. :package-version '(Org . "8.3")
  3579. :type '(repeat
  3580. (choice
  3581. (list :tag "options/package pair"
  3582. (string :tag "options")
  3583. (string :tag "package")
  3584. (boolean :tag "Snippet"))
  3585. (string :tag "A line of LaTeX"))))
  3586. (defcustom org-latex-packages-alist nil
  3587. "Alist of packages to be inserted in every LaTeX header.
  3588. These will be inserted after `org-latex-default-packages-alist'.
  3589. Each element is either a cell or a string.
  3590. A cell is of the format:
  3591. (\"options\" \"package\" SNIPPET-FLAG)
  3592. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3593. needed when turning LaTeX snippets into images for inclusion into
  3594. non-LaTeX output.
  3595. A string will be inserted as-is in the header of the document.
  3596. Make sure that you only list packages here which:
  3597. - you want in every file;
  3598. - do not conflict with the setup in `org-format-latex-header';
  3599. - do not conflict with the default packages in
  3600. `org-latex-default-packages-alist'."
  3601. :group 'org-latex
  3602. :group 'org-export-latex
  3603. :set 'org-set-packages-alist
  3604. :get 'org-get-packages-alist
  3605. :type '(repeat
  3606. (choice
  3607. (list :tag "options/package pair"
  3608. (string :tag "options")
  3609. (string :tag "package")
  3610. (boolean :tag "Snippet"))
  3611. (string :tag "A line of LaTeX"))))
  3612. (defgroup org-appearance nil
  3613. "Settings for Org-mode appearance."
  3614. :tag "Org Appearance"
  3615. :group 'org)
  3616. (defcustom org-level-color-stars-only nil
  3617. "Non-nil means fontify only the stars in each headline.
  3618. When nil, the entire headline is fontified.
  3619. Changing it requires restart of `font-lock-mode' to become effective
  3620. also in regions already fontified."
  3621. :group 'org-appearance
  3622. :type 'boolean)
  3623. (defcustom org-hide-leading-stars nil
  3624. "Non-nil means hide the first N-1 stars in a headline.
  3625. This works by using the face `org-hide' for these stars. This
  3626. face is white for a light background, and black for a dark
  3627. background. You may have to customize the face `org-hide' to
  3628. make this work.
  3629. Changing it requires restart of `font-lock-mode' to become effective
  3630. also in regions already fontified.
  3631. You may also set this on a per-file basis by adding one of the following
  3632. lines to the buffer:
  3633. #+STARTUP: hidestars
  3634. #+STARTUP: showstars"
  3635. :group 'org-appearance
  3636. :type 'boolean)
  3637. (defcustom org-hidden-keywords nil
  3638. "List of symbols corresponding to keywords to be hidden the org buffer.
  3639. For example, a value \\='(title) for this list will make the document's title
  3640. appear in the buffer without the initial #+TITLE: keyword."
  3641. :group 'org-appearance
  3642. :version "24.1"
  3643. :type '(set (const :tag "#+AUTHOR" author)
  3644. (const :tag "#+DATE" date)
  3645. (const :tag "#+EMAIL" email)
  3646. (const :tag "#+TITLE" title)))
  3647. (defcustom org-custom-properties nil
  3648. "List of properties (as strings) with a special meaning.
  3649. The default use of these custom properties is to let the user
  3650. hide them with `org-toggle-custom-properties-visibility'."
  3651. :group 'org-properties
  3652. :group 'org-appearance
  3653. :version "24.3"
  3654. :type '(repeat (string :tag "Property Name")))
  3655. (defcustom org-fontify-done-headline nil
  3656. "Non-nil means change the face of a headline if it is marked DONE.
  3657. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3658. When this is non-nil, the headline after the keyword is set to the
  3659. `org-headline-done' as an additional indication."
  3660. :group 'org-appearance
  3661. :type 'boolean)
  3662. (defcustom org-fontify-emphasized-text t
  3663. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3664. Changing this variable requires a restart of Emacs to take effect."
  3665. :group 'org-appearance
  3666. :type 'boolean)
  3667. (defcustom org-fontify-whole-heading-line nil
  3668. "Non-nil means fontify the whole line for headings.
  3669. This is useful when setting a background color for the
  3670. org-level-* faces."
  3671. :group 'org-appearance
  3672. :type 'boolean)
  3673. (defcustom org-highlight-latex-and-related nil
  3674. "Non-nil means highlight LaTeX related syntax in the buffer.
  3675. When non nil, the value should be a list containing any of the
  3676. following symbols:
  3677. `latex' Highlight LaTeX snippets and environments.
  3678. `script' Highlight subscript and superscript.
  3679. `entities' Highlight entities."
  3680. :group 'org-appearance
  3681. :version "24.4"
  3682. :package-version '(Org . "8.0")
  3683. :type '(choice
  3684. (const :tag "No highlighting" nil)
  3685. (set :greedy t :tag "Highlight"
  3686. (const :tag "LaTeX snippets and environments" latex)
  3687. (const :tag "Subscript and superscript" script)
  3688. (const :tag "Entities" entities))))
  3689. (defcustom org-hide-emphasis-markers nil
  3690. "Non-nil mean font-lock should hide the emphasis marker characters."
  3691. :group 'org-appearance
  3692. :type 'boolean)
  3693. (defcustom org-hide-macro-markers nil
  3694. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3695. :group 'org-appearance
  3696. :type 'boolean)
  3697. (defcustom org-pretty-entities nil
  3698. "Non-nil means show entities as UTF8 characters.
  3699. When nil, the \\name form remains in the buffer."
  3700. :group 'org-appearance
  3701. :version "24.1"
  3702. :type 'boolean)
  3703. (defcustom org-pretty-entities-include-sub-superscripts t
  3704. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3705. :group 'org-appearance
  3706. :version "24.1"
  3707. :type 'boolean)
  3708. (defvar org-emph-re nil
  3709. "Regular expression for matching emphasis.
  3710. After a match, the match groups contain these elements:
  3711. 0 The match of the full regular expression, including the characters
  3712. before and after the proper match
  3713. 1 The character before the proper match, or empty at beginning of line
  3714. 2 The proper match, including the leading and trailing markers
  3715. 3 The leading marker like * or /, indicating the type of highlighting
  3716. 4 The text between the emphasis markers, not including the markers
  3717. 5 The character after the match, empty at the end of a line")
  3718. (defvar org-verbatim-re nil
  3719. "Regular expression for matching verbatim text.")
  3720. (defvar org-emphasis-regexp-components) ; defined just below
  3721. (defvar org-emphasis-alist) ; defined just below
  3722. (defun org-set-emph-re (var val)
  3723. "Set variable and compute the emphasis regular expression."
  3724. (set var val)
  3725. (when (and (boundp 'org-emphasis-alist)
  3726. (boundp 'org-emphasis-regexp-components)
  3727. org-emphasis-alist org-emphasis-regexp-components)
  3728. (let* ((e org-emphasis-regexp-components)
  3729. (pre (car e))
  3730. (post (nth 1 e))
  3731. (border (nth 2 e))
  3732. (body (nth 3 e))
  3733. (nl (nth 4 e))
  3734. (body1 (concat body "*?"))
  3735. (markers (mapconcat 'car org-emphasis-alist ""))
  3736. (vmarkers (mapconcat
  3737. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3738. org-emphasis-alist "")))
  3739. ;; make sure special characters appear at the right position in the class
  3740. (if (string-match "\\^" markers)
  3741. (setq markers (concat (replace-match "" t t markers) "^")))
  3742. (if (string-match "-" markers)
  3743. (setq markers (concat (replace-match "" t t markers) "-")))
  3744. (if (string-match "\\^" vmarkers)
  3745. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3746. (if (string-match "-" vmarkers)
  3747. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3748. (if (> nl 0)
  3749. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3750. (int-to-string nl) "\\}")))
  3751. ;; Make the regexp
  3752. (setq org-emph-re
  3753. (concat "\\([" pre "]\\|^\\)"
  3754. "\\("
  3755. "\\([" markers "]\\)"
  3756. "\\("
  3757. "[^" border "]\\|"
  3758. "[^" border "]"
  3759. body1
  3760. "[^" border "]"
  3761. "\\)"
  3762. "\\3\\)"
  3763. "\\([" post "]\\|$\\)"))
  3764. (setq org-verbatim-re
  3765. (concat "\\([" pre "]\\|^\\)"
  3766. "\\("
  3767. "\\([" vmarkers "]\\)"
  3768. "\\("
  3769. "[^" border "]\\|"
  3770. "[^" border "]"
  3771. body1
  3772. "[^" border "]"
  3773. "\\)"
  3774. "\\3\\)"
  3775. "\\([" post "]\\|$\\)")))))
  3776. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3777. ;; set this option proved cumbersome. See this message/thread:
  3778. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3779. (defvar org-emphasis-regexp-components
  3780. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3781. "Components used to build the regular expression for emphasis.
  3782. This is a list with five entries. Terminology: In an emphasis string
  3783. like \" *strong word* \", we call the initial space PREMATCH, the final
  3784. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3785. and \"trong wor\" is the body. The different components in this variable
  3786. specify what is allowed/forbidden in each part:
  3787. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3788. post Chars allowed as postmatch. End of line will be allowed too.
  3789. border The chars *forbidden* as border characters.
  3790. body-regexp A regexp like \".\" to match a body character. Don't use
  3791. non-shy groups here, and don't allow newline here.
  3792. newline The maximum number of newlines allowed in an emphasis exp.
  3793. You need to reload Org or to restart Emacs after customizing this.")
  3794. (defcustom org-emphasis-alist
  3795. `(("*" bold)
  3796. ("/" italic)
  3797. ("_" underline)
  3798. ("=" org-verbatim verbatim)
  3799. ("~" org-code verbatim)
  3800. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3801. "Alist of characters and faces to emphasize text.
  3802. Text starting and ending with a special character will be emphasized,
  3803. for example *bold*, _underlined_ and /italic/. This variable sets the
  3804. marker characters and the face to be used by font-lock for highlighting
  3805. in Org-mode Emacs buffers.
  3806. You need to reload Org or to restart Emacs after customizing this."
  3807. :group 'org-appearance
  3808. :set 'org-set-emph-re
  3809. :version "24.4"
  3810. :package-version '(Org . "8.0")
  3811. :type '(repeat
  3812. (list
  3813. (string :tag "Marker character")
  3814. (choice
  3815. (face :tag "Font-lock-face")
  3816. (plist :tag "Face property list"))
  3817. (option (const verbatim)))))
  3818. (defvar org-protecting-blocks
  3819. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3820. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3821. This is needed for font-lock setup.")
  3822. ;;; Miscellaneous options
  3823. (defgroup org-completion nil
  3824. "Completion in Org-mode."
  3825. :tag "Org Completion"
  3826. :group 'org)
  3827. (defcustom org-completion-use-ido nil
  3828. "Non-nil means use ido completion wherever possible.
  3829. Note that `ido-mode' must be active for this variable to be relevant.
  3830. If you decide to turn this variable on, you might well want to turn off
  3831. `org-outline-path-complete-in-steps'.
  3832. See also `org-completion-use-iswitchb'."
  3833. :group 'org-completion
  3834. :type 'boolean)
  3835. (defcustom org-completion-use-iswitchb nil
  3836. "Non-nil means use iswitchb completion wherever possible.
  3837. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3838. If you decide to turn this variable on, you might well want to turn off
  3839. `org-outline-path-complete-in-steps'.
  3840. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3841. :group 'org-completion
  3842. :type 'boolean)
  3843. (defcustom org-completion-fallback-command 'hippie-expand
  3844. "The expansion command called by \\[pcomplete] in normal context.
  3845. Normal means, no org-mode-specific context."
  3846. :group 'org-completion
  3847. :type 'function)
  3848. ;;; Functions and variables from their packages
  3849. ;; Declared here to avoid compiler warnings
  3850. ;; XEmacs only
  3851. (defvar outline-mode-menu-heading)
  3852. (defvar outline-mode-menu-show)
  3853. (defvar outline-mode-menu-hide)
  3854. (defvar zmacs-regions) ; XEmacs regions
  3855. ;; Emacs only
  3856. (defvar mark-active)
  3857. ;; Various packages
  3858. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3859. (declare-function calendar-forward-day "cal-move" (arg))
  3860. (declare-function calendar-goto-date "cal-move" (date))
  3861. (declare-function calendar-goto-today "cal-move" ())
  3862. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3863. (defvar calc-embedded-close-formula)
  3864. (defvar calc-embedded-open-formula)
  3865. (declare-function cdlatex-tab "ext:cdlatex" ())
  3866. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3867. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3868. (defvar font-lock-unfontify-region-function)
  3869. (declare-function iswitchb-read-buffer "iswitchb"
  3870. (prompt &optional default require-match start matches-set))
  3871. (defvar iswitchb-temp-buflist)
  3872. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3873. (defvar org-agenda-tags-todo-honor-ignore-options)
  3874. (declare-function org-agenda-skip "org-agenda" ())
  3875. (declare-function
  3876. org-agenda-format-item "org-agenda"
  3877. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3878. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3879. (declare-function org-agenda-change-all-lines "org-agenda"
  3880. (newhead hdmarker &optional fixface just-this))
  3881. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3882. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3883. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3884. (beg end))
  3885. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3886. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3887. "org-agenda" (&optional end))
  3888. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3889. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3890. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3891. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3892. (declare-function org-indent-mode "org-indent" (&optional arg))
  3893. (declare-function parse-time-string "parse-time" (string))
  3894. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3895. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3896. (defvar remember-data-file)
  3897. (defvar texmathp-why)
  3898. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3899. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3900. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3901. ;;;###autoload
  3902. (defun turn-on-orgtbl ()
  3903. "Unconditionally turn on `orgtbl-mode'."
  3904. (require 'org-table)
  3905. (orgtbl-mode 1))
  3906. (defun org-at-table-p (&optional table-type)
  3907. "Non-nil if the cursor is inside an Org table.
  3908. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3909. If `org-enable-table-editor' is nil, return nil unconditionally."
  3910. (and
  3911. org-enable-table-editor
  3912. (save-excursion
  3913. (beginning-of-line)
  3914. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3915. (or (not (derived-mode-p 'org-mode))
  3916. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3917. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3918. (defsubst org-table-p () (org-at-table-p))
  3919. (defun org-at-table.el-p ()
  3920. "Non-nil when point is at a table.el table."
  3921. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3922. (let ((element (org-element-at-point)))
  3923. (and (eq (org-element-type element) 'table)
  3924. (eq (org-element-property :type element) 'table.el)))))
  3925. (defun org-table-recognize-table.el ()
  3926. "If there is a table.el table nearby, recognize it and move into it."
  3927. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3928. (beginning-of-line)
  3929. (unless (or (looking-at org-table-dataline-regexp)
  3930. (not (looking-at org-table1-hline-regexp)))
  3931. (forward-line)
  3932. (when (looking-at org-table-any-border-regexp)
  3933. (forward-line -2)))
  3934. (if (re-search-forward "|" (org-table-end t) t)
  3935. (progn
  3936. (require 'table)
  3937. (if (table--at-cell-p (point)) t
  3938. (message "recognizing table.el table...")
  3939. (table-recognize-table)
  3940. (message "recognizing table.el table...done")))
  3941. (error "This should not happen"))))
  3942. (defun org-at-table-hline-p ()
  3943. "Non-nil when point is inside a hline in a table.
  3944. Assume point is already in a table. If `org-enable-table-editor'
  3945. is nil, return nil unconditionally."
  3946. (and org-enable-table-editor
  3947. (save-excursion
  3948. (beginning-of-line)
  3949. (looking-at org-table-hline-regexp))))
  3950. (defun org-table-map-tables (function &optional quietly)
  3951. "Apply FUNCTION to the start of all tables in the buffer."
  3952. (save-excursion
  3953. (save-restriction
  3954. (widen)
  3955. (goto-char (point-min))
  3956. (while (re-search-forward org-table-any-line-regexp nil t)
  3957. (unless quietly
  3958. (message "Mapping tables: %d%%"
  3959. (floor (* 100.0 (point)) (buffer-size))))
  3960. (beginning-of-line 1)
  3961. (when (and (looking-at org-table-line-regexp)
  3962. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3963. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3964. (save-excursion (funcall function))
  3965. (or (looking-at org-table-line-regexp)
  3966. (forward-char 1)))
  3967. (re-search-forward org-table-any-border-regexp nil 1))))
  3968. (unless quietly (message "Mapping tables: done")))
  3969. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3970. (declare-function org-clock-update-mode-line "org-clock" ())
  3971. (declare-function org-resolve-clocks "org-clock"
  3972. (&optional also-non-dangling-p prompt last-valid))
  3973. (defun org-at-TBLFM-p (&optional pos)
  3974. "Non-nil when point (or POS) is in #+TBLFM line."
  3975. (save-excursion
  3976. (goto-char (or pos (point)))
  3977. (beginning-of-line)
  3978. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3979. (eq (org-element-type (org-element-at-point)) 'table))))
  3980. (defvar org-clock-start-time)
  3981. (defvar org-clock-marker (make-marker)
  3982. "Marker recording the last clock-in.")
  3983. (defvar org-clock-hd-marker (make-marker)
  3984. "Marker recording the last clock-in, but the headline position.")
  3985. (defvar org-clock-heading ""
  3986. "The heading of the current clock entry.")
  3987. (defun org-clock-is-active ()
  3988. "Return the buffer where the clock is currently running.
  3989. Return nil if no clock is running."
  3990. (marker-buffer org-clock-marker))
  3991. (defun org-check-running-clock ()
  3992. "Check if the current buffer contains the running clock.
  3993. If yes, offer to stop it and to save the buffer with the changes."
  3994. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3995. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3996. (buffer-name))))
  3997. (org-clock-out)
  3998. (when (y-or-n-p "Save changed buffer?")
  3999. (save-buffer))))
  4000. (defun org-clocktable-try-shift (dir n)
  4001. "Check if this line starts a clock table, if yes, shift the time block."
  4002. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  4003. (org-clocktable-shift dir n)))
  4004. ;;;###autoload
  4005. (defun org-clock-persistence-insinuate ()
  4006. "Set up hooks for clock persistence."
  4007. (require 'org-clock)
  4008. (add-hook 'org-mode-hook 'org-clock-load)
  4009. (add-hook 'kill-emacs-hook 'org-clock-save))
  4010. (defgroup org-archive nil
  4011. "Options concerning archiving in Org-mode."
  4012. :tag "Org Archive"
  4013. :group 'org-structure)
  4014. (defcustom org-archive-location "%s_archive::"
  4015. "The location where subtrees should be archived.
  4016. The value of this variable is a string, consisting of two parts,
  4017. separated by a double-colon. The first part is a filename and
  4018. the second part is a headline.
  4019. When the filename is omitted, archiving happens in the same file.
  4020. %s in the filename will be replaced by the current file
  4021. name (without the directory part). Archiving to a different file
  4022. is useful to keep archived entries from contributing to the
  4023. Org-mode Agenda.
  4024. The archived entries will be filed as subtrees of the specified
  4025. headline. When the headline is omitted, the subtrees are simply
  4026. filed away at the end of the file, as top-level entries. Also in
  4027. the heading you can use %s to represent the file name, this can be
  4028. useful when using the same archive for a number of different files.
  4029. Here are a few examples:
  4030. \"%s_archive::\"
  4031. If the current file is Projects.org, archive in file
  4032. Projects.org_archive, as top-level trees. This is the default.
  4033. \"::* Archived Tasks\"
  4034. Archive in the current file, under the top-level headline
  4035. \"* Archived Tasks\".
  4036. \"~/org/archive.org::\"
  4037. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4038. \"~/org/archive.org::* From %s\"
  4039. Archive in file ~/org/archive.org (absolute path), under headlines
  4040. \"From FILENAME\" where file name is the current file name.
  4041. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4042. The \"datetree/\" string is special, signifying to archive
  4043. items to the datetree. Items are placed in either the CLOSED
  4044. date of the item, or the current date if there is no CLOSED date.
  4045. The heading will be a subentry to the current date. There doesn't
  4046. need to be a heading, but there always needs to be a slash after
  4047. datetree. For example, to store archived items directly in the
  4048. datetree, use \"~/org/datetree.org::datetree/\".
  4049. \"basement::** Finished Tasks\"
  4050. Archive in file ./basement (relative path), as level 3 trees
  4051. below the level 2 heading \"** Finished Tasks\".
  4052. You may set this option on a per-file basis by adding to the buffer a
  4053. line like
  4054. #+ARCHIVE: basement::** Finished Tasks
  4055. You may also define it locally for a subtree by setting an ARCHIVE property
  4056. in the entry. If such a property is found in an entry, or anywhere up
  4057. the hierarchy, it will be used."
  4058. :group 'org-archive
  4059. :type 'string)
  4060. (defcustom org-agenda-skip-archived-trees t
  4061. "Non-nil means the agenda will skip any items located in archived trees.
  4062. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4063. variable is no longer recommended, you should leave it at the value t.
  4064. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4065. :group 'org-archive
  4066. :group 'org-agenda-skip
  4067. :type 'boolean)
  4068. (defcustom org-columns-skip-archived-trees t
  4069. "Non-nil means ignore archived trees when creating column view."
  4070. :group 'org-archive
  4071. :group 'org-properties
  4072. :type 'boolean)
  4073. (defcustom org-cycle-open-archived-trees nil
  4074. "Non-nil means `org-cycle' will open archived trees.
  4075. An archived tree is a tree marked with the tag ARCHIVE.
  4076. When nil, archived trees will stay folded. You can still open them with
  4077. normal outline commands like `show-all', but not with the cycling commands."
  4078. :group 'org-archive
  4079. :group 'org-cycle
  4080. :type 'boolean)
  4081. (defcustom org-sparse-tree-open-archived-trees nil
  4082. "Non-nil means sparse tree construction shows matches in archived trees.
  4083. When nil, matches in these trees are highlighted, but the trees are kept in
  4084. collapsed state."
  4085. :group 'org-archive
  4086. :group 'org-sparse-trees
  4087. :type 'boolean)
  4088. (defcustom org-sparse-tree-default-date-type nil
  4089. "The default date type when building a sparse tree.
  4090. When this is nil, a date is a scheduled or a deadline timestamp.
  4091. Otherwise, these types are allowed:
  4092. all: all timestamps
  4093. active: only active timestamps (<...>)
  4094. inactive: only inactive timestamps ([...])
  4095. scheduled: only scheduled timestamps
  4096. deadline: only deadline timestamps"
  4097. :type '(choice (const :tag "Scheduled or deadline" nil)
  4098. (const :tag "All timestamps" all)
  4099. (const :tag "Only active timestamps" active)
  4100. (const :tag "Only inactive timestamps" inactive)
  4101. (const :tag "Only scheduled timestamps" scheduled)
  4102. (const :tag "Only deadline timestamps" deadline)
  4103. (const :tag "Only closed timestamps" closed))
  4104. :version "25.1"
  4105. :package-version '(Org . "8.3")
  4106. :group 'org-sparse-trees)
  4107. (defun org-cycle-hide-archived-subtrees (state)
  4108. "Re-hide all archived subtrees after a visibility state change."
  4109. (when (and (not org-cycle-open-archived-trees)
  4110. (not (memq state '(overview folded))))
  4111. (save-excursion
  4112. (let* ((globalp (memq state '(contents all)))
  4113. (beg (if globalp (point-min) (point)))
  4114. (end (if globalp (point-max) (org-end-of-subtree t))))
  4115. (org-hide-archived-subtrees beg end)
  4116. (goto-char beg)
  4117. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4118. (message "%s" (substitute-command-keys
  4119. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4120. (defun org-force-cycle-archived ()
  4121. "Cycle subtree even if it is archived."
  4122. (interactive)
  4123. (setq this-command 'org-cycle)
  4124. (let ((org-cycle-open-archived-trees t))
  4125. (call-interactively 'org-cycle)))
  4126. (defun org-hide-archived-subtrees (beg end)
  4127. "Re-hide all archived subtrees after a visibility state change."
  4128. (org-with-wide-buffer
  4129. (let ((case-fold-search nil)
  4130. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4131. (goto-char beg)
  4132. ;; Include headline point is currently on.
  4133. (beginning-of-line)
  4134. (while (and (< (point) end) (re-search-forward re end t))
  4135. (when (member org-archive-tag (org-get-tags))
  4136. (org-flag-subtree t)
  4137. (org-end-of-subtree t))))))
  4138. (declare-function outline-end-of-heading "outline" ())
  4139. (declare-function outline-flag-region "outline" (from to flag))
  4140. (defun org-flag-subtree (flag)
  4141. (save-excursion
  4142. (org-back-to-heading t)
  4143. (outline-end-of-heading)
  4144. (outline-flag-region (point)
  4145. (progn (org-end-of-subtree t) (point))
  4146. flag)))
  4147. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4148. ;; Declare Column View Code
  4149. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4150. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4151. (declare-function org-columns-compute "org-colview" (property))
  4152. ;; Declare ID code
  4153. (declare-function org-id-store-link "org-id")
  4154. (declare-function org-id-locations-load "org-id")
  4155. (declare-function org-id-locations-save "org-id")
  4156. (defvar org-id-track-globally)
  4157. ;;; Variables for pre-computed regular expressions, all buffer local
  4158. (defvar org-todo-regexp nil
  4159. "Matches any of the TODO state keywords.")
  4160. (make-variable-buffer-local 'org-todo-regexp)
  4161. (defvar org-not-done-regexp nil
  4162. "Matches any of the TODO state keywords except the last one.")
  4163. (make-variable-buffer-local 'org-not-done-regexp)
  4164. (defvar org-not-done-heading-regexp nil
  4165. "Matches a TODO headline that is not done.")
  4166. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4167. (defvar org-todo-line-regexp nil
  4168. "Matches a headline and puts TODO state into group 2 if present.")
  4169. (make-variable-buffer-local 'org-todo-line-regexp)
  4170. (defvar org-complex-heading-regexp nil
  4171. "Matches a headline and puts everything into groups:
  4172. group 1: the stars
  4173. group 2: The todo keyword, maybe
  4174. group 3: Priority cookie
  4175. group 4: True headline
  4176. group 5: Tags")
  4177. (make-variable-buffer-local 'org-complex-heading-regexp)
  4178. (defvar org-complex-heading-regexp-format nil
  4179. "Printf format to make regexp to match an exact headline.
  4180. This regexp will match the headline of any node which has the
  4181. exact headline text that is put into the format, but may have any
  4182. TODO state, priority and tags.")
  4183. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4184. (defvar org-todo-line-tags-regexp nil
  4185. "Matches a headline and puts TODO state into group 2 if present.
  4186. Also put tags into group 4 if tags are present.")
  4187. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4188. (defconst org-plain-time-of-day-regexp
  4189. (concat
  4190. "\\(\\<[012]?[0-9]"
  4191. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4192. "\\(--?"
  4193. "\\(\\<[012]?[0-9]"
  4194. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4195. "\\)?")
  4196. "Regular expression to match a plain time or time range.
  4197. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4198. groups carry important information:
  4199. 0 the full match
  4200. 1 the first time, range or not
  4201. 8 the second time, if it is a range.")
  4202. (defconst org-plain-time-extension-regexp
  4203. (concat
  4204. "\\(\\<[012]?[0-9]"
  4205. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4206. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4207. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4208. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4209. groups carry important information:
  4210. 0 the full match
  4211. 7 hours of duration
  4212. 9 minutes of duration")
  4213. (defconst org-stamp-time-of-day-regexp
  4214. (concat
  4215. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4216. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4217. "\\(--?"
  4218. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4219. "Regular expression to match a timestamp time or time range.
  4220. After a match, the following groups carry important information:
  4221. 0 the full match
  4222. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4223. 2 the first time, range or not
  4224. 4 the second time, if it is a range.")
  4225. (defconst org-startup-options
  4226. '(("fold" org-startup-folded t)
  4227. ("overview" org-startup-folded t)
  4228. ("nofold" org-startup-folded nil)
  4229. ("showall" org-startup-folded nil)
  4230. ("showeverything" org-startup-folded showeverything)
  4231. ("content" org-startup-folded content)
  4232. ("indent" org-startup-indented t)
  4233. ("noindent" org-startup-indented nil)
  4234. ("hidestars" org-hide-leading-stars t)
  4235. ("showstars" org-hide-leading-stars nil)
  4236. ("odd" org-odd-levels-only t)
  4237. ("oddeven" org-odd-levels-only nil)
  4238. ("align" org-startup-align-all-tables t)
  4239. ("noalign" org-startup-align-all-tables nil)
  4240. ("inlineimages" org-startup-with-inline-images t)
  4241. ("noinlineimages" org-startup-with-inline-images nil)
  4242. ("latexpreview" org-startup-with-latex-preview t)
  4243. ("nolatexpreview" org-startup-with-latex-preview nil)
  4244. ("customtime" org-display-custom-times t)
  4245. ("logdone" org-log-done time)
  4246. ("lognotedone" org-log-done note)
  4247. ("nologdone" org-log-done nil)
  4248. ("lognoteclock-out" org-log-note-clock-out t)
  4249. ("nolognoteclock-out" org-log-note-clock-out nil)
  4250. ("logrepeat" org-log-repeat state)
  4251. ("lognoterepeat" org-log-repeat note)
  4252. ("logdrawer" org-log-into-drawer t)
  4253. ("nologdrawer" org-log-into-drawer nil)
  4254. ("logstatesreversed" org-log-states-order-reversed t)
  4255. ("nologstatesreversed" org-log-states-order-reversed nil)
  4256. ("nologrepeat" org-log-repeat nil)
  4257. ("logreschedule" org-log-reschedule time)
  4258. ("lognotereschedule" org-log-reschedule note)
  4259. ("nologreschedule" org-log-reschedule nil)
  4260. ("logredeadline" org-log-redeadline time)
  4261. ("lognoteredeadline" org-log-redeadline note)
  4262. ("nologredeadline" org-log-redeadline nil)
  4263. ("logrefile" org-log-refile time)
  4264. ("lognoterefile" org-log-refile note)
  4265. ("nologrefile" org-log-refile nil)
  4266. ("fninline" org-footnote-define-inline t)
  4267. ("nofninline" org-footnote-define-inline nil)
  4268. ("fnlocal" org-footnote-section nil)
  4269. ("fnauto" org-footnote-auto-label t)
  4270. ("fnprompt" org-footnote-auto-label nil)
  4271. ("fnconfirm" org-footnote-auto-label confirm)
  4272. ("fnplain" org-footnote-auto-label plain)
  4273. ("fnadjust" org-footnote-auto-adjust t)
  4274. ("nofnadjust" org-footnote-auto-adjust nil)
  4275. ("constcgs" constants-unit-system cgs)
  4276. ("constSI" constants-unit-system SI)
  4277. ("noptag" org-tag-persistent-alist nil)
  4278. ("hideblocks" org-hide-block-startup t)
  4279. ("nohideblocks" org-hide-block-startup nil)
  4280. ("beamer" org-startup-with-beamer-mode t)
  4281. ("entitiespretty" org-pretty-entities t)
  4282. ("entitiesplain" org-pretty-entities nil))
  4283. "Variable associated with STARTUP options for org-mode.
  4284. Each element is a list of three items: the startup options (as written
  4285. in the #+STARTUP line), the corresponding variable, and the value to set
  4286. this variable to if the option is found. An optional forth element PUSH
  4287. means to push this value onto the list in the variable.")
  4288. (defcustom org-group-tags t
  4289. "When non-nil (the default), use group tags.
  4290. This can be turned on/off through `org-toggle-tags-groups'."
  4291. :group 'org-tags
  4292. :group 'org-startup
  4293. :type 'boolean)
  4294. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4295. (defun org-toggle-tags-groups ()
  4296. "Toggle support for group tags.
  4297. Support for group tags is controlled by the option
  4298. `org-group-tags', which is non-nil by default."
  4299. (interactive)
  4300. (setq org-group-tags (not org-group-tags))
  4301. (cond ((and (derived-mode-p 'org-agenda-mode)
  4302. org-group-tags)
  4303. (org-agenda-redo))
  4304. ((derived-mode-p 'org-mode)
  4305. (let ((org-inhibit-startup t)) (org-mode))))
  4306. (message "Groups tags support has been turned %s"
  4307. (if org-group-tags "on" "off")))
  4308. (defun org-set-regexps-and-options (&optional tags-only)
  4309. "Precompute regular expressions used in the current buffer.
  4310. When optional argument TAGS-ONLY is non-nil, only compute tags
  4311. related expressions."
  4312. (when (derived-mode-p 'org-mode)
  4313. (let ((alist (org--setup-collect-keywords
  4314. (org-make-options-regexp
  4315. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4316. (and (not tags-only)
  4317. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4318. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4319. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4320. (org--setup-process-tags
  4321. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4322. (unless tags-only
  4323. ;; File properties.
  4324. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4325. ;; Archive location.
  4326. (let ((archive (cdr (assq 'archive alist))))
  4327. (when archive (org-set-local 'org-archive-location archive)))
  4328. ;; Category.
  4329. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4330. (when cat
  4331. (org-set-local 'org-category (intern cat))
  4332. (org-set-local 'org-file-properties
  4333. (org--update-property-plist
  4334. "CATEGORY" cat org-file-properties))))
  4335. ;; Columns.
  4336. (let ((column (cdr (assq 'columns alist))))
  4337. (when column (org-set-local 'org-columns-default-format column)))
  4338. ;; Constants.
  4339. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4340. ;; Link abbreviations.
  4341. (let ((links (cdr (assq 'link alist))))
  4342. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4343. ;; Priorities.
  4344. (let ((priorities (cdr (assq 'priorities alist))))
  4345. (when priorities
  4346. (org-set-local 'org-highest-priority (nth 0 priorities))
  4347. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4348. (org-set-local 'org-default-priority (nth 2 priorities))))
  4349. ;; Scripts.
  4350. (let ((scripts (assq 'scripts alist)))
  4351. (when scripts
  4352. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4353. ;; Startup options.
  4354. (let ((startup (cdr (assq 'startup alist))))
  4355. (dolist (option startup)
  4356. (let ((entry (assoc-string option org-startup-options t)))
  4357. (when entry
  4358. (let ((var (nth 1 entry))
  4359. (val (nth 2 entry)))
  4360. (if (not (nth 3 entry)) (org-set-local var val)
  4361. (unless (listp (symbol-value var))
  4362. (org-set-local var nil))
  4363. (add-to-list var val)))))))
  4364. ;; TODO keywords.
  4365. (org-set-local 'org-todo-kwd-alist nil)
  4366. (org-set-local 'org-todo-key-alist nil)
  4367. (org-set-local 'org-todo-key-trigger nil)
  4368. (org-set-local 'org-todo-keywords-1 nil)
  4369. (org-set-local 'org-done-keywords nil)
  4370. (org-set-local 'org-todo-heads nil)
  4371. (org-set-local 'org-todo-sets nil)
  4372. (org-set-local 'org-todo-log-states nil)
  4373. (let ((todo-sequences
  4374. (or (nreverse (cdr (assq 'todo alist)))
  4375. (let ((d (default-value 'org-todo-keywords)))
  4376. (if (not (stringp (car d))) d
  4377. ;; XXX: Backward compatibility code.
  4378. (list (cons org-todo-interpretation d)))))))
  4379. (dolist (sequence todo-sequences)
  4380. (let* ((sequence (or (run-hook-with-args-until-success
  4381. 'org-todo-setup-filter-hook sequence)
  4382. sequence))
  4383. (sequence-type (car sequence))
  4384. (keywords (cdr sequence))
  4385. (sep (member "|" keywords))
  4386. names alist)
  4387. (dolist (k (remove "|" keywords))
  4388. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4389. k)
  4390. (error "Invalid TODO keyword %s" k))
  4391. (let ((name (match-string 1 k))
  4392. (key (match-string 2 k))
  4393. (log (org-extract-log-state-settings k)))
  4394. (push name names)
  4395. (push (cons name (and key (string-to-char key))) alist)
  4396. (when log (push log org-todo-log-states))))
  4397. (let* ((names (nreverse names))
  4398. (done (if sep (org-remove-keyword-keys (cdr sep))
  4399. (last names)))
  4400. (head (car names))
  4401. (tail (list sequence-type head (car done) (org-last done))))
  4402. (add-to-list 'org-todo-heads head 'append)
  4403. (push names org-todo-sets)
  4404. (setq org-done-keywords (append org-done-keywords done nil))
  4405. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4406. (setq org-todo-key-alist
  4407. (append org-todo-key-alist
  4408. (and alist
  4409. (append '((:startgroup))
  4410. (nreverse alist)
  4411. '((:endgroup))))))
  4412. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4413. (setq org-todo-sets (nreverse org-todo-sets)
  4414. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4415. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4416. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4417. ;; Compute the regular expressions and other local variables.
  4418. ;; Using `org-outline-regexp-bol' would complicate them much,
  4419. ;; because of the fixed white space at the end of that string.
  4420. (if (not org-done-keywords)
  4421. (setq org-done-keywords
  4422. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4423. (setq org-not-done-keywords
  4424. (org-delete-all org-done-keywords
  4425. (copy-sequence org-todo-keywords-1))
  4426. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4427. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4428. org-not-done-heading-regexp
  4429. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4430. org-todo-line-regexp
  4431. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4432. org-complex-heading-regexp
  4433. (concat "^\\(\\*+\\)"
  4434. "\\(?: +" org-todo-regexp "\\)?"
  4435. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4436. "\\(?: +\\(.*?\\)\\)??"
  4437. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4438. "[ \t]*$")
  4439. org-complex-heading-regexp-format
  4440. (concat "^\\(\\*+\\)"
  4441. "\\(?: +" org-todo-regexp "\\)?"
  4442. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4443. "\\(?: +"
  4444. ;; Stats cookies can be stuck to body.
  4445. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4446. "\\(%s\\)"
  4447. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4448. "\\)"
  4449. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4450. "[ \t]*$")
  4451. org-todo-line-tags-regexp
  4452. (concat "^\\(\\*+\\)"
  4453. "\\(?: +" org-todo-regexp "\\)?"
  4454. "\\(?: +\\(.*?\\)\\)??"
  4455. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4456. "[ \t]*$"))
  4457. (org-compute-latex-and-related-regexp)))))
  4458. (defun org--setup-collect-keywords (regexp &optional files alist)
  4459. "Return setup keywords values as an alist.
  4460. REGEXP matches a subset of setup keywords. FILES is a list of
  4461. file names already visited. It is used to avoid circular setup
  4462. files. ALIST, when non-nil, is the alist computed so far.
  4463. Return value contains the following keys: `archive', `category',
  4464. `columns', `constants', `filetags', `link', `priorities',
  4465. `property', `scripts', `startup', `tags' and `todo'."
  4466. (org-with-wide-buffer
  4467. (goto-char (point-min))
  4468. (let ((case-fold-search t))
  4469. (while (re-search-forward regexp nil t)
  4470. (let ((element (org-element-at-point)))
  4471. (when (eq (org-element-type element) 'keyword)
  4472. (let ((key (org-element-property :key element))
  4473. (value (org-element-property :value element)))
  4474. (cond
  4475. ((equal key "ARCHIVE")
  4476. (when (org-string-nw-p value)
  4477. (push (cons 'archive value) alist)))
  4478. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4479. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4480. ((equal key "CONSTANTS")
  4481. (let* ((constants (assq 'constants alist))
  4482. (store (cdr constants)))
  4483. (dolist (pair (org-split-string value))
  4484. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4485. pair)
  4486. (let* ((name (match-string 1 pair))
  4487. (value (match-string 2 pair))
  4488. (old (assoc name store)))
  4489. (if old (setcdr old value)
  4490. (push (cons name value) store)))))
  4491. (if constants (setcdr constants store)
  4492. (push (cons 'constants store) alist))))
  4493. ((equal key "FILETAGS")
  4494. (when (org-string-nw-p value)
  4495. (let ((old (assq 'filetags alist))
  4496. (new (apply #'nconc
  4497. (mapcar (lambda (x) (org-split-string x ":"))
  4498. (org-split-string value)))))
  4499. (if old (setcdr old (append new (cdr old)))
  4500. (push (cons 'filetags new) alist)))))
  4501. ((equal key "LINK")
  4502. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4503. (let ((links (assq 'link alist))
  4504. (pair (cons (org-match-string-no-properties 1 value)
  4505. (org-match-string-no-properties 2 value))))
  4506. (if links (push pair (cdr links))
  4507. (push (list 'link pair) alist)))))
  4508. ((equal key "OPTIONS")
  4509. (when (and (org-string-nw-p value)
  4510. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4511. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4512. ((equal key "PRIORITIES")
  4513. (push (cons 'priorities
  4514. (let ((prio (org-split-string value)))
  4515. (if (< (length prio) 3) '(?A ?C ?B)
  4516. (mapcar #'string-to-char prio))))
  4517. alist))
  4518. ((equal key "PROPERTY")
  4519. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4520. (let* ((property (assq 'property alist))
  4521. (value (org--update-property-plist
  4522. (org-match-string-no-properties 1 value)
  4523. (org-match-string-no-properties 2 value)
  4524. (cdr property))))
  4525. (if property (setcdr property value)
  4526. (push (cons 'property value) alist)))))
  4527. ((equal key "STARTUP")
  4528. (let ((startup (assq 'startup alist)))
  4529. (if startup
  4530. (setcdr startup
  4531. (append (cdr startup) (org-split-string value)))
  4532. (push (cons 'startup (org-split-string value)) alist))))
  4533. ((equal key "TAGS")
  4534. (let ((tag-cell (assq 'tags alist)))
  4535. (if tag-cell
  4536. (setcdr tag-cell
  4537. (append (cdr tag-cell)
  4538. '("\\n")
  4539. (org-split-string value)))
  4540. (push (cons 'tags (org-split-string value)) alist))))
  4541. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4542. (let ((todo (assq 'todo alist))
  4543. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4544. (org-split-string value))))
  4545. (if todo (push value (cdr todo))
  4546. (push (list 'todo value) alist))))
  4547. ((equal key "SETUPFILE")
  4548. (unless buffer-read-only ; Do not check in Gnus messages.
  4549. (let ((f (and (org-string-nw-p value)
  4550. (expand-file-name
  4551. (org-remove-double-quotes value)))))
  4552. (when (and f (file-readable-p f) (not (member f files)))
  4553. (with-temp-buffer
  4554. (setq default-directory (file-name-directory f))
  4555. (insert-file-contents f)
  4556. (setq alist
  4557. ;; Fake Org mode to benefit from cache
  4558. ;; without recurring needlessly.
  4559. (let ((major-mode 'org-mode))
  4560. (org--setup-collect-keywords
  4561. regexp (cons f files) alist)))))))))))))))
  4562. alist)
  4563. (defun org--setup-process-tags (tags filetags)
  4564. "Precompute variables used for tags.
  4565. TAGS is a list of tags and tag group symbols, as strings.
  4566. FILETAGS is a list of tags, as strings."
  4567. ;; Process the file tags.
  4568. (org-set-local 'org-file-tags
  4569. (mapcar #'org-add-prop-inherited filetags))
  4570. ;; Provide default tags if no local tags are found.
  4571. (when (and (not tags) org-tag-alist)
  4572. (setq tags
  4573. (mapcar (lambda (tag)
  4574. (case (car tag)
  4575. (:startgroup "{")
  4576. (:endgroup "}")
  4577. (:startgrouptag "[")
  4578. (:endgrouptag "]")
  4579. (:grouptags ":")
  4580. (:newline "\\n")
  4581. (otherwise (concat (car tag)
  4582. (and (characterp (cdr tag))
  4583. (format "(%c)" (cdr tag)))))))
  4584. org-tag-alist)))
  4585. ;; Process the tags.
  4586. (org-set-local 'org-tag-groups-alist nil)
  4587. (org-set-local 'org-tag-alist nil)
  4588. (let (group-flag)
  4589. (while tags
  4590. (let ((e (car tags)))
  4591. (setq tags (cdr tags))
  4592. (cond
  4593. ((equal e "{")
  4594. (push '(:startgroup) org-tag-alist)
  4595. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4596. ((equal e "}")
  4597. (push '(:endgroup) org-tag-alist)
  4598. (setq group-flag nil))
  4599. ((equal e "[")
  4600. (push '(:startgrouptag) org-tag-alist)
  4601. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4602. ((equal e "]")
  4603. (push '(:endgrouptag) org-tag-alist)
  4604. (setq group-flag nil))
  4605. ((equal e ":")
  4606. (push '(:grouptags) org-tag-alist)
  4607. (setq group-flag 'append))
  4608. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4609. ((string-match
  4610. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4611. "\\|{.+?}\\)" ; regular expression
  4612. "\\(?:(\\(.\\))\\)?\\'")) e)
  4613. (let ((tag (match-string 1 e))
  4614. (key (and (match-beginning 2)
  4615. (string-to-char (match-string 2 e)))))
  4616. (cond ((eq group-flag 'append)
  4617. (setcar org-tag-groups-alist
  4618. (append (car org-tag-groups-alist) (list tag))))
  4619. (group-flag (push (list tag) org-tag-groups-alist)))
  4620. ;; Push all tags in groups, no matter if they already exist.
  4621. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4622. (push (cons tag key) org-tag-alist))))))))
  4623. (setq org-tag-alist (nreverse org-tag-alist)))
  4624. (defun org-file-contents (file &optional noerror)
  4625. "Return the contents of FILE, as a string."
  4626. (if (and file (file-readable-p file))
  4627. (with-temp-buffer
  4628. (insert-file-contents file)
  4629. (buffer-string))
  4630. (funcall (if noerror 'message 'error)
  4631. "Cannot read file \"%s\"%s"
  4632. file
  4633. (let ((from (buffer-file-name (buffer-base-buffer))))
  4634. (if from (concat " (referenced in file \"" from "\")") "")))))
  4635. (defun org-extract-log-state-settings (x)
  4636. "Extract the log state setting from a TODO keyword string.
  4637. This will extract info from a string like \"WAIT(w@/!)\"."
  4638. (let (kw key log1 log2)
  4639. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4640. (setq kw (match-string 1 x)
  4641. key (and (match-end 2) (match-string 2 x))
  4642. log1 (and (match-end 3) (match-string 3 x))
  4643. log2 (and (match-end 4) (match-string 4 x)))
  4644. (and (or log1 log2)
  4645. (list kw
  4646. (and log1 (if (equal log1 "!") 'time 'note))
  4647. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4648. (defun org-remove-keyword-keys (list)
  4649. "Remove a pair of parenthesis at the end of each string in LIST."
  4650. (mapcar (lambda (x)
  4651. (if (string-match "(.*)$" x)
  4652. (substring x 0 (match-beginning 0))
  4653. x))
  4654. list))
  4655. (defun org-assign-fast-keys (alist)
  4656. "Assign fast keys to a keyword-key alist.
  4657. Respect keys that are already there."
  4658. (let (new e (alt ?0))
  4659. (while (setq e (pop alist))
  4660. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4661. (cdr e)) ;; Key already assigned.
  4662. (push e new)
  4663. (let ((clist (string-to-list (downcase (car e))))
  4664. (used (append new alist)))
  4665. (when (= (car clist) ?@)
  4666. (pop clist))
  4667. (while (and clist (rassoc (car clist) used))
  4668. (pop clist))
  4669. (unless clist
  4670. (while (rassoc alt used)
  4671. (incf alt)))
  4672. (push (cons (car e) (or (car clist) alt)) new))))
  4673. (nreverse new)))
  4674. ;;; Some variables used in various places
  4675. (defvar org-window-configuration nil
  4676. "Used in various places to store a window configuration.")
  4677. (defvar org-selected-window nil
  4678. "Used in various places to store a window configuration.")
  4679. (defvar org-finish-function nil
  4680. "Function to be called when `C-c C-c' is used.
  4681. This is for getting out of special buffers like capture.")
  4682. ;; FIXME: Occasionally check by commenting these, to make sure
  4683. ;; no other functions uses these, forgetting to let-bind them.
  4684. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4685. (defvar org-last-state)
  4686. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4687. ;; Defined somewhere in this file, but used before definition.
  4688. (defvar org-entities) ;; defined in org-entities.el
  4689. (defvar org-struct-menu)
  4690. (defvar org-org-menu)
  4691. (defvar org-tbl-menu)
  4692. ;;;; Define the Org-mode
  4693. ;; We use a before-change function to check if a table might need
  4694. ;; an update.
  4695. (defvar org-table-may-need-update t
  4696. "Indicates that a table might need an update.
  4697. This variable is set by `org-before-change-function'.
  4698. `org-table-align' sets it back to nil.")
  4699. (defun org-before-change-function (beg end)
  4700. "Every change indicates that a table might need an update."
  4701. (setq org-table-may-need-update t))
  4702. (defvar org-mode-map)
  4703. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4704. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4705. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4706. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4707. (defvar org-table-buffer-is-an nil)
  4708. (defvar bidi-paragraph-direction)
  4709. (defvar buffer-face-mode-face)
  4710. (require 'outline)
  4711. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4712. (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"))
  4713. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4714. ;; Other stuff we need.
  4715. (require 'time-date)
  4716. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4717. (require 'easymenu)
  4718. (autoload 'easy-menu-add "easymenu")
  4719. (require 'overlay)
  4720. ;; (require 'org-macs) moved higher up in the file before it is first used
  4721. (require 'org-entities)
  4722. ;; (require 'org-compat) moved higher up in the file before it is first used
  4723. (require 'org-faces)
  4724. (require 'org-list)
  4725. (require 'org-pcomplete)
  4726. (require 'org-src)
  4727. (require 'org-footnote)
  4728. (require 'org-macro)
  4729. ;; babel
  4730. (require 'ob)
  4731. ;;;###autoload
  4732. (define-derived-mode org-mode outline-mode "Org"
  4733. "Outline-based notes management and organizer, alias
  4734. \"Carsten's outline-mode for keeping track of everything.\"
  4735. Org-mode develops organizational tasks around a NOTES file which
  4736. contains information about projects as plain text. Org-mode is
  4737. implemented on top of outline-mode, which is ideal to keep the content
  4738. of large files well structured. It supports ToDo items, deadlines and
  4739. time stamps, which magically appear in the diary listing of the Emacs
  4740. calendar. Tables are easily created with a built-in table editor.
  4741. Plain text URL-like links connect to websites, emails (VM), Usenet
  4742. messages (Gnus), BBDB entries, and any files related to the project.
  4743. For printing and sharing of notes, an Org-mode file (or a part of it)
  4744. can be exported as a structured ASCII or HTML file.
  4745. The following commands are available:
  4746. \\{org-mode-map}"
  4747. ;; Get rid of Outline menus, they are not needed
  4748. ;; Need to do this here because define-derived-mode sets up
  4749. ;; the keymap so late. Still, it is a waste to call this each time
  4750. ;; we switch another buffer into org-mode.
  4751. (if (featurep 'xemacs)
  4752. (when (boundp 'outline-mode-menu-heading)
  4753. ;; Assume this is Greg's port, it uses easymenu
  4754. (easy-menu-remove outline-mode-menu-heading)
  4755. (easy-menu-remove outline-mode-menu-show)
  4756. (easy-menu-remove outline-mode-menu-hide))
  4757. (define-key org-mode-map [menu-bar headings] 'undefined)
  4758. (define-key org-mode-map [menu-bar hide] 'undefined)
  4759. (define-key org-mode-map [menu-bar show] 'undefined))
  4760. (org-load-modules-maybe)
  4761. (when (featurep 'xemacs)
  4762. (easy-menu-add org-org-menu)
  4763. (easy-menu-add org-tbl-menu))
  4764. (org-install-agenda-files-menu)
  4765. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4766. (add-to-invisibility-spec '(org-cwidth))
  4767. (add-to-invisibility-spec '(org-hide-block . t))
  4768. (when (featurep 'xemacs)
  4769. (org-set-local 'line-move-ignore-invisible t))
  4770. (org-set-local 'outline-regexp org-outline-regexp)
  4771. (org-set-local 'outline-level 'org-outline-level)
  4772. (setq bidi-paragraph-direction 'left-to-right)
  4773. (when (and org-ellipsis
  4774. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4775. (fboundp 'make-glyph-code))
  4776. (unless org-display-table
  4777. (setq org-display-table (make-display-table)))
  4778. (set-display-table-slot
  4779. org-display-table 4
  4780. (vconcat (mapcar
  4781. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4782. org-ellipsis)))
  4783. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4784. (setq buffer-display-table org-display-table))
  4785. (org-set-regexps-and-options)
  4786. (org-set-font-lock-defaults)
  4787. (when (and org-tag-faces (not org-tags-special-faces-re))
  4788. ;; tag faces set outside customize.... force initialization.
  4789. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4790. ;; Calc embedded
  4791. (org-set-local 'calc-embedded-open-mode "# ")
  4792. ;; Modify a few syntax entries
  4793. (modify-syntax-entry ?@ "w")
  4794. (modify-syntax-entry ?\" "\"")
  4795. (modify-syntax-entry ?\\ "_")
  4796. (modify-syntax-entry ?~ "_")
  4797. (if org-startup-truncated (setq truncate-lines t))
  4798. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4799. (org-set-local 'font-lock-unfontify-region-function
  4800. 'org-unfontify-region)
  4801. ;; Activate before-change-function
  4802. (org-set-local 'org-table-may-need-update t)
  4803. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4804. 'local)
  4805. ;; Check for running clock before killing a buffer
  4806. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4807. ;; Initialize macros templates.
  4808. (org-macro-initialize-templates)
  4809. ;; Initialize radio targets.
  4810. (org-update-radio-target-regexp)
  4811. ;; Indentation.
  4812. (org-set-local 'indent-line-function 'org-indent-line)
  4813. (org-set-local 'indent-region-function 'org-indent-region)
  4814. ;; Filling and auto-filling.
  4815. (org-setup-filling)
  4816. ;; Comments.
  4817. (org-setup-comments-handling)
  4818. ;; Initialize cache.
  4819. (org-element-cache-reset)
  4820. ;; Beginning/end of defun
  4821. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4822. (org-set-local 'end-of-defun-function
  4823. (lambda ()
  4824. (if (not (org-at-heading-p))
  4825. (org-forward-element)
  4826. (org-forward-element)
  4827. (forward-char -1))))
  4828. ;; Next error for sparse trees
  4829. (org-set-local 'next-error-function 'org-occur-next-match)
  4830. ;; Make sure dependence stuff works reliably, even for users who set it
  4831. ;; too late :-(
  4832. (if org-enforce-todo-dependencies
  4833. (add-hook 'org-blocker-hook
  4834. 'org-block-todo-from-children-or-siblings-or-parent)
  4835. (remove-hook 'org-blocker-hook
  4836. 'org-block-todo-from-children-or-siblings-or-parent))
  4837. (if org-enforce-todo-checkbox-dependencies
  4838. (add-hook 'org-blocker-hook
  4839. 'org-block-todo-from-checkboxes)
  4840. (remove-hook 'org-blocker-hook
  4841. 'org-block-todo-from-checkboxes))
  4842. ;; Align options lines
  4843. (org-set-local
  4844. 'align-mode-rules-list
  4845. '((org-in-buffer-settings
  4846. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4847. (modes . '(org-mode)))))
  4848. ;; Imenu
  4849. (org-set-local 'imenu-create-index-function
  4850. 'org-imenu-get-tree)
  4851. ;; Make isearch reveal context
  4852. (if (or (featurep 'xemacs)
  4853. (not (boundp 'outline-isearch-open-invisible-function)))
  4854. ;; Emacs 21 and XEmacs make use of the hook
  4855. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4856. ;; Emacs 22 deals with this through a special variable
  4857. (org-set-local 'outline-isearch-open-invisible-function
  4858. (lambda (&rest ignore) (org-show-context 'isearch))))
  4859. ;; Setup the pcomplete hooks
  4860. (set (make-local-variable 'pcomplete-command-completion-function)
  4861. 'org-pcomplete-initial)
  4862. (set (make-local-variable 'pcomplete-command-name-function)
  4863. 'org-command-at-point)
  4864. (set (make-local-variable 'pcomplete-default-completion-function)
  4865. 'ignore)
  4866. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4867. 'org-parse-arguments)
  4868. (set (make-local-variable 'pcomplete-termination-string) "")
  4869. (when (>= emacs-major-version 23)
  4870. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4871. ;; If empty file that did not turn on org-mode automatically, make it to.
  4872. (if (and org-insert-mode-line-in-empty-file
  4873. (org-called-interactively-p 'any)
  4874. (= (point-min) (point-max)))
  4875. (insert "# -*- mode: org -*-\n\n"))
  4876. (unless org-inhibit-startup
  4877. (org-unmodified
  4878. (and org-startup-with-beamer-mode (org-beamer-mode))
  4879. (when org-startup-align-all-tables
  4880. (org-table-map-tables 'org-table-align 'quietly))
  4881. (when org-startup-with-inline-images
  4882. (org-display-inline-images))
  4883. (when org-startup-with-latex-preview
  4884. (org-toggle-latex-fragment))
  4885. (unless org-inhibit-startup-visibility-stuff
  4886. (org-set-startup-visibility))
  4887. (org-refresh-effort-properties)))
  4888. ;; Try to set org-hide correctly
  4889. (let ((foreground (org-find-invisible-foreground)))
  4890. (if foreground
  4891. (set-face-foreground 'org-hide foreground))))
  4892. ;; Update `customize-package-emacs-version-alist'
  4893. (add-to-list 'customize-package-emacs-version-alist
  4894. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4895. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4896. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4897. (defvar org-mode-transpose-word-syntax-table
  4898. (let ((st (make-syntax-table text-mode-syntax-table)))
  4899. (mapc (lambda(c) (modify-syntax-entry
  4900. (string-to-char (car c)) "w p" st))
  4901. org-emphasis-alist)
  4902. st))
  4903. (when (fboundp 'abbrev-table-put)
  4904. (abbrev-table-put org-mode-abbrev-table
  4905. :parents (list text-mode-abbrev-table)))
  4906. (defun org-find-invisible-foreground ()
  4907. (let ((candidates (remove
  4908. "unspecified-bg"
  4909. (nconc
  4910. (list (face-background 'default)
  4911. (face-background 'org-default))
  4912. (mapcar
  4913. (lambda (alist)
  4914. (when (boundp alist)
  4915. (cdr (assoc 'background-color (symbol-value alist)))))
  4916. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4917. (list (face-foreground 'org-hide))))))
  4918. (car (remove nil candidates))))
  4919. (defun org-current-time (&optional rounding-minutes past)
  4920. "Current time, possibly rounded to ROUNDING-MINUTES.
  4921. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4922. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4923. the rounding returns a past time."
  4924. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4925. (car org-time-stamp-rounding-minutes)))
  4926. (time (decode-time)) res)
  4927. (if (< r 1)
  4928. (current-time)
  4929. (setq res
  4930. (apply 'encode-time
  4931. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4932. (nthcdr 2 time))))
  4933. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4934. (seconds-to-time (- (org-float-time res) (* r 60)))
  4935. res))))
  4936. (defun org-today ()
  4937. "Return today date, considering `org-extend-today-until'."
  4938. (time-to-days
  4939. (time-subtract (current-time)
  4940. (list 0 (* 3600 org-extend-today-until) 0))))
  4941. ;;;; Font-Lock stuff, including the activators
  4942. (defvar org-mouse-map (make-sparse-keymap))
  4943. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4944. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4945. (when org-mouse-1-follows-link
  4946. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4947. (when org-tab-follows-link
  4948. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4949. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4950. (require 'font-lock)
  4951. (defconst org-non-link-chars "]\t\n\r<>")
  4952. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4953. "file+sys" "news" "shell" "elisp" "doi" "message"
  4954. "help"))
  4955. (defvar org-link-types-re nil
  4956. "Matches a link that has a url-like prefix like \"http:\"")
  4957. (defvar org-link-re-with-space nil
  4958. "Matches a link with spaces, optional angular brackets around it.")
  4959. (defvar org-link-re-with-space2 nil
  4960. "Matches a link with spaces, optional angular brackets around it.")
  4961. (defvar org-link-re-with-space3 nil
  4962. "Matches a link with spaces, only for internal part in bracket links.")
  4963. (defvar org-angle-link-re nil
  4964. "Matches link with angular brackets, spaces are allowed.")
  4965. (defvar org-plain-link-re nil
  4966. "Matches plain link, without spaces.")
  4967. (defvar org-bracket-link-regexp nil
  4968. "Matches a link in double brackets.")
  4969. (defvar org-bracket-link-analytic-regexp nil
  4970. "Regular expression used to analyze links.
  4971. Here is what the match groups contain after a match:
  4972. 1: http:
  4973. 2: http
  4974. 3: path
  4975. 4: [desc]
  4976. 5: desc")
  4977. (defvar org-bracket-link-analytic-regexp++ nil
  4978. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4979. (defvar org-any-link-re nil
  4980. "Regular expression matching any link.")
  4981. (defconst org-match-sexp-depth 3
  4982. "Number of stacked braces for sub/superscript matching.")
  4983. (defun org-create-multibrace-regexp (left right n)
  4984. "Create a regular expression which will match a balanced sexp.
  4985. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4986. as single character strings.
  4987. The regexp returned will match the entire expression including the
  4988. delimiters. It will also define a single group which contains the
  4989. match except for the outermost delimiters. The maximum depth of
  4990. stacked delimiters is N. Escaping delimiters is not possible."
  4991. (let* ((nothing (concat "[^" left right "]*?"))
  4992. (or "\\|")
  4993. (re nothing)
  4994. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4995. (while (> n 1)
  4996. (setq n (1- n)
  4997. re (concat re or next)
  4998. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4999. (concat left "\\(" re "\\)" right)))
  5000. (defconst org-match-substring-regexp
  5001. (concat
  5002. "\\(\\S-\\)\\([_^]\\)\\("
  5003. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  5004. "\\|"
  5005. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  5006. "\\|"
  5007. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  5008. "The regular expression matching a sub- or superscript.")
  5009. (defconst org-match-substring-with-braces-regexp
  5010. (concat
  5011. "\\(\\S-\\)\\([_^]\\)"
  5012. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5013. "The regular expression matching a sub- or superscript, forcing braces.")
  5014. (defun org-make-link-regexps ()
  5015. "Update the link regular expressions.
  5016. This should be called after the variable `org-link-types' has changed."
  5017. (let ((types-re (regexp-opt org-link-types t)))
  5018. (setq org-link-types-re
  5019. (concat "\\`" types-re ":")
  5020. org-link-re-with-space
  5021. (concat "<?" types-re ":"
  5022. "\\([^" org-non-link-chars " ]"
  5023. "[^" org-non-link-chars "]*"
  5024. "[^" org-non-link-chars " ]\\)>?")
  5025. org-link-re-with-space2
  5026. (concat "<?" types-re ":"
  5027. "\\([^" org-non-link-chars " ]"
  5028. "[^\t\n\r]*"
  5029. "[^" org-non-link-chars " ]\\)>?")
  5030. org-link-re-with-space3
  5031. (concat "<?" types-re ":"
  5032. "\\([^" org-non-link-chars " ]"
  5033. "[^\t\n\r]*\\)")
  5034. org-angle-link-re
  5035. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5036. types-re)
  5037. org-plain-link-re
  5038. (concat
  5039. "\\<" types-re ":"
  5040. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5041. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5042. org-bracket-link-regexp
  5043. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5044. org-bracket-link-analytic-regexp
  5045. (concat
  5046. "\\[\\["
  5047. "\\(" types-re ":\\)?"
  5048. "\\([^]]+\\)"
  5049. "\\]"
  5050. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5051. "\\]")
  5052. org-bracket-link-analytic-regexp++
  5053. (concat
  5054. "\\[\\["
  5055. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5056. "\\([^]]+\\)"
  5057. "\\]"
  5058. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5059. "\\]")
  5060. org-any-link-re
  5061. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5062. org-angle-link-re "\\)\\|\\("
  5063. org-plain-link-re "\\)"))))
  5064. (org-make-link-regexps)
  5065. (defvar org-emph-face nil)
  5066. (defun org-do-emphasis-faces (limit)
  5067. "Run through the buffer and emphasize strings."
  5068. (let (rtn a)
  5069. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5070. (let* ((border (char-after (match-beginning 3)))
  5071. (bre (regexp-quote (char-to-string border))))
  5072. (if (and (not (= border (char-after (match-beginning 4))))
  5073. (not (save-match-data
  5074. (string-match (concat bre ".*" bre)
  5075. (replace-regexp-in-string
  5076. "\n" " "
  5077. (substring (match-string 2) 1 -1))))))
  5078. (progn
  5079. (setq rtn t)
  5080. (setq a (assoc (match-string 3) org-emphasis-alist))
  5081. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5082. 'face
  5083. (nth 1 a))
  5084. (and (nth 2 a)
  5085. (org-remove-flyspell-overlays-in
  5086. (match-beginning 0) (match-end 0)))
  5087. (add-text-properties (match-beginning 2) (match-end 2)
  5088. '(font-lock-multiline t org-emphasis t))
  5089. (when org-hide-emphasis-markers
  5090. (add-text-properties (match-end 4) (match-beginning 5)
  5091. '(invisible org-link))
  5092. (add-text-properties (match-beginning 3) (match-end 3)
  5093. '(invisible org-link))))))
  5094. (goto-char (1+ (match-beginning 0))))
  5095. rtn))
  5096. (defun org-emphasize (&optional char)
  5097. "Insert or change an emphasis, i.e. a font like bold or italic.
  5098. If there is an active region, change that region to a new emphasis.
  5099. If there is no region, just insert the marker characters and position
  5100. the cursor between them.
  5101. CHAR should be the marker character. If it is a space, it means to
  5102. remove the emphasis of the selected region.
  5103. If CHAR is not given (for example in an interactive call) it will be
  5104. prompted for."
  5105. (interactive)
  5106. (let ((erc org-emphasis-regexp-components)
  5107. (prompt "")
  5108. (string "") beg end move c s)
  5109. (if (org-region-active-p)
  5110. (setq beg (region-beginning) end (region-end)
  5111. string (buffer-substring beg end))
  5112. (setq move t))
  5113. (unless char
  5114. (message "Emphasis marker or tag: [%s]"
  5115. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5116. (setq char (read-char-exclusive)))
  5117. (if (equal char ?\ )
  5118. (setq s "" move nil)
  5119. (unless (assoc (char-to-string char) org-emphasis-alist)
  5120. (user-error "No such emphasis marker: \"%c\"" char))
  5121. (setq s (char-to-string char)))
  5122. (while (and (> (length string) 1)
  5123. (equal (substring string 0 1) (substring string -1))
  5124. (assoc (substring string 0 1) org-emphasis-alist))
  5125. (setq string (substring string 1 -1)))
  5126. (setq string (concat s string s))
  5127. (if beg (delete-region beg end))
  5128. (unless (or (bolp)
  5129. (string-match (concat "[" (nth 0 erc) "\n]")
  5130. (char-to-string (char-before (point)))))
  5131. (insert " "))
  5132. (unless (or (eobp)
  5133. (string-match (concat "[" (nth 1 erc) "\n]")
  5134. (char-to-string (char-after (point)))))
  5135. (insert " ") (backward-char 1))
  5136. (insert string)
  5137. (and move (backward-char 1))))
  5138. (defconst org-nonsticky-props
  5139. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5140. (defsubst org-rear-nonsticky-at (pos)
  5141. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5142. (defun org-activate-plain-links (limit)
  5143. "Add link properties for plain links."
  5144. (when (and (re-search-forward org-plain-link-re limit t)
  5145. (not (org-in-src-block-p)))
  5146. (let ((face (get-text-property (max (1- (match-beginning 0)) (point-min))
  5147. 'face))
  5148. (link (org-match-string-no-properties 0)))
  5149. (unless (if (consp face) (memq 'org-tag face) (eq 'org-tag face))
  5150. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5151. (add-text-properties (match-beginning 0) (match-end 0)
  5152. (list 'mouse-face 'highlight
  5153. 'face 'org-link
  5154. 'htmlize-link `(:uri ,link)
  5155. 'keymap org-mouse-map))
  5156. (org-rear-nonsticky-at (match-end 0))
  5157. t))))
  5158. (defun org-activate-code (limit)
  5159. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5160. (progn
  5161. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5162. (remove-text-properties (match-beginning 0) (match-end 0)
  5163. '(display t invisible t intangible t))
  5164. t)))
  5165. (defcustom org-src-fontify-natively t
  5166. "When non-nil, fontify code in code blocks."
  5167. :type 'boolean
  5168. :version "24.4"
  5169. :package-version '(Org . "8.3")
  5170. :group 'org-appearance
  5171. :group 'org-babel)
  5172. (defcustom org-allow-promoting-top-level-subtree nil
  5173. "When non-nil, allow promoting a top level subtree.
  5174. The leading star of the top level headline will be replaced
  5175. by a #."
  5176. :type 'boolean
  5177. :version "24.1"
  5178. :group 'org-appearance)
  5179. (defun org-fontify-meta-lines-and-blocks (limit)
  5180. (condition-case nil
  5181. (org-fontify-meta-lines-and-blocks-1 limit)
  5182. (error (message "org-mode fontification error"))))
  5183. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5184. "Fontify #+ lines and blocks."
  5185. (let ((case-fold-search t))
  5186. (if (re-search-forward
  5187. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5188. limit t)
  5189. (let ((beg (match-beginning 0))
  5190. (block-start (match-end 0))
  5191. (block-end nil)
  5192. (lang (match-string 7))
  5193. (beg1 (line-beginning-position 2))
  5194. (dc1 (downcase (match-string 2)))
  5195. (dc3 (downcase (match-string 3)))
  5196. end end1 quoting block-type ovl)
  5197. (cond
  5198. ((and (match-end 4) (equal dc3 "+begin"))
  5199. ;; Truly a block
  5200. (setq block-type (downcase (match-string 5))
  5201. quoting (member block-type org-protecting-blocks))
  5202. (when (re-search-forward
  5203. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5204. nil t) ;; on purpose, we look further than LIMIT
  5205. (setq end (min (point-max) (match-end 0))
  5206. end1 (min (point-max) (1- (match-beginning 0))))
  5207. (setq block-end (match-beginning 0))
  5208. (when quoting
  5209. (org-remove-flyspell-overlays-in beg1 end1)
  5210. (remove-text-properties beg end
  5211. '(display t invisible t intangible t)))
  5212. (add-text-properties
  5213. beg end '(font-lock-fontified t font-lock-multiline t))
  5214. (add-text-properties beg beg1 '(face org-meta-line))
  5215. (org-remove-flyspell-overlays-in beg beg1)
  5216. (add-text-properties ; For end_src
  5217. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5218. (org-remove-flyspell-overlays-in end1 end)
  5219. (cond
  5220. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5221. (org-src-font-lock-fontify-block lang block-start block-end)
  5222. (add-text-properties beg1 block-end '(src-block t)))
  5223. (quoting
  5224. (add-text-properties beg1 (min (point-max) (1+ end1))
  5225. '(face org-block))) ; end of source block
  5226. ((not org-fontify-quote-and-verse-blocks))
  5227. ((string= block-type "quote")
  5228. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5229. ((string= block-type "verse")
  5230. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5231. (add-text-properties beg beg1 '(face org-block-begin-line))
  5232. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5233. '(face org-block-end-line))
  5234. t))
  5235. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5236. (org-remove-flyspell-overlays-in
  5237. (match-beginning 0)
  5238. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5239. (add-text-properties
  5240. beg (match-end 3)
  5241. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5242. '(font-lock-fontified t invisible t)
  5243. '(font-lock-fontified t face org-document-info-keyword)))
  5244. (add-text-properties
  5245. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5246. (if (string-equal dc1 "+title:")
  5247. '(font-lock-fontified t face org-document-title)
  5248. '(font-lock-fontified t face org-document-info))))
  5249. ((equal dc1 "+caption:")
  5250. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5251. (remove-text-properties (match-beginning 0) (match-end 0)
  5252. '(display t invisible t intangible t))
  5253. (add-text-properties (match-beginning 1) (match-end 3)
  5254. '(font-lock-fontified t face org-meta-line))
  5255. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5256. '(font-lock-fontified t face org-block))
  5257. t)
  5258. ((member dc3 '(" " ""))
  5259. (org-remove-flyspell-overlays-in beg (match-end 0))
  5260. (add-text-properties
  5261. beg (match-end 0)
  5262. '(font-lock-fontified t face font-lock-comment-face)))
  5263. (t ;; just any other in-buffer setting, but not indented
  5264. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5265. (remove-text-properties (match-beginning 0) (match-end 0)
  5266. '(display t invisible t intangible t))
  5267. (add-text-properties beg (match-end 0)
  5268. '(font-lock-fontified t face org-meta-line))
  5269. t))))))
  5270. (defun org-fontify-drawers (limit)
  5271. "Fontify drawers."
  5272. (when (re-search-forward org-drawer-regexp limit t)
  5273. (add-text-properties
  5274. (match-beginning 0) (match-end 0)
  5275. '(font-lock-fontified t face org-special-keyword))
  5276. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5277. t))
  5278. (defun org-fontify-macros (limit)
  5279. "Fontify macros."
  5280. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5281. (add-text-properties
  5282. (match-beginning 0) (match-end 0)
  5283. '(font-lock-fontified t face org-macro))
  5284. (when org-hide-macro-markers
  5285. (add-text-properties (match-end 2) (match-beginning 2)
  5286. '(invisible t))
  5287. (add-text-properties (match-beginning 1) (match-end 1)
  5288. '(invisible t)))
  5289. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5290. t))
  5291. (defun org-activate-angle-links (limit)
  5292. "Add text properties for angle links."
  5293. (if (and (re-search-forward org-angle-link-re limit t)
  5294. (not (org-in-src-block-p)))
  5295. (progn
  5296. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5297. (add-text-properties (match-beginning 0) (match-end 0)
  5298. (list 'mouse-face 'highlight
  5299. 'keymap org-mouse-map
  5300. 'font-lock-multiline t))
  5301. (org-rear-nonsticky-at (match-end 0))
  5302. t)))
  5303. (defun org-activate-footnote-links (limit)
  5304. "Add text properties for footnotes."
  5305. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5306. (when fn
  5307. (let* ((beg (nth 1 fn))
  5308. (end (nth 2 fn))
  5309. (label (car fn))
  5310. (referencep (/= (line-beginning-position) beg)))
  5311. (when (and referencep (nth 3 fn))
  5312. (save-excursion
  5313. (goto-char beg)
  5314. (search-forward (or label "fn:"))
  5315. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5316. (add-text-properties beg end
  5317. (list 'mouse-face 'highlight
  5318. 'keymap org-mouse-map
  5319. 'help-echo
  5320. (if referencep "Footnote reference"
  5321. "Footnote definition")
  5322. 'font-lock-fontified t
  5323. 'font-lock-multiline t
  5324. 'face 'org-footnote))))))
  5325. (defun org-activate-bracket-links (limit)
  5326. "Add text properties for bracketed links."
  5327. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5328. (not (org-in-src-block-p)))
  5329. (let* ((hl (org-match-string-no-properties 1))
  5330. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5331. (ip (org-maybe-intangible
  5332. (list 'invisible 'org-link
  5333. 'keymap org-mouse-map 'mouse-face 'highlight
  5334. 'font-lock-multiline t 'help-echo help
  5335. 'htmlize-link `(:uri ,hl))))
  5336. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5337. 'font-lock-multiline t 'help-echo help
  5338. 'htmlize-link `(:uri ,hl))))
  5339. ;; We need to remove the invisible property here. Table narrowing
  5340. ;; may have made some of this invisible.
  5341. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5342. (remove-text-properties (match-beginning 0) (match-end 0)
  5343. '(invisible nil))
  5344. (if (match-end 3)
  5345. (progn
  5346. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5347. (org-rear-nonsticky-at (match-beginning 3))
  5348. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5349. (org-rear-nonsticky-at (match-end 3))
  5350. (add-text-properties (match-end 3) (match-end 0) ip)
  5351. (org-rear-nonsticky-at (match-end 0)))
  5352. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5353. (org-rear-nonsticky-at (match-beginning 1))
  5354. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5355. (org-rear-nonsticky-at (match-end 1))
  5356. (add-text-properties (match-end 1) (match-end 0) ip)
  5357. (org-rear-nonsticky-at (match-end 0)))
  5358. t)))
  5359. (defun org-activate-dates (limit)
  5360. "Add text properties for dates."
  5361. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5362. (not (equal (char-before (match-beginning 0)) 91)))
  5363. (progn
  5364. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5365. (add-text-properties (match-beginning 0) (match-end 0)
  5366. (list 'mouse-face 'highlight
  5367. 'keymap org-mouse-map))
  5368. (org-rear-nonsticky-at (match-end 0))
  5369. (when org-display-custom-times
  5370. (if (match-end 3)
  5371. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5372. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5373. t)))
  5374. (defvar org-target-link-regexp nil
  5375. "Regular expression matching radio targets in plain text.")
  5376. (make-variable-buffer-local 'org-target-link-regexp)
  5377. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5378. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5379. border border border))
  5380. "Regular expression matching a link target.")
  5381. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5382. "Regular expression matching a radio target.")
  5383. (defconst org-any-target-regexp
  5384. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5385. "Regular expression matching any target.")
  5386. (defun org-activate-target-links (limit)
  5387. "Add text properties for target matches."
  5388. (when org-target-link-regexp
  5389. (let ((case-fold-search t))
  5390. (if (re-search-forward org-target-link-regexp limit t)
  5391. (progn
  5392. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5393. (add-text-properties (match-beginning 1) (match-end 1)
  5394. (list 'mouse-face 'highlight
  5395. 'keymap org-mouse-map
  5396. 'help-echo "Radio target link"
  5397. 'org-linked-text t))
  5398. (org-rear-nonsticky-at (match-end 1))
  5399. t)))))
  5400. (defun org-update-radio-target-regexp ()
  5401. "Find all radio targets in this file and update the regular expression.
  5402. Also refresh fontification if needed."
  5403. (interactive)
  5404. (let ((old-regexp org-target-link-regexp)
  5405. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5406. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5407. (targets
  5408. (org-with-wide-buffer
  5409. (goto-char (point-min))
  5410. (let (rtn)
  5411. (while (re-search-forward org-radio-target-regexp nil t)
  5412. ;; Make sure point is really within the object.
  5413. (backward-char)
  5414. (let ((obj (org-element-context)))
  5415. (when (eq (org-element-type obj) 'radio-target)
  5416. (add-to-list 'rtn (org-element-property :value obj)))))
  5417. rtn))))
  5418. (setq org-target-link-regexp
  5419. (and targets
  5420. (concat before-re
  5421. (mapconcat
  5422. (lambda (x)
  5423. (replace-regexp-in-string
  5424. " +" "\\s-+" (regexp-quote x) t t))
  5425. targets
  5426. "\\|")
  5427. after-re)))
  5428. (unless (equal old-regexp org-target-link-regexp)
  5429. ;; Clean-up cache.
  5430. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5431. ((not org-target-link-regexp) old-regexp)
  5432. (t
  5433. (concat before-re
  5434. (mapconcat
  5435. (lambda (re)
  5436. (substring re (length before-re)
  5437. (- (length after-re))))
  5438. (list old-regexp org-target-link-regexp)
  5439. "\\|")
  5440. after-re)))))
  5441. (org-with-wide-buffer
  5442. (goto-char (point-min))
  5443. (while (re-search-forward regexp nil t)
  5444. (org-element-cache-refresh (match-beginning 1)))))
  5445. ;; Re fontify buffer.
  5446. (when (memq 'radio org-highlight-links)
  5447. (org-restart-font-lock)))))
  5448. (defun org-hide-wide-columns (limit)
  5449. (let (s e)
  5450. (setq s (text-property-any (point) (or limit (point-max))
  5451. 'org-cwidth t))
  5452. (when s
  5453. (setq e (next-single-property-change s 'org-cwidth))
  5454. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5455. (goto-char e)
  5456. t)))
  5457. (defvar org-latex-and-related-regexp nil
  5458. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5459. (defun org-compute-latex-and-related-regexp ()
  5460. "Compute regular expression for LaTeX, entities and sub/superscript.
  5461. Result depends on variable `org-highlight-latex-and-related'."
  5462. (org-set-local
  5463. 'org-latex-and-related-regexp
  5464. (let* ((re-sub
  5465. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5466. ((eq org-use-sub-superscripts '{})
  5467. (list org-match-substring-with-braces-regexp))
  5468. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5469. (re-latex
  5470. (when (memq 'latex org-highlight-latex-and-related)
  5471. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5472. (delq nil
  5473. (mapcar (lambda (x)
  5474. (and (member (car x) matchers) (nth 1 x)))
  5475. org-latex-regexps)))))
  5476. (re-entities
  5477. (when (memq 'entities org-highlight-latex-and-related)
  5478. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5479. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5480. (defun org-do-latex-and-related (limit)
  5481. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5482. LIMIT bounds the search for syntax to highlight. Stop at first
  5483. highlighted object, if any. Return t if some highlighting was
  5484. done, nil otherwise."
  5485. (when (org-string-nw-p org-latex-and-related-regexp)
  5486. (catch 'found
  5487. (while (re-search-forward org-latex-and-related-regexp limit t)
  5488. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5489. 'face))
  5490. '(org-code org-verbatim underline))
  5491. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5492. '(?_ ?^))
  5493. 1
  5494. 0)))
  5495. (font-lock-prepend-text-property
  5496. (+ offset (match-beginning 0)) (match-end 0)
  5497. 'face 'org-latex-and-related)
  5498. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5499. '(font-lock-multiline t)))
  5500. (throw 'found t)))
  5501. nil)))
  5502. (defun org-restart-font-lock ()
  5503. "Restart `font-lock-mode', to force refontification."
  5504. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5505. (font-lock-mode -1)
  5506. (font-lock-mode 1)))
  5507. (defun org-activate-tags (limit)
  5508. (when (re-search-forward
  5509. (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") limit t)
  5510. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5511. (add-text-properties (match-beginning 1) (match-end 1)
  5512. (list 'mouse-face 'highlight
  5513. 'keymap org-mouse-map))
  5514. (org-rear-nonsticky-at (match-end 1))
  5515. t))
  5516. (defun org-outline-level ()
  5517. "Compute the outline level of the heading at point.
  5518. If this is called at a normal headline, the level is the number
  5519. of stars. Use `org-reduced-level' to remove the effect of
  5520. `org-odd-levels'. Unlike to `org-current-level', this function
  5521. takes into consideration inlinetasks."
  5522. (org-with-wide-buffer
  5523. (end-of-line)
  5524. (if (re-search-backward org-outline-regexp-bol nil t)
  5525. (1- (- (match-end 0) (match-beginning 0)))
  5526. 0)))
  5527. (defvar org-font-lock-keywords nil)
  5528. (defsubst org-re-property (property &optional literal allow-null value)
  5529. "Return a regexp matching a PROPERTY line.
  5530. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5531. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5532. non-nil, match properties even without a value.
  5533. Match group 3 is set to the value when it exists. If there is no
  5534. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5535. With optional argument VALUE, match only property lines with
  5536. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5537. unless LITERAL is non-nil."
  5538. (concat
  5539. "^\\(?4:[ \t]*\\)"
  5540. (format "\\(?1::\\(?2:%s\\):\\)"
  5541. (if literal property (regexp-quote property)))
  5542. (cond (value
  5543. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5544. (if literal value (regexp-quote value))))
  5545. (allow-null
  5546. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5547. (t
  5548. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5549. (defconst org-property-re
  5550. (org-re-property "\\S-+" 'literal t)
  5551. "Regular expression matching a property line.
  5552. There are four matching groups:
  5553. 1: :PROPKEY: including the leading and trailing colon,
  5554. 2: PROPKEY without the leading and trailing colon,
  5555. 3: PROPVAL without leading or trailing spaces,
  5556. 4: the indentation of the current line,
  5557. 5: trailing whitespace.")
  5558. (defvar org-font-lock-hook nil
  5559. "Functions to be called for special font lock stuff.")
  5560. (defvar org-font-lock-set-keywords-hook nil
  5561. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5562. This is called after `org-font-lock-extra-keywords' is defined, but before
  5563. it is installed to be used by font lock. This can be useful if something
  5564. needs to be inserted at a specific position in the font-lock sequence.")
  5565. (defun org-font-lock-hook (limit)
  5566. "Run `org-font-lock-hook' within LIMIT."
  5567. (run-hook-with-args 'org-font-lock-hook limit))
  5568. (defun org-set-font-lock-defaults ()
  5569. "Set font lock defaults for the current buffer."
  5570. (let* ((em org-fontify-emphasized-text)
  5571. (lk org-highlight-links)
  5572. (org-font-lock-extra-keywords
  5573. (list
  5574. ;; Call the hook
  5575. '(org-font-lock-hook)
  5576. ;; Headlines
  5577. `(,(if org-fontify-whole-heading-line
  5578. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5579. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5580. (1 (org-get-level-face 1))
  5581. (2 (org-get-level-face 2))
  5582. (3 (org-get-level-face 3)))
  5583. ;; Table lines
  5584. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5585. (1 'org-table t))
  5586. ;; Table internals
  5587. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5588. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5589. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5590. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5591. ;; Drawers
  5592. '(org-fontify-drawers)
  5593. ;; Properties
  5594. (list org-property-re
  5595. '(1 'org-special-keyword t)
  5596. '(3 'org-property-value t))
  5597. ;; Links
  5598. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5599. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5600. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5601. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5602. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5603. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5604. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5605. ;; Targets.
  5606. (list org-any-target-regexp '(0 'org-target t))
  5607. ;; Diary sexps.
  5608. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5609. ;; Macro
  5610. '(org-fontify-macros)
  5611. '(org-hide-wide-columns (0 nil append))
  5612. ;; TODO keyword
  5613. (list (format org-heading-keyword-regexp-format
  5614. org-todo-regexp)
  5615. '(2 (org-get-todo-face 2) t))
  5616. ;; DONE
  5617. (if org-fontify-done-headline
  5618. (list (format org-heading-keyword-regexp-format
  5619. (concat
  5620. "\\(?:"
  5621. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5622. "\\)"))
  5623. '(2 'org-headline-done t))
  5624. nil)
  5625. ;; Priorities
  5626. '(org-font-lock-add-priority-faces)
  5627. ;; Tags
  5628. '(org-font-lock-add-tag-faces)
  5629. ;; Tags groups
  5630. (if (and org-group-tags org-tag-groups-alist)
  5631. (list (concat org-outline-regexp-bol ".+\\(:"
  5632. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5633. ":\\).*$")
  5634. '(1 'org-tag-group prepend)))
  5635. ;; Special keywords
  5636. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5637. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5638. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5639. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5640. ;; Emphasis
  5641. (if em
  5642. (if (featurep 'xemacs)
  5643. '(org-do-emphasis-faces (0 nil append))
  5644. '(org-do-emphasis-faces)))
  5645. ;; Checkboxes
  5646. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5647. 1 'org-checkbox prepend)
  5648. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5649. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5650. (0 (org-get-checkbox-statistics-face) t)))
  5651. ;; Description list items
  5652. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5653. 1 'org-list-dt prepend)
  5654. ;; ARCHIVEd headings
  5655. (list (concat
  5656. org-outline-regexp-bol
  5657. "\\(.*:" org-archive-tag ":.*\\)")
  5658. '(1 'org-archived prepend))
  5659. ;; Specials
  5660. '(org-do-latex-and-related)
  5661. '(org-fontify-entities)
  5662. '(org-raise-scripts)
  5663. ;; Code
  5664. '(org-activate-code (1 'org-code t))
  5665. ;; COMMENT
  5666. (list (format
  5667. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5668. org-todo-regexp
  5669. org-comment-string)
  5670. '(9 'org-special-keyword t))
  5671. ;; Blocks and meta lines
  5672. '(org-fontify-meta-lines-and-blocks))))
  5673. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5674. (run-hooks 'org-font-lock-set-keywords-hook)
  5675. ;; Now set the full font-lock-keywords
  5676. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5677. (org-set-local 'font-lock-defaults
  5678. '(org-font-lock-keywords t nil nil backward-paragraph))
  5679. (kill-local-variable 'font-lock-keywords) nil))
  5680. (defun org-toggle-pretty-entities ()
  5681. "Toggle the composition display of entities as UTF8 characters."
  5682. (interactive)
  5683. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5684. (org-restart-font-lock)
  5685. (if org-pretty-entities
  5686. (message "Entities are now displayed as UTF8 characters")
  5687. (save-restriction
  5688. (widen)
  5689. (org-decompose-region (point-min) (point-max))
  5690. (message "Entities are now displayed as plain text"))))
  5691. (defvar org-custom-properties-overlays nil
  5692. "List of overlays used for custom properties.")
  5693. (make-variable-buffer-local 'org-custom-properties-overlays)
  5694. (defun org-toggle-custom-properties-visibility ()
  5695. "Display or hide properties in `org-custom-properties'."
  5696. (interactive)
  5697. (if org-custom-properties-overlays
  5698. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5699. (setq org-custom-properties-overlays nil))
  5700. (when org-custom-properties
  5701. (org-with-wide-buffer
  5702. (goto-char (point-min))
  5703. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5704. (while (re-search-forward regexp nil t)
  5705. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5706. (when (and end (< (point) end))
  5707. ;; Hide first custom property in current drawer.
  5708. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5709. (overlay-put o 'invisible t)
  5710. (overlay-put o 'org-custom-property t)
  5711. (push o org-custom-properties-overlays))
  5712. ;; Hide additional custom properties in the same drawer.
  5713. (while (re-search-forward regexp end t)
  5714. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5715. (overlay-put o 'invisible t)
  5716. (overlay-put o 'org-custom-property t)
  5717. (push o org-custom-properties-overlays)))))
  5718. ;; Each entry is limited to a single property drawer.
  5719. (outline-next-heading)))))))
  5720. (defun org-fontify-entities (limit)
  5721. "Find an entity to fontify."
  5722. (let (ee)
  5723. (when org-pretty-entities
  5724. (catch 'match
  5725. ;; "\_ "-family is left out on purpose. Only the first one,
  5726. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5727. ;; confusing when adding a second white space character.
  5728. (while (re-search-forward
  5729. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5730. limit t)
  5731. (if (and (not (org-at-comment-p))
  5732. (setq ee (org-entity-get (match-string 1)))
  5733. (= (length (nth 6 ee)) 1))
  5734. (let*
  5735. ((end (if (equal (match-string 2) "{}")
  5736. (match-end 2)
  5737. (match-end 1))))
  5738. (add-text-properties
  5739. (match-beginning 0) end
  5740. (list 'font-lock-fontified t))
  5741. (compose-region (match-beginning 0) end
  5742. (nth 6 ee) nil)
  5743. (backward-char 1)
  5744. (throw 'match t))))
  5745. nil))))
  5746. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5747. "Fontify string S like in Org-mode."
  5748. (with-temp-buffer
  5749. (insert s)
  5750. (let ((org-odd-levels-only odd-levels))
  5751. (org-mode)
  5752. (org-font-lock-ensure)
  5753. (buffer-string))))
  5754. (defvar org-m nil)
  5755. (defvar org-l nil)
  5756. (defvar org-f nil)
  5757. (defun org-get-level-face (n)
  5758. "Get the right face for match N in font-lock matching of headlines."
  5759. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5760. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5761. (if org-cycle-level-faces
  5762. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5763. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5764. (cond
  5765. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5766. ((eq n 2) org-f)
  5767. (t (if org-level-color-stars-only nil org-f))))
  5768. (defun org-face-from-face-or-color (context inherit face-or-color)
  5769. "Create a face list that inherits INHERIT, but sets the foreground color.
  5770. When FACE-OR-COLOR is not a string, just return it."
  5771. (if (stringp face-or-color)
  5772. (list :inherit inherit
  5773. (cdr (assoc context org-faces-easy-properties))
  5774. face-or-color)
  5775. face-or-color))
  5776. (defun org-get-todo-face (kwd)
  5777. "Get the right face for a TODO keyword KWD.
  5778. If KWD is a number, get the corresponding match group."
  5779. (if (numberp kwd) (setq kwd (match-string kwd)))
  5780. (or (org-face-from-face-or-color
  5781. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5782. (and (member kwd org-done-keywords) 'org-done)
  5783. 'org-todo))
  5784. (defun org-get-priority-face (priority)
  5785. "Get the right face for PRIORITY.
  5786. PRIORITY is a character."
  5787. (or (org-face-from-face-or-color
  5788. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5789. 'org-priority))
  5790. (defun org-get-tag-face (tag)
  5791. "Get the right face for TAG.
  5792. If TAG is a number, get the corresponding match group."
  5793. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5794. (or (org-face-from-face-or-color
  5795. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5796. 'org-tag)))
  5797. (defun org-font-lock-add-priority-faces (limit)
  5798. "Add the special priority faces."
  5799. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5800. (add-text-properties
  5801. (match-beginning 1) (match-end 1)
  5802. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5803. 'font-lock-fontified t))))
  5804. (defun org-font-lock-add-tag-faces (limit)
  5805. "Add the special tag faces."
  5806. (when (and org-tag-faces org-tags-special-faces-re)
  5807. (while (re-search-forward org-tags-special-faces-re limit t)
  5808. (add-text-properties (match-beginning 1) (match-end 1)
  5809. (list 'face (org-get-tag-face 1)
  5810. 'font-lock-fontified t))
  5811. (backward-char 1))))
  5812. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5813. "Remove fontification and activation overlays from links."
  5814. (font-lock-default-unfontify-region beg end)
  5815. (let* ((buffer-undo-list t)
  5816. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5817. (inhibit-modification-hooks t)
  5818. deactivate-mark buffer-file-name buffer-file-truename)
  5819. (org-decompose-region beg end)
  5820. (remove-text-properties beg end
  5821. '(mouse-face t keymap t org-linked-text t
  5822. invisible t intangible t
  5823. org-emphasis t))
  5824. (org-remove-font-lock-display-properties beg end)))
  5825. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5826. ((raise 0.3) (height 0.7))
  5827. ((raise -0.5))
  5828. ((raise 0.5)))
  5829. "Display properties for showing superscripts and subscripts.")
  5830. (defun org-remove-font-lock-display-properties (beg end)
  5831. "Remove specific display properties that have been added by font lock.
  5832. The will remove the raise properties that are used to show superscripts
  5833. and subscripts."
  5834. (let (next prop)
  5835. (while (< beg end)
  5836. (setq next (next-single-property-change beg 'display nil end)
  5837. prop (get-text-property beg 'display))
  5838. (if (member prop org-script-display)
  5839. (put-text-property beg next 'display nil))
  5840. (setq beg next))))
  5841. (defun org-raise-scripts (limit)
  5842. "Add raise properties to sub/superscripts."
  5843. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5844. (if (re-search-forward
  5845. (if (eq org-use-sub-superscripts t)
  5846. org-match-substring-regexp
  5847. org-match-substring-with-braces-regexp)
  5848. limit t)
  5849. (let* ((pos (point)) table-p comment-p
  5850. (mpos (match-beginning 3))
  5851. (emph-p (get-text-property mpos 'org-emphasis))
  5852. (link-p (get-text-property mpos 'mouse-face))
  5853. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5854. (goto-char (point-at-bol))
  5855. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5856. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5857. (goto-char pos)
  5858. ;; Handle a_b^c
  5859. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5860. (if (or comment-p emph-p link-p keyw-p)
  5861. t
  5862. (put-text-property (match-beginning 3) (match-end 0)
  5863. 'display
  5864. (if (equal (char-after (match-beginning 2)) ?^)
  5865. (nth (if table-p 3 1) org-script-display)
  5866. (nth (if table-p 2 0) org-script-display)))
  5867. (add-text-properties (match-beginning 2) (match-end 2)
  5868. (list 'invisible t
  5869. 'org-dwidth t 'org-dwidth-n 1))
  5870. (if (and (eq (char-after (match-beginning 3)) ?{)
  5871. (eq (char-before (match-end 3)) ?}))
  5872. (progn
  5873. (add-text-properties
  5874. (match-beginning 3) (1+ (match-beginning 3))
  5875. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5876. (add-text-properties
  5877. (1- (match-end 3)) (match-end 3)
  5878. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5879. t)))))
  5880. ;;;; Visibility cycling, including org-goto and indirect buffer
  5881. ;;; Cycling
  5882. (defvar org-cycle-global-status nil)
  5883. (make-variable-buffer-local 'org-cycle-global-status)
  5884. (put 'org-cycle-global-status 'org-state t)
  5885. (defvar org-cycle-subtree-status nil)
  5886. (make-variable-buffer-local 'org-cycle-subtree-status)
  5887. (put 'org-cycle-subtree-status 'org-state t)
  5888. (defvar org-inlinetask-min-level)
  5889. (defun org-unlogged-message (&rest args)
  5890. "Display a message, but avoid logging it in the *Messages* buffer."
  5891. (let ((message-log-max nil))
  5892. (apply 'message args)))
  5893. ;;;###autoload
  5894. (defun org-cycle (&optional arg)
  5895. "TAB-action and visibility cycling for Org-mode.
  5896. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5897. is outline visibility cycling, but it also invokes other actions
  5898. in special contexts.
  5899. - When this function is called with a prefix argument, rotate the entire
  5900. buffer through 3 states (global cycling)
  5901. 1. OVERVIEW: Show only top-level headlines.
  5902. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5903. 3. SHOW ALL: Show everything.
  5904. With a double \\[universal-argument] prefix argument, \
  5905. switch to the startup visibility,
  5906. determined by the variable `org-startup-folded', and by any VISIBILITY
  5907. properties in the buffer.
  5908. With a triple \\[universal-argument] prefix argument, \
  5909. show the entire buffer, including any drawers.
  5910. - When inside a table, re-align the table and move to the next field.
  5911. - When point is at the beginning of a headline, rotate the subtree started
  5912. by this line through 3 different states (local cycling)
  5913. 1. FOLDED: Only the main headline is shown.
  5914. 2. CHILDREN: The main headline and the direct children are shown.
  5915. From this state, you can move to one of the children
  5916. and zoom in further.
  5917. 3. SUBTREE: Show the entire subtree, including body text.
  5918. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5919. - When point is at the beginning of an empty headline and the variable
  5920. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5921. of the headline by demoting and promoting it to likely levels. This
  5922. speeds up creation document structure by pressing TAB once or several
  5923. times right after creating a new headline.
  5924. - When there is a numeric prefix, go up to a heading with level ARG, do
  5925. a `show-subtree' and return to the previous cursor position. If ARG
  5926. is negative, go up that many levels.
  5927. - When point is not at the beginning of a headline, execute the global
  5928. binding for TAB, which is re-indenting the line. See the option
  5929. `org-cycle-emulate-tab' for details.
  5930. - Special case: if point is at the beginning of the buffer and there is
  5931. no headline in line 1, this function will act as if called with prefix arg
  5932. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5933. But only if also the variable `org-cycle-global-at-bob' is t."
  5934. (interactive "P")
  5935. (org-load-modules-maybe)
  5936. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5937. (and org-cycle-level-after-item/entry-creation
  5938. (or (org-cycle-level)
  5939. (org-cycle-item-indentation))))
  5940. (let* ((limit-level
  5941. (or org-cycle-max-level
  5942. (and (boundp 'org-inlinetask-min-level)
  5943. org-inlinetask-min-level
  5944. (1- org-inlinetask-min-level))))
  5945. (nstars (and limit-level
  5946. (if org-odd-levels-only
  5947. (and limit-level (1- (* limit-level 2)))
  5948. limit-level)))
  5949. (org-outline-regexp
  5950. (if (not (derived-mode-p 'org-mode))
  5951. outline-regexp
  5952. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5953. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5954. (not (looking-at org-outline-regexp))))
  5955. (org-cycle-hook
  5956. (if bob-special
  5957. (delq 'org-optimize-window-after-visibility-change
  5958. (copy-sequence org-cycle-hook))
  5959. org-cycle-hook))
  5960. (pos (point)))
  5961. (if (or bob-special (equal arg '(4)))
  5962. ;; special case: use global cycling
  5963. (setq arg t))
  5964. (cond
  5965. ((equal arg '(16))
  5966. (setq last-command 'dummy)
  5967. (org-set-startup-visibility)
  5968. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5969. ((equal arg '(64))
  5970. (outline-show-all)
  5971. (org-unlogged-message "Entire buffer visible, including drawers"))
  5972. ;; Try cdlatex TAB completion
  5973. ((org-try-cdlatex-tab))
  5974. ;; Table: enter it or move to the next field.
  5975. ((org-at-table-p 'any)
  5976. (if (org-at-table.el-p)
  5977. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5978. Use \\[org-edit-special] to edit table.el tables"))
  5979. (if arg (org-table-edit-field t)
  5980. (org-table-justify-field-maybe)
  5981. (call-interactively 'org-table-next-field))))
  5982. ((run-hook-with-args-until-success
  5983. 'org-tab-after-check-for-table-hook))
  5984. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5985. ((eq arg t) (org-cycle-internal-global))
  5986. ;; Drawers: delegate to `org-flag-drawer'.
  5987. ((save-excursion
  5988. (beginning-of-line 1)
  5989. (looking-at org-drawer-regexp))
  5990. (org-flag-drawer ; toggle block visibility
  5991. (not (get-char-property (match-end 0) 'invisible))))
  5992. ;; Show-subtree, ARG levels up from here.
  5993. ((integerp arg)
  5994. (save-excursion
  5995. (org-back-to-heading)
  5996. (outline-up-heading (if (< arg 0) (- arg)
  5997. (- (funcall outline-level) arg)))
  5998. (org-show-subtree)))
  5999. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  6000. ((and (featurep 'org-inlinetask)
  6001. (org-inlinetask-at-task-p)
  6002. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6003. (org-inlinetask-toggle-visibility))
  6004. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  6005. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  6006. (save-excursion (move-beginning-of-line 1)
  6007. (looking-at org-outline-regexp)))
  6008. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6009. (org-cycle-internal-local))
  6010. ;; From there: TAB emulation and template completion.
  6011. (buffer-read-only (org-back-to-heading))
  6012. ((run-hook-with-args-until-success
  6013. 'org-tab-after-check-for-cycling-hook))
  6014. ((org-try-structure-completion))
  6015. ((run-hook-with-args-until-success
  6016. 'org-tab-before-tab-emulation-hook))
  6017. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6018. (or (not (bolp))
  6019. (not (looking-at org-outline-regexp))))
  6020. (call-interactively (global-key-binding "\t")))
  6021. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6022. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6023. (or (and (eq org-cycle-emulate-tab 'white)
  6024. (= (match-end 0) (point-at-eol)))
  6025. (and (eq org-cycle-emulate-tab 'whitestart)
  6026. (>= (match-end 0) pos))))
  6027. t
  6028. (eq org-cycle-emulate-tab t))
  6029. (call-interactively (global-key-binding "\t")))
  6030. (t (save-excursion
  6031. (org-back-to-heading)
  6032. (org-cycle)))))))
  6033. (defun org-cycle-internal-global ()
  6034. "Do the global cycling action."
  6035. ;; Hack to avoid display of messages for .org attachments in Gnus
  6036. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6037. (cond
  6038. ((and (eq last-command this-command)
  6039. (eq org-cycle-global-status 'overview))
  6040. ;; We just created the overview - now do table of contents
  6041. ;; This can be slow in very large buffers, so indicate action
  6042. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6043. (unless ga (org-unlogged-message "CONTENTS..."))
  6044. (org-content)
  6045. (unless ga (org-unlogged-message "CONTENTS...done"))
  6046. (setq org-cycle-global-status 'contents)
  6047. (run-hook-with-args 'org-cycle-hook 'contents))
  6048. ((and (eq last-command this-command)
  6049. (eq org-cycle-global-status 'contents))
  6050. ;; We just showed the table of contents - now show everything
  6051. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6052. (outline-show-all)
  6053. (unless ga (org-unlogged-message "SHOW ALL"))
  6054. (setq org-cycle-global-status 'all)
  6055. (run-hook-with-args 'org-cycle-hook 'all))
  6056. (t
  6057. ;; Default action: go to overview
  6058. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6059. (org-overview)
  6060. (unless ga (org-unlogged-message "OVERVIEW"))
  6061. (setq org-cycle-global-status 'overview)
  6062. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6063. (defvar org-called-with-limited-levels nil
  6064. "Non-nil when `org-with-limited-levels' is currently active.")
  6065. (defun org-cycle-internal-local ()
  6066. "Do the local cycling action."
  6067. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6068. ;; First, determine end of headline (EOH), end of subtree or item
  6069. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6070. (save-excursion
  6071. (if (org-at-item-p)
  6072. (progn
  6073. (beginning-of-line)
  6074. (setq struct (org-list-struct))
  6075. (setq eoh (point-at-eol))
  6076. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6077. (setq has-children (org-list-has-child-p (point) struct)))
  6078. (org-back-to-heading)
  6079. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6080. (setq eos (save-excursion (org-end-of-subtree t t)
  6081. (when (bolp) (backward-char)) (point)))
  6082. (setq has-children
  6083. (or (save-excursion
  6084. (let ((level (funcall outline-level)))
  6085. (outline-next-heading)
  6086. (and (org-at-heading-p t)
  6087. (> (funcall outline-level) level))))
  6088. (save-excursion
  6089. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6090. ;; Determine end invisible part of buffer (EOL)
  6091. (beginning-of-line 2)
  6092. ;; XEmacs doesn't have `next-single-char-property-change'
  6093. (if (featurep 'xemacs)
  6094. (while (and (not (eobp)) ;; this is like `next-line'
  6095. (get-char-property (1- (point)) 'invisible))
  6096. (beginning-of-line 2))
  6097. (while (and (not (eobp)) ;; this is like `next-line'
  6098. (get-char-property (1- (point)) 'invisible))
  6099. (goto-char (next-single-char-property-change (point) 'invisible))
  6100. (and (eolp) (beginning-of-line 2))))
  6101. (setq eol (point)))
  6102. ;; Find out what to do next and set `this-command'
  6103. (cond
  6104. ((= eos eoh)
  6105. ;; Nothing is hidden behind this heading
  6106. (unless (org-before-first-heading-p)
  6107. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6108. (org-unlogged-message "EMPTY ENTRY")
  6109. (setq org-cycle-subtree-status nil)
  6110. (save-excursion
  6111. (goto-char eos)
  6112. (outline-next-heading)
  6113. (if (outline-invisible-p) (org-flag-heading nil))))
  6114. ((and (or (>= eol eos)
  6115. (not (string-match "\\S-" (buffer-substring eol eos))))
  6116. (or has-children
  6117. (not (setq children-skipped
  6118. org-cycle-skip-children-state-if-no-children))))
  6119. ;; Entire subtree is hidden in one line: children view
  6120. (unless (org-before-first-heading-p)
  6121. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6122. (if (org-at-item-p)
  6123. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6124. (org-show-entry)
  6125. (org-with-limited-levels (outline-show-children))
  6126. ;; FIXME: This slows down the func way too much.
  6127. ;; How keep drawers hidden in subtree anyway?
  6128. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6129. ;; (org-cycle-hide-drawers 'subtree))
  6130. ;; Fold every list in subtree to top-level items.
  6131. (when (eq org-cycle-include-plain-lists 'integrate)
  6132. (save-excursion
  6133. (org-back-to-heading)
  6134. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6135. (beginning-of-line 1)
  6136. (let* ((struct (org-list-struct))
  6137. (prevs (org-list-prevs-alist struct))
  6138. (end (org-list-get-bottom-point struct)))
  6139. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6140. (org-list-get-all-items (point) struct prevs))
  6141. (goto-char (if (< end eos) end eos)))))))
  6142. (org-unlogged-message "CHILDREN")
  6143. (save-excursion
  6144. (goto-char eos)
  6145. (outline-next-heading)
  6146. (if (outline-invisible-p) (org-flag-heading nil)))
  6147. (setq org-cycle-subtree-status 'children)
  6148. (unless (org-before-first-heading-p)
  6149. (run-hook-with-args 'org-cycle-hook 'children)))
  6150. ((or children-skipped
  6151. (and (eq last-command this-command)
  6152. (eq org-cycle-subtree-status 'children)))
  6153. ;; We just showed the children, or no children are there,
  6154. ;; now show everything.
  6155. (unless (org-before-first-heading-p)
  6156. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6157. (outline-flag-region eoh eos nil)
  6158. (org-unlogged-message
  6159. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6160. (setq org-cycle-subtree-status 'subtree)
  6161. (unless (org-before-first-heading-p)
  6162. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6163. (t
  6164. ;; Default action: hide the subtree.
  6165. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6166. (outline-flag-region eoh eos t)
  6167. (org-unlogged-message "FOLDED")
  6168. (setq org-cycle-subtree-status 'folded)
  6169. (unless (org-before-first-heading-p)
  6170. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6171. ;;;###autoload
  6172. (defun org-global-cycle (&optional arg)
  6173. "Cycle the global visibility. For details see `org-cycle'.
  6174. With \\[universal-argument] prefix arg, switch to startup visibility.
  6175. With a numeric prefix, show all headlines up to that level."
  6176. (interactive "P")
  6177. (let ((org-cycle-include-plain-lists
  6178. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6179. (cond
  6180. ((integerp arg)
  6181. (outline-show-all)
  6182. (outline-hide-sublevels arg)
  6183. (setq org-cycle-global-status 'contents))
  6184. ((equal arg '(4))
  6185. (org-set-startup-visibility)
  6186. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6187. (t
  6188. (org-cycle '(4))))))
  6189. (defun org-set-startup-visibility ()
  6190. "Set the visibility required by startup options and properties."
  6191. (cond
  6192. ((eq org-startup-folded t)
  6193. (org-overview))
  6194. ((eq org-startup-folded 'content)
  6195. (org-content))
  6196. ((or (eq org-startup-folded 'showeverything)
  6197. (eq org-startup-folded nil))
  6198. (outline-show-all)))
  6199. (unless (eq org-startup-folded 'showeverything)
  6200. (if org-hide-block-startup (org-hide-block-all))
  6201. (org-set-visibility-according-to-property 'no-cleanup)
  6202. (org-cycle-hide-archived-subtrees 'all)
  6203. (org-cycle-hide-drawers 'all)
  6204. (org-cycle-show-empty-lines t)))
  6205. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6206. "Switch subtree visibilities according to :VISIBILITY: property."
  6207. (interactive)
  6208. (let (org-show-entry-below)
  6209. (org-with-wide-buffer
  6210. (goto-char (point-min))
  6211. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6212. (if (not (org-at-property-p)) (outline-next-heading)
  6213. (let ((state (match-string 3)))
  6214. (save-excursion
  6215. (org-back-to-heading t)
  6216. (outline-hide-subtree)
  6217. (org-reveal)
  6218. (cond
  6219. ((equal state "folded")
  6220. (outline-hide-subtree))
  6221. ((equal state "children")
  6222. (org-show-hidden-entry)
  6223. (outline-show-children))
  6224. ((equal state "content")
  6225. (save-excursion
  6226. (save-restriction
  6227. (org-narrow-to-subtree)
  6228. (org-content))))
  6229. ((member state '("all" "showall"))
  6230. (outline-show-subtree)))))))
  6231. (unless no-cleanup
  6232. (org-cycle-hide-archived-subtrees 'all)
  6233. (org-cycle-hide-drawers 'all)
  6234. (org-cycle-show-empty-lines 'all)))))
  6235. ;; This function uses outline-regexp instead of the more fundamental
  6236. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6237. ;; buffers, where outline-regexp is needed.
  6238. (defun org-overview ()
  6239. "Switch to overview mode, showing only top-level headlines.
  6240. This shows all headlines with a level equal or greater than the level
  6241. of the first headline in the buffer. This is important, because if the
  6242. first headline is not level one, then (hide-sublevels 1) gives confusing
  6243. results."
  6244. (interactive)
  6245. (save-excursion
  6246. (let ((level
  6247. (save-excursion
  6248. (goto-char (point-min))
  6249. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6250. (progn
  6251. (goto-char (match-beginning 0))
  6252. (funcall outline-level))))))
  6253. (and level (outline-hide-sublevels level)))))
  6254. (defun org-content (&optional arg)
  6255. "Show all headlines in the buffer, like a table of contents.
  6256. With numerical argument N, show content up to level N."
  6257. (interactive "P")
  6258. (org-overview)
  6259. (save-excursion
  6260. ;; Visit all headings and show their offspring
  6261. (and (integerp arg) (org-overview))
  6262. (goto-char (point-max))
  6263. (catch 'exit
  6264. (while (and (progn (condition-case nil
  6265. (outline-previous-visible-heading 1)
  6266. (error (goto-char (point-min))))
  6267. t)
  6268. (looking-at org-outline-regexp))
  6269. (if (integerp arg)
  6270. (outline-show-children (1- arg))
  6271. (outline-show-branches))
  6272. (if (bobp) (throw 'exit nil))))))
  6273. (defun org-optimize-window-after-visibility-change (state)
  6274. "Adjust the window after a change in outline visibility.
  6275. This function is the default value of the hook `org-cycle-hook'."
  6276. (when (get-buffer-window (current-buffer))
  6277. (cond
  6278. ((eq state 'content) nil)
  6279. ((eq state 'all) nil)
  6280. ((eq state 'folded) nil)
  6281. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6282. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6283. (defun org-remove-empty-overlays-at (pos)
  6284. "Remove outline overlays that do not contain non-white stuff."
  6285. (mapc
  6286. (lambda (o)
  6287. (and (eq 'outline (overlay-get o 'invisible))
  6288. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6289. (overlay-end o))))
  6290. (delete-overlay o)))
  6291. (overlays-at pos)))
  6292. (defun org-clean-visibility-after-subtree-move ()
  6293. "Fix visibility issues after moving a subtree."
  6294. ;; First, find a reasonable region to look at:
  6295. ;; Start two siblings above, end three below
  6296. (let* ((beg (save-excursion
  6297. (and (org-get-last-sibling)
  6298. (org-get-last-sibling))
  6299. (point)))
  6300. (end (save-excursion
  6301. (and (org-get-next-sibling)
  6302. (org-get-next-sibling)
  6303. (org-get-next-sibling))
  6304. (if (org-at-heading-p)
  6305. (point-at-eol)
  6306. (point))))
  6307. (level (looking-at "\\*+"))
  6308. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6309. (save-excursion
  6310. (save-restriction
  6311. (narrow-to-region beg end)
  6312. (when re
  6313. ;; Properly fold already folded siblings
  6314. (goto-char (point-min))
  6315. (while (re-search-forward re nil t)
  6316. (if (and (not (outline-invisible-p))
  6317. (save-excursion
  6318. (goto-char (point-at-eol)) (outline-invisible-p)))
  6319. (outline-hide-entry))))
  6320. (org-cycle-show-empty-lines 'overview)
  6321. (org-cycle-hide-drawers 'overview)))))
  6322. (defun org-cycle-show-empty-lines (state)
  6323. "Show empty lines above all visible headlines.
  6324. The region to be covered depends on STATE when called through
  6325. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6326. entire buffer covered. Note that an empty line is only shown if there
  6327. are at least `org-cycle-separator-lines' empty lines before the headline."
  6328. (when (/= org-cycle-separator-lines 0)
  6329. (save-excursion
  6330. (let* ((n (abs org-cycle-separator-lines))
  6331. (re (cond
  6332. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6333. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6334. (t (let ((ns (number-to-string (- n 2))))
  6335. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6336. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6337. beg end)
  6338. (cond
  6339. ((memq state '(overview contents t))
  6340. (setq beg (point-min) end (point-max)))
  6341. ((memq state '(children folded))
  6342. (setq beg (point)
  6343. end (progn (org-end-of-subtree t t)
  6344. (line-beginning-position 2)))))
  6345. (when beg
  6346. (goto-char beg)
  6347. (while (re-search-forward re end t)
  6348. (unless (get-char-property (match-end 1) 'invisible)
  6349. (let ((e (match-end 1))
  6350. (b (if (>= org-cycle-separator-lines 0)
  6351. (match-beginning 1)
  6352. (save-excursion
  6353. (goto-char (match-beginning 0))
  6354. (skip-chars-backward " \t\n")
  6355. (line-end-position)))))
  6356. (outline-flag-region b e nil))))))))
  6357. ;; Never hide empty lines at the end of the file.
  6358. (save-excursion
  6359. (goto-char (point-max))
  6360. (outline-previous-heading)
  6361. (outline-end-of-heading)
  6362. (if (and (looking-at "[ \t\n]+")
  6363. (= (match-end 0) (point-max)))
  6364. (outline-flag-region (point) (match-end 0) nil))))
  6365. (defun org-show-empty-lines-in-parent ()
  6366. "Move to the parent and re-show empty lines before visible headlines."
  6367. (save-excursion
  6368. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6369. (org-cycle-show-empty-lines context))))
  6370. (defun org-files-list ()
  6371. "Return `org-agenda-files' list, plus all open org-mode files.
  6372. This is useful for operations that need to scan all of a user's
  6373. open and agenda-wise Org files."
  6374. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6375. (dolist (buf (buffer-list))
  6376. (with-current-buffer buf
  6377. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6378. (let ((file (expand-file-name (buffer-file-name))))
  6379. (unless (member file files)
  6380. (push file files))))))
  6381. files))
  6382. (defsubst org-entry-beginning-position ()
  6383. "Return the beginning position of the current entry."
  6384. (save-excursion (org-back-to-heading t) (point)))
  6385. (defsubst org-entry-end-position ()
  6386. "Return the end position of the current entry."
  6387. (save-excursion (outline-next-heading) (point)))
  6388. (defun org-cycle-hide-drawers (state &optional exceptions)
  6389. "Re-hide all drawers after a visibility state change.
  6390. When non-nil, optional argument EXCEPTIONS is a list of strings
  6391. specifying which drawers should not be hidden."
  6392. (when (and (derived-mode-p 'org-mode)
  6393. (not (memq state '(overview folded contents))))
  6394. (save-excursion
  6395. (let* ((globalp (memq state '(contents all)))
  6396. (beg (if globalp (point-min) (point)))
  6397. (end (if globalp (point-max)
  6398. (if (eq state 'children)
  6399. (save-excursion (outline-next-heading) (point))
  6400. (org-end-of-subtree t)))))
  6401. (goto-char beg)
  6402. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6403. (unless (member-ignore-case (match-string 1) exceptions)
  6404. (let ((drawer (org-element-at-point)))
  6405. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6406. (org-flag-drawer t drawer)
  6407. ;; Make sure to skip drawer entirely or we might flag
  6408. ;; it another time when matching its ending line with
  6409. ;; `org-drawer-regexp'.
  6410. (goto-char (org-element-property :end drawer))))))))))
  6411. (defun org-flag-drawer (flag &optional element)
  6412. "When FLAG is non-nil, hide the drawer we are at.
  6413. Otherwise make it visible. When optional argument ELEMENT is
  6414. a parsed drawer, as returned by `org-element-at-point', hide or
  6415. show that drawer instead."
  6416. (when (save-excursion
  6417. (beginning-of-line)
  6418. (org-looking-at-p org-drawer-regexp))
  6419. (let ((drawer (or element (org-element-at-point))))
  6420. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6421. (let ((post (org-element-property :post-affiliated drawer)))
  6422. (save-excursion
  6423. (outline-flag-region
  6424. (progn (goto-char post) (line-end-position))
  6425. (progn (goto-char (org-element-property :end drawer))
  6426. (skip-chars-backward " \r\t\n")
  6427. (line-end-position))
  6428. flag))
  6429. ;; When the drawer is hidden away, make sure point lies in
  6430. ;; a visible part of the buffer.
  6431. (when (and flag (> (line-beginning-position) post))
  6432. (goto-char post)))))))
  6433. (defun org-subtree-end-visible-p ()
  6434. "Is the end of the current subtree visible?"
  6435. (pos-visible-in-window-p
  6436. (save-excursion (org-end-of-subtree t) (point))))
  6437. (defun org-first-headline-recenter ()
  6438. "Move cursor to the first headline and recenter the headline."
  6439. (goto-char (point-min))
  6440. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6441. (set-window-start (selected-window) (point-at-bol))))
  6442. ;;; Saving and restoring visibility
  6443. (defun org-outline-overlay-data (&optional use-markers)
  6444. "Return a list of the locations of all outline overlays.
  6445. These are overlays with the `invisible' property value `outline'.
  6446. The return value is a list of cons cells, with start and stop
  6447. positions for each overlay.
  6448. If USE-MARKERS is set, return the positions as markers."
  6449. (let (beg end)
  6450. (save-excursion
  6451. (save-restriction
  6452. (widen)
  6453. (delq nil
  6454. (mapcar (lambda (o)
  6455. (when (eq (overlay-get o 'invisible) 'outline)
  6456. (setq beg (overlay-start o)
  6457. end (overlay-end o))
  6458. (and beg end (> end beg)
  6459. (if use-markers
  6460. (cons (copy-marker beg)
  6461. (copy-marker end t))
  6462. (cons beg end)))))
  6463. (overlays-in (point-min) (point-max))))))))
  6464. (defun org-set-outline-overlay-data (data)
  6465. "Create visibility overlays for all positions in DATA.
  6466. DATA should have been made by `org-outline-overlay-data'."
  6467. (let (o)
  6468. (save-excursion
  6469. (save-restriction
  6470. (widen)
  6471. (outline-show-all)
  6472. (mapc (lambda (c)
  6473. (outline-flag-region (car c) (cdr c) t))
  6474. data)))))
  6475. ;;; Folding of blocks
  6476. (defvar org-hide-block-overlays nil
  6477. "Overlays hiding blocks.")
  6478. (make-variable-buffer-local 'org-hide-block-overlays)
  6479. (defun org-block-map (function &optional start end)
  6480. "Call FUNCTION at the head of all source blocks in the current buffer.
  6481. Optional arguments START and END can be used to limit the range."
  6482. (let ((start (or start (point-min)))
  6483. (end (or end (point-max))))
  6484. (save-excursion
  6485. (goto-char start)
  6486. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6487. (save-excursion
  6488. (save-match-data
  6489. (goto-char (match-beginning 0))
  6490. (funcall function)))))))
  6491. (defun org-hide-block-toggle-all ()
  6492. "Toggle the visibility of all blocks in the current buffer."
  6493. (org-block-map 'org-hide-block-toggle))
  6494. (defun org-hide-block-all ()
  6495. "Fold all blocks in the current buffer."
  6496. (interactive)
  6497. (org-show-block-all)
  6498. (org-block-map 'org-hide-block-toggle-maybe))
  6499. (defun org-show-block-all ()
  6500. "Unfold all blocks in the current buffer."
  6501. (interactive)
  6502. (mapc 'delete-overlay org-hide-block-overlays)
  6503. (setq org-hide-block-overlays nil))
  6504. (defun org-hide-block-toggle-maybe ()
  6505. "Toggle visibility of block at point.
  6506. Unlike to `org-hide-block-toggle', this function does not throw
  6507. an error. Return a non-nil value when toggling is successful."
  6508. (interactive)
  6509. (ignore-errors (org-hide-block-toggle)))
  6510. (defun org-hide-block-toggle (&optional force)
  6511. "Toggle the visibility of the current block.
  6512. When optional argument FORCE is `off', make block visible. If it
  6513. is non-nil, hide it unconditionally. Throw an error when not at
  6514. a block. Return a non-nil value when toggling is successful."
  6515. (interactive)
  6516. (let ((element (org-element-at-point)))
  6517. (unless (memq (org-element-type element)
  6518. '(center-block comment-block dynamic-block example-block
  6519. export-block quote-block special-block
  6520. src-block verse-block))
  6521. (user-error "Not at a block"))
  6522. (let* ((start (save-excursion
  6523. (goto-char (org-element-property :post-affiliated element))
  6524. (line-end-position)))
  6525. (end (save-excursion
  6526. (goto-char (org-element-property :end element))
  6527. (skip-chars-backward " \r\t\n")
  6528. (line-end-position)))
  6529. (overlays (overlays-at start)))
  6530. (cond
  6531. ;; Do nothing when not before or at the block opening line or
  6532. ;; at the block closing line.
  6533. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6534. ((and (not (eq force 'off))
  6535. (not (memq t (mapcar
  6536. (lambda (o)
  6537. (eq (overlay-get o 'invisible) 'org-hide-block))
  6538. overlays))))
  6539. (let ((ov (make-overlay start end)))
  6540. (overlay-put ov 'invisible 'org-hide-block)
  6541. ;; Make the block accessible to `isearch'.
  6542. (overlay-put
  6543. ov 'isearch-open-invisible
  6544. (lambda (ov)
  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. (push ov org-hide-block-overlays)
  6550. ;; When the block is hidden away, make sure point is left in
  6551. ;; a visible part of the buffer.
  6552. (when (> (line-beginning-position) start)
  6553. (goto-char start)
  6554. (beginning-of-line))
  6555. ;; Signal successful toggling.
  6556. t))
  6557. ((or (not force) (eq force 'off))
  6558. (dolist (ov overlays t)
  6559. (when (memq ov org-hide-block-overlays)
  6560. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6561. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6562. (delete-overlay ov))))))))
  6563. ;; org-tab-after-check-for-cycling-hook
  6564. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6565. ;; Remove overlays when changing major mode
  6566. (add-hook 'org-mode-hook
  6567. (lambda () (org-add-hook 'change-major-mode-hook
  6568. 'org-show-block-all 'append 'local)))
  6569. ;;; Org-goto
  6570. (defvar org-goto-window-configuration nil)
  6571. (defvar org-goto-marker nil)
  6572. (defvar org-goto-map)
  6573. (defun org-goto-map ()
  6574. "Set the keymap `org-goto'."
  6575. (setq org-goto-map
  6576. (let ((map (make-sparse-keymap)))
  6577. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6578. mouse-drag-region universal-argument org-occur)))
  6579. (dolist (cmd cmds)
  6580. (substitute-key-definition cmd cmd map global-map)))
  6581. (suppress-keymap map)
  6582. (org-defkey map "\C-m" 'org-goto-ret)
  6583. (org-defkey map [(return)] 'org-goto-ret)
  6584. (org-defkey map [(left)] 'org-goto-left)
  6585. (org-defkey map [(right)] 'org-goto-right)
  6586. (org-defkey map [(control ?g)] 'org-goto-quit)
  6587. (org-defkey map "\C-i" 'org-cycle)
  6588. (org-defkey map [(tab)] 'org-cycle)
  6589. (org-defkey map [(down)] 'outline-next-visible-heading)
  6590. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6591. (if org-goto-auto-isearch
  6592. (if (fboundp 'define-key-after)
  6593. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6594. nil)
  6595. (org-defkey map "q" 'org-goto-quit)
  6596. (org-defkey map "n" 'outline-next-visible-heading)
  6597. (org-defkey map "p" 'outline-previous-visible-heading)
  6598. (org-defkey map "f" 'outline-forward-same-level)
  6599. (org-defkey map "b" 'outline-backward-same-level)
  6600. (org-defkey map "u" 'outline-up-heading))
  6601. (org-defkey map "/" 'org-occur)
  6602. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6603. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6604. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6605. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6606. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6607. map)))
  6608. (defconst org-goto-help
  6609. "Browse buffer copy, to find location or copy text.%s
  6610. RET=jump to location C-g=quit and return to previous location
  6611. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6612. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6613. (defun org-goto (&optional alternative-interface)
  6614. "Look up a different location in the current file, keeping current visibility.
  6615. When you want look-up or go to a different location in a
  6616. document, the fastest way is often to fold the entire buffer and
  6617. then dive into the tree. This method has the disadvantage, that
  6618. the previous location will be folded, which may not be what you
  6619. want.
  6620. This command works around this by showing a copy of the current
  6621. buffer in an indirect buffer, in overview mode. You can dive
  6622. into the tree in that copy, use org-occur and incremental search
  6623. to find a location. When pressing RET or `Q', the command
  6624. returns to the original buffer in which the visibility is still
  6625. unchanged. After RET it will also jump to the location selected
  6626. in the indirect buffer and expose the headline hierarchy above.
  6627. With a prefix argument, use the alternative interface: e.g., if
  6628. `org-goto-interface' is `outline' use `outline-path-completion'."
  6629. (interactive "P")
  6630. (org-goto-map)
  6631. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6632. (org-refile-use-outline-path t)
  6633. (org-refile-target-verify-function nil)
  6634. (interface
  6635. (if (not alternative-interface)
  6636. org-goto-interface
  6637. (if (eq org-goto-interface 'outline)
  6638. 'outline-path-completion
  6639. 'outline)))
  6640. (org-goto-start-pos (point))
  6641. (selected-point
  6642. (if (eq interface 'outline)
  6643. (car (org-get-location (current-buffer) org-goto-help))
  6644. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6645. (org-refile-check-position pa)
  6646. (nth 3 pa)))))
  6647. (if selected-point
  6648. (progn
  6649. (org-mark-ring-push org-goto-start-pos)
  6650. (goto-char selected-point)
  6651. (if (or (outline-invisible-p) (org-invisible-p2))
  6652. (org-show-context 'org-goto)))
  6653. (message "Quit"))))
  6654. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6655. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6656. (defvar org-goto-local-auto-isearch-map) ; defined below
  6657. (defun org-get-location (buf help)
  6658. "Let the user select a location in the Org-mode buffer BUF.
  6659. This function uses a recursive edit. It returns the selected position
  6660. or nil."
  6661. (org-no-popups
  6662. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6663. (isearch-hide-immediately nil)
  6664. (isearch-search-fun-function
  6665. (lambda () 'org-goto-local-search-headings))
  6666. (org-goto-selected-point org-goto-exit-command))
  6667. (save-excursion
  6668. (save-window-excursion
  6669. (delete-other-windows)
  6670. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6671. (org-pop-to-buffer-same-window
  6672. (condition-case nil
  6673. (make-indirect-buffer (current-buffer) "*org-goto*")
  6674. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6675. (with-output-to-temp-buffer "*Org Help*"
  6676. (princ (format help (if org-goto-auto-isearch
  6677. " Just type for auto-isearch."
  6678. " n/p/f/b/u to navigate, q to quit."))))
  6679. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6680. (setq buffer-read-only nil)
  6681. (let ((org-startup-truncated t)
  6682. (org-startup-folded nil)
  6683. (org-startup-align-all-tables nil))
  6684. (org-mode)
  6685. (org-overview))
  6686. (setq buffer-read-only t)
  6687. (if (and (boundp 'org-goto-start-pos)
  6688. (integer-or-marker-p org-goto-start-pos))
  6689. (progn (goto-char org-goto-start-pos)
  6690. (when (outline-invisible-p)
  6691. (org-show-set-visibility 'lineage)))
  6692. (goto-char (point-min)))
  6693. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6694. (message "Select location and press RET")
  6695. (use-local-map org-goto-map)
  6696. (recursive-edit)))
  6697. (kill-buffer "*org-goto*")
  6698. (cons org-goto-selected-point org-goto-exit-command))))
  6699. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6700. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6701. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6702. (if (fboundp 'isearch-other-control-char)
  6703. (progn
  6704. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6705. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6706. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6707. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6708. (define-key org-goto-local-auto-isearch-map [return] nil))
  6709. (defun org-goto-local-search-headings (string bound noerror)
  6710. "Search and make sure that any matches are in headlines."
  6711. (catch 'return
  6712. (while (if isearch-forward
  6713. (search-forward string bound noerror)
  6714. (search-backward string bound noerror))
  6715. (when (save-match-data
  6716. (and (save-excursion
  6717. (beginning-of-line)
  6718. (looking-at org-complex-heading-regexp))
  6719. (or (not (match-beginning 5))
  6720. (< (point) (match-beginning 5)))))
  6721. (throw 'return (point))))))
  6722. (defun org-goto-local-auto-isearch ()
  6723. "Start isearch."
  6724. (interactive)
  6725. (goto-char (point-min))
  6726. (let ((keys (this-command-keys)))
  6727. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6728. (isearch-mode t)
  6729. (isearch-process-search-char (string-to-char keys)))))
  6730. (defun org-goto-ret (&optional arg)
  6731. "Finish `org-goto' by going to the new location."
  6732. (interactive "P")
  6733. (setq org-goto-selected-point (point)
  6734. org-goto-exit-command 'return)
  6735. (throw 'exit nil))
  6736. (defun org-goto-left ()
  6737. "Finish `org-goto' by going to the new location."
  6738. (interactive)
  6739. (if (org-at-heading-p)
  6740. (progn
  6741. (beginning-of-line 1)
  6742. (setq org-goto-selected-point (point)
  6743. org-goto-exit-command 'left)
  6744. (throw 'exit nil))
  6745. (user-error "Not on a heading")))
  6746. (defun org-goto-right ()
  6747. "Finish `org-goto' by going to the new location."
  6748. (interactive)
  6749. (if (org-at-heading-p)
  6750. (progn
  6751. (setq org-goto-selected-point (point)
  6752. org-goto-exit-command 'right)
  6753. (throw 'exit nil))
  6754. (user-error "Not on a heading")))
  6755. (defun org-goto-quit ()
  6756. "Finish `org-goto' without cursor motion."
  6757. (interactive)
  6758. (setq org-goto-selected-point nil)
  6759. (setq org-goto-exit-command 'quit)
  6760. (throw 'exit nil))
  6761. ;;; Indirect buffer display of subtrees
  6762. (defvar org-indirect-dedicated-frame nil
  6763. "This is the frame being used for indirect tree display.")
  6764. (defvar org-last-indirect-buffer nil)
  6765. (defun org-tree-to-indirect-buffer (&optional arg)
  6766. "Create indirect buffer and narrow it to current subtree.
  6767. With a numerical prefix ARG, go up to this level and then take that tree.
  6768. If ARG is negative, go up that many levels.
  6769. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6770. indirect buffer previously made with this command, to avoid proliferation of
  6771. indirect buffers. However, when you call the command with a \
  6772. \\[universal-argument] prefix, or
  6773. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6774. is kept so that you can work with several indirect buffers at the same time.
  6775. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6776. \\[universal-argument] prefix also
  6777. requests that a new frame be made for the new buffer, so that the dedicated
  6778. frame is not changed."
  6779. (interactive "P")
  6780. (let ((cbuf (current-buffer))
  6781. (cwin (selected-window))
  6782. (pos (point))
  6783. beg end level heading ibuf)
  6784. (save-excursion
  6785. (org-back-to-heading t)
  6786. (when (numberp arg)
  6787. (setq level (org-outline-level))
  6788. (if (< arg 0) (setq arg (+ level arg)))
  6789. (while (> (setq level (org-outline-level)) arg)
  6790. (org-up-heading-safe)))
  6791. (setq beg (point)
  6792. heading (org-get-heading))
  6793. (org-end-of-subtree t t)
  6794. (if (org-at-heading-p) (backward-char 1))
  6795. (setq end (point)))
  6796. (if (and (buffer-live-p org-last-indirect-buffer)
  6797. (not (eq org-indirect-buffer-display 'new-frame))
  6798. (not arg))
  6799. (kill-buffer org-last-indirect-buffer))
  6800. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6801. org-last-indirect-buffer ibuf)
  6802. (cond
  6803. ((or (eq org-indirect-buffer-display 'new-frame)
  6804. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6805. (select-frame (make-frame))
  6806. (delete-other-windows)
  6807. (org-pop-to-buffer-same-window ibuf)
  6808. (org-set-frame-title heading))
  6809. ((eq org-indirect-buffer-display 'dedicated-frame)
  6810. (raise-frame
  6811. (select-frame (or (and org-indirect-dedicated-frame
  6812. (frame-live-p org-indirect-dedicated-frame)
  6813. org-indirect-dedicated-frame)
  6814. (setq org-indirect-dedicated-frame (make-frame)))))
  6815. (delete-other-windows)
  6816. (org-pop-to-buffer-same-window ibuf)
  6817. (org-set-frame-title (concat "Indirect: " heading)))
  6818. ((eq org-indirect-buffer-display 'current-window)
  6819. (org-pop-to-buffer-same-window ibuf))
  6820. ((eq org-indirect-buffer-display 'other-window)
  6821. (pop-to-buffer ibuf))
  6822. (t (error "Invalid value")))
  6823. (if (featurep 'xemacs)
  6824. (save-excursion (org-mode) (turn-on-font-lock)))
  6825. (narrow-to-region beg end)
  6826. (outline-show-all)
  6827. (goto-char pos)
  6828. (run-hook-with-args 'org-cycle-hook 'all)
  6829. (and (window-live-p cwin) (select-window cwin))))
  6830. (defun org-get-indirect-buffer (&optional buffer heading)
  6831. (setq buffer (or buffer (current-buffer)))
  6832. (let ((n 1) (base (buffer-name buffer)) bname)
  6833. (while (buffer-live-p
  6834. (get-buffer
  6835. (setq bname
  6836. (concat base "-"
  6837. (if heading (concat heading "-" (number-to-string n))
  6838. (number-to-string n))))))
  6839. (setq n (1+ n)))
  6840. (condition-case nil
  6841. (make-indirect-buffer buffer bname 'clone)
  6842. (error (make-indirect-buffer buffer bname)))))
  6843. (defun org-set-frame-title (title)
  6844. "Set the title of the current frame to the string TITLE."
  6845. ;; FIXME: how to name a single frame in XEmacs???
  6846. (unless (featurep 'xemacs)
  6847. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6848. ;;;; Structure editing
  6849. ;;; Inserting headlines
  6850. (defun org-previous-line-empty-p (&optional next)
  6851. "Is the previous line a blank line?
  6852. When NEXT is non-nil, check the next line instead."
  6853. (save-excursion
  6854. (and (not (bobp))
  6855. (or (beginning-of-line (if next 2 0)) t)
  6856. (save-match-data
  6857. (looking-at "[ \t]*$")))))
  6858. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6859. "Insert a new heading or an item with the same depth at point.
  6860. If point is at the beginning of a heading or a list item, insert
  6861. a new heading or a new item above the current one. If point is
  6862. at the beginning of a normal line, turn the line into a heading.
  6863. If point is in the middle of a headline or a list item, split the
  6864. headline or the item and create a new headline/item with the text
  6865. in the current line after point \(see `org-M-RET-may-split-line'
  6866. on how to modify this behavior).
  6867. With one universal prefix argument, set the user option
  6868. `org-insert-heading-respect-content' to t for the duration of
  6869. the command. This modifies the behavior described above in this
  6870. ways: on list items and at the beginning of normal lines, force
  6871. the insertion of a heading after the current subtree.
  6872. With two universal prefix arguments, insert the heading at the
  6873. end of the grandparent subtree. For example, if point is within
  6874. a 2nd-level heading, then it will insert a 2nd-level heading at
  6875. the end of the 1st-level parent heading.
  6876. If point is at the beginning of a headline, insert a sibling
  6877. before the current headline. If point is not at the beginning,
  6878. split the line and create a new headline with the text in the
  6879. current line after point \(see `org-M-RET-may-split-line' on how
  6880. to modify this behavior).
  6881. If point is at the beginning of a normal line, turn this line
  6882. into a heading.
  6883. When INVISIBLE-OK is set, stop at invisible headlines when going
  6884. back. This is important for non-interactive uses of the
  6885. command.
  6886. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6887. heading, unconditionally."
  6888. (interactive "P")
  6889. (if (org-called-interactively-p 'any) (org-reveal))
  6890. (let ((itemp (and (not top-level) (org-in-item-p)))
  6891. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6892. (respect-content (or org-insert-heading-respect-content
  6893. (equal arg '(4))))
  6894. (initial-content ""))
  6895. (cond
  6896. ((or (= (buffer-size) 0)
  6897. (and (not (save-excursion
  6898. (and (ignore-errors (org-back-to-heading invisible-ok))
  6899. (org-at-heading-p))))
  6900. (or arg (not itemp))))
  6901. ;; At beginning of buffer or so high up that only a heading
  6902. ;; makes sense.
  6903. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6904. ((not respect-content)
  6905. (unless may-split (end-of-line))
  6906. (insert "\n* "))
  6907. ((re-search-forward org-outline-regexp-bol nil t)
  6908. (beginning-of-line)
  6909. (insert "* \n")
  6910. (backward-char))
  6911. (t (goto-char (point-max))
  6912. (insert "\n* ")))
  6913. (run-hooks 'org-insert-heading-hook))
  6914. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6915. (t
  6916. ;; Maybe move at the end of the subtree
  6917. (when (equal arg '(16))
  6918. (org-up-heading-safe)
  6919. (org-end-of-subtree t))
  6920. ;; Insert a heading
  6921. (save-restriction
  6922. (widen)
  6923. (let* ((level nil)
  6924. (on-heading (org-at-heading-p))
  6925. (empty-line-p (if on-heading
  6926. (org-previous-line-empty-p)
  6927. ;; We will decide later
  6928. nil))
  6929. ;; Get a level string to fall back on.
  6930. (fix-level
  6931. (if (org-before-first-heading-p) "*"
  6932. (save-excursion
  6933. (org-back-to-heading t)
  6934. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6935. (looking-at org-outline-regexp)
  6936. (make-string (1- (length (match-string 0))) ?*))))
  6937. (stars
  6938. (save-excursion
  6939. (condition-case nil
  6940. (if top-level "* "
  6941. (org-back-to-heading invisible-ok)
  6942. (when (and (not on-heading)
  6943. (featurep 'org-inlinetask)
  6944. (integerp org-inlinetask-min-level)
  6945. (>= (length (match-string 0))
  6946. org-inlinetask-min-level))
  6947. ;; Find a heading level before the inline
  6948. ;; task.
  6949. (while (and (setq level (org-up-heading-safe))
  6950. (>= level org-inlinetask-min-level)))
  6951. (if (org-at-heading-p)
  6952. (org-back-to-heading invisible-ok)
  6953. (error "This should not happen")))
  6954. (unless (and (save-excursion
  6955. (save-match-data
  6956. (org-backward-heading-same-level
  6957. 1 invisible-ok))
  6958. (= (point) (match-beginning 0)))
  6959. (not (org-previous-line-empty-p t)))
  6960. (setq empty-line-p (or empty-line-p
  6961. (org-previous-line-empty-p))))
  6962. (match-string 0))
  6963. (error (or fix-level "* ")))))
  6964. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6965. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6966. pos hide-previous previous-pos)
  6967. ;; If we insert after content, move there and clean up
  6968. ;; whitespace.
  6969. (when (and respect-content
  6970. (not (org-looking-at-p org-outline-regexp-bol)))
  6971. (if (not (org-before-first-heading-p))
  6972. (org-end-of-subtree nil t)
  6973. (re-search-forward org-outline-regexp-bol)
  6974. (beginning-of-line 0))
  6975. (skip-chars-backward " \r\t\n")
  6976. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6977. (looking-at "[ \t]+") (replace-match ""))
  6978. (unless (eobp) (forward-char 1))
  6979. (when (looking-at "^\\*")
  6980. (unless (bobp) (backward-char 1))
  6981. (insert "\n")))
  6982. ;; If we are splitting, grab the text that should be moved
  6983. ;; to the new headline.
  6984. (when may-split
  6985. (if (org-on-heading-p)
  6986. ;; This is a heading: split intelligently (keeping
  6987. ;; tags).
  6988. (let ((pos (point)))
  6989. (beginning-of-line)
  6990. (unless (looking-at org-complex-heading-regexp)
  6991. (error "This should not happen"))
  6992. (when (and (match-beginning 4)
  6993. (> pos (match-beginning 4))
  6994. (< pos (match-end 4)))
  6995. (setq initial-content (buffer-substring pos (match-end 4)))
  6996. (goto-char pos)
  6997. (delete-region (point) (match-end 4))
  6998. (if (looking-at "[ \t]*$")
  6999. (replace-match "")
  7000. (insert (make-string (length initial-content) ?\s)))
  7001. (setq initial-content (org-trim initial-content)))
  7002. (goto-char pos))
  7003. ;; A normal line.
  7004. (setq initial-content
  7005. (org-trim
  7006. (delete-and-extract-region (point) (line-end-position))))))
  7007. ;; If we are at the beginning of the line, insert before it.
  7008. ;; Otherwise, after it.
  7009. (cond
  7010. ((and (bolp) (looking-at "[ \t]*$")))
  7011. ((bolp) (save-excursion (insert "\n")))
  7012. (t (end-of-line)
  7013. (insert "\n")))
  7014. ;; Insert the new heading
  7015. (insert stars)
  7016. (just-one-space)
  7017. (insert initial-content)
  7018. (unless (and blank (org-previous-line-empty-p))
  7019. (org-N-empty-lines-before-current (if blank 1 0)))
  7020. ;; Adjust visibility, which may be messed up if we removed
  7021. ;; blank lines while previous entry was hidden.
  7022. (let ((bol (line-beginning-position)))
  7023. (dolist (o (overlays-at (1- bol)))
  7024. (when (and (eq (overlay-get o 'invisible) 'outline)
  7025. (eq (overlay-end o) bol))
  7026. (move-overlay o (overlay-start o) (1- bol)))))
  7027. (run-hooks 'org-insert-heading-hook)))))))
  7028. (defun org-N-empty-lines-before-current (N)
  7029. "Make the number of empty lines before current exactly N.
  7030. So this will delete or add empty lines."
  7031. (save-excursion
  7032. (beginning-of-line)
  7033. (let ((p (point)))
  7034. (skip-chars-backward " \r\t\n")
  7035. (unless (bolp) (forward-line))
  7036. (delete-region (point) p))
  7037. (when (> N 0) (insert (make-string N ?\n)))))
  7038. (defun org-get-heading (&optional no-tags no-todo)
  7039. "Return the heading of the current entry, without the stars.
  7040. When NO-TAGS is non-nil, don't include tags.
  7041. When NO-TODO is non-nil, don't include TODO keywords."
  7042. (save-excursion
  7043. (org-back-to-heading t)
  7044. (cond
  7045. ((and no-tags no-todo)
  7046. (looking-at org-complex-heading-regexp)
  7047. (match-string 4))
  7048. (no-tags
  7049. (looking-at (concat org-outline-regexp
  7050. "\\(.*?\\)"
  7051. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7052. (match-string 1))
  7053. (no-todo
  7054. (looking-at org-todo-line-regexp)
  7055. (match-string 3))
  7056. (t (looking-at org-heading-regexp)
  7057. (match-string 2)))))
  7058. (defvar orgstruct-mode) ; defined below
  7059. (defun org-heading-components ()
  7060. "Return the components of the current heading.
  7061. This is a list with the following elements:
  7062. - the level as an integer
  7063. - the reduced level, different if `org-odd-levels-only' is set.
  7064. - the TODO keyword, or nil
  7065. - the priority character, like ?A, or nil if no priority is given
  7066. - the headline text itself, or the tags string if no headline text
  7067. - the tags string, or nil."
  7068. (save-excursion
  7069. (org-back-to-heading t)
  7070. (if (let (case-fold-search)
  7071. (looking-at
  7072. (if orgstruct-mode
  7073. org-heading-regexp
  7074. org-complex-heading-regexp)))
  7075. (if orgstruct-mode
  7076. (list (length (match-string 1))
  7077. (org-reduced-level (length (match-string 1)))
  7078. nil
  7079. nil
  7080. (match-string 2)
  7081. nil)
  7082. (list (length (match-string 1))
  7083. (org-reduced-level (length (match-string 1)))
  7084. (org-match-string-no-properties 2)
  7085. (and (match-end 3) (aref (match-string 3) 2))
  7086. (org-match-string-no-properties 4)
  7087. (org-match-string-no-properties 5))))))
  7088. (defun org-get-entry ()
  7089. "Get the entry text, after heading, entire subtree."
  7090. (save-excursion
  7091. (org-back-to-heading t)
  7092. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7093. (defun org-insert-heading-after-current ()
  7094. "Insert a new heading with same level as current, after current subtree."
  7095. (interactive)
  7096. (org-back-to-heading)
  7097. (org-insert-heading)
  7098. (org-move-subtree-down)
  7099. (end-of-line 1))
  7100. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7101. "Insert heading with `org-insert-heading-respect-content' set to t."
  7102. (interactive)
  7103. (org-insert-heading '(4) invisible-ok))
  7104. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7105. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7106. (interactive)
  7107. (org-insert-todo-heading force-state '(4)))
  7108. (defun org-insert-todo-heading (arg &optional force-heading)
  7109. "Insert a new heading with the same level and TODO state as current heading.
  7110. If the heading has no TODO state, or if the state is DONE, use the first
  7111. state (TODO by default). Also with one prefix arg, force first state. With
  7112. two prefix args, force inserting at the end of the parent subtree."
  7113. (interactive "P")
  7114. (when (or force-heading (not (org-insert-item 'checkbox)))
  7115. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7116. force-heading))
  7117. (save-excursion
  7118. (org-back-to-heading)
  7119. (outline-previous-heading)
  7120. (looking-at org-todo-line-regexp))
  7121. (let*
  7122. ((new-mark-x
  7123. (if (or (equal arg '(4))
  7124. (not (match-beginning 2))
  7125. (member (match-string 2) org-done-keywords))
  7126. (car org-todo-keywords-1)
  7127. (match-string 2)))
  7128. (new-mark
  7129. (or
  7130. (run-hook-with-args-until-success
  7131. 'org-todo-get-default-hook new-mark-x nil)
  7132. new-mark-x)))
  7133. (beginning-of-line 1)
  7134. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7135. (if org-treat-insert-todo-heading-as-state-change
  7136. (org-todo new-mark)
  7137. (insert new-mark " "))))
  7138. (when org-provide-todo-statistics
  7139. (org-update-parent-todo-statistics))))
  7140. (defun org-insert-subheading (arg)
  7141. "Insert a new subheading and demote it.
  7142. Works for outline headings and for plain lists alike."
  7143. (interactive "P")
  7144. (org-insert-heading arg)
  7145. (cond
  7146. ((org-at-heading-p) (org-do-demote))
  7147. ((org-at-item-p) (org-indent-item))))
  7148. (defun org-insert-todo-subheading (arg)
  7149. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7150. Works for outline headings and for plain lists alike."
  7151. (interactive "P")
  7152. (org-insert-todo-heading arg)
  7153. (cond
  7154. ((org-at-heading-p) (org-do-demote))
  7155. ((org-at-item-p) (org-indent-item))))
  7156. ;;; Promotion and Demotion
  7157. (defvar org-after-demote-entry-hook nil
  7158. "Hook run after an entry has been demoted.
  7159. The cursor will be at the beginning of the entry.
  7160. When a subtree is being demoted, the hook will be called for each node.")
  7161. (defvar org-after-promote-entry-hook nil
  7162. "Hook run after an entry has been promoted.
  7163. The cursor will be at the beginning of the entry.
  7164. When a subtree is being promoted, the hook will be called for each node.")
  7165. (defun org-promote-subtree ()
  7166. "Promote the entire subtree.
  7167. See also `org-promote'."
  7168. (interactive)
  7169. (save-excursion
  7170. (org-with-limited-levels (org-map-tree 'org-promote)))
  7171. (org-fix-position-after-promote))
  7172. (defun org-demote-subtree ()
  7173. "Demote the entire subtree.
  7174. See `org-demote' and `org-promote'."
  7175. (interactive)
  7176. (save-excursion
  7177. (org-with-limited-levels (org-map-tree 'org-demote)))
  7178. (org-fix-position-after-promote))
  7179. (defun org-do-promote ()
  7180. "Promote the current heading higher up the tree.
  7181. If the region is active in `transient-mark-mode', promote all
  7182. headings in the region."
  7183. (interactive)
  7184. (save-excursion
  7185. (if (org-region-active-p)
  7186. (org-map-region 'org-promote (region-beginning) (region-end))
  7187. (org-promote)))
  7188. (org-fix-position-after-promote))
  7189. (defun org-do-demote ()
  7190. "Demote the current heading lower down the tree.
  7191. If the region is active in `transient-mark-mode', demote all
  7192. headings in the region."
  7193. (interactive)
  7194. (save-excursion
  7195. (if (org-region-active-p)
  7196. (org-map-region 'org-demote (region-beginning) (region-end))
  7197. (org-demote)))
  7198. (org-fix-position-after-promote))
  7199. (defun org-fix-position-after-promote ()
  7200. "Fix cursor position and indentation after demoting/promoting."
  7201. (let ((pos (point)))
  7202. (when (save-excursion
  7203. (beginning-of-line 1)
  7204. (looking-at org-todo-line-regexp)
  7205. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7206. (cond ((eobp) (insert " "))
  7207. ((eolp) (insert " "))
  7208. ((equal (char-after) ?\ ) (forward-char 1))))))
  7209. (defun org-current-level ()
  7210. "Return the level of the current entry, or nil if before the first headline.
  7211. The level is the number of stars at the beginning of the
  7212. headline. Use `org-reduced-level' to remove the effect of
  7213. `org-odd-levels'. Unlike to `org-outline-level', this function
  7214. ignores inlinetasks."
  7215. (let ((level (org-with-limited-levels (org-outline-level))))
  7216. (and (> level 0) level)))
  7217. (defun org-get-previous-line-level ()
  7218. "Return the outline depth of the last headline before the current line.
  7219. Returns 0 for the first headline in the buffer, and nil if before the
  7220. first headline."
  7221. (and (org-current-level)
  7222. (or (and (/= (line-beginning-position) (point-min))
  7223. (save-excursion (beginning-of-line 0) (org-current-level)))
  7224. 0)))
  7225. (defun org-reduced-level (l)
  7226. "Compute the effective level of a heading.
  7227. This takes into account the setting of `org-odd-levels-only'."
  7228. (cond
  7229. ((zerop l) 0)
  7230. (org-odd-levels-only (1+ (floor (/ l 2))))
  7231. (t l)))
  7232. (defun org-level-increment ()
  7233. "Return the number of stars that will be added or removed at a
  7234. time to headlines when structure editing, based on the value of
  7235. `org-odd-levels-only'."
  7236. (if org-odd-levels-only 2 1))
  7237. (defun org-get-valid-level (level &optional change)
  7238. "Rectify a level change under the influence of `org-odd-levels-only'
  7239. LEVEL is a current level, CHANGE is by how much the level should be
  7240. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7241. even level numbers will become the next higher odd number."
  7242. (if org-odd-levels-only
  7243. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7244. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7245. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7246. (max 1 (+ level (or change 0)))))
  7247. (if (boundp 'define-obsolete-function-alias)
  7248. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7249. (define-obsolete-function-alias 'org-get-legal-level
  7250. 'org-get-valid-level)
  7251. (define-obsolete-function-alias 'org-get-legal-level
  7252. 'org-get-valid-level "23.1")))
  7253. (defun org-promote ()
  7254. "Promote the current heading higher up the tree."
  7255. (org-with-wide-buffer
  7256. (org-back-to-heading t)
  7257. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7258. after-change-functions))
  7259. (level (save-match-data (funcall outline-level)))
  7260. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7261. (diff (abs (- level (length up-head) -1))))
  7262. (cond
  7263. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7264. (replace-match "# " nil t))
  7265. ((= level 1)
  7266. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7267. (t (replace-match up-head nil t)))
  7268. (unless (= level 1)
  7269. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7270. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7271. (run-hooks 'org-after-promote-entry-hook))))
  7272. (defun org-demote ()
  7273. "Demote the current heading lower down the tree."
  7274. (org-with-wide-buffer
  7275. (org-back-to-heading t)
  7276. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7277. after-change-functions))
  7278. (level (save-match-data (funcall outline-level)))
  7279. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7280. (diff (abs (- level (length down-head) -1))))
  7281. (replace-match down-head nil t)
  7282. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7283. (when org-adapt-indentation (org-fixup-indentation diff))
  7284. (run-hooks 'org-after-demote-entry-hook))))
  7285. (defun org-cycle-level ()
  7286. "Cycle the level of an empty headline through possible states.
  7287. This goes first to child, then to parent, level, then up the hierarchy.
  7288. After top level, it switches back to sibling level."
  7289. (interactive)
  7290. (let ((org-adapt-indentation nil))
  7291. (when (org-point-at-end-of-empty-headline)
  7292. (setq this-command 'org-cycle-level) ; Only needed for caching
  7293. (let ((cur-level (org-current-level))
  7294. (prev-level (org-get-previous-line-level)))
  7295. (cond
  7296. ;; If first headline in file, promote to top-level.
  7297. ((= prev-level 0)
  7298. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7299. do (org-do-promote)))
  7300. ;; If same level as prev, demote one.
  7301. ((= prev-level cur-level)
  7302. (org-do-demote))
  7303. ;; If parent is top-level, promote to top level if not already.
  7304. ((= prev-level 1)
  7305. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7306. do (org-do-promote)))
  7307. ;; If top-level, return to prev-level.
  7308. ((= cur-level 1)
  7309. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7310. do (org-do-demote)))
  7311. ;; If less than prev-level, promote one.
  7312. ((< cur-level prev-level)
  7313. (org-do-promote))
  7314. ;; If deeper than prev-level, promote until higher than
  7315. ;; prev-level.
  7316. ((> cur-level prev-level)
  7317. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7318. do (org-do-promote))))
  7319. t))))
  7320. (defun org-map-tree (fun)
  7321. "Call FUN for every heading underneath the current one."
  7322. (org-back-to-heading)
  7323. (let ((level (funcall outline-level)))
  7324. (save-excursion
  7325. (funcall fun)
  7326. (while (and (progn
  7327. (outline-next-heading)
  7328. (> (funcall outline-level) level))
  7329. (not (eobp)))
  7330. (funcall fun)))))
  7331. (defun org-map-region (fun beg end)
  7332. "Call FUN for every heading between BEG and END."
  7333. (let ((org-ignore-region t))
  7334. (save-excursion
  7335. (setq end (copy-marker end))
  7336. (goto-char beg)
  7337. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7338. (< (point) end))
  7339. (funcall fun))
  7340. (while (and (progn
  7341. (outline-next-heading)
  7342. (< (point) end))
  7343. (not (eobp)))
  7344. (funcall fun)))))
  7345. (defun org-fixup-indentation (diff)
  7346. "Change the indentation in the current entry by DIFF.
  7347. DIFF is an integer. Indentation is done according to the
  7348. following rules:
  7349. - Planning information and property drawers are always indented
  7350. according to the new level of the headline;
  7351. - Footnote definitions and their contents are ignored;
  7352. - Inlinetasks' boundaries are not shifted;
  7353. - Empty lines are ignored;
  7354. - Other lines' indentation are shifted by DIFF columns, unless
  7355. it would introduce a structural change in the document, in
  7356. which case no shifting is done at all.
  7357. Assume point is at a heading or an inlinetask beginning."
  7358. (org-with-wide-buffer
  7359. (narrow-to-region (line-beginning-position)
  7360. (save-excursion
  7361. (if (org-with-limited-levels (org-at-heading-p))
  7362. (org-with-limited-levels (outline-next-heading))
  7363. (org-inlinetask-goto-end))
  7364. (point)))
  7365. (forward-line)
  7366. ;; Indent properly planning info and property drawer.
  7367. (when (org-looking-at-p org-planning-line-re)
  7368. (org-indent-line)
  7369. (forward-line))
  7370. (when (looking-at org-property-drawer-re)
  7371. (goto-char (match-end 0))
  7372. (forward-line)
  7373. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7374. (catch 'no-shift
  7375. (when (zerop diff) (throw 'no-shift nil))
  7376. ;; If DIFF is negative, first check if a shift is possible at all
  7377. ;; (e.g., it doesn't break structure). This can only happen if
  7378. ;; some contents are not properly indented.
  7379. (let ((case-fold-search t))
  7380. (when (< diff 0)
  7381. (let ((diff (- diff))
  7382. (forbidden-re (concat org-outline-regexp
  7383. "\\|"
  7384. (substring org-footnote-definition-re 1))))
  7385. (save-excursion
  7386. (while (not (eobp))
  7387. (cond
  7388. ((org-looking-at-p "[ \t]*$") (forward-line))
  7389. ((and (org-looking-at-p org-footnote-definition-re)
  7390. (let ((e (org-element-at-point)))
  7391. (and (eq (org-element-type e) 'footnote-definition)
  7392. (goto-char (org-element-property :end e))))))
  7393. ((org-looking-at-p org-outline-regexp) (forward-line))
  7394. ;; Give up if shifting would move before column 0 or
  7395. ;; if it would introduce a headline or a footnote
  7396. ;; definition.
  7397. (t
  7398. (skip-chars-forward " \t")
  7399. (let ((ind (current-column)))
  7400. (when (or (< ind diff)
  7401. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7402. (throw 'no-shift nil)))
  7403. ;; Ignore contents of example blocks and source
  7404. ;; blocks if their indentation is meant to be
  7405. ;; preserved. Jump to block's closing line.
  7406. (beginning-of-line)
  7407. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7408. (let ((e (org-element-at-point)))
  7409. (and (memq (org-element-type e)
  7410. '(example-block src-block))
  7411. (or org-src-preserve-indentation
  7412. (org-element-property :preserve-indent e))
  7413. (goto-char (org-element-property :end e))
  7414. (progn (skip-chars-backward " \r\t\n")
  7415. (beginning-of-line)
  7416. t))))
  7417. (forward-line))))))))
  7418. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7419. ;; by DIFF. Also skip contents of source or example blocks
  7420. ;; when indentation is meant to be preserved.
  7421. (while (not (eobp))
  7422. (cond
  7423. ((and (org-looking-at-p org-footnote-definition-re)
  7424. (let ((e (org-element-at-point)))
  7425. (and (eq (org-element-type e) 'footnote-definition)
  7426. (goto-char (org-element-property :end e))))))
  7427. ((org-looking-at-p org-outline-regexp) (forward-line))
  7428. ((org-looking-at-p "[ \t]*$") (forward-line))
  7429. (t
  7430. (org-indent-line-to (+ (org-get-indentation) diff))
  7431. (beginning-of-line)
  7432. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7433. (let ((e (org-element-at-point)))
  7434. (and (memq (org-element-type e)
  7435. '(example-block src-block))
  7436. (or org-src-preserve-indentation
  7437. (org-element-property :preserve-indent e))
  7438. (goto-char (org-element-property :end e))
  7439. (progn (skip-chars-backward " \r\t\n")
  7440. (beginning-of-line)
  7441. t))))
  7442. (forward-line)))))))))
  7443. (defun org-convert-to-odd-levels ()
  7444. "Convert an org-mode file with all levels allowed to one with odd levels.
  7445. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7446. level 5 etc."
  7447. (interactive)
  7448. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7449. (let ((outline-level 'org-outline-level)
  7450. (org-odd-levels-only nil) n)
  7451. (save-excursion
  7452. (goto-char (point-min))
  7453. (while (re-search-forward "^\\*\\*+ " nil t)
  7454. (setq n (- (length (match-string 0)) 2))
  7455. (while (>= (setq n (1- n)) 0)
  7456. (org-demote))
  7457. (end-of-line 1))))))
  7458. (defun org-convert-to-oddeven-levels ()
  7459. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7460. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7461. file contains a section with an even level, conversion would
  7462. destroy the structure of the file. An error is signaled in this
  7463. case."
  7464. (interactive)
  7465. (goto-char (point-min))
  7466. ;; First check if there are no even levels
  7467. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7468. (org-show-set-visibility 'canonical)
  7469. (error "Not all levels are odd in this file. Conversion not possible"))
  7470. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7471. (let ((outline-regexp org-outline-regexp)
  7472. (outline-level 'org-outline-level)
  7473. (org-odd-levels-only nil) n)
  7474. (save-excursion
  7475. (goto-char (point-min))
  7476. (while (re-search-forward "^\\*\\*+ " nil t)
  7477. (setq n (/ (1- (length (match-string 0))) 2))
  7478. (while (>= (setq n (1- n)) 0)
  7479. (org-promote))
  7480. (end-of-line 1))))))
  7481. (defun org-tr-level (n)
  7482. "Make N odd if required."
  7483. (if org-odd-levels-only (1+ (/ n 2)) n))
  7484. ;;; Vertical tree motion, cutting and pasting of subtrees
  7485. (defun org-move-subtree-up (&optional arg)
  7486. "Move the current subtree up past ARG headlines of the same level."
  7487. (interactive "p")
  7488. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7489. (defun org-move-subtree-down (&optional arg)
  7490. "Move the current subtree down past ARG headlines of the same level."
  7491. (interactive "p")
  7492. (setq arg (prefix-numeric-value arg))
  7493. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7494. 'org-get-last-sibling))
  7495. (ins-point (make-marker))
  7496. (cnt (abs arg))
  7497. (col (current-column))
  7498. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7499. ;; Select the tree
  7500. (org-back-to-heading)
  7501. (setq beg0 (point))
  7502. (save-excursion
  7503. (setq ne-beg (org-back-over-empty-lines))
  7504. (setq beg (point)))
  7505. (save-match-data
  7506. (save-excursion (outline-end-of-heading)
  7507. (setq folded (outline-invisible-p)))
  7508. (progn (org-end-of-subtree nil t)
  7509. (unless (eobp) (backward-char))))
  7510. (outline-next-heading)
  7511. (setq ne-end (org-back-over-empty-lines))
  7512. (setq end (point))
  7513. (goto-char beg0)
  7514. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7515. ;; include less whitespace
  7516. (save-excursion
  7517. (goto-char beg)
  7518. (forward-line (- ne-beg ne-end))
  7519. (setq beg (point))))
  7520. ;; Find insertion point, with error handling
  7521. (while (> cnt 0)
  7522. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7523. (progn (goto-char beg0)
  7524. (user-error "Cannot move past superior level or buffer limit")))
  7525. (setq cnt (1- cnt)))
  7526. (if (> arg 0)
  7527. ;; Moving forward - still need to move over subtree
  7528. (progn (org-end-of-subtree t t)
  7529. (save-excursion
  7530. (org-back-over-empty-lines)
  7531. (or (bolp) (newline)))))
  7532. (setq ne-ins (org-back-over-empty-lines))
  7533. (move-marker ins-point (point))
  7534. (setq txt (buffer-substring beg end))
  7535. (org-save-markers-in-region beg end)
  7536. (delete-region beg end)
  7537. (org-remove-empty-overlays-at beg)
  7538. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7539. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7540. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7541. (let ((bbb (point)))
  7542. (insert-before-markers txt)
  7543. (org-reinstall-markers-in-region bbb)
  7544. (move-marker ins-point bbb))
  7545. (or (bolp) (insert "\n"))
  7546. (setq ins-end (point))
  7547. (goto-char ins-point)
  7548. (org-skip-whitespace)
  7549. (when (and (< arg 0)
  7550. (org-first-sibling-p)
  7551. (> ne-ins ne-beg))
  7552. ;; Move whitespace back to beginning
  7553. (save-excursion
  7554. (goto-char ins-end)
  7555. (let ((kill-whole-line t))
  7556. (kill-line (- ne-ins ne-beg)) (point)))
  7557. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7558. (move-marker ins-point nil)
  7559. (if folded
  7560. (outline-hide-subtree)
  7561. (org-show-entry)
  7562. (outline-show-children)
  7563. (org-cycle-hide-drawers 'children))
  7564. (org-clean-visibility-after-subtree-move)
  7565. ;; move back to the initial column we were at
  7566. (move-to-column col)))
  7567. (defvar org-subtree-clip ""
  7568. "Clipboard for cut and paste of subtrees.
  7569. This is actually only a copy of the kill, because we use the normal kill
  7570. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7571. (defvar org-subtree-clip-folded nil
  7572. "Was the last copied subtree folded?
  7573. This is used to fold the tree back after pasting.")
  7574. (defun org-cut-subtree (&optional n)
  7575. "Cut the current subtree into the clipboard.
  7576. With prefix arg N, cut this many sequential subtrees.
  7577. This is a short-hand for marking the subtree and then cutting it."
  7578. (interactive "p")
  7579. (org-copy-subtree n 'cut))
  7580. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7581. "Copy the current subtree it in the clipboard.
  7582. With prefix arg N, copy this many sequential subtrees.
  7583. This is a short-hand for marking the subtree and then copying it.
  7584. If CUT is non-nil, actually cut the subtree.
  7585. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7586. of some markers in the region, even if CUT is non-nil. This is
  7587. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7588. (interactive "p")
  7589. (let (beg end folded (beg0 (point)))
  7590. (if (org-called-interactively-p 'any)
  7591. (org-back-to-heading nil) ; take what looks like a subtree
  7592. (org-back-to-heading t)) ; take what is really there
  7593. (setq beg (point))
  7594. (skip-chars-forward " \t\r\n")
  7595. (save-match-data
  7596. (if nosubtrees
  7597. (outline-next-heading)
  7598. (save-excursion (outline-end-of-heading)
  7599. (setq folded (outline-invisible-p)))
  7600. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7601. (org-end-of-subtree t t)))
  7602. ;; Include the end of an inlinetask
  7603. (when (and (featurep 'org-inlinetask)
  7604. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7605. "END[ \t]*$")))
  7606. (end-of-line))
  7607. (setq end (point))
  7608. (goto-char beg0)
  7609. (when (> end beg)
  7610. (setq org-subtree-clip-folded folded)
  7611. (when (or cut force-store-markers)
  7612. (org-save-markers-in-region beg end))
  7613. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7614. (setq org-subtree-clip (current-kill 0))
  7615. (message "%s: Subtree(s) with %d characters"
  7616. (if cut "Cut" "Copied")
  7617. (length org-subtree-clip)))))
  7618. (defun org-paste-subtree (&optional level tree for-yank remove)
  7619. "Paste the clipboard as a subtree, with modification of headline level.
  7620. The entire subtree is promoted or demoted in order to match a new headline
  7621. level.
  7622. If the cursor is at the beginning of a headline, the same level as
  7623. that headline is used to paste the tree.
  7624. If not, the new level is derived from the *visible* headings
  7625. before and after the insertion point, and taken to be the inferior headline
  7626. level of the two. So if the previous visible heading is level 3 and the
  7627. next is level 4 (or vice versa), level 4 will be used for insertion.
  7628. This makes sure that the subtree remains an independent subtree and does
  7629. not swallow low level entries.
  7630. You can also force a different level, either by using a numeric prefix
  7631. argument, or by inserting the heading marker by hand. For example, if the
  7632. cursor is after \"*****\", then the tree will be shifted to level 5.
  7633. If optional TREE is given, use this text instead of the kill ring.
  7634. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7635. move back over whitespace before inserting, and move point to the end of
  7636. the inserted text when done.
  7637. When REMOVE is non-nil, remove the subtree from the clipboard."
  7638. (interactive "P")
  7639. (setq tree (or tree (and kill-ring (current-kill 0))))
  7640. (unless (org-kill-is-subtree-p tree)
  7641. (user-error "%s"
  7642. (substitute-command-keys
  7643. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7644. (org-with-limited-levels
  7645. (let* ((visp (not (outline-invisible-p)))
  7646. (txt tree)
  7647. (^re_ "\\(\\*+\\)[ \t]*")
  7648. (old-level (if (string-match org-outline-regexp-bol txt)
  7649. (- (match-end 0) (match-beginning 0) 1)
  7650. -1))
  7651. (force-level (cond (level (prefix-numeric-value level))
  7652. ((and (looking-at "[ \t]*$")
  7653. (string-match
  7654. "^\\*+$" (buffer-substring
  7655. (point-at-bol) (point))))
  7656. (- (match-end 0) (match-beginning 0)))
  7657. ((and (bolp)
  7658. (looking-at org-outline-regexp))
  7659. (- (match-end 0) (point) 1))))
  7660. (previous-level (save-excursion
  7661. (condition-case nil
  7662. (progn
  7663. (outline-previous-visible-heading 1)
  7664. (if (looking-at ^re_)
  7665. (- (match-end 0) (match-beginning 0) 1)
  7666. 1))
  7667. (error 1))))
  7668. (next-level (save-excursion
  7669. (condition-case nil
  7670. (progn
  7671. (or (looking-at org-outline-regexp)
  7672. (outline-next-visible-heading 1))
  7673. (if (looking-at ^re_)
  7674. (- (match-end 0) (match-beginning 0) 1)
  7675. 1))
  7676. (error 1))))
  7677. (new-level (or force-level (max previous-level next-level)))
  7678. (shift (if (or (= old-level -1)
  7679. (= new-level -1)
  7680. (= old-level new-level))
  7681. 0
  7682. (- new-level old-level)))
  7683. (delta (if (> shift 0) -1 1))
  7684. (func (if (> shift 0) 'org-demote 'org-promote))
  7685. (org-odd-levels-only nil)
  7686. beg end newend)
  7687. ;; Remove the forced level indicator
  7688. (if force-level
  7689. (delete-region (point-at-bol) (point)))
  7690. ;; Paste
  7691. (beginning-of-line (if (bolp) 1 2))
  7692. (setq beg (point))
  7693. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7694. (insert-before-markers txt)
  7695. (unless (string-match "\n\\'" txt) (insert "\n"))
  7696. (setq newend (point))
  7697. (org-reinstall-markers-in-region beg)
  7698. (setq end (point))
  7699. (goto-char beg)
  7700. (skip-chars-forward " \t\n\r")
  7701. (setq beg (point))
  7702. (if (and (outline-invisible-p) visp)
  7703. (save-excursion (outline-show-heading)))
  7704. ;; Shift if necessary
  7705. (unless (= shift 0)
  7706. (save-restriction
  7707. (narrow-to-region beg end)
  7708. (while (not (= shift 0))
  7709. (org-map-region func (point-min) (point-max))
  7710. (setq shift (+ delta shift)))
  7711. (goto-char (point-min))
  7712. (setq newend (point-max))))
  7713. (when (or (org-called-interactively-p 'interactive) for-yank)
  7714. (message "Clipboard pasted as level %d subtree" new-level))
  7715. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7716. kill-ring
  7717. (eq org-subtree-clip (current-kill 0))
  7718. org-subtree-clip-folded)
  7719. ;; The tree was folded before it was killed/copied
  7720. (outline-hide-subtree))
  7721. (and for-yank (goto-char newend))
  7722. (and remove (setq kill-ring (cdr kill-ring))))))
  7723. (defun org-kill-is-subtree-p (&optional txt)
  7724. "Check if the current kill is an outline subtree, or a set of trees.
  7725. Returns nil if kill does not start with a headline, or if the first
  7726. headline level is not the largest headline level in the tree.
  7727. So this will actually accept several entries of equal levels as well,
  7728. which is OK for `org-paste-subtree'.
  7729. If optional TXT is given, check this string instead of the current kill."
  7730. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7731. (re (org-get-limited-outline-regexp))
  7732. (^re (concat "^" re))
  7733. (start-level (and kill
  7734. (string-match
  7735. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7736. kill)
  7737. (- (match-end 2) (match-beginning 2) 1)))
  7738. (start (1+ (or (match-beginning 2) -1))))
  7739. (if (not start-level)
  7740. (progn
  7741. nil) ;; does not even start with a heading
  7742. (catch 'exit
  7743. (while (setq start (string-match ^re kill (1+ start)))
  7744. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7745. (throw 'exit nil)))
  7746. t))))
  7747. (defvar org-markers-to-move nil
  7748. "Markers that should be moved with a cut-and-paste operation.
  7749. Those markers are stored together with their positions relative to
  7750. the start of the region.")
  7751. (defun org-save-markers-in-region (beg end)
  7752. "Check markers in region.
  7753. If these markers are between BEG and END, record their position relative
  7754. to BEG, so that after moving the block of text, we can put the markers back
  7755. into place.
  7756. This function gets called just before an entry or tree gets cut from the
  7757. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7758. called immediately, to move the markers with the entries."
  7759. (setq org-markers-to-move nil)
  7760. (when (featurep 'org-clock)
  7761. (org-clock-save-markers-for-cut-and-paste beg end))
  7762. (when (featurep 'org-agenda)
  7763. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7764. (defun org-check-and-save-marker (marker beg end)
  7765. "Check if MARKER is between BEG and END.
  7766. If yes, remember the marker and the distance to BEG."
  7767. (when (and (marker-buffer marker)
  7768. (equal (marker-buffer marker) (current-buffer)))
  7769. (if (and (>= marker beg) (< marker end))
  7770. (push (cons marker (- marker beg)) org-markers-to-move))))
  7771. (defun org-reinstall-markers-in-region (beg)
  7772. "Move all remembered markers to their position relative to BEG."
  7773. (mapc (lambda (x)
  7774. (move-marker (car x) (+ beg (cdr x))))
  7775. org-markers-to-move)
  7776. (setq org-markers-to-move nil))
  7777. (defun org-narrow-to-subtree ()
  7778. "Narrow buffer to the current subtree."
  7779. (interactive)
  7780. (save-excursion
  7781. (save-match-data
  7782. (org-with-limited-levels
  7783. (narrow-to-region
  7784. (progn (org-back-to-heading t) (point))
  7785. (progn (org-end-of-subtree t t)
  7786. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7787. (point)))))))
  7788. (defun org-narrow-to-block ()
  7789. "Narrow buffer to the current block."
  7790. (interactive)
  7791. (let* ((case-fold-search t)
  7792. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7793. "^[ \t]*#\\+end_.*")))
  7794. (if blockp
  7795. (narrow-to-region (car blockp) (cdr blockp))
  7796. (user-error "Not in a block"))))
  7797. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7798. "Clone the task (subtree) at point N times.
  7799. The clones will be inserted as siblings.
  7800. In interactive use, the user will be prompted for the number of
  7801. clones to be produced. If the entry has a timestamp, the user
  7802. will also be prompted for a time shift, which may be a repeater
  7803. as used in time stamps, for example `+3d'. To disable this,
  7804. you can call the function with a universal prefix argument.
  7805. When a valid repeater is given and the entry contains any time
  7806. stamps, the clones will become a sequence in time, with time
  7807. stamps in the subtree shifted for each clone produced. If SHIFT
  7808. is nil or the empty string, time stamps will be left alone. The
  7809. ID property of the original subtree is removed.
  7810. If the original subtree did contain time stamps with a repeater,
  7811. the following will happen:
  7812. - the repeater will be removed in each clone
  7813. - an additional clone will be produced, with the current, unshifted
  7814. date(s) in the entry.
  7815. - the original entry will be placed *after* all the clones, with
  7816. repeater intact.
  7817. - the start days in the repeater in the original entry will be shifted
  7818. to past the last clone.
  7819. In this way you can spell out a number of instances of a repeating task,
  7820. and still retain the repeater to cover future instances of the task.
  7821. As described above, N+1 clones are produced when the original
  7822. subtree has a repeater. Setting N to 0, then, can be used to
  7823. remove the repeater from a subtree and create a shifted clone
  7824. with the original repeater."
  7825. (interactive "nNumber of clones to produce: ")
  7826. (let ((shift
  7827. (or shift
  7828. (if (and (not (equal current-prefix-arg '(4)))
  7829. (save-excursion
  7830. (re-search-forward org-ts-regexp-both
  7831. (save-excursion
  7832. (org-end-of-subtree t)
  7833. (point)) t)))
  7834. (read-from-minibuffer
  7835. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7836. ""))) ;; No time shift
  7837. (n-no-remove -1)
  7838. (drawer-re org-drawer-regexp)
  7839. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7840. beg end template task idprop
  7841. shift-n shift-what doshift nmin nmax)
  7842. (unless (wholenump n)
  7843. (user-error "Invalid number of replications %s" n))
  7844. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7845. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7846. shift)))
  7847. (user-error "Invalid shift specification %s" shift))
  7848. (when doshift
  7849. (setq shift-n (string-to-number (match-string 1 shift))
  7850. shift-what (cdr (assoc (match-string 2 shift)
  7851. '(("d" . day) ("w" . week)
  7852. ("m" . month) ("y" . year))))))
  7853. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7854. (setq nmin 1 nmax n)
  7855. (org-back-to-heading t)
  7856. (setq beg (point))
  7857. (setq idprop (org-entry-get nil "ID"))
  7858. (org-end-of-subtree t t)
  7859. (or (bolp) (insert "\n"))
  7860. (setq end (point))
  7861. (setq template (buffer-substring beg end))
  7862. (when (and doshift
  7863. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7864. (delete-region beg end)
  7865. (setq end beg)
  7866. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7867. (goto-char end)
  7868. (loop for n from nmin to nmax do
  7869. ;; prepare clone
  7870. (with-temp-buffer
  7871. (insert template)
  7872. (org-mode)
  7873. (goto-char (point-min))
  7874. (org-show-subtree)
  7875. (and idprop (if org-clone-delete-id
  7876. (org-entry-delete nil "ID")
  7877. (org-id-get-create t)))
  7878. (unless (= n 0)
  7879. (while (re-search-forward org-clock-re nil t)
  7880. (kill-whole-line))
  7881. (goto-char (point-min))
  7882. (while (re-search-forward drawer-re nil t)
  7883. (org-remove-empty-drawer-at (point))))
  7884. (goto-char (point-min))
  7885. (when doshift
  7886. (while (re-search-forward org-ts-regexp-both nil t)
  7887. (org-timestamp-change (* n shift-n) shift-what))
  7888. (unless (= n n-no-remove)
  7889. (goto-char (point-min))
  7890. (while (re-search-forward org-ts-regexp nil t)
  7891. (save-excursion
  7892. (goto-char (match-beginning 0))
  7893. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7894. (delete-region (match-beginning 1) (match-end 1)))))))
  7895. (setq task (buffer-string)))
  7896. (insert task))
  7897. (goto-char beg)))
  7898. ;;; Outline Sorting
  7899. (defun org-sort (with-case)
  7900. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7901. Optional argument WITH-CASE means sort case-sensitively."
  7902. (interactive "P")
  7903. (cond
  7904. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7905. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7906. (t
  7907. (org-call-with-arg 'org-sort-entries with-case))))
  7908. (defun org-sort-remove-invisible (s)
  7909. "Remove invisible links from string S."
  7910. (remove-text-properties 0 (length s) org-rm-props s)
  7911. (while (string-match org-bracket-link-regexp s)
  7912. (setq s (replace-match (if (match-end 2)
  7913. (match-string 3 s)
  7914. (match-string 1 s))
  7915. t t s)))
  7916. (let ((st (format " %s " s)))
  7917. (while (string-match org-emph-re st)
  7918. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7919. (setq s (substring st 1 -1)))
  7920. s)
  7921. (defvar org-priority-regexp) ; defined later in the file
  7922. (defvar org-after-sorting-entries-or-items-hook nil
  7923. "Hook that is run after a bunch of entries or items have been sorted.
  7924. When children are sorted, the cursor is in the parent line when this
  7925. hook gets called. When a region or a plain list is sorted, the cursor
  7926. will be in the first entry of the sorted region/list.")
  7927. (defun org-sort-entries
  7928. (&optional with-case sorting-type getkey-func compare-func property)
  7929. "Sort entries on a certain level of an outline tree.
  7930. If there is an active region, the entries in the region are sorted.
  7931. Else, if the cursor is before the first entry, sort the top-level items.
  7932. Else, the children of the entry at point are sorted.
  7933. Sorting can be alphabetically, numerically, by date/time as given by
  7934. a time stamp, by a property, by priority order, or by a custom function.
  7935. The command prompts for the sorting type unless it has been given to the
  7936. function through the SORTING-TYPE argument, which needs to be a character,
  7937. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7938. the precise meaning of each character:
  7939. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7940. c By creation time, which is assumed to be the first inactive time stamp
  7941. at the beginning of a line.
  7942. d By deadline date/time.
  7943. k By clocking time.
  7944. n Numerically, by converting the beginning of the entry/item to a number.
  7945. o By order of TODO keywords.
  7946. p By priority according to the cookie.
  7947. r By the value of a property.
  7948. s By scheduled date/time.
  7949. t By date/time, either the first active time stamp in the entry, or, if
  7950. none exist, by the first inactive one.
  7951. Capital letters will reverse the sort order.
  7952. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7953. called with point at the beginning of the record. It must return either
  7954. a string or a number that should serve as the sorting key for that record.
  7955. Comparing entries ignores case by default. However, with an optional argument
  7956. WITH-CASE, the sorting considers case as well.
  7957. Sorting is done against the visible part of the headlines, it ignores hidden
  7958. links.
  7959. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7960. (interactive "P")
  7961. (let ((case-func (if with-case 'identity 'downcase))
  7962. (cmstr
  7963. ;; The clock marker is lost when using `sort-subr', let's
  7964. ;; store the clocking string.
  7965. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7966. (save-excursion
  7967. (goto-char org-clock-marker)
  7968. (buffer-substring-no-properties (line-beginning-position)
  7969. (point)))))
  7970. start beg end stars re re2
  7971. txt what tmp)
  7972. ;; Find beginning and end of region to sort
  7973. (cond
  7974. ((org-region-active-p)
  7975. ;; we will sort the region
  7976. (setq end (region-end)
  7977. what "region")
  7978. (goto-char (region-beginning))
  7979. (if (not (org-at-heading-p)) (outline-next-heading))
  7980. (setq start (point)))
  7981. ((or (org-at-heading-p)
  7982. (ignore-errors (progn (org-back-to-heading) t)))
  7983. ;; we will sort the children of the current headline
  7984. (org-back-to-heading)
  7985. (setq start (point)
  7986. end (progn (org-end-of-subtree t t)
  7987. (or (bolp) (insert "\n"))
  7988. (when (>= (org-back-over-empty-lines) 1)
  7989. (forward-line 1))
  7990. (point))
  7991. what "children")
  7992. (goto-char start)
  7993. (outline-show-subtree)
  7994. (outline-next-heading))
  7995. (t
  7996. ;; we will sort the top-level entries in this file
  7997. (goto-char (point-min))
  7998. (or (org-at-heading-p) (outline-next-heading))
  7999. (setq start (point))
  8000. (goto-char (point-max))
  8001. (beginning-of-line 1)
  8002. (when (looking-at ".*?\\S-")
  8003. ;; File ends in a non-white line
  8004. (end-of-line 1)
  8005. (insert "\n"))
  8006. (setq end (point-max))
  8007. (setq what "top-level")
  8008. (goto-char start)
  8009. (outline-show-all)))
  8010. (setq beg (point))
  8011. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  8012. (looking-at "\\(\\*+\\)")
  8013. (setq stars (match-string 1)
  8014. re (concat "^" (regexp-quote stars) " +")
  8015. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8016. txt (buffer-substring beg end))
  8017. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8018. (if (and (not (equal stars "*")) (string-match re2 txt))
  8019. (user-error "Region to sort contains a level above the first entry"))
  8020. (unless sorting-type
  8021. (message
  8022. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8023. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8024. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8025. what)
  8026. (setq sorting-type (read-char-exclusive))
  8027. (unless getkey-func
  8028. (and (= (downcase sorting-type) ?f)
  8029. (setq getkey-func
  8030. (org-icompleting-read "Sort using function: "
  8031. obarray 'fboundp t nil nil))
  8032. (setq getkey-func (intern getkey-func))))
  8033. (and (= (downcase sorting-type) ?r)
  8034. (not property)
  8035. (setq property
  8036. (org-icompleting-read "Property: "
  8037. (mapcar 'list (org-buffer-property-keys t))
  8038. nil t))))
  8039. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8040. (message "Sorting entries...")
  8041. (save-restriction
  8042. (narrow-to-region start end)
  8043. (let ((dcst (downcase sorting-type))
  8044. (case-fold-search nil)
  8045. (now (current-time)))
  8046. (sort-subr
  8047. (/= dcst sorting-type)
  8048. ;; This function moves to the beginning character of the "record" to
  8049. ;; be sorted.
  8050. (lambda nil
  8051. (if (re-search-forward re nil t)
  8052. (goto-char (match-beginning 0))
  8053. (goto-char (point-max))))
  8054. ;; This function moves to the last character of the "record" being
  8055. ;; sorted.
  8056. (lambda nil
  8057. (save-match-data
  8058. (condition-case nil
  8059. (outline-forward-same-level 1)
  8060. (error
  8061. (goto-char (point-max))))))
  8062. ;; This function returns the value that gets sorted against.
  8063. (lambda nil
  8064. (cond
  8065. ((= dcst ?n)
  8066. (if (looking-at org-complex-heading-regexp)
  8067. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8068. nil))
  8069. ((= dcst ?a)
  8070. (if (looking-at org-complex-heading-regexp)
  8071. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8072. nil))
  8073. ((= dcst ?k)
  8074. (or (get-text-property (point) :org-clock-minutes) 0))
  8075. ((= dcst ?t)
  8076. (let ((end (save-excursion (outline-next-heading) (point))))
  8077. (if (or (re-search-forward org-ts-regexp end t)
  8078. (re-search-forward org-ts-regexp-both end t))
  8079. (org-time-string-to-seconds (match-string 0))
  8080. (org-float-time now))))
  8081. ((= dcst ?c)
  8082. (let ((end (save-excursion (outline-next-heading) (point))))
  8083. (if (re-search-forward
  8084. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8085. end t)
  8086. (org-time-string-to-seconds (match-string 0))
  8087. (org-float-time now))))
  8088. ((= dcst ?s)
  8089. (let ((end (save-excursion (outline-next-heading) (point))))
  8090. (if (re-search-forward org-scheduled-time-regexp end t)
  8091. (org-time-string-to-seconds (match-string 1))
  8092. (org-float-time now))))
  8093. ((= dcst ?d)
  8094. (let ((end (save-excursion (outline-next-heading) (point))))
  8095. (if (re-search-forward org-deadline-time-regexp end t)
  8096. (org-time-string-to-seconds (match-string 1))
  8097. (org-float-time now))))
  8098. ((= dcst ?p)
  8099. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8100. (string-to-char (match-string 2))
  8101. org-default-priority))
  8102. ((= dcst ?r)
  8103. (or (org-entry-get nil property) ""))
  8104. ((= dcst ?o)
  8105. (if (looking-at org-complex-heading-regexp)
  8106. (let* ((m (match-string 2))
  8107. (s (if (member m org-done-keywords) '- '+)))
  8108. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8109. ((= dcst ?f)
  8110. (if getkey-func
  8111. (progn
  8112. (setq tmp (funcall getkey-func))
  8113. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8114. tmp)
  8115. (error "Invalid key function `%s'" getkey-func)))
  8116. (t (error "Invalid sorting type `%c'" sorting-type))))
  8117. nil
  8118. (cond
  8119. ((= dcst ?a) 'string<)
  8120. ((= dcst ?f) compare-func)
  8121. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8122. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8123. ;; Reset the clock marker if needed
  8124. (when cmstr
  8125. (save-excursion
  8126. (goto-char start)
  8127. (search-forward cmstr nil t)
  8128. (move-marker org-clock-marker (point))))
  8129. (message "Sorting entries...done")))
  8130. ;;; The orgstruct minor mode
  8131. ;; Define a minor mode which can be used in other modes in order to
  8132. ;; integrate the org-mode structure editing commands.
  8133. ;; This is really a hack, because the org-mode structure commands use
  8134. ;; keys which normally belong to the major mode. Here is how it
  8135. ;; works: The minor mode defines all the keys necessary to operate the
  8136. ;; structure commands, but wraps the commands into a function which
  8137. ;; tests if the cursor is currently at a headline or a plain list
  8138. ;; item. If that is the case, the structure command is used,
  8139. ;; temporarily setting many Org-mode variables like regular
  8140. ;; expressions for filling etc. However, when any of those keys is
  8141. ;; used at a different location, function uses `key-binding' to look
  8142. ;; up if the key has an associated command in another currently active
  8143. ;; keymap (minor modes, major mode, global), and executes that
  8144. ;; command. There might be problems if any of the keys is otherwise
  8145. ;; used as a prefix key.
  8146. (defcustom orgstruct-heading-prefix-regexp ""
  8147. "Regexp that matches the custom prefix of Org headlines in
  8148. orgstruct(++)-mode."
  8149. :group 'org
  8150. :version "24.4"
  8151. :package-version '(Org . "8.3")
  8152. :type 'regexp)
  8153. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8154. (defcustom orgstruct-setup-hook nil
  8155. "Hook run after orgstruct-mode-map is filled."
  8156. :group 'org
  8157. :version "24.4"
  8158. :package-version '(Org . "8.0")
  8159. :type 'hook)
  8160. (defvar orgstruct-initialized nil)
  8161. (defvar org-local-vars nil
  8162. "List of local variables, for use by `orgstruct-mode'.")
  8163. ;;;###autoload
  8164. (define-minor-mode orgstruct-mode
  8165. "Toggle the minor mode `orgstruct-mode'.
  8166. This mode is for using Org-mode structure commands in other
  8167. modes. The following keys behave as if Org-mode were active, if
  8168. the cursor is on a headline, or on a plain list item (both as
  8169. defined by Org-mode)."
  8170. nil " OrgStruct" (make-sparse-keymap)
  8171. (funcall (if orgstruct-mode
  8172. 'add-to-invisibility-spec
  8173. 'remove-from-invisibility-spec)
  8174. '(outline . t))
  8175. (when orgstruct-mode
  8176. (org-load-modules-maybe)
  8177. (unless orgstruct-initialized
  8178. (orgstruct-setup)
  8179. (setq orgstruct-initialized t))))
  8180. ;;;###autoload
  8181. (defun turn-on-orgstruct ()
  8182. "Unconditionally turn on `orgstruct-mode'."
  8183. (orgstruct-mode 1))
  8184. (defvar org-fb-vars nil)
  8185. (make-variable-buffer-local 'org-fb-vars)
  8186. (defun orgstruct++-mode (&optional arg)
  8187. "Toggle `orgstruct-mode', the enhanced version of it.
  8188. In addition to setting orgstruct-mode, this also exports all
  8189. indentation and autofilling variables from org-mode into the
  8190. buffer. It will also recognize item context in multiline items."
  8191. (interactive "P")
  8192. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8193. (if (< arg 1)
  8194. (progn (orgstruct-mode -1)
  8195. (mapc (lambda(v)
  8196. (org-set-local (car v)
  8197. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8198. org-fb-vars))
  8199. (orgstruct-mode 1)
  8200. (setq org-fb-vars nil)
  8201. (unless org-local-vars
  8202. (setq org-local-vars (org-get-local-variables)))
  8203. (let (var val)
  8204. (mapc
  8205. (lambda (x)
  8206. (when (string-match
  8207. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8208. (symbol-name (car x)))
  8209. (setq var (car x) val (nth 1 x))
  8210. (push (list var `(quote ,(eval var))) org-fb-vars)
  8211. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8212. org-local-vars)
  8213. (org-set-local 'orgstruct-is-++ t))))
  8214. (defvar orgstruct-is-++ nil
  8215. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8216. (make-variable-buffer-local 'orgstruct-is-++)
  8217. ;;;###autoload
  8218. (defun turn-on-orgstruct++ ()
  8219. "Unconditionally turn on `orgstruct++-mode'."
  8220. (orgstruct++-mode 1))
  8221. (defun orgstruct-error ()
  8222. "Error when there is no default binding for a structure key."
  8223. (interactive)
  8224. (funcall (if (fboundp 'user-error)
  8225. 'user-error
  8226. 'error)
  8227. "This key has no function outside structure elements"))
  8228. (defun orgstruct-setup ()
  8229. "Setup orgstruct keymap."
  8230. (dolist (cell '((org-demote . t)
  8231. (org-metaleft . t)
  8232. (org-metaright . t)
  8233. (org-promote . t)
  8234. (org-shiftmetaleft . t)
  8235. (org-shiftmetaright . t)
  8236. org-backward-element
  8237. org-backward-heading-same-level
  8238. org-ctrl-c-ret
  8239. org-ctrl-c-minus
  8240. org-ctrl-c-star
  8241. org-cycle
  8242. org-forward-heading-same-level
  8243. org-insert-heading
  8244. org-insert-heading-respect-content
  8245. org-kill-note-or-show-branches
  8246. org-mark-subtree
  8247. org-meta-return
  8248. org-metadown
  8249. org-metaup
  8250. org-narrow-to-subtree
  8251. org-promote-subtree
  8252. org-reveal
  8253. org-shiftdown
  8254. org-shiftleft
  8255. org-shiftmetadown
  8256. org-shiftmetaup
  8257. org-shiftright
  8258. org-shifttab
  8259. org-shifttab
  8260. org-shiftup
  8261. org-show-subtree
  8262. org-sort
  8263. org-up-element
  8264. outline-demote
  8265. outline-next-visible-heading
  8266. outline-previous-visible-heading
  8267. outline-promote
  8268. outline-up-heading
  8269. outline-show-children))
  8270. (let ((f (or (car-safe cell) cell))
  8271. (disable-when-heading-prefix (cdr-safe cell)))
  8272. (when (fboundp f)
  8273. (let ((new-bindings))
  8274. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8275. (where-is-internal f outline-mode-map)))
  8276. (push binding new-bindings)
  8277. ;; TODO use local-function-key-map
  8278. (dolist (rep '(("<tab>" . "TAB")
  8279. ("<return>" . "RET")
  8280. ("<escape>" . "ESC")
  8281. ("<delete>" . "DEL")))
  8282. (setq binding (read-kbd-macro
  8283. (let ((case-fold-search))
  8284. (replace-regexp-in-string
  8285. (regexp-quote (cdr rep))
  8286. (car rep)
  8287. (key-description binding)))))
  8288. (pushnew binding new-bindings :test 'equal)))
  8289. (dolist (binding new-bindings)
  8290. (let ((key (lookup-key orgstruct-mode-map binding)))
  8291. (when (or (not key) (numberp key))
  8292. (ignore-errors
  8293. (org-defkey orgstruct-mode-map
  8294. binding
  8295. (orgstruct-make-binding
  8296. f binding disable-when-heading-prefix))))))))))
  8297. (run-hooks 'orgstruct-setup-hook))
  8298. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8299. "Create a function for binding in the structure minor mode.
  8300. FUN is the command to call inside a table. KEY is the key that
  8301. should be checked in for a command to execute outside of tables.
  8302. Non-nil `disable-when-heading-prefix' means to disable the command
  8303. if `orgstruct-heading-prefix-regexp' is not empty."
  8304. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8305. (let ((nname name)
  8306. (i 0))
  8307. (while (fboundp (intern nname))
  8308. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8309. (setq name (intern nname)))
  8310. (eval
  8311. (let ((bindings '((org-heading-regexp
  8312. (concat "^"
  8313. orgstruct-heading-prefix-regexp
  8314. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8315. (org-outline-regexp
  8316. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8317. (org-outline-regexp-bol
  8318. (concat "^" org-outline-regexp))
  8319. (outline-regexp org-outline-regexp)
  8320. (outline-heading-end-regexp "\n")
  8321. (outline-level 'org-outline-level)
  8322. (outline-heading-alist))))
  8323. `(defun ,name (arg)
  8324. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8325. "Outside of structure, run the binding of `"
  8326. (key-description key) "'."
  8327. (when disable-when-heading-prefix
  8328. (concat
  8329. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8330. "back to the default binding due to limitations of Org's implementation of\n"
  8331. "`" (symbol-name fun) "'.")))
  8332. (interactive "p")
  8333. (let* ((disable
  8334. ,(and disable-when-heading-prefix
  8335. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8336. (fallback
  8337. (or disable
  8338. (not
  8339. (let* ,bindings
  8340. (org-context-p 'headline 'item
  8341. ,(when (memq fun
  8342. '(org-insert-heading
  8343. org-insert-heading-respect-content
  8344. org-meta-return))
  8345. '(when orgstruct-is-++
  8346. 'item-body))))))))
  8347. (if fallback
  8348. (let* ((orgstruct-mode)
  8349. (binding
  8350. (let ((key ,key))
  8351. (catch 'exit
  8352. (dolist
  8353. (rep
  8354. '(nil
  8355. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8356. ("<\\([^>]*\\)return>" . "\\1RET")
  8357. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8358. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8359. nil)
  8360. (when rep
  8361. (setq key (read-kbd-macro
  8362. (let ((case-fold-search))
  8363. (replace-regexp-in-string
  8364. (car rep)
  8365. (cdr rep)
  8366. (key-description key))))))
  8367. (when (key-binding key)
  8368. (throw 'exit (key-binding key))))))))
  8369. (if (keymapp binding)
  8370. (org-set-transient-map binding)
  8371. (let ((func (or binding
  8372. (unless disable
  8373. 'orgstruct-error))))
  8374. (when func
  8375. (call-interactively func)))))
  8376. (org-run-like-in-org-mode
  8377. (lambda ()
  8378. (interactive)
  8379. (let* ,bindings
  8380. (call-interactively ',fun)))))))))
  8381. name))
  8382. (defun org-contextualize-keys (alist contexts)
  8383. "Return valid elements in ALIST depending on CONTEXTS.
  8384. `org-agenda-custom-commands' or `org-capture-templates' are the
  8385. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8386. or `org-capture-templates-contexts' are the associated contexts
  8387. definitions."
  8388. (let ((contexts
  8389. ;; normalize contexts
  8390. (mapcar
  8391. (lambda(c) (cond ((listp (cadr c))
  8392. (list (car c) (car c) (cadr c)))
  8393. ((string= "" (cadr c))
  8394. (list (car c) (car c) (caddr c)))
  8395. (t c)))
  8396. contexts))
  8397. (a alist) r s)
  8398. ;; loop over all commands or templates
  8399. (dolist (c a)
  8400. (let (vrules repl)
  8401. (cond
  8402. ((not (assoc (car c) contexts))
  8403. (push c r))
  8404. ((and (assoc (car c) contexts)
  8405. (setq vrules (org-contextualize-validate-key
  8406. (car c) contexts)))
  8407. (mapc (lambda (vr)
  8408. (when (not (equal (car vr) (cadr vr)))
  8409. (setq repl vr)))
  8410. vrules)
  8411. (if (not repl) (push c r)
  8412. (push (cadr repl) s)
  8413. (push
  8414. (cons (car c)
  8415. (cdr (or (assoc (cadr repl) alist)
  8416. (error "Undefined key `%s' as contextual replacement for `%s'"
  8417. (cadr repl) (car c)))))
  8418. r))))))
  8419. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8420. (delq
  8421. nil
  8422. (delete-dups
  8423. (mapcar (lambda (x)
  8424. (let ((tpl (car x)))
  8425. (when (not (delq
  8426. nil
  8427. (mapcar (lambda (y)
  8428. (equal y tpl))
  8429. s)))
  8430. x)))
  8431. (reverse r))))))
  8432. (defun org-contextualize-validate-key (key contexts)
  8433. "Check CONTEXTS for agenda or capture KEY."
  8434. (let (rr res)
  8435. (dolist (r contexts)
  8436. (mapc
  8437. (lambda (rr)
  8438. (when
  8439. (and (equal key (car r))
  8440. (if (functionp rr) (funcall rr)
  8441. (or (and (eq (car rr) 'in-file)
  8442. (buffer-file-name)
  8443. (string-match (cdr rr) (buffer-file-name)))
  8444. (and (eq (car rr) 'in-mode)
  8445. (string-match (cdr rr) (symbol-name major-mode)))
  8446. (and (eq (car rr) 'in-buffer)
  8447. (string-match (cdr rr) (buffer-name)))
  8448. (when (and (eq (car rr) 'not-in-file)
  8449. (buffer-file-name))
  8450. (not (string-match (cdr rr) (buffer-file-name))))
  8451. (when (eq (car rr) 'not-in-mode)
  8452. (not (string-match (cdr rr) (symbol-name major-mode))))
  8453. (when (eq (car rr) 'not-in-buffer)
  8454. (not (string-match (cdr rr) (buffer-name)))))))
  8455. (push r res)))
  8456. (car (last r))))
  8457. (delete-dups (delq nil res))))
  8458. (defun org-context-p (&rest contexts)
  8459. "Check if local context is any of CONTEXTS.
  8460. Possible values in the list of contexts are `table', `headline', and `item'."
  8461. (let ((pos (point)))
  8462. (goto-char (point-at-bol))
  8463. (prog1 (or (and (memq 'table contexts)
  8464. (looking-at "[ \t]*|"))
  8465. (and (memq 'headline contexts)
  8466. (looking-at org-outline-regexp))
  8467. (and (memq 'item contexts)
  8468. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8469. (and (memq 'item-body contexts)
  8470. (org-in-item-p)))
  8471. (goto-char pos))))
  8472. (defun org-get-local-variables ()
  8473. "Return a list of all local variables in an Org mode buffer."
  8474. (let (varlist)
  8475. (with-current-buffer (get-buffer-create "*Org tmp*")
  8476. (erase-buffer)
  8477. (org-mode)
  8478. (setq varlist (buffer-local-variables)))
  8479. (kill-buffer "*Org tmp*")
  8480. (delq nil
  8481. (mapcar
  8482. (lambda (x)
  8483. (setq x
  8484. (if (symbolp x)
  8485. (list x)
  8486. (list (car x) (cdr x))))
  8487. (if (and (not (get (car x) 'org-state))
  8488. (string-match
  8489. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8490. (symbol-name (car x))))
  8491. x nil))
  8492. varlist))))
  8493. (defun org-clone-local-variables (from-buffer &optional regexp)
  8494. "Clone local variables from FROM-BUFFER.
  8495. Optional argument REGEXP selects variables to clone."
  8496. (mapc
  8497. (lambda (pair)
  8498. (and (symbolp (car pair))
  8499. (or (null regexp)
  8500. (string-match regexp (symbol-name (car pair))))
  8501. (set (make-local-variable (car pair))
  8502. (cdr pair))))
  8503. (buffer-local-variables from-buffer)))
  8504. ;;;###autoload
  8505. (defun org-run-like-in-org-mode (cmd)
  8506. "Run a command, pretending that the current buffer is in Org-mode.
  8507. This will temporarily bind local variables that are typically bound in
  8508. Org-mode to the values they have in Org-mode, and then interactively
  8509. call CMD."
  8510. (org-load-modules-maybe)
  8511. (unless org-local-vars
  8512. (setq org-local-vars (org-get-local-variables)))
  8513. (let (binds)
  8514. (dolist (var org-local-vars)
  8515. (when (or (not (boundp (car var)))
  8516. (eq (symbol-value (car var))
  8517. (default-value (car var))))
  8518. (push (list (car var) `(quote ,(cadr var))) binds)))
  8519. (eval `(let ,binds
  8520. (call-interactively (quote ,cmd))))))
  8521. (defun org-get-category (&optional pos force-refresh)
  8522. "Get the category applying to position POS."
  8523. (save-match-data
  8524. (if force-refresh (org-refresh-category-properties))
  8525. (let ((pos (or pos (point))))
  8526. (or (get-text-property pos 'org-category)
  8527. (progn (org-refresh-category-properties)
  8528. (get-text-property pos 'org-category))))))
  8529. ;;; Refresh properties
  8530. (defun org-refresh-properties (dprop tprop)
  8531. "Refresh buffer text properties.
  8532. DPROP is the drawer property and TPROP is either the
  8533. corresponding text property to set, or an alist with each element
  8534. being a text property (as a symbol) and a function to apply to
  8535. the value of the drawer property."
  8536. (let ((case-fold-search t)
  8537. (inhibit-read-only t))
  8538. (org-with-silent-modifications
  8539. (save-excursion
  8540. (save-restriction
  8541. (widen)
  8542. (goto-char (point-min))
  8543. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8544. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8545. (defun org-refresh-property (tprop p)
  8546. "Refresh the buffer text property TPROP from the drawer property P.
  8547. The refresh happens only for the current tree (not subtree)."
  8548. (unless (org-before-first-heading-p)
  8549. (save-excursion
  8550. (org-back-to-heading t)
  8551. (if (symbolp tprop)
  8552. ;; TPROP is a text property symbol
  8553. (put-text-property
  8554. (point) (or (outline-next-heading) (point-max)) tprop p)
  8555. ;; TPROP is an alist with (properties . function) elements
  8556. (dolist (al tprop)
  8557. (save-excursion
  8558. (put-text-property
  8559. (line-beginning-position) (or (outline-next-heading) (point-max))
  8560. (car al)
  8561. (funcall (cdr al) p))))))))
  8562. (defun org-refresh-category-properties ()
  8563. "Refresh category text properties in the buffer."
  8564. (let ((case-fold-search t)
  8565. (inhibit-read-only t)
  8566. (default-category
  8567. (cond ((null org-category)
  8568. (if buffer-file-name
  8569. (file-name-sans-extension
  8570. (file-name-nondirectory buffer-file-name))
  8571. "???"))
  8572. ((symbolp org-category) (symbol-name org-category))
  8573. (t org-category))))
  8574. (org-with-silent-modifications
  8575. (org-with-wide-buffer
  8576. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8577. ;; This is the default category for the buffer. If none is
  8578. ;; found, fall-back to `org-category' or buffer file name.
  8579. (put-text-property
  8580. (point-min) (point-max)
  8581. 'org-category
  8582. (catch 'buffer-category
  8583. (goto-char (point-max))
  8584. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8585. (let ((element (org-element-at-point)))
  8586. (when (eq (org-element-type element) 'keyword)
  8587. (throw 'buffer-category
  8588. (org-element-property :value element)))))
  8589. default-category))
  8590. ;; Set sub-tree specific categories.
  8591. (goto-char (point-min))
  8592. (let ((regexp (org-re-property "CATEGORY")))
  8593. (while (re-search-forward regexp nil t)
  8594. (let ((value (org-match-string-no-properties 3)))
  8595. (when (org-at-property-p)
  8596. (put-text-property
  8597. (save-excursion (org-back-to-heading t) (point))
  8598. (save-excursion (org-end-of-subtree t t) (point))
  8599. 'org-category
  8600. value)))))))))
  8601. (defun org-refresh-stats-properties ()
  8602. "Refresh stats text properties in the buffer."
  8603. (let (stats)
  8604. (org-with-silent-modifications
  8605. (save-excursion
  8606. (save-restriction
  8607. (widen)
  8608. (goto-char (point-min))
  8609. (while (re-search-forward
  8610. (concat org-outline-regexp-bol ".*"
  8611. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8612. nil t)
  8613. (setq stats (cond ((equal (match-string 3) "0") 0)
  8614. ((match-string 2)
  8615. (/ (* (string-to-number (match-string 2)) 100)
  8616. (string-to-number (match-string 3))))
  8617. (t (string-to-number (match-string 1)))))
  8618. (org-back-to-heading t)
  8619. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8620. 'org-stats stats)))))))
  8621. (defun org-refresh-effort-properties ()
  8622. "Refresh effort properties"
  8623. (org-refresh-properties
  8624. org-effort-property
  8625. '((effort . identity)
  8626. (effort-minutes . org-duration-string-to-minutes))))
  8627. ;;;; Link Stuff
  8628. ;;; Link abbreviations
  8629. (defun org-link-expand-abbrev (link)
  8630. "Apply replacements as defined in `org-link-abbrev-alist'."
  8631. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8632. (let* ((key (match-string 1 link))
  8633. (as (or (assoc key org-link-abbrev-alist-local)
  8634. (assoc key org-link-abbrev-alist)))
  8635. (tag (and (match-end 2) (match-string 3 link)))
  8636. rpl)
  8637. (if (not as)
  8638. link
  8639. (setq rpl (cdr as))
  8640. (cond
  8641. ((symbolp rpl) (funcall rpl tag))
  8642. ((string-match "%(\\([^)]+\\))" rpl)
  8643. (replace-match
  8644. (save-match-data
  8645. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8646. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8647. ((string-match "%h" rpl)
  8648. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8649. (t (concat rpl tag)))))
  8650. link))
  8651. ;;; Storing and inserting links
  8652. (defvar org-insert-link-history nil
  8653. "Minibuffer history for links inserted with `org-insert-link'.")
  8654. (defvar org-stored-links nil
  8655. "Contains the links stored with `org-store-link'.")
  8656. (defvar org-store-link-plist nil
  8657. "Plist with info about the most recently link created with `org-store-link'.")
  8658. (defvar org-link-protocols nil
  8659. "Link protocols added to Org-mode using `org-add-link-type'.")
  8660. (defvar org-store-link-functions nil
  8661. "List of functions that are called to create and store a link.
  8662. Each function will be called in turn until one returns a non-nil
  8663. value. Each function should check if it is responsible for creating
  8664. this link (for example by looking at the major mode).
  8665. If not, it must exit and return nil.
  8666. If yes, it should return a non-nil value after a calling
  8667. `org-store-link-props' with a list of properties and values.
  8668. Special properties are:
  8669. :type The link prefix, like \"http\". This must be given.
  8670. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8671. This is obligatory as well.
  8672. :description Optional default description for the second pair
  8673. of brackets in an Org-mode link. The user can still change
  8674. this when inserting this link into an Org-mode buffer.
  8675. In addition to these, any additional properties can be specified
  8676. and then used in capture templates.")
  8677. (defun org-add-link-type (type &optional follow export)
  8678. "Add TYPE to the list of `org-link-types'.
  8679. Re-compute all regular expressions depending on `org-link-types'
  8680. FOLLOW and EXPORT are two functions.
  8681. FOLLOW should take the link path as the single argument and do whatever
  8682. is necessary to follow the link, for example find a file or display
  8683. a mail message.
  8684. EXPORT should format the link path for export to one of the export formats.
  8685. It should be a function accepting three arguments:
  8686. path the path of the link, the text after the prefix (like \"http:\")
  8687. desc the description of the link, if any
  8688. format the export format, a symbol like `html' or `latex' or `ascii'.
  8689. The function may use the FORMAT information to return different values
  8690. depending on the format. The return value will be put literally into
  8691. the exported file. If the return value is nil, this means Org should
  8692. do what it normally does with links which do not have EXPORT defined.
  8693. Org mode has a built-in default for exporting links. If you are happy with
  8694. this default, there is no need to define an export function for the link
  8695. type. For a simple example of an export function, see `org-bbdb.el'."
  8696. (add-to-list 'org-link-types type t)
  8697. (org-make-link-regexps)
  8698. (org-element-update-syntax)
  8699. (if (assoc type org-link-protocols)
  8700. (setcdr (assoc type org-link-protocols) (list follow export))
  8701. (push (list type follow export) org-link-protocols)))
  8702. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8703. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8704. ;;;###autoload
  8705. (defun org-store-link (arg)
  8706. "\\<org-mode-map>Store an org-link to the current location.
  8707. This link is added to `org-stored-links' and can later be inserted
  8708. into an Org buffer with \\[org-insert-link].
  8709. For some link types, a prefix ARG is interpreted.
  8710. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8711. For file links, ARG negates `org-context-in-file-links'.
  8712. A double prefix ARG force skipping storing functions that are not
  8713. part of Org's core.
  8714. A triple prefix ARG force storing a link for each line in the
  8715. active region."
  8716. (interactive "P")
  8717. (org-load-modules-maybe)
  8718. (if (and (equal arg '(64)) (org-region-active-p))
  8719. (save-excursion
  8720. (let ((end (region-end)))
  8721. (goto-char (region-beginning))
  8722. (set-mark (point))
  8723. (while (< (point-at-eol) end)
  8724. (move-end-of-line 1) (activate-mark)
  8725. (let (current-prefix-arg)
  8726. (call-interactively 'org-store-link))
  8727. (move-beginning-of-line 2)
  8728. (set-mark (point)))))
  8729. (org-with-limited-levels
  8730. (setq org-store-link-plist nil)
  8731. (let (link cpltxt desc description search
  8732. txt custom-id agenda-link sfuns sfunsn)
  8733. (cond
  8734. ;; Store a link using an external link type
  8735. ((and (not (equal arg '(16)))
  8736. (setq sfuns
  8737. (delq
  8738. nil (mapcar (lambda (f)
  8739. (let (fs) (if (funcall f) (push f fs))))
  8740. org-store-link-functions))
  8741. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8742. (or (and (cdr sfuns)
  8743. (funcall (intern
  8744. (completing-read
  8745. "Which function for creating the link? "
  8746. sfunsn nil t (car sfunsn)))))
  8747. (funcall (caar sfuns)))
  8748. (setq link (plist-get org-store-link-plist :link)
  8749. desc (or (plist-get org-store-link-plist
  8750. :description)
  8751. link))))
  8752. ;; Store a link from a source code buffer.
  8753. ((org-src-edit-buffer-p)
  8754. (cond
  8755. ((save-excursion
  8756. (beginning-of-line)
  8757. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8758. "[ \t]*$")))
  8759. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8760. ((org-called-interactively-p 'any)
  8761. (let (label)
  8762. (while (or (not label)
  8763. (org-with-wide-buffer
  8764. (goto-char (point-min))
  8765. (re-search-forward
  8766. (regexp-quote (format org-coderef-label-format label))
  8767. nil t)))
  8768. (when label (message "Label exists already") (sit-for 2))
  8769. (setq label (read-string "Code line label: " label)))
  8770. (end-of-line)
  8771. (setq link (format org-coderef-label-format label))
  8772. (let ((gc (- 79 (length link))))
  8773. (if (< (current-column) gc) (org-move-to-column gc t)
  8774. (insert " ")))
  8775. (insert link)
  8776. (setq link (concat "(" label ")") desc nil)))
  8777. (t (setq link nil))))
  8778. ;; We are in the agenda, link to referenced location
  8779. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8780. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8781. (get-text-property (point) 'org-marker))))
  8782. (when m
  8783. (org-with-point-at m
  8784. (setq agenda-link
  8785. (if (org-called-interactively-p 'any)
  8786. (call-interactively 'org-store-link)
  8787. (org-store-link nil)))))))
  8788. ((eq major-mode 'calendar-mode)
  8789. (let ((cd (calendar-cursor-to-date)))
  8790. (setq link
  8791. (format-time-string
  8792. (car org-time-stamp-formats)
  8793. (apply 'encode-time
  8794. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8795. nil nil nil))))
  8796. (org-store-link-props :type "calendar" :date cd)))
  8797. ((eq major-mode 'help-mode)
  8798. (setq link (concat "help:" (save-excursion
  8799. (goto-char (point-min))
  8800. (looking-at "^[^ ]+")
  8801. (match-string 0))))
  8802. (org-store-link-props :type "help"))
  8803. ((eq major-mode 'w3-mode)
  8804. (setq cpltxt (if (and (buffer-name)
  8805. (not (string-match "Untitled" (buffer-name))))
  8806. (buffer-name)
  8807. (url-view-url t))
  8808. link (url-view-url t))
  8809. (org-store-link-props :type "w3" :url (url-view-url t)))
  8810. ((eq major-mode 'image-mode)
  8811. (setq cpltxt (concat "file:"
  8812. (abbreviate-file-name buffer-file-name))
  8813. link cpltxt)
  8814. (org-store-link-props :type "image" :file buffer-file-name))
  8815. ;; In dired, store a link to the file of the current line
  8816. ((derived-mode-p 'dired-mode)
  8817. (let ((file (dired-get-filename nil t)))
  8818. (setq file (if file
  8819. (abbreviate-file-name
  8820. (expand-file-name (dired-get-filename nil t)))
  8821. ;; otherwise, no file so use current directory.
  8822. default-directory))
  8823. (setq cpltxt (concat "file:" file)
  8824. link cpltxt)))
  8825. ((setq search (run-hook-with-args-until-success
  8826. 'org-create-file-search-functions))
  8827. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8828. "::" search))
  8829. (setq cpltxt (or description link)))
  8830. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8831. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8832. (cond
  8833. ;; Store a link using the target at point
  8834. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8835. (setq cpltxt
  8836. (concat "file:"
  8837. (abbreviate-file-name
  8838. (buffer-file-name (buffer-base-buffer)))
  8839. "::" (match-string 1))
  8840. link cpltxt))
  8841. ((and (featurep 'org-id)
  8842. (or (eq org-id-link-to-org-use-id t)
  8843. (and (org-called-interactively-p 'any)
  8844. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8845. (and (eq org-id-link-to-org-use-id
  8846. 'create-if-interactive-and-no-custom-id)
  8847. (not custom-id))))
  8848. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8849. ;; Store a link using the ID at point
  8850. (setq link (condition-case nil
  8851. (prog1 (org-id-store-link)
  8852. (setq desc (or (plist-get org-store-link-plist
  8853. :description)
  8854. "")))
  8855. (error
  8856. ;; Probably before first headline, link only to file
  8857. (concat "file:"
  8858. (abbreviate-file-name
  8859. (buffer-file-name (buffer-base-buffer))))))))
  8860. (t
  8861. ;; Just link to current headline
  8862. (setq cpltxt (concat "file:"
  8863. (abbreviate-file-name
  8864. (buffer-file-name (buffer-base-buffer)))))
  8865. ;; Add a context search string
  8866. (when (org-xor org-context-in-file-links arg)
  8867. (let* ((ee (org-element-at-point))
  8868. (et (org-element-type ee))
  8869. (ev (plist-get (cadr ee) :value))
  8870. (ek (plist-get (cadr ee) :key))
  8871. (eok (and (stringp ek) (string-match "name" ek))))
  8872. (setq txt (cond
  8873. ((org-at-heading-p) nil)
  8874. ((and (eq et 'keyword) eok) ev)
  8875. ((org-region-active-p)
  8876. (buffer-substring (region-beginning) (region-end)))))
  8877. (when (or (null txt) (string-match "\\S-" txt))
  8878. (setq cpltxt
  8879. (concat cpltxt "::"
  8880. (condition-case nil
  8881. (org-make-org-heading-search-string txt)
  8882. (error "")))
  8883. desc (or (and (eq et 'keyword) eok ev)
  8884. (nth 4 (ignore-errors (org-heading-components)))
  8885. "NONE")))))
  8886. (if (string-match "::\\'" cpltxt)
  8887. (setq cpltxt (substring cpltxt 0 -2)))
  8888. (setq link cpltxt))))
  8889. ((buffer-file-name (buffer-base-buffer))
  8890. ;; Just link to this file here.
  8891. (setq cpltxt (concat "file:"
  8892. (abbreviate-file-name
  8893. (buffer-file-name (buffer-base-buffer)))))
  8894. ;; Add a context string.
  8895. (when (org-xor org-context-in-file-links arg)
  8896. (setq txt (if (org-region-active-p)
  8897. (buffer-substring (region-beginning) (region-end))
  8898. (buffer-substring (point-at-bol) (point-at-eol))))
  8899. ;; Only use search option if there is some text.
  8900. (when (string-match "\\S-" txt)
  8901. (setq cpltxt
  8902. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8903. desc "NONE")))
  8904. (setq link cpltxt))
  8905. ((org-called-interactively-p 'interactive)
  8906. (user-error "No method for storing a link from this buffer"))
  8907. (t (setq link nil)))
  8908. ;; We're done setting link and desc, clean up
  8909. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8910. (setq link (or link cpltxt)
  8911. desc (or desc cpltxt))
  8912. (cond ((equal desc "NONE") (setq desc nil))
  8913. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8914. (let ((d0 (match-string 3 desc))
  8915. (p0 (match-string 5 desc)))
  8916. (setq desc
  8917. (replace-regexp-in-string
  8918. org-bracket-link-regexp
  8919. (concat (or p0 d0)
  8920. (if (equal (length (match-string 0 desc))
  8921. (length desc)) "*" "")) desc)))))
  8922. ;; Return the link
  8923. (if (not (and (or (org-called-interactively-p 'any)
  8924. executing-kbd-macro)
  8925. link))
  8926. (or agenda-link (and link (org-make-link-string link desc)))
  8927. (push (list link desc) org-stored-links)
  8928. (message "Stored: %s" (or desc link))
  8929. (when custom-id
  8930. (setq link (concat "file:" (abbreviate-file-name
  8931. (buffer-file-name)) "::#" custom-id))
  8932. (push (list link desc) org-stored-links))
  8933. (car org-stored-links))))))
  8934. (defun org-store-link-props (&rest plist)
  8935. "Store link properties, extract names and addresses."
  8936. (let (x adr)
  8937. (when (setq x (plist-get plist :from))
  8938. (setq adr (mail-extract-address-components x))
  8939. (setq plist (plist-put plist :fromname (car adr)))
  8940. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8941. (when (setq x (plist-get plist :to))
  8942. (setq adr (mail-extract-address-components x))
  8943. (setq plist (plist-put plist :toname (car adr)))
  8944. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8945. (let ((from (plist-get plist :from))
  8946. (to (plist-get plist :to)))
  8947. (when (and from to org-from-is-user-regexp)
  8948. (setq plist
  8949. (plist-put plist :fromto
  8950. (if (string-match org-from-is-user-regexp from)
  8951. (concat "to %t")
  8952. (concat "from %f"))))))
  8953. (setq org-store-link-plist plist))
  8954. (defun org-add-link-props (&rest plist)
  8955. "Add these properties to the link property list."
  8956. (let (key value)
  8957. (while plist
  8958. (setq key (pop plist) value (pop plist))
  8959. (setq org-store-link-plist
  8960. (plist-put org-store-link-plist key value)))))
  8961. (defun org-email-link-description (&optional fmt)
  8962. "Return the description part of an email link.
  8963. This takes information from `org-store-link-plist' and formats it
  8964. according to FMT (default from `org-email-link-description-format')."
  8965. (setq fmt (or fmt org-email-link-description-format))
  8966. (let* ((p org-store-link-plist)
  8967. (to (plist-get p :toaddress))
  8968. (from (plist-get p :fromaddress))
  8969. (table
  8970. (list
  8971. (cons "%c" (plist-get p :fromto))
  8972. (cons "%F" (plist-get p :from))
  8973. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8974. (cons "%T" (plist-get p :to))
  8975. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8976. (cons "%s" (plist-get p :subject))
  8977. (cons "%d" (plist-get p :date))
  8978. (cons "%m" (plist-get p :message-id)))))
  8979. (when (string-match "%c" fmt)
  8980. ;; Check if the user wrote this message
  8981. (if (and org-from-is-user-regexp from to
  8982. (save-match-data (string-match org-from-is-user-regexp from)))
  8983. (setq fmt (replace-match "to %t" t t fmt))
  8984. (setq fmt (replace-match "from %f" t t fmt))))
  8985. (org-replace-escapes fmt table)))
  8986. (defun org-make-org-heading-search-string (&optional string)
  8987. "Make search string for the current headline or STRING."
  8988. (let ((s (or string
  8989. (and (derived-mode-p 'org-mode)
  8990. (save-excursion
  8991. (org-back-to-heading t)
  8992. (org-element-property :raw-value (org-element-at-point))))))
  8993. (lines org-context-in-file-links))
  8994. (or string (setq s (concat "*" s))) ; Add * for headlines
  8995. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8996. (when (and string (integerp lines) (> lines 0))
  8997. (let ((slines (org-split-string s "\n")))
  8998. (when (< lines (length slines))
  8999. (setq s (mapconcat
  9000. 'identity
  9001. (reverse (nthcdr (- (length slines) lines)
  9002. (reverse slines))) "\n")))))
  9003. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9004. (defun org-make-link-string (link &optional description)
  9005. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9006. (unless (org-string-nw-p link) (error "Empty link"))
  9007. (let ((uri (cond ((string-match org-link-types-re link)
  9008. (concat (match-string 1 link)
  9009. (org-link-escape (substring link (match-end 1)))))
  9010. ;; For readability, url-encode internal links only
  9011. ;; when absolutely needed (i.e, when they contain
  9012. ;; square brackets). File links however, are
  9013. ;; encoded since, e.g., spaces are significant.
  9014. ((or (file-name-absolute-p link)
  9015. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9016. (org-link-escape link))
  9017. (t link)))
  9018. (description
  9019. (and (org-string-nw-p description)
  9020. ;; Remove brackets from description, as they are fatal.
  9021. (replace-regexp-in-string
  9022. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9023. (org-trim description)))))
  9024. (format "[[%s]%s]"
  9025. uri
  9026. (if description (format "[%s]" description) ""))))
  9027. (defconst org-link-escape-chars
  9028. ;;%20 %5B %5D %25
  9029. '(?\s ?\[ ?\] ?%)
  9030. "List of characters that should be escaped in a link when stored to Org.
  9031. This is the list that is used for internal purposes.")
  9032. (defconst org-link-escape-chars-browser
  9033. ;;%20 %22
  9034. '(?\s ?\")
  9035. "List of characters to be escaped before handing over to the browser.
  9036. If you consider using this constant then you probably want to use
  9037. the function `org-link-escape-browser' instead. See there why
  9038. this constant is a candidate to be removed once Org drops support
  9039. for Emacs 24.1 and 24.2.")
  9040. (defun org-link-escape (text &optional table merge)
  9041. "Return percent escaped representation of TEXT.
  9042. TEXT is a string with the text to escape.
  9043. Optional argument TABLE is a list with characters that should be
  9044. escaped. When nil, `org-link-escape-chars' is used.
  9045. If optional argument MERGE is set, merge TABLE into
  9046. `org-link-escape-chars'."
  9047. (let ((characters-to-encode
  9048. (cond ((null table) org-link-escape-chars)
  9049. (merge (append org-link-escape-chars table))
  9050. (t table))))
  9051. (mapconcat
  9052. (lambda (c)
  9053. (if (or (memq c characters-to-encode)
  9054. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9055. (mapconcat (lambda (e) (format "%%%.2X" e))
  9056. (or (encode-coding-char c 'utf-8)
  9057. (error "Unable to percent escape character: %c" c))
  9058. "")
  9059. (char-to-string c)))
  9060. text "")))
  9061. (defun org-link-escape-browser (text)
  9062. "Escape some characters before handing over to the browser.
  9063. This function is a candidate to be removed together with the
  9064. constant `org-link-escape-chars-browser' once Org drops support
  9065. for Emacs 24.1 and 24.2. All calls to this function will have to
  9066. be replaced with `url-encode-url' which is available since Emacs
  9067. 24.3.1."
  9068. ;; Example with the Org link
  9069. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9070. ;; to open the browser with +subject:"Release 8.2" filled into the
  9071. ;; query field: In this case the variable TEXT contains the
  9072. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9073. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9074. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9075. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9076. (if (fboundp 'url-encode-url)
  9077. (url-encode-url text)
  9078. (if (org-string-match-p
  9079. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9080. text)
  9081. (org-link-escape text org-link-escape-chars-browser)
  9082. text)))
  9083. (defun org-link-unescape (str)
  9084. "Unhex hexified Unicode parts in string STR.
  9085. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9086. reciprocal of `org-link-escape', which see."
  9087. (if (org-string-nw-p str)
  9088. (replace-regexp-in-string
  9089. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9090. str))
  9091. (defun org-link-unescape-compound (hex)
  9092. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9093. Note: this function also decodes single byte encodings like
  9094. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9095. (save-match-data
  9096. (let* ((bytes (cdr (split-string hex "%")))
  9097. (ret "")
  9098. (eat 0)
  9099. (sum 0))
  9100. (while bytes
  9101. (let* ((val (string-to-number (pop bytes) 16))
  9102. (shift-xor
  9103. (if (= 0 eat)
  9104. (cond
  9105. ((>= val 252) (cons 6 252))
  9106. ((>= val 248) (cons 5 248))
  9107. ((>= val 240) (cons 4 240))
  9108. ((>= val 224) (cons 3 224))
  9109. ((>= val 192) (cons 2 192))
  9110. (t (cons 0 0)))
  9111. (cons 6 128))))
  9112. (if (>= val 192) (setq eat (car shift-xor)))
  9113. (setq val (logxor val (cdr shift-xor)))
  9114. (setq sum (+ (lsh sum (car shift-xor)) val))
  9115. (if (> eat 0) (setq eat (- eat 1)))
  9116. (cond
  9117. ((= 0 eat) ;multi byte
  9118. (setq ret (concat ret (org-char-to-string sum)))
  9119. (setq sum 0))
  9120. ((not bytes) ; single byte(s)
  9121. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9122. ret)))
  9123. (defun org-link-unescape-single-byte-sequence (hex)
  9124. "Unhexify hex-encoded single byte character sequences."
  9125. (mapconcat (lambda (byte)
  9126. (char-to-string (string-to-number byte 16)))
  9127. (cdr (split-string hex "%")) ""))
  9128. (defun org-xor (a b)
  9129. "Exclusive or."
  9130. (if a (not b) b))
  9131. (defun org-fixup-message-id-for-http (s)
  9132. "Replace special characters in a message id, so it can be used in an http query."
  9133. (when (string-match "%" s)
  9134. (setq s (mapconcat (lambda (c)
  9135. (if (eq c ?%)
  9136. "%25"
  9137. (char-to-string c)))
  9138. s "")))
  9139. (while (string-match "<" s)
  9140. (setq s (replace-match "%3C" t t s)))
  9141. (while (string-match ">" s)
  9142. (setq s (replace-match "%3E" t t s)))
  9143. (while (string-match "@" s)
  9144. (setq s (replace-match "%40" t t s)))
  9145. s)
  9146. (defun org-link-prettify (link)
  9147. "Return a human-readable representation of LINK.
  9148. The car of LINK must be a raw link.
  9149. The cdr of LINK must be either a link description or nil."
  9150. (let ((desc (or (cadr link) "<no description>")))
  9151. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9152. "<" (car link) ">")))
  9153. ;;;###autoload
  9154. (defun org-insert-link-global ()
  9155. "Insert a link like Org-mode does.
  9156. This command can be called in any mode to insert a link in Org-mode syntax."
  9157. (interactive)
  9158. (org-load-modules-maybe)
  9159. (org-run-like-in-org-mode 'org-insert-link))
  9160. (defun org-insert-all-links (arg &optional pre post)
  9161. "Insert all links in `org-stored-links'.
  9162. When a universal prefix, do not delete the links from `org-stored-links'.
  9163. When `ARG' is a number, insert the last N link(s).
  9164. `PRE' and `POST' are optional arguments to define a string to
  9165. prepend or to append."
  9166. (interactive "P")
  9167. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9168. (links (copy-seq org-stored-links))
  9169. (pr (or pre "- "))
  9170. (po (or post "\n"))
  9171. (cnt 1) l)
  9172. (if (null org-stored-links)
  9173. (message "No link to insert")
  9174. (while (and (or (listp arg) (>= arg cnt))
  9175. (setq l (if (listp arg)
  9176. (pop links)
  9177. (pop org-stored-links))))
  9178. (setq cnt (1+ cnt))
  9179. (insert pr)
  9180. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9181. (insert po)))))
  9182. (defun org-insert-last-stored-link (arg)
  9183. "Insert the last link stored in `org-stored-links'."
  9184. (interactive "p")
  9185. (org-insert-all-links arg "" "\n"))
  9186. (defun org-link-fontify-links-to-this-file ()
  9187. "Fontify links to the current file in `org-stored-links'."
  9188. (let ((f (buffer-file-name)) a b)
  9189. (setq a (mapcar (lambda(l)
  9190. (let ((ll (car l)))
  9191. (when (and (string-match "^file:\\(.+\\)::" ll)
  9192. (equal f (expand-file-name (match-string 1 ll))))
  9193. ll)))
  9194. org-stored-links))
  9195. (when (featurep 'org-id)
  9196. (setq b (mapcar (lambda(l)
  9197. (let ((ll (car l)))
  9198. (when (and (string-match "^id:\\(.+\\)$" ll)
  9199. (equal f (expand-file-name
  9200. (or (org-id-find-id-file
  9201. (match-string 1 ll)) ""))))
  9202. ll)))
  9203. org-stored-links)))
  9204. (mapcar (lambda(l)
  9205. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9206. (delq nil (append a b)))))
  9207. (defvar org-link-links-in-this-file nil)
  9208. (defun org-insert-link (&optional complete-file link-location default-description)
  9209. "Insert a link. At the prompt, enter the link.
  9210. Completion can be used to insert any of the link protocol prefixes like
  9211. http or ftp in use.
  9212. The history can be used to select a link previously stored with
  9213. `org-store-link'. When the empty string is entered (i.e. if you just
  9214. press RET at the prompt), the link defaults to the most recently
  9215. stored link. As SPC triggers completion in the minibuffer, you need to
  9216. use M-SPC or C-q SPC to force the insertion of a space character.
  9217. You will also be prompted for a description, and if one is given, it will
  9218. be displayed in the buffer instead of the link.
  9219. If there is already a link at point, this command will allow you to edit link
  9220. and description parts.
  9221. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9222. be selected using completion. The path to the file will be relative to the
  9223. current directory if the file is in the current directory or a subdirectory.
  9224. Otherwise, the link will be the absolute path as completed in the minibuffer
  9225. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9226. option `org-link-file-path-type'.
  9227. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9228. the current directory or below.
  9229. With three \\[universal-argument] prefixes, negate the meaning of
  9230. `org-keep-stored-link-after-insertion'.
  9231. If `org-make-link-description-function' is non-nil, this function will be
  9232. called with the link target, and the result will be the default
  9233. link description.
  9234. If the LINK-LOCATION parameter is non-nil, this value will be
  9235. used as the link location instead of reading one interactively.
  9236. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9237. be used as the default description."
  9238. (interactive "P")
  9239. (let* ((wcf (current-window-configuration))
  9240. (origbuf (current-buffer))
  9241. (region (if (org-region-active-p)
  9242. (buffer-substring (region-beginning) (region-end))))
  9243. (remove (and region (list (region-beginning) (region-end))))
  9244. (desc region)
  9245. tmphist ; byte-compile incorrectly complains about this
  9246. (link link-location)
  9247. (abbrevs org-link-abbrev-alist-local)
  9248. entry file all-prefixes auto-desc)
  9249. (cond
  9250. (link-location) ; specified by arg, just use it.
  9251. ((org-in-regexp org-bracket-link-regexp 1)
  9252. ;; We do have a link at point, and we are going to edit it.
  9253. (setq remove (list (match-beginning 0) (match-end 0)))
  9254. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9255. (setq link (read-string "Link: "
  9256. (org-link-unescape
  9257. (org-match-string-no-properties 1)))))
  9258. ((or (org-in-regexp org-angle-link-re)
  9259. (org-in-regexp org-plain-link-re))
  9260. ;; Convert to bracket link
  9261. (setq remove (list (match-beginning 0) (match-end 0))
  9262. link (read-string "Link: "
  9263. (org-remove-angle-brackets (match-string 0)))))
  9264. ((member complete-file '((4) (16)))
  9265. ;; Completing read for file names.
  9266. (setq link (org-file-complete-link complete-file)))
  9267. (t
  9268. ;; Read link, with completion for stored links.
  9269. (org-link-fontify-links-to-this-file)
  9270. (org-switch-to-buffer-other-window "*Org Links*")
  9271. (with-current-buffer "*Org Links*"
  9272. (erase-buffer)
  9273. (insert "Insert a link.
  9274. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9275. (when org-stored-links
  9276. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9277. (insert (mapconcat 'org-link-prettify
  9278. (reverse org-stored-links) "\n")))
  9279. (goto-char (point-min)))
  9280. (let ((cw (selected-window)))
  9281. (select-window (get-buffer-window "*Org Links*" 'visible))
  9282. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9283. (unless (pos-visible-in-window-p (point-max))
  9284. (org-fit-window-to-buffer))
  9285. (and (window-live-p cw) (select-window cw)))
  9286. ;; Fake a link history, containing the stored links.
  9287. (setq tmphist (append (mapcar 'car org-stored-links)
  9288. org-insert-link-history))
  9289. (setq all-prefixes (append (mapcar 'car abbrevs)
  9290. (mapcar 'car org-link-abbrev-alist)
  9291. org-link-types))
  9292. (unwind-protect
  9293. (progn
  9294. (setq link
  9295. (org-completing-read
  9296. "Link: "
  9297. (append
  9298. (mapcar (lambda (x) (concat x ":"))
  9299. all-prefixes)
  9300. (mapcar 'car org-stored-links))
  9301. nil nil nil
  9302. 'tmphist
  9303. (caar org-stored-links)))
  9304. (if (not (string-match "\\S-" link))
  9305. (user-error "No link selected"))
  9306. (mapc (lambda(l)
  9307. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9308. org-stored-links)
  9309. (if (or (member link all-prefixes)
  9310. (and (equal ":" (substring link -1))
  9311. (member (substring link 0 -1) all-prefixes)
  9312. (setq link (substring link 0 -1))))
  9313. (setq link (with-current-buffer origbuf
  9314. (org-link-try-special-completion link)))))
  9315. (set-window-configuration wcf)
  9316. (kill-buffer "*Org Links*"))
  9317. (setq entry (assoc link org-stored-links))
  9318. (or entry (push link org-insert-link-history))
  9319. (setq desc (or desc (nth 1 entry)))))
  9320. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9321. (not org-keep-stored-link-after-insertion))
  9322. (setq org-stored-links (delq (assoc link org-stored-links)
  9323. org-stored-links)))
  9324. (if (and (string-match org-plain-link-re link)
  9325. (not (string-match org-ts-regexp link)))
  9326. ;; URL-like link, normalize the use of angular brackets.
  9327. (setq link (org-remove-angle-brackets link)))
  9328. ;; Check if we are linking to the current file with a search
  9329. ;; option If yes, simplify the link by using only the search
  9330. ;; option.
  9331. (when (and buffer-file-name
  9332. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9333. (let* ((path (match-string 1 link))
  9334. (case-fold-search nil)
  9335. (search (match-string 2 link)))
  9336. (save-match-data
  9337. (if (equal (file-truename buffer-file-name) (file-truename path))
  9338. ;; We are linking to this same file, with a search option
  9339. (setq link search)))))
  9340. ;; Check if we can/should use a relative path. If yes, simplify the link
  9341. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9342. (let* ((type (match-string 1 link))
  9343. (path (match-string 2 link))
  9344. (origpath path)
  9345. (case-fold-search nil))
  9346. (cond
  9347. ((or (eq org-link-file-path-type 'absolute)
  9348. (equal complete-file '(16)))
  9349. (setq path (abbreviate-file-name (expand-file-name path))))
  9350. ((eq org-link-file-path-type 'noabbrev)
  9351. (setq path (expand-file-name path)))
  9352. ((eq org-link-file-path-type 'relative)
  9353. (setq path (file-relative-name path)))
  9354. (t
  9355. (save-match-data
  9356. (if (string-match (concat "^" (regexp-quote
  9357. (expand-file-name
  9358. (file-name-as-directory
  9359. default-directory))))
  9360. (expand-file-name path))
  9361. ;; We are linking a file with relative path name.
  9362. (setq path (substring (expand-file-name path)
  9363. (match-end 0)))
  9364. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9365. (setq link (concat type path))
  9366. (if (equal desc origpath)
  9367. (setq desc path))))
  9368. (if org-make-link-description-function
  9369. (setq desc
  9370. (or (condition-case nil
  9371. (funcall org-make-link-description-function link desc)
  9372. (error (progn (message "Can't get link description from `%s'"
  9373. (symbol-name org-make-link-description-function))
  9374. (sit-for 2) nil)))
  9375. (read-string "Description: " default-description)))
  9376. (if default-description (setq desc default-description)
  9377. (setq desc (or (and auto-desc desc)
  9378. (read-string "Description: " desc)))))
  9379. (unless (string-match "\\S-" desc) (setq desc nil))
  9380. (if remove (apply 'delete-region remove))
  9381. (insert (org-make-link-string link desc))
  9382. ;; Redisplay so as the new link has proper invisible characters.
  9383. (sit-for 0)))
  9384. (defun org-link-try-special-completion (type)
  9385. "If there is completion support for link type TYPE, offer it."
  9386. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9387. (if (functionp fun)
  9388. (funcall fun)
  9389. (read-string "Link (no completion support): " (concat type ":")))))
  9390. (defun org-file-complete-link (&optional arg)
  9391. "Create a file link using completion."
  9392. (let ((file (org-iread-file-name "File: "))
  9393. (pwd (file-name-as-directory (expand-file-name ".")))
  9394. (pwd1 (file-name-as-directory (abbreviate-file-name
  9395. (expand-file-name ".")))))
  9396. (cond ((equal arg '(16))
  9397. (concat "file:"
  9398. (abbreviate-file-name (expand-file-name file))))
  9399. ((string-match
  9400. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9401. (concat "file:" (match-string 1 file)))
  9402. ((string-match
  9403. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9404. (expand-file-name file))
  9405. (concat "file:"
  9406. (match-string 1 (expand-file-name file))))
  9407. (t (concat "file:" file)))))
  9408. (defun org-iread-file-name (&rest args)
  9409. "Read-file-name using `ido-mode' speedup if available.
  9410. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9411. See `read-file-name' for a description of parameters."
  9412. (org-without-partial-completion
  9413. (if (and org-completion-use-ido
  9414. (fboundp 'ido-read-file-name)
  9415. (org-bound-and-true-p ido-mode)
  9416. (listp (nth 1 args)))
  9417. (let ((ido-enter-matching-directory nil))
  9418. (apply #'ido-read-file-name args))
  9419. (apply #'read-file-name args))))
  9420. (defun org-completing-read (&rest args)
  9421. "Completing-read with SPACE being a normal character."
  9422. (let ((enable-recursive-minibuffers t)
  9423. (minibuffer-local-completion-map
  9424. (copy-keymap minibuffer-local-completion-map)))
  9425. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9426. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9427. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9428. (apply 'org-icompleting-read args)))
  9429. (defun org-completing-read-no-i (&rest args)
  9430. (let (org-completion-use-ido org-completion-use-iswitchb)
  9431. (apply 'org-completing-read args)))
  9432. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9433. "Use iswitch as a completing-read replacement to choose from choices.
  9434. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9435. from."
  9436. (let* ((iswitchb-use-virtual-buffers nil)
  9437. (iswitchb-make-buflist-hook
  9438. (lambda ()
  9439. (setq iswitchb-temp-buflist choices))))
  9440. (iswitchb-read-buffer prompt)))
  9441. (defun org-icompleting-read (&rest args)
  9442. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9443. Should be called like `completing-read'."
  9444. (org-without-partial-completion
  9445. (if (not (listp (nth 1 args)))
  9446. ;; Ido only supports lists as the COLLECTION argument. Use
  9447. ;; default completion function when second argument is not
  9448. ;; a list.
  9449. (apply #'completing-read args)
  9450. (let ((ido-enter-matching-directory nil))
  9451. (apply (cond ((and org-completion-use-ido
  9452. (fboundp 'ido-completing-read)
  9453. (org-bound-and-true-p ido-mode))
  9454. #'ido-completing-read)
  9455. ((and org-completion-use-iswitchb
  9456. (org-bound-and-true-p iswitchb-mode))
  9457. #'org-iswitchb-completing-read)
  9458. (t #'completing-read))
  9459. args)))))
  9460. ;;; Opening/following a link
  9461. (defvar org-link-search-failed nil)
  9462. (defvar org-open-link-functions nil
  9463. "Hook for functions finding a plain text link.
  9464. These functions must take a single argument, the link content.
  9465. They will be called for links that look like [[link text][description]]
  9466. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9467. like a filename (e.g. \"./blue.png\").
  9468. These functions will be called *before* Org attempts to resolve the
  9469. link by doing text searches in the current buffer - so if you want a
  9470. link \"[[target]]\" to still find \"<<target>>\", your function should
  9471. handle this as a special case.
  9472. When the function does handle the link, it must return a non-nil value.
  9473. If it decides that it is not responsible for this link, it must return
  9474. nil to indicate that that Org-mode can continue with other options
  9475. like exact and fuzzy text search.")
  9476. (defun org-next-link (&optional search-backward)
  9477. "Move forward to the next link.
  9478. If the link is in hidden text, expose it."
  9479. (interactive "P")
  9480. (when (and org-link-search-failed (eq this-command last-command))
  9481. (goto-char (point-min))
  9482. (message "Link search wrapped back to beginning of buffer"))
  9483. (setq org-link-search-failed nil)
  9484. (let* ((pos (point))
  9485. (ct (org-context))
  9486. (a (assoc :link ct))
  9487. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9488. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9489. ((looking-at org-any-link-re)
  9490. ;; Don't stay stuck at link without an org-link face
  9491. (forward-char (if search-backward -1 1))))
  9492. (if (funcall srch-fun org-any-link-re nil t)
  9493. (progn
  9494. (goto-char (match-beginning 0))
  9495. (if (outline-invisible-p) (org-show-context)))
  9496. (goto-char pos)
  9497. (setq org-link-search-failed t)
  9498. (message "No further link found"))))
  9499. (defun org-previous-link ()
  9500. "Move backward to the previous link.
  9501. If the link is in hidden text, expose it."
  9502. (interactive)
  9503. (funcall 'org-next-link t))
  9504. (defun org-translate-link (s)
  9505. "Translate a link string if a translation function has been defined."
  9506. (with-temp-buffer
  9507. (insert (org-trim s))
  9508. (org-trim (org-element-interpret-data (org-element-context)))))
  9509. (defun org-translate-link-from-planner (type path)
  9510. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9511. This is still an experimental function, your mileage may vary."
  9512. (cond
  9513. ((member type '("http" "https" "news" "ftp"))
  9514. ;; standard Internet links are the same.
  9515. nil)
  9516. ((and (equal type "irc") (string-match "^//" path))
  9517. ;; Planner has two / at the beginning of an irc link, we have 1.
  9518. ;; We should have zero, actually....
  9519. (setq path (substring path 1)))
  9520. ((and (equal type "lisp") (string-match "^/" path))
  9521. ;; Planner has a slash, we do not.
  9522. (setq type "elisp" path (substring path 1)))
  9523. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9524. ;; A typical message link. Planner has the id after the final slash,
  9525. ;; we separate it with a hash mark
  9526. (setq path (concat (match-string 1 path) "#"
  9527. (org-remove-angle-brackets (match-string 2 path))))))
  9528. (cons type path))
  9529. (defun org-find-file-at-mouse (ev)
  9530. "Open file link or URL at mouse."
  9531. (interactive "e")
  9532. (mouse-set-point ev)
  9533. (org-open-at-point 'in-emacs))
  9534. (defun org-open-at-mouse (ev)
  9535. "Open file link or URL at mouse.
  9536. See the docstring of `org-open-file' for details."
  9537. (interactive "e")
  9538. (mouse-set-point ev)
  9539. (if (eq major-mode 'org-agenda-mode)
  9540. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9541. (org-open-at-point))
  9542. (defvar org-window-config-before-follow-link nil
  9543. "The window configuration before following a link.
  9544. This is saved in case the need arises to restore it.")
  9545. ;;;###autoload
  9546. (defun org-open-at-point-global ()
  9547. "Follow a link like Org-mode does.
  9548. This command can be called in any mode to follow a link that has
  9549. Org-mode syntax."
  9550. (interactive)
  9551. (org-run-like-in-org-mode 'org-open-at-point))
  9552. ;;;###autoload
  9553. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9554. "Open a link in the string S, as if it was in Org-mode."
  9555. (interactive "sLink: \nP")
  9556. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9557. (with-temp-buffer
  9558. (let ((org-inhibit-startup (not reference-buffer)))
  9559. (org-mode)
  9560. (insert s)
  9561. (goto-char (point-min))
  9562. (when reference-buffer
  9563. (setq org-link-abbrev-alist-local
  9564. (with-current-buffer reference-buffer
  9565. org-link-abbrev-alist-local)))
  9566. (org-open-at-point arg reference-buffer)))))
  9567. (defvar org-open-at-point-functions nil
  9568. "Hook that is run when following a link at point.
  9569. Functions in this hook must return t if they identify and follow
  9570. a link at point. If they don't find anything interesting at point,
  9571. they must return nil.")
  9572. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9573. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9574. (defun org-open-at-point (&optional arg reference-buffer)
  9575. "Open link, timestamp, footnote or tags at point.
  9576. When point is on a link, follow it. Normally, files will be
  9577. opened by an appropriate application. If the optional prefix
  9578. argument ARG is non-nil, Emacs will visit the file. With
  9579. a double prefix argument, try to open outside of Emacs, in the
  9580. application the system uses for this file type.
  9581. When point is on a timestamp, open the agenda at the day
  9582. specified.
  9583. When point is a footnote definition, move to the first reference
  9584. found. If it is on a reference, move to the associated
  9585. definition.
  9586. When point is on a headline, display a list of every link in the
  9587. entry, so it is possible to pick one, or all, of them. If point
  9588. is on a tag, call `org-tags-view' instead.
  9589. When optional argument REFERENCE-BUFFER is non-nil, it should
  9590. specify a buffer from where the link search should happen. This
  9591. is used internally by `org-open-link-from-string'.
  9592. On top of syntactically correct links, this function will open
  9593. the link at point in comments or comment blocks and the first
  9594. link in a property drawer line."
  9595. (interactive "P")
  9596. ;; On a code block, open block's results.
  9597. (unless (call-interactively 'org-babel-open-src-block-result)
  9598. (org-load-modules-maybe)
  9599. (setq org-window-config-before-follow-link (current-window-configuration))
  9600. (org-remove-occur-highlights nil nil t)
  9601. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9602. (let* ((context
  9603. ;; Only consider supported types, even if they are not
  9604. ;; the closest one.
  9605. (org-element-lineage
  9606. (org-element-context)
  9607. '(clock comment comment-block footnote-definition
  9608. footnote-reference headline inlinetask keyword link
  9609. node-property timestamp)
  9610. t))
  9611. (type (org-element-type context))
  9612. (value (org-element-property :value context)))
  9613. (cond
  9614. ((not context) (user-error "No link found"))
  9615. ;; Exception: open timestamps and links in properties
  9616. ;; drawers, keywords and comments.
  9617. ((memq type '(comment comment-block keyword node-property))
  9618. (cond ((org-in-regexp org-any-link-re)
  9619. (org-open-link-from-string (match-string-no-properties 0)))
  9620. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9621. (org-follow-timestamp-link))
  9622. (t (user-error "No link found"))))
  9623. ;; On a headline or an inlinetask, but not on a timestamp,
  9624. ;; a link, a footnote reference or on tags.
  9625. ((and (memq type '(headline inlinetask))
  9626. ;; Not on tags.
  9627. (progn (save-excursion (beginning-of-line)
  9628. (looking-at org-complex-heading-regexp))
  9629. (or (not (match-beginning 5))
  9630. (< (point) (match-beginning 5)))))
  9631. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9632. (links (car data))
  9633. (links-end (cdr data)))
  9634. (if links
  9635. (dolist (link (if (stringp links) (list links) links))
  9636. (search-forward link nil links-end)
  9637. (goto-char (match-beginning 0))
  9638. (org-open-at-point))
  9639. (require 'org-attach)
  9640. (org-attach-reveal 'if-exists))))
  9641. ;; On a clock line, make sure point is on the timestamp
  9642. ;; before opening it.
  9643. ((and (eq type 'clock)
  9644. value
  9645. (>= (point) (org-element-property :begin value))
  9646. (<= (point) (org-element-property :end value)))
  9647. (org-follow-timestamp-link))
  9648. ;; Do nothing on white spaces after an object, unless point
  9649. ;; is right after it.
  9650. ((> (point)
  9651. (save-excursion
  9652. (goto-char (org-element-property :end context))
  9653. (skip-chars-backward " \t")
  9654. (point)))
  9655. (user-error "No link found"))
  9656. ((eq type 'timestamp) (org-follow-timestamp-link))
  9657. ;; On tags within a headline or an inlinetask.
  9658. ((and (memq type '(headline inlinetask))
  9659. (progn (save-excursion (beginning-of-line)
  9660. (looking-at org-complex-heading-regexp))
  9661. (and (match-beginning 5)
  9662. (>= (point) (match-beginning 5)))))
  9663. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9664. ((eq type 'link)
  9665. ;; When link is located within the description of another
  9666. ;; link (e.g., an inline image), always open the parent
  9667. ;; link.
  9668. (let*((link (let ((up (org-element-property :parent context)))
  9669. (if (eq (org-element-type up) 'link) up context)))
  9670. (type (org-element-property :type link))
  9671. (path (org-link-unescape (org-element-property :path link))))
  9672. ;; Switch back to REFERENCE-BUFFER needed when called in
  9673. ;; a temporary buffer through `org-open-link-from-string'.
  9674. (with-current-buffer (or reference-buffer (current-buffer))
  9675. (cond
  9676. ((equal type "file")
  9677. (if (string-match "[*?{]" (file-name-nondirectory path))
  9678. (dired path)
  9679. ;; Look into `org-link-protocols' in order to find
  9680. ;; a DEDICATED-FUNCTION to open file. The function
  9681. ;; will be applied on raw link instead of parsed
  9682. ;; link due to the limitation in `org-add-link-type'
  9683. ;; ("open" function called with a single argument).
  9684. ;; If no such function is found, fallback to
  9685. ;; `org-open-file'.
  9686. ;;
  9687. ;; Note : "file+emacs" and "file+sys" types are
  9688. ;; hard-coded in order to escape the previous
  9689. ;; limitation.
  9690. (let* ((option (org-element-property :search-option link))
  9691. (app (org-element-property :application link))
  9692. (dedicated-function
  9693. (nth 1 (assoc app org-link-protocols))))
  9694. (if dedicated-function
  9695. (funcall dedicated-function
  9696. (concat path
  9697. (and option (concat "::" option))))
  9698. (apply #'org-open-file
  9699. path
  9700. (cond (arg)
  9701. ((equal app "emacs") 'emacs)
  9702. ((equal app "sys") 'system))
  9703. (cond ((not option) nil)
  9704. ((org-string-match-p "\\`[0-9]+\\'" option)
  9705. (list (string-to-number option)))
  9706. (t (list nil
  9707. (org-link-unescape option)))))))))
  9708. ((assoc type org-link-protocols)
  9709. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9710. ((equal type "help")
  9711. (let ((f-or-v (intern path)))
  9712. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9713. ((boundp f-or-v) (describe-variable f-or-v))
  9714. (t (error "Not a known function or variable")))))
  9715. ((member type '("http" "https" "ftp" "mailto" "news"))
  9716. (browse-url (org-link-escape-browser (concat type ":" path))))
  9717. ((equal type "doi")
  9718. (browse-url
  9719. (org-link-escape-browser (concat org-doi-server-url path))))
  9720. ((equal type "message") (browse-url (concat type ":" path)))
  9721. ((equal type "shell")
  9722. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9723. (cmd path))
  9724. (if (or (and (org-string-nw-p
  9725. org-confirm-shell-link-not-regexp)
  9726. (string-match
  9727. org-confirm-shell-link-not-regexp cmd))
  9728. (not org-confirm-shell-link-function)
  9729. (funcall org-confirm-shell-link-function
  9730. (format "Execute \"%s\" in shell? "
  9731. (org-add-props cmd nil
  9732. 'face 'org-warning))))
  9733. (progn
  9734. (message "Executing %s" cmd)
  9735. (shell-command cmd buf)
  9736. (when (featurep 'midnight)
  9737. (setq clean-buffer-list-kill-buffer-names
  9738. (cons (buffer-name buf)
  9739. clean-buffer-list-kill-buffer-names))))
  9740. (user-error "Abort"))))
  9741. ((equal type "elisp")
  9742. (let ((cmd path))
  9743. (if (or (and (org-string-nw-p
  9744. org-confirm-elisp-link-not-regexp)
  9745. (org-string-match-p
  9746. org-confirm-elisp-link-not-regexp cmd))
  9747. (not org-confirm-elisp-link-function)
  9748. (funcall org-confirm-elisp-link-function
  9749. (format "Execute \"%s\" as elisp? "
  9750. (org-add-props cmd nil
  9751. 'face 'org-warning))))
  9752. (message "%s => %s" cmd
  9753. (if (eq (string-to-char cmd) ?\()
  9754. (eval (read cmd))
  9755. (call-interactively (read cmd))))
  9756. (user-error "Abort"))))
  9757. ((equal type "id")
  9758. (require 'org-id)
  9759. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9760. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9761. (unless (run-hook-with-args-until-success
  9762. 'org-open-link-functions path)
  9763. (if (not arg) (org-mark-ring-push)
  9764. (switch-to-buffer-other-window
  9765. (org-get-buffer-for-internal-link (current-buffer))))
  9766. (let ((destination
  9767. (org-with-wide-buffer
  9768. (if (equal type "radio")
  9769. (org-search-radio-target
  9770. (org-element-property :path link))
  9771. (org-link-search
  9772. (if (member type '("custom-id" "coderef"))
  9773. (org-element-property :raw-link link)
  9774. path)
  9775. ;; Prevent fuzzy links from matching
  9776. ;; themselves.
  9777. (and (equal type "fuzzy")
  9778. (+ 2 (org-element-property :begin link)))))
  9779. (point))))
  9780. (unless (and (<= (point-min) destination)
  9781. (>= (point-max) destination))
  9782. (widen))
  9783. (goto-char destination))))
  9784. (t (browse-url-at-point))))))
  9785. ;; On a footnote reference or at a footnote definition's label.
  9786. ((or (eq type 'footnote-reference)
  9787. (and (eq type 'footnote-definition)
  9788. (save-excursion
  9789. ;; Do not validate action when point is on the
  9790. ;; spaces right after the footnote label, in
  9791. ;; order to be on par with behaviour on links.
  9792. (skip-chars-forward " \t")
  9793. (let ((begin
  9794. (org-element-property :contents-begin context)))
  9795. (if begin (< (point) begin)
  9796. (= (org-element-property :post-affiliated context)
  9797. (line-beginning-position)))))))
  9798. (org-footnote-action))
  9799. (t (user-error "No link found")))))
  9800. (run-hook-with-args 'org-follow-link-hook)))
  9801. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9802. "Offer links in the current entry and return the selected link.
  9803. If there is only one link, return it.
  9804. If NTH is an integer, return the NTH link found.
  9805. If ZERO is a string, check also this string for a link, and if
  9806. there is one, return it."
  9807. (with-current-buffer buffer
  9808. (save-excursion
  9809. (save-restriction
  9810. (widen)
  9811. (goto-char marker)
  9812. (let ((cnt ?0)
  9813. (in-emacs (if (integerp nth) nil nth))
  9814. have-zero end links link c)
  9815. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9816. (push (match-string 0 zero) links)
  9817. (setq cnt (1- cnt) have-zero t))
  9818. (save-excursion
  9819. (org-back-to-heading t)
  9820. (setq end (save-excursion (outline-next-heading) (point)))
  9821. (while (re-search-forward org-any-link-re end t)
  9822. (push (match-string 0) links))
  9823. (setq links (org-uniquify (reverse links))))
  9824. (cond
  9825. ((null links)
  9826. (message "No links"))
  9827. ((equal (length links) 1)
  9828. (setq link (car links)))
  9829. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9830. (setq link (nth (if have-zero nth (1- nth)) links)))
  9831. (t ; we have to select a link
  9832. (save-excursion
  9833. (save-window-excursion
  9834. (delete-other-windows)
  9835. (with-output-to-temp-buffer "*Select Link*"
  9836. (mapc (lambda (l)
  9837. (if (not (string-match org-bracket-link-regexp l))
  9838. (princ (format "[%c] %s\n" (incf cnt)
  9839. (org-remove-angle-brackets l)))
  9840. (if (match-end 3)
  9841. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9842. (match-string 3 l) (match-string 1 l)))
  9843. (princ (format "[%c] %s\n" (incf cnt)
  9844. (match-string 1 l))))))
  9845. links))
  9846. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9847. (message "Select link to open, RET to open all:")
  9848. (setq c (read-char-exclusive))
  9849. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9850. (when (equal c ?q) (user-error "Abort"))
  9851. (if (equal c ?\C-m)
  9852. (setq link links)
  9853. (setq nth (- c ?0))
  9854. (if have-zero (setq nth (1+ nth)))
  9855. (unless (and (integerp nth) (>= (length links) nth))
  9856. (user-error "Invalid link selection"))
  9857. (setq link (nth (1- nth) links)))))
  9858. (cons link end))))))
  9859. ;; TODO: These functions are deprecated since `org-open-at-point'
  9860. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9861. (defun org-open-file-with-system (path)
  9862. "Open file at PATH using the system way of opening it."
  9863. (org-open-file path 'system))
  9864. (defun org-open-file-with-emacs (path)
  9865. "Open file at PATH in Emacs."
  9866. (org-open-file path 'emacs))
  9867. ;;; File search
  9868. (defvar org-create-file-search-functions nil
  9869. "List of functions to construct the right search string for a file link.
  9870. These functions are called in turn with point at the location to
  9871. which the link should point.
  9872. A function in the hook should first test if it would like to
  9873. handle this file type, for example by checking the `major-mode'
  9874. or the file extension. If it decides not to handle this file, it
  9875. should just return nil to give other functions a chance. If it
  9876. does handle the file, it must return the search string to be used
  9877. when following the link. The search string will be part of the
  9878. file link, given after a double colon, and `org-open-at-point'
  9879. will automatically search for it. If special measures must be
  9880. taken to make the search successful, another function should be
  9881. added to the companion hook `org-execute-file-search-functions',
  9882. which see.
  9883. A function in this hook may also use `setq' to set the variable
  9884. `description' to provide a suggestion for the descriptive text to
  9885. be used for this link when it gets inserted into an Org-mode
  9886. buffer with \\[org-insert-link].")
  9887. (defvar org-execute-file-search-functions nil
  9888. "List of functions to execute a file search triggered by a link.
  9889. Functions added to this hook must accept a single argument, the
  9890. search string that was part of the file link, the part after the
  9891. double colon. The function must first check if it would like to
  9892. handle this search, for example by checking the `major-mode' or
  9893. the file extension. If it decides not to handle this search, it
  9894. should just return nil to give other functions a chance. If it
  9895. does handle the search, it must return a non-nil value to keep
  9896. other functions from trying.
  9897. Each function can access the current prefix argument through the
  9898. variable `current-prefix-arg'. Note that a single prefix is used
  9899. to force opening a link in Emacs, so it may be good to only use a
  9900. numeric or double prefix to guide the search function.
  9901. In case this is needed, a function in this hook can also restore
  9902. the window configuration before `org-open-at-point' was called using:
  9903. (set-window-configuration org-window-config-before-follow-link)")
  9904. (defun org-search-radio-target (target)
  9905. "Search a radio target matching TARGET in current buffer.
  9906. White spaces are not significant."
  9907. (let ((re (format "<<<%s>>>"
  9908. (mapconcat #'regexp-quote
  9909. (org-split-string target "[ \t\n]+")
  9910. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9911. (origin (point)))
  9912. (goto-char (point-min))
  9913. (catch :radio-match
  9914. (while (re-search-forward re nil t)
  9915. (backward-char)
  9916. (let ((object (org-element-context)))
  9917. (when (eq (org-element-type object) 'radio-target)
  9918. (goto-char (org-element-property :begin object))
  9919. (org-show-context 'link-search)
  9920. (throw :radio-match nil))))
  9921. (goto-char origin)
  9922. (user-error "No match for radio target: %s" target))))
  9923. (defun org-link-search (s &optional avoid-pos stealth)
  9924. "Search for a search string S.
  9925. If S starts with \"#\", it triggers a custom ID search.
  9926. If S is enclosed within parenthesis, it initiates a coderef
  9927. search.
  9928. If S is surrounded by forward slashes, it is interpreted as
  9929. a regular expression. In Org mode files, this will create an
  9930. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9931. to list matches. If the current buffer is in `dired-mode', grep
  9932. will be used to search in all files.
  9933. When AVOID-POS is given, ignore matches near that position.
  9934. When optional argument STEALTH is non-nil, do not modify
  9935. visibility around point, thus ignoring `org-show-context-detail'
  9936. variable.
  9937. Search is case-insensitive and ignores white spaces. Return type
  9938. of matched result, with is either `dedicated' or `fuzzy'."
  9939. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9940. (let* ((case-fold-search t)
  9941. (origin (point))
  9942. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9943. (words (org-split-string s "[ \t\n]+"))
  9944. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9945. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9946. type)
  9947. (cond
  9948. ;; Check if there are any special search functions.
  9949. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9950. ((eq (string-to-char s) ?#)
  9951. ;; Look for a custom ID S if S starts with "#".
  9952. (let* ((id (substring normalized 1))
  9953. (match (org-find-property "CUSTOM_ID" id)))
  9954. (if match (progn (goto-char match) (setf type 'dedicated))
  9955. (error "No match for custom ID: %s" id))))
  9956. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9957. ;; Look for coderef targets if S is enclosed within parenthesis.
  9958. (let ((coderef (match-string-no-properties 1 normalized))
  9959. (re (substring s-single-re 1 -1)))
  9960. (goto-char (point-min))
  9961. (catch :coderef-match
  9962. (while (re-search-forward re nil t)
  9963. (let ((element (org-element-at-point)))
  9964. (when (and (memq (org-element-type element)
  9965. '(example-block src-block))
  9966. ;; Build proper regexp according to current
  9967. ;; block's label format.
  9968. (let ((label-fmt
  9969. (regexp-quote
  9970. (or (org-element-property :label-fmt element)
  9971. org-coderef-label-format))))
  9972. (save-excursion
  9973. (beginning-of-line)
  9974. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9975. (format label-fmt coderef))))))
  9976. (setq type 'dedicated)
  9977. (goto-char (match-beginning 1))
  9978. (throw :coderef-match nil))))
  9979. (goto-char origin)
  9980. (error "No match for coderef: %s" coderef))))
  9981. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9982. ;; Look for a regular expression.
  9983. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9984. (match-string 1 s)))
  9985. ;; Fuzzy links.
  9986. (t
  9987. (let* ((starred (eq (string-to-char normalized) ?*))
  9988. (headline-search (and (derived-mode-p 'org-mode)
  9989. (or org-link-search-must-match-exact-headline
  9990. starred))))
  9991. (cond
  9992. ;; Look for targets, only if not in a headline search.
  9993. ((and (not starred)
  9994. (let ((target (format "<<%s>>" s-multi-re)))
  9995. (catch :target-match
  9996. (goto-char (point-min))
  9997. (while (re-search-forward target nil t)
  9998. (backward-char)
  9999. (let ((context (org-element-context)))
  10000. (when (eq (org-element-type context) 'target)
  10001. (setq type 'dedicated)
  10002. (goto-char (org-element-property :begin context))
  10003. (throw :target-match t))))
  10004. nil))))
  10005. ;; Look for elements named after S, only if not in a headline
  10006. ;; search.
  10007. ((and (not starred)
  10008. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  10009. (catch :name-match
  10010. (goto-char (point-min))
  10011. (while (re-search-forward name nil t)
  10012. (let ((element (org-element-at-point)))
  10013. (when (equal (org-split-string
  10014. (org-element-property :name element)
  10015. "[ \t]+")
  10016. words)
  10017. (setq type 'dedicated)
  10018. (beginning-of-line)
  10019. (throw :name-match t))))
  10020. nil))))
  10021. ;; Regular text search. Prefer headlines in Org mode
  10022. ;; buffers.
  10023. ((and (derived-mode-p 'org-mode)
  10024. (let* ((wspace "[ \t]")
  10025. (wspaceopt (concat wspace "*"))
  10026. (cookie (concat "\\(?:"
  10027. wspaceopt
  10028. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10029. wspaceopt
  10030. "\\)"))
  10031. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10032. (re (concat
  10033. org-outline-regexp-bol
  10034. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10035. "\\(?:\\[#.\\][ \t]+\\)?"
  10036. "\\(?:" org-comment-string "[ \t]+\\)?"
  10037. sep "?"
  10038. (let ((title (mapconcat #'regexp-quote
  10039. words
  10040. sep)))
  10041. (if starred (substring title 1) title))
  10042. sep "?"
  10043. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10044. "[ \t]*$")))
  10045. (goto-char (point-min))
  10046. (re-search-forward re nil t)))
  10047. (goto-char (match-beginning 0))
  10048. (setq type 'dedicated))
  10049. ;; Offer to create non-existent headline depending on
  10050. ;; `org-link-search-must-match-exact-headline'.
  10051. ((and (derived-mode-p 'org-mode)
  10052. (not org-link-search-inhibit-query)
  10053. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10054. (yes-or-no-p "No match - create this as a new heading? "))
  10055. (goto-char (point-max))
  10056. (unless (bolp) (newline))
  10057. (org-insert-heading nil t t)
  10058. (insert s "\n")
  10059. (beginning-of-line 0))
  10060. ;; Only headlines are looked after. No need to process
  10061. ;; further: throw an error.
  10062. ((and (derived-mode-p 'org-mode)
  10063. (or starred org-link-search-must-match-exact-headline))
  10064. (goto-char origin)
  10065. (error "No match for fuzzy expression: %s" normalized))
  10066. ;; Regular text search.
  10067. ((catch :fuzzy-match
  10068. (goto-char (point-min))
  10069. (while (re-search-forward s-multi-re nil t)
  10070. ;; Skip match if it contains AVOID-POS or it is included
  10071. ;; in a link with a description but outside the
  10072. ;; description.
  10073. (unless (or (and avoid-pos
  10074. (<= (match-beginning 0) avoid-pos)
  10075. (> (match-end 0) avoid-pos))
  10076. (and (save-match-data
  10077. (org-in-regexp org-bracket-link-regexp))
  10078. (match-beginning 3)
  10079. (or (> (match-beginning 3) (point))
  10080. (<= (match-end 3) (point)))
  10081. (org-element-lineage
  10082. (save-match-data (org-element-context))
  10083. '(link) t)))
  10084. (goto-char (match-beginning 0))
  10085. (setq type 'fuzzy)
  10086. (throw :fuzzy-match t)))
  10087. nil))
  10088. ;; All failed. Throw an error.
  10089. (t (goto-char origin)
  10090. (error "No match for fuzzy expression: %s" normalized))))))
  10091. ;; Disclose surroundings of match, if appropriate.
  10092. (when (and (derived-mode-p 'org-mode) (not stealth))
  10093. (org-show-context 'link-search))
  10094. type))
  10095. (defun org-get-buffer-for-internal-link (buffer)
  10096. "Return a buffer to be used for displaying the link target of internal links."
  10097. (cond
  10098. ((not org-display-internal-link-with-indirect-buffer)
  10099. buffer)
  10100. ((string-match "(Clone)$" (buffer-name buffer))
  10101. (message "Buffer is already a clone, not making another one")
  10102. ;; we also do not modify visibility in this case
  10103. buffer)
  10104. (t ; make a new indirect buffer for displaying the link
  10105. (let* ((bn (buffer-name buffer))
  10106. (ibn (concat bn "(Clone)"))
  10107. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10108. (with-current-buffer ib (org-overview))
  10109. ib))))
  10110. (defun org-do-occur (regexp &optional cleanup)
  10111. "Call the Emacs command `occur'.
  10112. If CLEANUP is non-nil, remove the printout of the regular expression
  10113. in the *Occur* buffer. This is useful if the regex is long and not useful
  10114. to read."
  10115. (occur regexp)
  10116. (when cleanup
  10117. (let ((cwin (selected-window)) win beg end)
  10118. (when (setq win (get-buffer-window "*Occur*"))
  10119. (select-window win))
  10120. (goto-char (point-min))
  10121. (when (re-search-forward "match[a-z]+" nil t)
  10122. (setq beg (match-end 0))
  10123. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10124. (setq end (1- (match-beginning 0)))))
  10125. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10126. (goto-char (point-min))
  10127. (select-window cwin))))
  10128. ;;; The mark ring for links jumps
  10129. (defvar org-mark-ring nil
  10130. "Mark ring for positions before jumps in Org-mode.")
  10131. (defvar org-mark-ring-last-goto nil
  10132. "Last position in the mark ring used to go back.")
  10133. ;; Fill and close the ring
  10134. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10135. (loop for i from 1 to org-mark-ring-length do
  10136. (push (make-marker) org-mark-ring))
  10137. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10138. org-mark-ring)
  10139. (defun org-mark-ring-push (&optional pos buffer)
  10140. "Put the current position or POS into the mark ring and rotate it."
  10141. (interactive)
  10142. (setq pos (or pos (point)))
  10143. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10144. (move-marker (car org-mark-ring)
  10145. (or pos (point))
  10146. (or buffer (current-buffer)))
  10147. (message "%s"
  10148. (substitute-command-keys
  10149. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10150. (defun org-mark-ring-goto (&optional n)
  10151. "Jump to the previous position in the mark ring.
  10152. With prefix arg N, jump back that many stored positions. When
  10153. called several times in succession, walk through the entire ring.
  10154. Org-mode commands jumping to a different position in the current file,
  10155. or to another Org-mode file, automatically push the old position
  10156. onto the ring."
  10157. (interactive "p")
  10158. (let (p m)
  10159. (if (eq last-command this-command)
  10160. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10161. (setq p org-mark-ring))
  10162. (setq org-mark-ring-last-goto p)
  10163. (setq m (car p))
  10164. (org-pop-to-buffer-same-window (marker-buffer m))
  10165. (goto-char m)
  10166. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10167. (defun org-remove-angle-brackets (s)
  10168. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10169. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10170. s)
  10171. (defun org-add-angle-brackets (s)
  10172. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10173. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10174. s)
  10175. (defun org-remove-double-quotes (s)
  10176. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10177. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10178. s)
  10179. ;;; Following specific links
  10180. (defun org-follow-timestamp-link ()
  10181. "Open an agenda view for the time-stamp date/range at point."
  10182. (cond
  10183. ((org-at-date-range-p t)
  10184. (let ((org-agenda-start-on-weekday)
  10185. (t1 (match-string 1))
  10186. (t2 (match-string 2)) tt1 tt2)
  10187. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10188. tt2 (time-to-days (org-time-string-to-time t2)))
  10189. (let ((org-agenda-buffer-tmp-name
  10190. (format "*Org Agenda(a:%s)"
  10191. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10192. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10193. ((org-at-timestamp-p t)
  10194. (let ((org-agenda-buffer-tmp-name
  10195. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10196. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10197. (substring (match-string 1) 0 10)))
  10198. 1)))
  10199. (t (error "This should not happen"))))
  10200. ;;; Following file links
  10201. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10202. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10203. (declare-function mailcap-mime-info
  10204. "mailcap" (string &optional request no-decode))
  10205. (defvar org-wait nil)
  10206. (defun org-open-file (path &optional in-emacs line search)
  10207. "Open the file at PATH.
  10208. First, this expands any special file name abbreviations. Then the
  10209. configuration variable `org-file-apps' is checked if it contains an
  10210. entry for this file type, and if yes, the corresponding command is launched.
  10211. If no application is found, Emacs simply visits the file.
  10212. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10213. With a double \\[universal-argument] \\[universal-argument] \
  10214. prefix arg, Org tries to avoid opening in Emacs
  10215. and to use an external application to visit the file.
  10216. Optional LINE specifies a line to go to, optional SEARCH a string
  10217. to search for. If LINE or SEARCH is given, the file will be
  10218. opened in Emacs, unless an entry from org-file-apps that makes
  10219. use of groups in a regexp matches.
  10220. If you want to change the way frames are used when following a
  10221. link, please customize `org-link-frame-setup'.
  10222. If the file does not exist, an error is thrown."
  10223. (let* ((file (if (equal path "")
  10224. buffer-file-name
  10225. (substitute-in-file-name (expand-file-name path))))
  10226. (file-apps (append org-file-apps (org-default-apps)))
  10227. (apps (org-remove-if
  10228. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10229. (apps-dlink (org-remove-if-not
  10230. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10231. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10232. (dirp (if remp nil (file-directory-p file)))
  10233. (file (if (and dirp org-open-directory-means-index-dot-org)
  10234. (concat (file-name-as-directory file) "index.org")
  10235. file))
  10236. (a-m-a-p (assq 'auto-mode apps))
  10237. (dfile (downcase file))
  10238. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10239. (link (cond ((and (eq line nil)
  10240. (eq search nil))
  10241. file)
  10242. (line
  10243. (concat file "::" (number-to-string line)))
  10244. (search
  10245. (concat file "::" search))))
  10246. (dlink (downcase link))
  10247. (old-buffer (current-buffer))
  10248. (old-pos (point))
  10249. (old-mode major-mode)
  10250. ext cmd link-match-data)
  10251. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10252. (setq ext (match-string 1 dfile))
  10253. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10254. (setq ext (match-string 1 dfile))))
  10255. (cond
  10256. ((member in-emacs '((16) system))
  10257. (setq cmd (cdr (assoc 'system apps))))
  10258. (in-emacs (setq cmd 'emacs))
  10259. (t
  10260. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10261. (and dirp (cdr (assoc 'directory apps)))
  10262. ; first, try matching against apps-dlink
  10263. ; if we get a match here, store the match data for later
  10264. (let ((match (assoc-default dlink apps-dlink
  10265. 'string-match)))
  10266. (if match
  10267. (progn (setq link-match-data (match-data))
  10268. match)
  10269. (progn (setq in-emacs (or in-emacs line search))
  10270. nil))) ; if we have no match in apps-dlink,
  10271. ; always open the file in emacs if line or search
  10272. ; is given (for backwards compatibility)
  10273. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10274. 'string-match)
  10275. (cdr (assoc ext apps))
  10276. (cdr (assoc t apps))))))
  10277. (when (eq cmd 'system)
  10278. (setq cmd (cdr (assoc 'system apps))))
  10279. (when (eq cmd 'default)
  10280. (setq cmd (cdr (assoc t apps))))
  10281. (when (eq cmd 'mailcap)
  10282. (require 'mailcap)
  10283. (mailcap-parse-mailcaps)
  10284. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10285. (command (mailcap-mime-info mime-type)))
  10286. (if (stringp command)
  10287. (setq cmd command)
  10288. (setq cmd 'emacs))))
  10289. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10290. (not (file-exists-p file))
  10291. (not org-open-non-existing-files))
  10292. (user-error "No such file: %s" file))
  10293. (cond
  10294. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10295. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10296. (while (string-match "['\"]%s['\"]" cmd)
  10297. (setq cmd (replace-match "%s" t t cmd)))
  10298. (while (string-match "%s" cmd)
  10299. (setq cmd (replace-match
  10300. (save-match-data
  10301. (shell-quote-argument
  10302. (convert-standard-filename file)))
  10303. t t cmd)))
  10304. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10305. (save-match-data
  10306. (let ((match-index 1)
  10307. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10308. (set-match-data link-match-data)
  10309. (while (<= match-index number-of-groups)
  10310. (let ((regex (concat "%" (number-to-string match-index)))
  10311. (replace-with (match-string match-index dlink)))
  10312. (while (string-match regex cmd)
  10313. (setq cmd (replace-match replace-with t t cmd))))
  10314. (setq match-index (+ match-index 1)))))
  10315. (save-window-excursion
  10316. (message "Running %s...done" cmd)
  10317. (start-process-shell-command cmd nil cmd)
  10318. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10319. ((or (stringp cmd)
  10320. (eq cmd 'emacs))
  10321. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10322. (widen)
  10323. (if line (progn (org-goto-line line)
  10324. (if (derived-mode-p 'org-mode)
  10325. (org-reveal)))
  10326. (if search (org-link-search search))))
  10327. ((consp cmd)
  10328. (let ((file (convert-standard-filename file)))
  10329. (save-match-data
  10330. (set-match-data link-match-data)
  10331. (eval cmd))))
  10332. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10333. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10334. (or (not (equal old-buffer (current-buffer)))
  10335. (not (equal old-pos (point))))
  10336. (org-mark-ring-push old-pos old-buffer))))
  10337. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10338. "This function returns non-nil if `entry' uses a regular
  10339. expression which should be matched against the whole link by
  10340. org-open-file.
  10341. It assumes that is the case when the entry uses a regular
  10342. expression which has at least one grouping construct and the
  10343. action is either a lisp form or a command string containing
  10344. `%1', i.e. using at least one subexpression match as a
  10345. parameter."
  10346. (let ((selector (car entry))
  10347. (action (cdr entry)))
  10348. (if (stringp selector)
  10349. (and (> (regexp-opt-depth selector) 0)
  10350. (or (and (stringp action)
  10351. (string-match "%[0-9]" action))
  10352. (consp action)))
  10353. nil)))
  10354. (defun org-default-apps ()
  10355. "Return the default applications for this operating system."
  10356. (cond
  10357. ((eq system-type 'darwin)
  10358. org-file-apps-defaults-macosx)
  10359. ((eq system-type 'windows-nt)
  10360. org-file-apps-defaults-windowsnt)
  10361. (t org-file-apps-defaults-gnu)))
  10362. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10363. "Convert extensions to regular expressions in the cars of LIST.
  10364. Also, weed out any non-string entries, because the return value is used
  10365. only for regexp matching.
  10366. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10367. point to the symbol `emacs', indicating that the file should
  10368. be opened in Emacs."
  10369. (append
  10370. (delq nil
  10371. (mapcar (lambda (x)
  10372. (if (not (stringp (car x)))
  10373. nil
  10374. (if (string-match "\\W" (car x))
  10375. x
  10376. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10377. list))
  10378. (if add-auto-mode
  10379. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10380. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10381. (defun org-file-remote-p (file)
  10382. "Test whether FILE specifies a location on a remote system.
  10383. Return non-nil if the location is indeed remote.
  10384. For example, the filename \"/user@host:/foo\" specifies a location
  10385. on the system \"/user@host:\"."
  10386. (cond ((fboundp 'file-remote-p)
  10387. (file-remote-p file))
  10388. ((fboundp 'tramp-handle-file-remote-p)
  10389. (tramp-handle-file-remote-p file))
  10390. ((and (boundp 'ange-ftp-name-format)
  10391. (string-match (car ange-ftp-name-format) file))
  10392. t)))
  10393. ;;;; Refiling
  10394. (defun org-get-org-file ()
  10395. "Read a filename, with default directory `org-directory'."
  10396. (let ((default (or org-default-notes-file remember-data-file)))
  10397. (read-file-name (format "File name [%s]: " default)
  10398. (file-name-as-directory org-directory)
  10399. default)))
  10400. (defun org-notes-order-reversed-p ()
  10401. "Check if the current file should receive notes in reversed order."
  10402. (cond
  10403. ((not org-reverse-note-order) nil)
  10404. ((eq t org-reverse-note-order) t)
  10405. ((not (listp org-reverse-note-order)) nil)
  10406. (t (catch 'exit
  10407. (dolist (entry org-reverse-note-order)
  10408. (if (string-match (car entry) buffer-file-name)
  10409. (throw 'exit (cdr entry))))))))
  10410. (defvar org-refile-target-table nil
  10411. "The list of refile targets, created by `org-refile'.")
  10412. (defvar org-agenda-new-buffers nil
  10413. "Buffers created to visit agenda files.")
  10414. (defvar org-refile-cache nil
  10415. "Cache for refile targets.")
  10416. (defvar org-refile-markers nil
  10417. "All the markers used for caching refile locations.")
  10418. (defun org-refile-marker (pos)
  10419. "Get a new refile marker, but only if caching is in use."
  10420. (if (not org-refile-use-cache)
  10421. pos
  10422. (let ((m (make-marker)))
  10423. (move-marker m pos)
  10424. (push m org-refile-markers)
  10425. m)))
  10426. (defun org-refile-cache-clear ()
  10427. "Clear the refile cache and disable all the markers."
  10428. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10429. (setq org-refile-markers nil)
  10430. (setq org-refile-cache nil)
  10431. (message "Refile cache has been cleared"))
  10432. (defun org-refile-cache-check-set (set)
  10433. "Check if all the markers in the cache still have live buffers."
  10434. (let (marker)
  10435. (catch 'exit
  10436. (while (and set (setq marker (nth 3 (pop set))))
  10437. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10438. (when (and marker (null (marker-buffer marker)))
  10439. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10440. (sit-for 3)
  10441. (throw 'exit nil)))
  10442. t)))
  10443. (defun org-refile-cache-put (set &rest identifiers)
  10444. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10445. (let* ((key (sha1 (prin1-to-string identifiers)))
  10446. (entry (assoc key org-refile-cache)))
  10447. (if entry
  10448. (setcdr entry set)
  10449. (push (cons key set) org-refile-cache))))
  10450. (defun org-refile-cache-get (&rest identifiers)
  10451. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10452. (cond
  10453. ((not org-refile-cache) nil)
  10454. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10455. (t
  10456. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10457. org-refile-cache))))
  10458. (and set (org-refile-cache-check-set set) set)))))
  10459. (defvar org-outline-path-cache nil
  10460. "Alist between buffer positions and outline paths.
  10461. It value is an alist (POSITION . PATH) where POSITION is the
  10462. buffer position at the beginning of an entry and PATH is a list
  10463. of strings describing the outline path for that entry, in reverse
  10464. order.")
  10465. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10466. "Produce a table with refile targets."
  10467. (let ((case-fold-search nil)
  10468. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10469. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10470. targets tgs files desc descre)
  10471. (message "Getting targets...")
  10472. (with-current-buffer (or default-buffer (current-buffer))
  10473. (dolist (entry entries)
  10474. (setq files (car entry) desc (cdr entry))
  10475. (cond
  10476. ((null files) (setq files (list (current-buffer))))
  10477. ((eq files 'org-agenda-files)
  10478. (setq files (org-agenda-files 'unrestricted)))
  10479. ((and (symbolp files) (fboundp files))
  10480. (setq files (funcall files)))
  10481. ((and (symbolp files) (boundp files))
  10482. (setq files (symbol-value files))))
  10483. (if (stringp files) (setq files (list files)))
  10484. (cond
  10485. ((eq (car desc) :tag)
  10486. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10487. ((eq (car desc) :todo)
  10488. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10489. ((eq (car desc) :regexp)
  10490. (setq descre (cdr desc)))
  10491. ((eq (car desc) :level)
  10492. (setq descre (concat "^\\*\\{" (number-to-string
  10493. (if org-odd-levels-only
  10494. (1- (* 2 (cdr desc)))
  10495. (cdr desc)))
  10496. "\\}[ \t]")))
  10497. ((eq (car desc) :maxlevel)
  10498. (setq descre (concat "^\\*\\{1," (number-to-string
  10499. (if org-odd-levels-only
  10500. (1- (* 2 (cdr desc)))
  10501. (cdr desc)))
  10502. "\\}[ \t]")))
  10503. (t (error "Bad refiling target description %s" desc)))
  10504. (dolist (f files)
  10505. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10506. (or
  10507. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10508. (progn
  10509. (when (bufferp f)
  10510. (setq f (buffer-file-name (buffer-base-buffer f))))
  10511. (setq f (and f (expand-file-name f)))
  10512. (when (eq org-refile-use-outline-path 'file)
  10513. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10514. (org-with-wide-buffer
  10515. (goto-char (point-min))
  10516. (setq org-outline-path-cache nil)
  10517. (while (re-search-forward descre nil t)
  10518. (beginning-of-line)
  10519. (looking-at org-complex-heading-regexp)
  10520. (let ((begin (point))
  10521. (heading (org-match-string-no-properties 4)))
  10522. (unless (or (and
  10523. org-refile-target-verify-function
  10524. (not
  10525. (funcall org-refile-target-verify-function)))
  10526. (not heading)
  10527. (member heading excluded-entries))
  10528. (let ((re (format org-complex-heading-regexp-format
  10529. (regexp-quote heading)))
  10530. (target
  10531. (if (not org-refile-use-outline-path) heading
  10532. (mapconcat
  10533. #'org-protect-slash
  10534. (append
  10535. (case org-refile-use-outline-path
  10536. (file (list (file-name-nondirectory
  10537. (buffer-file-name
  10538. (buffer-base-buffer)))))
  10539. (full-file-path
  10540. (list (buffer-file-name
  10541. (buffer-base-buffer))))
  10542. (t nil))
  10543. (org-get-outline-path t t))
  10544. "/"))))
  10545. (push (list target f re (org-refile-marker (point)))
  10546. tgs)))
  10547. (when (= (point) begin)
  10548. ;; Verification function has not moved point.
  10549. (end-of-line)))))))
  10550. (when org-refile-use-cache
  10551. (org-refile-cache-put tgs (buffer-file-name) descre))
  10552. (setq targets (append tgs targets))))))
  10553. (message "Getting targets...done")
  10554. (nreverse targets)))
  10555. (defun org-protect-slash (s)
  10556. (while (string-match "/" s)
  10557. (setq s (replace-match "\\" t t s)))
  10558. s)
  10559. (defun org--get-outline-path-1 (&optional use-cache)
  10560. "Return outline path to current headline.
  10561. Outline path is a list of strings, in reverse order. When
  10562. optional argument USE-CACHE is non-nil, make use of a cache. See
  10563. `org-get-outline-path' for details.
  10564. Assume buffer is widened and point is on a headline."
  10565. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10566. (let ((p (point))
  10567. (heading (progn
  10568. (looking-at org-complex-heading-regexp)
  10569. (if (not (match-end 4)) ""
  10570. ;; Remove statistics cookies.
  10571. (org-trim
  10572. (org-link-display-format
  10573. (replace-regexp-in-string
  10574. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10575. (org-match-string-no-properties 4))))))))
  10576. (if (org-up-heading-safe)
  10577. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10578. (when use-cache
  10579. (push (cons p path) org-outline-path-cache))
  10580. path)
  10581. ;; This is a new root node. Since we assume we are moving
  10582. ;; forward, we can drop previous cache so as to limit number
  10583. ;; of associations there.
  10584. (let ((path (list heading)))
  10585. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10586. path)))))
  10587. (defun org-get-outline-path (&optional with-self use-cache)
  10588. "Return the outline path to the current entry.
  10589. An outline path is a list of ancestors for current headline, as
  10590. a list of strings. Statistics cookies are removed and links are
  10591. replaced with their description, if any, or their path otherwise.
  10592. When optional argument WITH-SELF is non-nil, the path also
  10593. includes the current headline.
  10594. When optional argument USE-CACHE is non-nil, cache outline paths
  10595. between calls to this function so as to avoid backtracking. This
  10596. argument is useful when planning to find more than one outline
  10597. path in the same document. In that case, there are two
  10598. conditions to satisfy:
  10599. - `org-outline-path-cache' is set to nil before starting the
  10600. process;
  10601. - outline paths are computed by increasing buffer positions."
  10602. (org-with-wide-buffer
  10603. (and (or (and with-self (org-back-to-heading t))
  10604. (org-up-heading-safe))
  10605. (reverse (org--get-outline-path-1 use-cache)))))
  10606. (defun org-format-outline-path (path &optional width prefix separator)
  10607. "Format the outline path PATH for display.
  10608. WIDTH is the maximum number of characters that is available.
  10609. PREFIX is a prefix to be included in the returned string,
  10610. such as the file name.
  10611. SEPARATOR is inserted between the different parts of the path,
  10612. the default is \"/\"."
  10613. (setq width (or width 79))
  10614. (setq path (delq nil path))
  10615. (unless (> width 0)
  10616. (user-error "Argument `width' must be positive"))
  10617. (setq separator (or separator "/"))
  10618. (let* ((org-odd-levels-only nil)
  10619. (fpath (concat
  10620. prefix (and prefix path separator)
  10621. (mapconcat
  10622. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10623. (loop for head in path
  10624. for n from 0
  10625. collect (org-add-props
  10626. head nil 'face
  10627. (nth (% n org-n-level-faces) org-level-faces)))
  10628. separator))))
  10629. (when (> (length fpath) width)
  10630. (if (< width 7)
  10631. ;; It's unlikely that `width' will be this small, but don't
  10632. ;; waste characters by adding ".." if it is.
  10633. (setq fpath (substring fpath 0 width))
  10634. (setf (substring fpath (- width 2)) "..")))
  10635. fpath))
  10636. (defun org-display-outline-path (&optional file current separator just-return-string)
  10637. "Display the current outline path in the echo area.
  10638. If FILE is non-nil, prepend the output with the file name.
  10639. If CURRENT is non-nil, append the current heading to the output.
  10640. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10641. the different parts of the path and defaults to \"/\".
  10642. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10643. (interactive "P")
  10644. (let* (case-fold-search
  10645. (bfn (buffer-file-name (buffer-base-buffer)))
  10646. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10647. res)
  10648. (if current (setq path (append path
  10649. (save-excursion
  10650. (org-back-to-heading t)
  10651. (if (looking-at org-complex-heading-regexp)
  10652. (list (match-string 4)))))))
  10653. (setq res
  10654. (org-format-outline-path
  10655. path
  10656. (1- (frame-width))
  10657. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10658. separator))
  10659. (if just-return-string
  10660. (org-no-properties res)
  10661. (org-unlogged-message "%s" res))))
  10662. (defvar org-refile-history nil
  10663. "History for refiling operations.")
  10664. (defvar org-after-refile-insert-hook nil
  10665. "Hook run after `org-refile' has inserted its stuff at the new location.
  10666. Note that this is still *before* the stuff will be removed from
  10667. the *old* location.")
  10668. (defvar org-capture-last-stored-marker)
  10669. (defvar org-refile-keep nil
  10670. "Non-nil means `org-refile' will copy instead of refile.")
  10671. (defun org-copy ()
  10672. "Like `org-refile', but copy."
  10673. (interactive)
  10674. (let ((org-refile-keep t))
  10675. (funcall 'org-refile nil nil nil "Copy")))
  10676. (defun org-refile (&optional arg default-buffer rfloc msg)
  10677. "Move the entry or entries at point to another heading.
  10678. The list of target headings is compiled using the information in
  10679. `org-refile-targets', which see.
  10680. At the target location, the entry is filed as a subitem of the
  10681. target heading. Depending on `org-reverse-note-order', the new
  10682. subitem will either be the first or the last subitem.
  10683. If there is an active region, all entries in that region will be
  10684. refiled. However, the region must fulfill the requirement that
  10685. the first heading sets the top-level of the moved text.
  10686. With prefix arg ARG, the command will only visit the target
  10687. location and not actually move anything.
  10688. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10689. refiling operation has put the subtree.
  10690. With a numeric prefix argument of `2', refile to the running clock.
  10691. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10692. being set to t and copy to the target location, don't move it.
  10693. Beware that keeping refiled entries may result in duplicated ID
  10694. properties.
  10695. RFLOC can be a refile location obtained in a different way.
  10696. MSG is a string to replace \"Refile\" in the default prompt with
  10697. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10698. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10699. If you are using target caching (see `org-refile-use-cache'), you
  10700. have to clear the target cache in order to find new targets.
  10701. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10702. prefix argument (`C-u C-u C-u C-c C-w')."
  10703. (interactive "P")
  10704. (if (member arg '(0 (64)))
  10705. (org-refile-cache-clear)
  10706. (let* ((actionmsg (cond (msg msg)
  10707. ((equal arg 3) "Refile (and keep)")
  10708. (t "Refile")))
  10709. (cbuf (current-buffer))
  10710. (regionp (org-region-active-p))
  10711. (region-start (and regionp (region-beginning)))
  10712. (region-end (and regionp (region-end)))
  10713. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10714. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10715. pos it nbuf file re level reversed)
  10716. (setq last-command nil)
  10717. (when regionp
  10718. (goto-char region-start)
  10719. (or (bolp) (goto-char (point-at-bol)))
  10720. (setq region-start (point))
  10721. (unless (or (org-kill-is-subtree-p
  10722. (buffer-substring region-start region-end))
  10723. (prog1 org-refile-active-region-within-subtree
  10724. (let ((s (point-at-eol)))
  10725. (org-toggle-heading)
  10726. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10727. (user-error "The region is not a (sequence of) subtree(s)")))
  10728. (if (equal arg '(16))
  10729. (org-refile-goto-last-stored)
  10730. (when (or
  10731. (and (equal arg 2)
  10732. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10733. (prog1
  10734. (setq it (list (or org-clock-heading "running clock")
  10735. (buffer-file-name
  10736. (marker-buffer org-clock-hd-marker))
  10737. ""
  10738. (marker-position org-clock-hd-marker)))
  10739. (setq arg nil)))
  10740. (setq it (or rfloc
  10741. (let (heading-text)
  10742. (save-excursion
  10743. (unless (and arg (listp arg))
  10744. (org-back-to-heading t)
  10745. (setq heading-text
  10746. (replace-regexp-in-string
  10747. org-bracket-link-regexp
  10748. "\\3"
  10749. (nth 4 (org-heading-components)))))
  10750. (org-refile-get-location
  10751. (cond ((and arg (listp arg)) "Goto")
  10752. (regionp (concat actionmsg " region to"))
  10753. (t (concat actionmsg " subtree \""
  10754. heading-text "\" to")))
  10755. default-buffer
  10756. (and (not (equal '(4) arg))
  10757. org-refile-allow-creating-parent-nodes)
  10758. arg))))))
  10759. (setq file (nth 1 it)
  10760. re (nth 2 it)
  10761. pos (nth 3 it))
  10762. (if (and (not arg)
  10763. pos
  10764. (equal (buffer-file-name) file)
  10765. (if regionp
  10766. (and (>= pos region-start)
  10767. (<= pos region-end))
  10768. (and (>= pos (point))
  10769. (< pos (save-excursion
  10770. (org-end-of-subtree t t))))))
  10771. (error "Cannot refile to position inside the tree or region"))
  10772. (setq nbuf (or (find-buffer-visiting file)
  10773. (find-file-noselect file)))
  10774. (if (and arg (not (equal arg 3)))
  10775. (progn
  10776. (org-pop-to-buffer-same-window nbuf)
  10777. (goto-char pos)
  10778. (org-show-context 'org-goto))
  10779. (if regionp
  10780. (progn
  10781. (org-kill-new (buffer-substring region-start region-end))
  10782. (org-save-markers-in-region region-start region-end))
  10783. (org-copy-subtree 1 nil t))
  10784. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10785. (find-file-noselect file)))
  10786. (setq reversed (org-notes-order-reversed-p))
  10787. (save-excursion
  10788. (save-restriction
  10789. (widen)
  10790. (if pos
  10791. (progn
  10792. (goto-char pos)
  10793. (looking-at org-outline-regexp)
  10794. (setq level (org-get-valid-level (funcall outline-level) 1))
  10795. (goto-char
  10796. (if reversed
  10797. (or (outline-next-heading) (point-max))
  10798. (or (save-excursion (org-get-next-sibling))
  10799. (org-end-of-subtree t t)
  10800. (point-max)))))
  10801. (setq level 1)
  10802. (if (not reversed)
  10803. (goto-char (point-max))
  10804. (goto-char (point-min))
  10805. (or (outline-next-heading) (goto-char (point-max)))))
  10806. (if (not (bolp)) (newline))
  10807. (org-paste-subtree level nil nil t)
  10808. (when org-log-refile
  10809. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10810. (unless (eq org-log-refile 'note)
  10811. (save-excursion (org-add-log-note))))
  10812. (and org-auto-align-tags
  10813. (let ((org-loop-over-headlines-in-active-region nil))
  10814. (org-set-tags nil t)))
  10815. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10816. :last-refile)))
  10817. (when bookmark-name
  10818. (with-demoted-errors
  10819. (bookmark-set bookmark-name))))
  10820. ;; If we are refiling for capture, make sure that the
  10821. ;; last-capture pointers point here
  10822. (when (org-bound-and-true-p org-refile-for-capture)
  10823. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10824. :last-capture-marker)))
  10825. (when bookmark-name
  10826. (with-demoted-errors
  10827. (bookmark-set bookmark-name))))
  10828. (move-marker org-capture-last-stored-marker (point)))
  10829. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10830. (run-hooks 'org-after-refile-insert-hook))))
  10831. (unless org-refile-keep
  10832. (if regionp
  10833. (delete-region (point) (+ (point) (- region-end region-start)))
  10834. (delete-region
  10835. (and (org-back-to-heading t) (point))
  10836. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10837. (when (featurep 'org-inlinetask)
  10838. (org-inlinetask-remove-END-maybe))
  10839. (setq org-markers-to-move nil)
  10840. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10841. (defun org-refile-goto-last-stored ()
  10842. "Go to the location where the last refile was stored."
  10843. (interactive)
  10844. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10845. (message "This is the location of the last refile"))
  10846. (defun org-refile--get-location (refloc tbl)
  10847. "When user refile to REFLOC, find the associated target in TBL.
  10848. Also check `org-refile-target-table'."
  10849. (car (delq
  10850. nil
  10851. (mapcar
  10852. (lambda (r) (or (assoc r tbl)
  10853. (assoc r org-refile-target-table)))
  10854. (list (replace-regexp-in-string "/$" "" refloc)
  10855. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10856. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10857. no-exclude)
  10858. "Prompt the user for a refile location, using PROMPT.
  10859. PROMPT should not be suffixed with a colon and a space, because
  10860. this function appends the default value from
  10861. `org-refile-history' automatically, if that is not empty.
  10862. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10863. this is used for the GOTO interface."
  10864. (let ((org-refile-targets org-refile-targets)
  10865. (org-refile-use-outline-path org-refile-use-outline-path)
  10866. excluded-entries)
  10867. (when (and (derived-mode-p 'org-mode)
  10868. (not org-refile-use-cache)
  10869. (not no-exclude))
  10870. (org-map-tree
  10871. (lambda()
  10872. (setq excluded-entries
  10873. (append excluded-entries (list (org-get-heading t t)))))))
  10874. (setq org-refile-target-table
  10875. (org-refile-get-targets default-buffer excluded-entries)))
  10876. (unless org-refile-target-table
  10877. (user-error "No refile targets"))
  10878. (let* ((cbuf (current-buffer))
  10879. (partial-completion-mode nil)
  10880. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10881. (cfunc (if (and org-refile-use-outline-path
  10882. org-outline-path-complete-in-steps)
  10883. 'org-olpath-completing-read
  10884. 'org-icompleting-read))
  10885. (extra (if org-refile-use-outline-path "/" ""))
  10886. (cbnex (concat (buffer-name) extra))
  10887. (filename (and cfn (expand-file-name cfn)))
  10888. (tbl (mapcar
  10889. (lambda (x)
  10890. (if (and (not (member org-refile-use-outline-path
  10891. '(file full-file-path)))
  10892. (not (equal filename (nth 1 x))))
  10893. (cons (concat (car x) extra " ("
  10894. (file-name-nondirectory (nth 1 x)) ")")
  10895. (cdr x))
  10896. (cons (concat (car x) extra) (cdr x))))
  10897. org-refile-target-table))
  10898. (completion-ignore-case t)
  10899. cdef
  10900. (prompt (concat prompt
  10901. (or (and (car org-refile-history)
  10902. (concat " (default " (car org-refile-history) ")"))
  10903. (and (assoc cbnex tbl) (setq cdef cbnex)
  10904. (concat " (default " cbnex ")"))) ": "))
  10905. pa answ parent-target child parent old-hist)
  10906. (setq old-hist org-refile-history)
  10907. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10908. nil 'org-refile-history (or cdef (car org-refile-history))))
  10909. (if (setq pa (org-refile--get-location answ tbl))
  10910. (progn
  10911. (org-refile-check-position pa)
  10912. (when (or (not org-refile-history)
  10913. (not (eq old-hist org-refile-history))
  10914. (not (equal (car pa) (car org-refile-history))))
  10915. (setq org-refile-history
  10916. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10917. org-refile-history
  10918. (cdr org-refile-history))))
  10919. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10920. (pop org-refile-history)))
  10921. pa)
  10922. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10923. (progn
  10924. (setq parent (match-string 1 answ)
  10925. child (match-string 2 answ))
  10926. (setq parent-target (org-refile--get-location parent tbl))
  10927. (when (and parent-target
  10928. (or (eq new-nodes t)
  10929. (and (eq new-nodes 'confirm)
  10930. (y-or-n-p (format "Create new node \"%s\"? "
  10931. child)))))
  10932. (org-refile-new-child parent-target child)))
  10933. (user-error "Invalid target location")))))
  10934. (declare-function org-string-nw-p "org-macs" (s))
  10935. (defun org-refile-check-position (refile-pointer)
  10936. "Check if the refile pointer matches the headline to which it points."
  10937. (let* ((file (nth 1 refile-pointer))
  10938. (re (nth 2 refile-pointer))
  10939. (pos (nth 3 refile-pointer))
  10940. buffer)
  10941. (if (and (not (markerp pos)) (not file))
  10942. (user-error "Please indicate a target file in the refile path")
  10943. (when (org-string-nw-p re)
  10944. (setq buffer (if (markerp pos)
  10945. (marker-buffer pos)
  10946. (or (find-buffer-visiting file)
  10947. (find-file-noselect file))))
  10948. (with-current-buffer buffer
  10949. (save-excursion
  10950. (save-restriction
  10951. (widen)
  10952. (goto-char pos)
  10953. (beginning-of-line 1)
  10954. (unless (org-looking-at-p re)
  10955. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10956. (defun org-refile-new-child (parent-target child)
  10957. "Use refile target PARENT-TARGET to add new CHILD below it."
  10958. (unless parent-target
  10959. (error "Cannot find parent for new node"))
  10960. (let ((file (nth 1 parent-target))
  10961. (pos (nth 3 parent-target))
  10962. level)
  10963. (with-current-buffer (or (find-buffer-visiting file)
  10964. (find-file-noselect file))
  10965. (save-excursion
  10966. (save-restriction
  10967. (widen)
  10968. (if pos
  10969. (goto-char pos)
  10970. (goto-char (point-max))
  10971. (if (not (bolp)) (newline)))
  10972. (when (looking-at org-outline-regexp)
  10973. (setq level (funcall outline-level))
  10974. (org-end-of-subtree t t))
  10975. (org-back-over-empty-lines)
  10976. (insert "\n" (make-string
  10977. (if pos (org-get-valid-level level 1) 1) ?*)
  10978. " " child "\n")
  10979. (beginning-of-line 0)
  10980. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10981. (defun org-olpath-completing-read (prompt collection &rest args)
  10982. "Read an outline path like a file name."
  10983. (let ((thetable collection)
  10984. (org-completion-use-ido nil) ; does not work with ido.
  10985. (org-completion-use-iswitchb nil)) ; or iswitchb
  10986. (apply #'org-icompleting-read
  10987. prompt
  10988. (lambda (string predicate &optional flag)
  10989. (cond
  10990. ((eq flag nil) (try-completion string thetable))
  10991. ((eq flag t)
  10992. (let ((l (length string)))
  10993. (mapcar (lambda (x)
  10994. (let ((r (substring x l))
  10995. (f (if (string-match " ([^)]*)$" x)
  10996. (match-string 0 x)
  10997. "")))
  10998. (if (string-match "/" r)
  10999. (concat string (substring r 0 (match-end 0)) f)
  11000. x)))
  11001. (all-completions string thetable predicate))))
  11002. ;; Exact match?
  11003. ((eq flag 'lambda) (assoc string thetable))))
  11004. args)))
  11005. ;;;; Dynamic blocks
  11006. (defun org-find-dblock (name)
  11007. "Find the first dynamic block with name NAME in the buffer.
  11008. If not found, stay at current position and return nil."
  11009. (let ((case-fold-search t) pos)
  11010. (save-excursion
  11011. (goto-char (point-min))
  11012. (setq pos (and (re-search-forward
  11013. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  11014. (match-beginning 0))))
  11015. (if pos (goto-char pos))
  11016. pos))
  11017. (defun org-create-dblock (plist)
  11018. "Create a dynamic block section, with parameters taken from PLIST.
  11019. PLIST must contain a :name entry which is used as the name of the block."
  11020. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11021. (end-of-line 1)
  11022. (newline))
  11023. (let ((col (current-column))
  11024. (name (plist-get plist :name)))
  11025. (insert "#+BEGIN: " name)
  11026. (while plist
  11027. (if (eq (car plist) :name)
  11028. (setq plist (cddr plist))
  11029. (insert " " (prin1-to-string (pop plist)))))
  11030. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11031. (beginning-of-line -2)))
  11032. (defun org-prepare-dblock ()
  11033. "Prepare dynamic block for refresh.
  11034. This empties the block, puts the cursor at the insert position and returns
  11035. the property list including an extra property :name with the block name."
  11036. (unless (looking-at org-dblock-start-re)
  11037. (user-error "Not at a dynamic block"))
  11038. (let* ((begdel (1+ (match-end 0)))
  11039. (name (org-no-properties (match-string 1)))
  11040. (params (append (list :name name)
  11041. (read (concat "(" (match-string 3) ")")))))
  11042. (save-excursion
  11043. (beginning-of-line 1)
  11044. (skip-chars-forward " \t")
  11045. (setq params (plist-put params :indentation-column (current-column))))
  11046. (unless (re-search-forward org-dblock-end-re nil t)
  11047. (error "Dynamic block not terminated"))
  11048. (setq params
  11049. (append params
  11050. (list :content (buffer-substring
  11051. begdel (match-beginning 0)))))
  11052. (delete-region begdel (match-beginning 0))
  11053. (goto-char begdel)
  11054. (open-line 1)
  11055. params))
  11056. (defun org-map-dblocks (&optional command)
  11057. "Apply COMMAND to all dynamic blocks in the current buffer.
  11058. If COMMAND is not given, use `org-update-dblock'."
  11059. (let ((cmd (or command 'org-update-dblock)))
  11060. (save-excursion
  11061. (goto-char (point-min))
  11062. (while (re-search-forward org-dblock-start-re nil t)
  11063. (goto-char (match-beginning 0))
  11064. (save-excursion
  11065. (condition-case nil
  11066. (funcall cmd)
  11067. (error (message "Error during update of dynamic block"))))
  11068. (unless (re-search-forward org-dblock-end-re nil t)
  11069. (error "Dynamic block not terminated"))))))
  11070. (defun org-dblock-update (&optional arg)
  11071. "User command for updating dynamic blocks.
  11072. Update the dynamic block at point. With prefix ARG, update all dynamic
  11073. blocks in the buffer."
  11074. (interactive "P")
  11075. (if arg
  11076. (org-update-all-dblocks)
  11077. (or (looking-at org-dblock-start-re)
  11078. (org-beginning-of-dblock))
  11079. (org-update-dblock)))
  11080. (defun org-update-dblock ()
  11081. "Update the dynamic block at point.
  11082. This means to empty the block, parse for parameters and then call
  11083. the correct writing function."
  11084. (interactive)
  11085. (save-excursion
  11086. (let* ((win (selected-window))
  11087. (pos (point))
  11088. (line (org-current-line))
  11089. (params (org-prepare-dblock))
  11090. (name (plist-get params :name))
  11091. (indent (plist-get params :indentation-column))
  11092. (cmd (intern (concat "org-dblock-write:" name))))
  11093. (message "Updating dynamic block `%s' at line %d..." name line)
  11094. (funcall cmd params)
  11095. (message "Updating dynamic block `%s' at line %d...done" name line)
  11096. (goto-char pos)
  11097. (when (and indent (> indent 0))
  11098. (setq indent (make-string indent ?\ ))
  11099. (save-excursion
  11100. (select-window win)
  11101. (org-beginning-of-dblock)
  11102. (forward-line 1)
  11103. (while (not (looking-at org-dblock-end-re))
  11104. (insert indent)
  11105. (beginning-of-line 2))
  11106. (when (looking-at org-dblock-end-re)
  11107. (and (looking-at "[ \t]+")
  11108. (replace-match ""))
  11109. (insert indent)))))))
  11110. (defun org-beginning-of-dblock ()
  11111. "Find the beginning of the dynamic block at point.
  11112. Error if there is no such block at point."
  11113. (let ((pos (point))
  11114. beg)
  11115. (end-of-line 1)
  11116. (if (and (re-search-backward org-dblock-start-re nil t)
  11117. (setq beg (match-beginning 0))
  11118. (re-search-forward org-dblock-end-re nil t)
  11119. (> (match-end 0) pos))
  11120. (goto-char beg)
  11121. (goto-char pos)
  11122. (error "Not in a dynamic block"))))
  11123. (defun org-update-all-dblocks ()
  11124. "Update all dynamic blocks in the buffer.
  11125. This function can be used in a hook."
  11126. (interactive)
  11127. (when (derived-mode-p 'org-mode)
  11128. (org-map-dblocks 'org-update-dblock)))
  11129. ;;;; Completion
  11130. (declare-function org-export-backend-name "org-export" (cl-x))
  11131. (declare-function org-export-backend-options "org-export" (cl-x))
  11132. (defun org-get-export-keywords ()
  11133. "Return a list of all currently understood export keywords.
  11134. Export keywords include options, block names, attributes and
  11135. keywords relative to each registered export back-end."
  11136. (let (keywords)
  11137. (dolist (backend
  11138. (org-bound-and-true-p org-export-registered-backends)
  11139. (delq nil keywords))
  11140. ;; Back-end name (for keywords, like #+LATEX:)
  11141. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11142. (dolist (option-entry (org-export-backend-options backend))
  11143. ;; Back-end options.
  11144. (push (nth 1 option-entry) keywords)))))
  11145. (defconst org-options-keywords
  11146. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11147. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11148. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11149. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11150. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11151. (defcustom org-structure-template-alist
  11152. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11153. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11154. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11155. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11156. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11157. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11158. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11159. ("L" "#+LaTeX: ")
  11160. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11161. ("H" "#+HTML: ")
  11162. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11163. ("A" "#+ASCII: ")
  11164. ("i" "#+INDEX: ?")
  11165. ("I" "#+INCLUDE: %file ?"))
  11166. "Structure completion elements.
  11167. This is a list of abbreviation keys and values. The value gets inserted
  11168. if you type `<' followed by the key and then press the completion key,
  11169. usually `TAB'. %file will be replaced by a file name after prompting
  11170. for the file using completion. The cursor will be placed at the position
  11171. of the `?' in the template.
  11172. There are two templates for each key, the first uses the original Org syntax,
  11173. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11174. the default when the /org-mtags.el/ module has been loaded. See also the
  11175. variable `org-mtags-prefer-muse-templates'."
  11176. :group 'org-completion
  11177. :type '(repeat
  11178. (list
  11179. (string :tag "Key")
  11180. (string :tag "Template")))
  11181. :version "25.1"
  11182. :package-version '(Org . "8.3"))
  11183. (defun org-try-structure-completion ()
  11184. "Try to complete a structure template before point.
  11185. This looks for strings like \"<e\" on an otherwise empty line and
  11186. expands them."
  11187. (let ((l (buffer-substring (point-at-bol) (point)))
  11188. a)
  11189. (when (and (looking-at "[ \t]*$")
  11190. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11191. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11192. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11193. (match-beginning 1)) a)
  11194. t)))
  11195. (defun org-complete-expand-structure-template (start cell)
  11196. "Expand a structure template."
  11197. (let ((rpl (nth 1 cell))
  11198. (ind ""))
  11199. (delete-region start (point))
  11200. (when (string-match "\\`[ \t]*#\\+" rpl)
  11201. (cond
  11202. ((bolp))
  11203. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11204. (setq ind (buffer-substring (point-at-bol) (point))))
  11205. (t (newline))))
  11206. (setq start (point))
  11207. (if (string-match "%file" rpl)
  11208. (setq rpl (replace-match
  11209. (concat
  11210. "\""
  11211. (save-match-data
  11212. (abbreviate-file-name (read-file-name "Include file: ")))
  11213. "\"")
  11214. t t rpl)))
  11215. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11216. (concat "\n" ind)))
  11217. (insert rpl)
  11218. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11219. ;;;; TODO, DEADLINE, Comments
  11220. (defun org-toggle-comment ()
  11221. "Change the COMMENT state of an entry."
  11222. (interactive)
  11223. (save-excursion
  11224. (org-back-to-heading)
  11225. (looking-at org-complex-heading-regexp)
  11226. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11227. (skip-chars-forward " \t")
  11228. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11229. (if (org-in-commented-heading-p t)
  11230. (delete-region (point)
  11231. (progn (search-forward " " (line-end-position) 'move)
  11232. (skip-chars-forward " \t")
  11233. (point)))
  11234. (insert org-comment-string)
  11235. (unless (eolp) (insert " ")))))
  11236. (defvar org-last-todo-state-is-todo nil
  11237. "This is non-nil when the last TODO state change led to a TODO state.
  11238. If the last change removed the TODO tag or switched to DONE, then
  11239. this is nil.")
  11240. (defvar org-setting-tags nil) ; dynamically skipped
  11241. (defvar org-todo-setup-filter-hook nil
  11242. "Hook for functions that pre-filter todo specs.
  11243. Each function takes a todo spec and returns either nil or the spec
  11244. transformed into canonical form." )
  11245. (defvar org-todo-get-default-hook nil
  11246. "Hook for functions that get a default item for todo.
  11247. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11248. nil or a string to be used for the todo mark." )
  11249. (defvar org-agenda-headline-snapshot-before-repeat)
  11250. (defun org-current-effective-time ()
  11251. "Return current time adjusted for `org-extend-today-until' variable."
  11252. (let* ((ct (org-current-time))
  11253. (dct (decode-time ct))
  11254. (ct1
  11255. (cond
  11256. (org-use-last-clock-out-time-as-effective-time
  11257. (or (org-clock-get-last-clock-out-time) ct))
  11258. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11259. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11260. (t ct))))
  11261. ct1))
  11262. (defun org-todo-yesterday (&optional arg)
  11263. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11264. (interactive "P")
  11265. (if (eq major-mode 'org-agenda-mode)
  11266. (apply 'org-agenda-todo-yesterday arg)
  11267. (let* ((org-use-effective-time t)
  11268. (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. "Non-nil if entry at point is blocked."
  11654. (and (not (org-entry-get nil "NOBLOCKING"))
  11655. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11656. (not (run-hook-with-args-until-failure
  11657. 'org-blocker-hook
  11658. (list :type 'todo-state-change
  11659. :position (point)
  11660. :from 'todo
  11661. :to 'done)))))
  11662. (defun org-update-statistics-cookies (all)
  11663. "Update the statistics cookie, either from TODO or from checkboxes.
  11664. This should be called with the cursor in a line with a statistics cookie."
  11665. (interactive "P")
  11666. (if all
  11667. (progn
  11668. (org-update-checkbox-count 'all)
  11669. (org-map-entries 'org-update-parent-todo-statistics))
  11670. (if (not (org-at-heading-p))
  11671. (org-update-checkbox-count)
  11672. (let ((pos (point-marker))
  11673. end l1 l2)
  11674. (ignore-errors (org-back-to-heading t))
  11675. (if (not (org-at-heading-p))
  11676. (org-update-checkbox-count)
  11677. (setq l1 (org-outline-level))
  11678. (setq end (save-excursion
  11679. (outline-next-heading)
  11680. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11681. (point)))
  11682. (if (and (save-excursion
  11683. (re-search-forward
  11684. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11685. (not (save-excursion (re-search-forward
  11686. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11687. (org-update-checkbox-count)
  11688. (if (and l2 (> l2 l1))
  11689. (progn
  11690. (goto-char end)
  11691. (org-update-parent-todo-statistics))
  11692. (goto-char pos)
  11693. (beginning-of-line 1)
  11694. (while (re-search-forward
  11695. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11696. (point-at-eol) t)
  11697. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11698. (goto-char pos)
  11699. (move-marker pos nil)))))
  11700. (defvar org-entry-property-inherited-from) ;; defined below
  11701. (defun org-update-parent-todo-statistics ()
  11702. "Update any statistics cookie in the parent of the current headline.
  11703. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11704. the entire subtree and this will travel up the hierarchy and update
  11705. statistics everywhere."
  11706. (let* ((prop (save-excursion (org-up-heading-safe)
  11707. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11708. (recursive (or (not org-hierarchical-todo-statistics)
  11709. (and prop (string-match "\\<recursive\\>" prop))))
  11710. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11711. 0))
  11712. (first t)
  11713. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11714. level ltoggle l1 new ndel
  11715. (cnt-all 0) (cnt-done 0) is-percent kwd
  11716. checkbox-beg ov ovs ove cookie-present)
  11717. (catch 'exit
  11718. (save-excursion
  11719. (beginning-of-line 1)
  11720. (setq ltoggle (funcall outline-level))
  11721. ;; Three situations are to consider:
  11722. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11723. ;; to the top-level ancestor on the headline;
  11724. ;; 2. If parent has "recursive" property, repeat up to the
  11725. ;; headline setting that property, taking inheritance into
  11726. ;; account;
  11727. ;; 3. Else, move up to direct parent and proceed only once.
  11728. (while (and (setq level (org-up-heading-safe))
  11729. (or recursive first)
  11730. (>= (point) lim))
  11731. (setq first nil cookie-present nil)
  11732. (unless (and level
  11733. (not (string-match
  11734. "\\<checkbox\\>"
  11735. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11736. "")))))
  11737. (throw 'exit nil))
  11738. (while (re-search-forward box-re (point-at-eol) t)
  11739. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11740. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11741. (save-match-data
  11742. (unless (outline-next-heading) (throw 'exit nil))
  11743. (while (and (looking-at org-complex-heading-regexp)
  11744. (> (setq l1 (length (match-string 1))) level))
  11745. (setq kwd (and (or recursive (= l1 ltoggle))
  11746. (match-string 2)))
  11747. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11748. (and (eq org-provide-todo-statistics t)
  11749. (or (member kwd org-done-keywords)))
  11750. (and (listp org-provide-todo-statistics)
  11751. (stringp (car org-provide-todo-statistics))
  11752. (or (member kwd org-provide-todo-statistics)
  11753. (member kwd org-done-keywords)))
  11754. (and (listp org-provide-todo-statistics)
  11755. (listp (car org-provide-todo-statistics))
  11756. (or (member kwd (car org-provide-todo-statistics))
  11757. (and (member kwd org-done-keywords)
  11758. (member kwd (cadr org-provide-todo-statistics))))))
  11759. (setq cnt-all (1+ cnt-all))
  11760. (if (eq org-provide-todo-statistics t)
  11761. (and kwd (setq cnt-all (1+ cnt-all)))))
  11762. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11763. (member kwd org-done-keywords))
  11764. (and (listp org-provide-todo-statistics)
  11765. (listp (car org-provide-todo-statistics))
  11766. (member kwd org-done-keywords)
  11767. (member kwd (cadr org-provide-todo-statistics)))
  11768. (and (listp org-provide-todo-statistics)
  11769. (stringp (car org-provide-todo-statistics))
  11770. (member kwd org-done-keywords)))
  11771. (setq cnt-done (1+ cnt-done)))
  11772. (outline-next-heading)))
  11773. (setq new
  11774. (if is-percent
  11775. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11776. (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. ;; Directly set the variables, they are already local.
  11826. (setq org-log-done nil
  11827. org-log-repeat nil
  11828. org-todo-log-states nil)
  11829. (dolist (w (org-split-string value))
  11830. (let (a)
  11831. (cond
  11832. ((setq a (assoc w org-startup-options))
  11833. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11834. (set (nth 1 a) (nth 2 a))))
  11835. ((setq a (org-extract-log-state-settings w))
  11836. (and (member (car a) org-todo-keywords-1)
  11837. (push a org-todo-log-states)))))))
  11838. (defun org-get-todo-sequence-head (kwd)
  11839. "Return the head of the TODO sequence to which KWD belongs.
  11840. If KWD is not set, check if there is a text property remembering the
  11841. right sequence."
  11842. (let (p)
  11843. (cond
  11844. ((not kwd)
  11845. (or (get-text-property (point-at-bol) 'org-todo-head)
  11846. (progn
  11847. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11848. nil (point-at-eol)))
  11849. (get-text-property p 'org-todo-head))))
  11850. ((not (member kwd org-todo-keywords-1))
  11851. (car org-todo-keywords-1))
  11852. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11853. (defun org-fast-todo-selection ()
  11854. "Fast TODO keyword selection with single keys.
  11855. Returns the new TODO keyword, or nil if no state change should occur."
  11856. (let* ((fulltable org-todo-key-alist)
  11857. (done-keywords org-done-keywords) ;; needed for the faces.
  11858. (maxlen (apply 'max (mapcar
  11859. (lambda (x)
  11860. (if (stringp (car x)) (string-width (car x)) 0))
  11861. fulltable)))
  11862. (expert nil)
  11863. (fwidth (+ maxlen 3 1 3))
  11864. (ncol (/ (- (window-width) 4) fwidth))
  11865. tg cnt e c tbl
  11866. groups ingroup)
  11867. (save-excursion
  11868. (save-window-excursion
  11869. (if expert
  11870. (set-buffer (get-buffer-create " *Org todo*"))
  11871. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11872. (erase-buffer)
  11873. (org-set-local 'org-done-keywords done-keywords)
  11874. (setq tbl fulltable cnt 0)
  11875. (while (setq e (pop tbl))
  11876. (cond
  11877. ((equal e '(:startgroup))
  11878. (push '() groups) (setq ingroup t)
  11879. (when (not (= cnt 0))
  11880. (setq cnt 0)
  11881. (insert "\n"))
  11882. (insert "{ "))
  11883. ((equal e '(:endgroup))
  11884. (setq ingroup nil cnt 0)
  11885. (insert "}\n"))
  11886. ((equal e '(:newline))
  11887. (when (not (= cnt 0))
  11888. (setq cnt 0)
  11889. (insert "\n")
  11890. (setq e (car tbl))
  11891. (while (equal (car tbl) '(:newline))
  11892. (insert "\n")
  11893. (setq tbl (cdr tbl)))))
  11894. (t
  11895. (setq tg (car e) c (cdr e))
  11896. (if ingroup (push tg (car groups)))
  11897. (setq tg (org-add-props tg nil 'face
  11898. (org-get-todo-face tg)))
  11899. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11900. (insert "[" c "] " tg (make-string
  11901. (- fwidth 4 (length tg)) ?\ ))
  11902. (when (= (setq cnt (1+ cnt)) ncol)
  11903. (insert "\n")
  11904. (if ingroup (insert " "))
  11905. (setq cnt 0)))))
  11906. (insert "\n")
  11907. (goto-char (point-min))
  11908. (if (not expert) (org-fit-window-to-buffer))
  11909. (message "[a-z..]:Set [SPC]:clear")
  11910. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11911. (cond
  11912. ((or (= c ?\C-g)
  11913. (and (= c ?q) (not (rassoc c fulltable))))
  11914. (setq quit-flag t))
  11915. ((= c ?\ ) nil)
  11916. ((setq e (rassoc c fulltable) tg (car e))
  11917. tg)
  11918. (t (setq quit-flag t)))))))
  11919. (defun org-entry-is-todo-p ()
  11920. (member (org-get-todo-state) org-not-done-keywords))
  11921. (defun org-entry-is-done-p ()
  11922. (member (org-get-todo-state) org-done-keywords))
  11923. (defun org-get-todo-state ()
  11924. "Return the TODO keyword of the current subtree."
  11925. (save-excursion
  11926. (org-back-to-heading t)
  11927. (and (looking-at org-todo-line-regexp)
  11928. (match-end 2)
  11929. (match-string 2))))
  11930. (defun org-at-date-range-p (&optional inactive-ok)
  11931. "Non-nil if point is inside a date range.
  11932. When optional argument INACTIVE-OK is non-nil, also consider
  11933. inactive time ranges.
  11934. When this function returns a non-nil value, match data is set
  11935. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11936. on INACTIVE-OK."
  11937. (interactive)
  11938. (save-excursion
  11939. (catch 'exit
  11940. (let ((pos (point)))
  11941. (skip-chars-backward "^[<\r\n")
  11942. (skip-chars-backward "<[")
  11943. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11944. (>= (match-end 0) pos)
  11945. (throw 'exit t))
  11946. (skip-chars-backward "^<[\r\n")
  11947. (skip-chars-backward "<[")
  11948. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11949. (>= (match-end 0) pos)
  11950. (throw 'exit t)))
  11951. nil)))
  11952. (defun org-get-repeat (&optional tagline)
  11953. "Check if there is a deadline/schedule with repeater in this entry."
  11954. (save-match-data
  11955. (save-excursion
  11956. (org-back-to-heading t)
  11957. (and (re-search-forward (if tagline
  11958. (concat tagline "\\s-*" org-repeat-re)
  11959. org-repeat-re)
  11960. (org-entry-end-position) t)
  11961. (match-string-no-properties 1)))))
  11962. (defvar org-last-changed-timestamp)
  11963. (defvar org-last-inserted-timestamp)
  11964. (defvar org-log-post-message)
  11965. (defvar org-log-note-purpose)
  11966. (defvar org-log-note-how nil)
  11967. (defvar org-log-note-extra)
  11968. (defun org-auto-repeat-maybe (done-word)
  11969. "Check if the current headline contains a repeated deadline/schedule.
  11970. If yes, set TODO state back to what it was and change the base date
  11971. of repeating deadline/scheduled time stamps to new date.
  11972. This function is run automatically after each state change to a DONE state."
  11973. ;; last-state is dynamically scoped into this function
  11974. (let* ((repeat (org-get-repeat))
  11975. (aa (assoc org-last-state org-todo-kwd-alist))
  11976. (interpret (nth 1 aa))
  11977. (head (nth 2 aa))
  11978. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11979. (msg "Entry repeats: ")
  11980. (org-log-done nil)
  11981. (org-todo-log-states nil)
  11982. re type n what ts time to-state)
  11983. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11984. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11985. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11986. org-todo-repeat-to-state))
  11987. (unless (and to-state (member to-state org-todo-keywords-1))
  11988. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11989. (org-todo to-state)
  11990. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11991. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11992. (org-time-stamp-format t t))))
  11993. (when org-log-repeat
  11994. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11995. (memq 'org-add-log-note post-command-hook))
  11996. ;; OK, we are already setup for some record
  11997. (if (eq org-log-repeat 'note)
  11998. ;; make sure we take a note, not only a time stamp
  11999. (setq org-log-note-how 'note))
  12000. ;; Set up for taking a record
  12001. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  12002. org-last-state
  12003. 'findpos org-log-repeat)))
  12004. (org-back-to-heading t)
  12005. (org-add-planning-info nil nil 'closed)
  12006. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  12007. org-deadline-time-regexp "\\)\\|\\("
  12008. org-ts-regexp "\\)"))
  12009. (while (re-search-forward
  12010. re (save-excursion (outline-next-heading) (point)) t)
  12011. (setq type (if (match-end 1) org-scheduled-string
  12012. (if (match-end 3) org-deadline-string "Plain:"))
  12013. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  12014. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  12015. (setq n (string-to-number (match-string 2 ts))
  12016. what (match-string 3 ts))
  12017. (if (equal what "w") (setq n (* n 7) what "d"))
  12018. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  12019. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  12020. ;; Preparation, see if we need to modify the start date for the change
  12021. (when (match-end 1)
  12022. (setq time (save-match-data (org-time-string-to-time ts)))
  12023. (cond
  12024. ((equal (match-string 1 ts) ".")
  12025. ;; Shift starting date to today
  12026. (org-timestamp-change
  12027. (- (org-today) (time-to-days time))
  12028. 'day))
  12029. ((equal (match-string 1 ts) "+")
  12030. (let ((nshiftmax 10) (nshift 0))
  12031. (while (or (= nshift 0)
  12032. (<= (time-to-days time)
  12033. (time-to-days (current-time))))
  12034. (when (= (incf nshift) nshiftmax)
  12035. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12036. (user-error "Abort")))
  12037. (org-timestamp-change n (cdr (assoc what whata)))
  12038. (org-at-timestamp-p t)
  12039. (setq ts (match-string 1))
  12040. (setq time (save-match-data (org-time-string-to-time ts)))))
  12041. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12042. ;; rematch, so that we have everything in place for the real shift
  12043. (org-at-timestamp-p t)
  12044. (setq ts (match-string 1))
  12045. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12046. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12047. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12048. (setq org-log-post-message msg)
  12049. (message "%s" msg))))
  12050. (defun org-show-todo-tree (arg)
  12051. "Make a compact tree which shows all headlines marked with TODO.
  12052. The tree will show the lines where the regexp matches, and all higher
  12053. headlines above the match.
  12054. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12055. With a numeric prefix N, construct a sparse tree for the Nth element
  12056. of `org-todo-keywords-1'."
  12057. (interactive "P")
  12058. (let ((case-fold-search nil)
  12059. (kwd-re
  12060. (cond ((null arg) org-not-done-regexp)
  12061. ((equal arg '(4))
  12062. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12063. (mapcar 'list org-todo-keywords-1))))
  12064. (concat "\\("
  12065. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12066. "\\)\\>")))
  12067. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12068. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12069. org-todo-keywords-1)))
  12070. (t (user-error "Invalid prefix argument: %s" arg)))))
  12071. (message "%d TODO entries found"
  12072. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12073. (defun org-deadline (arg &optional time)
  12074. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12075. With one universal prefix argument, remove any deadline from the item.
  12076. With two universal prefix arguments, prompt for a warning delay.
  12077. With argument TIME, set the deadline at the corresponding date. TIME
  12078. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12079. (interactive "P")
  12080. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12081. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12082. 'region-start-level 'region))
  12083. org-loop-over-headlines-in-active-region)
  12084. (org-map-entries
  12085. `(org-deadline ',arg ,time)
  12086. org-loop-over-headlines-in-active-region
  12087. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12088. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12089. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12090. (repeater (and old-date
  12091. (string-match
  12092. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12093. old-date)
  12094. (match-string 1 old-date))))
  12095. (cond
  12096. ((equal arg '(4))
  12097. (when (and old-date org-log-redeadline)
  12098. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12099. org-log-redeadline))
  12100. (org-remove-timestamp-with-keyword org-deadline-string)
  12101. (message "Item no longer has a deadline."))
  12102. ((equal arg '(16))
  12103. (save-excursion
  12104. (org-back-to-heading t)
  12105. (if (re-search-forward
  12106. org-deadline-time-regexp
  12107. (save-excursion (outline-next-heading) (point)) t)
  12108. (let* ((rpl0 (match-string 1))
  12109. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12110. (replace-match
  12111. (concat org-deadline-string
  12112. " <" rpl
  12113. (format " -%dd"
  12114. (abs
  12115. (- (time-to-days
  12116. (save-match-data
  12117. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12118. (time-to-days old-date-time))))
  12119. ">") t t))
  12120. (user-error "No deadline information to update"))))
  12121. (t
  12122. (org-add-planning-info 'deadline time 'closed)
  12123. (when (and old-date
  12124. org-log-redeadline
  12125. (not (equal old-date org-last-inserted-timestamp)))
  12126. (org-add-log-setup
  12127. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12128. org-log-redeadline))
  12129. (when repeater
  12130. (save-excursion
  12131. (org-back-to-heading t)
  12132. (when (re-search-forward (concat org-deadline-string " "
  12133. org-last-inserted-timestamp)
  12134. (save-excursion
  12135. (outline-next-heading) (point)) t)
  12136. (goto-char (1- (match-end 0)))
  12137. (insert " " repeater)
  12138. (setq org-last-inserted-timestamp
  12139. (concat (substring org-last-inserted-timestamp 0 -1)
  12140. " " repeater
  12141. (substring org-last-inserted-timestamp -1))))))
  12142. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12143. (defun org-schedule (arg &optional time)
  12144. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12145. With one universal prefix argument, remove any scheduling date from the item.
  12146. With two universal prefix arguments, prompt for a delay cookie.
  12147. With argument TIME, scheduled at the corresponding date. TIME can
  12148. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12149. (interactive "P")
  12150. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12151. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12152. 'region-start-level 'region))
  12153. org-loop-over-headlines-in-active-region)
  12154. (org-map-entries
  12155. `(org-schedule ',arg ,time)
  12156. org-loop-over-headlines-in-active-region
  12157. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12158. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12159. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12160. (repeater (and old-date
  12161. (string-match
  12162. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12163. old-date)
  12164. (match-string 1 old-date))))
  12165. (cond
  12166. ((equal arg '(4))
  12167. (progn
  12168. (when (and old-date org-log-reschedule)
  12169. (org-add-log-setup 'delschedule nil old-date 'findpos
  12170. org-log-reschedule))
  12171. (org-remove-timestamp-with-keyword org-scheduled-string)
  12172. (message "Item is no longer scheduled.")))
  12173. ((equal arg '(16))
  12174. (save-excursion
  12175. (org-back-to-heading t)
  12176. (if (re-search-forward
  12177. org-scheduled-time-regexp
  12178. (save-excursion (outline-next-heading) (point)) t)
  12179. (let* ((rpl0 (match-string 1))
  12180. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12181. (replace-match
  12182. (concat org-scheduled-string
  12183. " <" rpl
  12184. (format " -%dd"
  12185. (abs
  12186. (- (time-to-days
  12187. (save-match-data
  12188. (org-read-date nil t nil "Delay until" old-date-time)))
  12189. (time-to-days old-date-time))))
  12190. ">") t t))
  12191. (user-error "No scheduled information to update"))))
  12192. (t
  12193. (org-add-planning-info 'scheduled time 'closed)
  12194. (when (and old-date
  12195. org-log-reschedule
  12196. (not (equal old-date org-last-inserted-timestamp)))
  12197. (org-add-log-setup
  12198. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12199. org-log-reschedule))
  12200. (when repeater
  12201. (save-excursion
  12202. (org-back-to-heading t)
  12203. (when (re-search-forward (concat org-scheduled-string " "
  12204. org-last-inserted-timestamp)
  12205. (save-excursion
  12206. (outline-next-heading) (point)) t)
  12207. (goto-char (1- (match-end 0)))
  12208. (insert " " repeater)
  12209. (setq org-last-inserted-timestamp
  12210. (concat (substring org-last-inserted-timestamp 0 -1)
  12211. " " repeater
  12212. (substring org-last-inserted-timestamp -1))))))
  12213. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12214. (defun org-get-scheduled-time (pom &optional inherit)
  12215. "Get the scheduled time as a time tuple, of a format suitable
  12216. for calling org-schedule with, or if there is no scheduling,
  12217. returns nil."
  12218. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12219. (when time
  12220. (apply 'encode-time (org-parse-time-string time)))))
  12221. (defun org-get-deadline-time (pom &optional inherit)
  12222. "Get the deadline as a time tuple, of a format suitable for
  12223. calling org-deadline with, or if there is no scheduling, returns
  12224. nil."
  12225. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12226. (when time
  12227. (apply 'encode-time (org-parse-time-string time)))))
  12228. (defun org-remove-timestamp-with-keyword (keyword)
  12229. "Remove all time stamps with KEYWORD in the current entry."
  12230. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12231. beg)
  12232. (save-excursion
  12233. (org-back-to-heading t)
  12234. (setq beg (point))
  12235. (outline-next-heading)
  12236. (while (re-search-backward re beg t)
  12237. (replace-match "")
  12238. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12239. (equal (char-before) ?\ ))
  12240. (backward-delete-char 1)
  12241. (if (string-match "^[ \t]*$" (buffer-substring
  12242. (point-at-bol) (point-at-eol)))
  12243. (delete-region (point-at-bol)
  12244. (min (point-max) (1+ (point-at-eol))))))))))
  12245. (defvar org-time-was-given) ; dynamically scoped parameter
  12246. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12247. (defun org-at-planning-p ()
  12248. "Non-nil when point is on a planning info line."
  12249. ;; This is as accurate and faster than `org-element-at-point' since
  12250. ;; planning info location is fixed in the section.
  12251. (org-with-wide-buffer
  12252. (beginning-of-line)
  12253. (and (org-looking-at-p org-planning-line-re)
  12254. (eq (point)
  12255. (ignore-errors
  12256. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12257. (org-back-to-heading t)
  12258. (org-with-limited-levels (org-back-to-heading t)))
  12259. (line-beginning-position 2))))))
  12260. (defun org-add-planning-info (what &optional time &rest remove)
  12261. "Insert new timestamp with keyword in the planning line.
  12262. WHAT indicates what kind of time stamp to add. It is a symbol
  12263. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12264. the time to use. If none is given, the user is prompted for
  12265. a date. REMOVE indicates what kind of entries to remove. An old
  12266. WHAT entry will also be removed."
  12267. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12268. (catch 'exit
  12269. (when (and (memq what '(scheduled deadline))
  12270. (or (not time)
  12271. (and (stringp time)
  12272. (string-match "^[-+]+[0-9]" time))))
  12273. ;; Try to get a default date/time from existing timestamp
  12274. (save-excursion
  12275. (org-back-to-heading t)
  12276. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12277. (when (re-search-forward (if (eq what 'scheduled)
  12278. org-scheduled-time-regexp
  12279. org-deadline-time-regexp)
  12280. end t)
  12281. (setq ts (match-string 1)
  12282. default-time (apply 'encode-time (org-parse-time-string ts))
  12283. default-input (and ts (org-get-compact-tod ts)))))))
  12284. (when what
  12285. (setq time
  12286. (if (stringp time)
  12287. ;; This is a string (relative or absolute), set
  12288. ;; proper date.
  12289. (apply #'encode-time
  12290. (org-read-date-analyze
  12291. time default-time (decode-time default-time)))
  12292. ;; If necessary, get the time from the user
  12293. (or time (org-read-date nil 'to-time nil nil
  12294. default-time default-input)))))
  12295. (org-with-wide-buffer
  12296. (org-back-to-heading t)
  12297. (forward-line)
  12298. (unless (bolp) (insert "\n"))
  12299. (cond ((org-looking-at-p org-planning-line-re)
  12300. ;; Move to current indentation.
  12301. (skip-chars-forward " \t")
  12302. ;; Check if we have to remove something.
  12303. (dolist (type (if what (cons what remove) remove))
  12304. (save-excursion
  12305. (when (re-search-forward
  12306. (case type
  12307. (closed org-closed-time-regexp)
  12308. (deadline org-deadline-time-regexp)
  12309. (scheduled org-scheduled-time-regexp)
  12310. (otherwise
  12311. (error "Invalid planning type: %s" type)))
  12312. (line-end-position) t)
  12313. ;; Delete until next keyword or end of line.
  12314. (delete-region
  12315. (match-beginning 0)
  12316. (if (re-search-forward org-keyword-time-not-clock-regexp
  12317. (line-end-position)
  12318. t)
  12319. (match-beginning 0)
  12320. (line-end-position))))))
  12321. ;; If there is nothing more to add and no more keyword
  12322. ;; is left, remove the line completely.
  12323. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12324. (delete-region (line-beginning-position)
  12325. (line-beginning-position 2))
  12326. ;; If we removed last keyword, do not leave trailing
  12327. ;; white space at the end of line.
  12328. (let ((p (point)))
  12329. (save-excursion
  12330. (end-of-line)
  12331. (unless (= (skip-chars-backward " \t" p) 0)
  12332. (delete-region (point) (line-end-position)))))))
  12333. ((not what) (throw 'exit nil)) ; Nothing to do.
  12334. (t (insert-before-markers "\n")
  12335. (backward-char 1)
  12336. (when org-adapt-indentation
  12337. (org-indent-to-column (1+ (org-outline-level))))))
  12338. (when what
  12339. ;; Insert planning keyword.
  12340. (insert (case what
  12341. (closed org-closed-string)
  12342. (deadline org-deadline-string)
  12343. (scheduled org-scheduled-string)
  12344. (otherwise (error "Invalid planning type: %s" what)))
  12345. " ")
  12346. ;; Insert associated timestamp.
  12347. (let ((ts (org-insert-time-stamp
  12348. time
  12349. (or org-time-was-given
  12350. (and (eq what 'closed) org-log-done-with-time))
  12351. (eq what 'closed)
  12352. nil nil (list org-end-time-was-given))))
  12353. (unless (eolp) (insert " "))
  12354. ts))))))
  12355. (defvar org-log-note-marker (make-marker))
  12356. (defvar org-log-note-purpose nil)
  12357. (defvar org-log-note-state nil)
  12358. (defvar org-log-note-previous-state nil)
  12359. (defvar org-log-note-extra nil)
  12360. (defvar org-log-note-window-configuration nil)
  12361. (defvar org-log-note-return-to (make-marker))
  12362. (defvar org-log-note-effective-time nil
  12363. "Remembered current time so that dynamically scoped
  12364. `org-extend-today-until' affects tha timestamps in state change
  12365. log")
  12366. (defvar org-log-post-message nil
  12367. "Message to be displayed after a log note has been stored.
  12368. The auto-repeater uses this.")
  12369. (defun org-add-note ()
  12370. "Add a note to the current entry.
  12371. This is done in the same way as adding a state change note."
  12372. (interactive)
  12373. (org-add-log-setup 'note nil nil 'findpos nil))
  12374. (defun org-log-beginning (&optional create)
  12375. "Return expected start of log notes in current entry.
  12376. When optional argument CREATE is non-nil, the function creates
  12377. a drawer to store notes, if necessary. Returned position ignores
  12378. narrowing."
  12379. (org-with-wide-buffer
  12380. (org-end-of-meta-data)
  12381. (let ((end (if (org-at-heading-p) (point)
  12382. (save-excursion (outline-next-heading) (point))))
  12383. (drawer (org-log-into-drawer)))
  12384. (cond
  12385. (drawer
  12386. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12387. (case-fold-search t))
  12388. (catch 'exit
  12389. ;; Try to find existing drawer.
  12390. (while (re-search-forward regexp end t)
  12391. (let ((element (org-element-at-point)))
  12392. (when (eq (org-element-type element) 'drawer)
  12393. (let ((cend (org-element-property :contents-end element)))
  12394. (when (and (not org-log-states-order-reversed) cend)
  12395. (goto-char cend)))
  12396. (throw 'exit nil))))
  12397. ;; No drawer found. Create one, if permitted.
  12398. (when create
  12399. (unless (bolp) (insert "\n"))
  12400. (let ((beg (point)))
  12401. (insert ":" drawer ":\n:END:\n")
  12402. (org-indent-region beg (point)))
  12403. (end-of-line -1)))))
  12404. (org-log-state-notes-insert-after-drawers
  12405. (while (and (looking-at org-drawer-regexp)
  12406. (progn (goto-char (match-end 0))
  12407. (re-search-forward org-property-end-re end t)))
  12408. (forward-line)))))
  12409. (if (bolp) (point) (line-beginning-position 2))))
  12410. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12411. "Set up the post command hook to take a note.
  12412. If this is about to TODO state change, the new state is expected in STATE.
  12413. When FINDPOS is non-nil, find the correct position for the note in
  12414. the current entry. If not, assume that it can be inserted at point.
  12415. HOW is an indicator what kind of note should be created.
  12416. EXTRA is additional text that will be inserted into the notes buffer."
  12417. (org-with-wide-buffer
  12418. (when findpos
  12419. (goto-char (org-log-beginning t))
  12420. (unless org-log-states-order-reversed
  12421. (org-skip-over-state-notes)
  12422. (skip-chars-backward " \t\n\r")
  12423. (forward-line)))
  12424. (move-marker org-log-note-marker (point))
  12425. ;; Preserve position even if a property drawer is inserted in the
  12426. ;; process.
  12427. (set-marker-insertion-type org-log-note-marker t)
  12428. (setq org-log-note-purpose purpose
  12429. org-log-note-state state
  12430. org-log-note-previous-state prev-state
  12431. org-log-note-how how
  12432. org-log-note-extra extra
  12433. org-log-note-effective-time (org-current-effective-time))
  12434. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12435. (defun org-skip-over-state-notes ()
  12436. "Skip past the list of State notes in an entry."
  12437. (when (ignore-errors (goto-char (org-in-item-p)))
  12438. (let* ((struct (org-list-struct))
  12439. (prevs (org-list-prevs-alist struct))
  12440. (regexp
  12441. (concat "[ \t]*- +"
  12442. (replace-regexp-in-string
  12443. " +" " +"
  12444. (org-replace-escapes
  12445. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12446. `(("%d" . ,org-ts-regexp-inactive)
  12447. ("%D" . ,org-ts-regexp)
  12448. ("%s" . "\"\\S-+\"")
  12449. ("%S" . "\"\\S-+\"")
  12450. ("%t" . ,org-ts-regexp-inactive)
  12451. ("%T" . ,org-ts-regexp)
  12452. ("%u" . ".*?")
  12453. ("%U" . ".*?")))))))
  12454. (while (org-looking-at-p regexp)
  12455. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12456. (org-list-get-item-end (point) struct)))))))
  12457. (defun org-add-log-note (&optional purpose)
  12458. "Pop up a window for taking a note, and add this note later at point."
  12459. (remove-hook 'post-command-hook 'org-add-log-note)
  12460. (setq org-log-note-window-configuration (current-window-configuration))
  12461. (delete-other-windows)
  12462. (move-marker org-log-note-return-to (point))
  12463. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12464. (goto-char org-log-note-marker)
  12465. (org-switch-to-buffer-other-window "*Org Note*")
  12466. (erase-buffer)
  12467. (if (memq org-log-note-how '(time state))
  12468. (let (current-prefix-arg) (org-store-log-note))
  12469. (let ((org-inhibit-startup t)) (org-mode))
  12470. (insert (format "# Insert note for %s.
  12471. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12472. (cond
  12473. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12474. ((eq org-log-note-purpose 'done) "closed todo item")
  12475. ((eq org-log-note-purpose 'state)
  12476. (format "state change from \"%s\" to \"%s\""
  12477. (or org-log-note-previous-state "")
  12478. (or org-log-note-state "")))
  12479. ((eq org-log-note-purpose 'reschedule)
  12480. "rescheduling")
  12481. ((eq org-log-note-purpose 'delschedule)
  12482. "no longer scheduled")
  12483. ((eq org-log-note-purpose 'redeadline)
  12484. "changing deadline")
  12485. ((eq org-log-note-purpose 'deldeadline)
  12486. "removing deadline")
  12487. ((eq org-log-note-purpose 'refile)
  12488. "refiling")
  12489. ((eq org-log-note-purpose 'note)
  12490. "this entry")
  12491. (t (error "This should not happen")))))
  12492. (if org-log-note-extra (insert org-log-note-extra))
  12493. (org-set-local 'org-finish-function 'org-store-log-note)
  12494. (run-hooks 'org-log-buffer-setup-hook)))
  12495. (defvar org-note-abort nil) ; dynamically scoped
  12496. (defun org-store-log-note ()
  12497. "Finish taking a log note, and insert it to where it belongs."
  12498. (let ((txt (buffer-string)))
  12499. (kill-buffer (current-buffer))
  12500. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12501. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12502. (setq txt (replace-match "" t t txt)))
  12503. (if (string-match "\\s-+\\'" txt)
  12504. (setq txt (replace-match "" t t txt)))
  12505. (setq lines (org-split-string txt "\n"))
  12506. (when (and note (string-match "\\S-" note))
  12507. (setq note
  12508. (org-replace-escapes
  12509. note
  12510. (list (cons "%u" (user-login-name))
  12511. (cons "%U" user-full-name)
  12512. (cons "%t" (format-time-string
  12513. (org-time-stamp-format 'long 'inactive)
  12514. org-log-note-effective-time))
  12515. (cons "%T" (format-time-string
  12516. (org-time-stamp-format 'long nil)
  12517. org-log-note-effective-time))
  12518. (cons "%d" (format-time-string
  12519. (org-time-stamp-format nil 'inactive)
  12520. org-log-note-effective-time))
  12521. (cons "%D" (format-time-string
  12522. (org-time-stamp-format nil nil)
  12523. org-log-note-effective-time))
  12524. (cons "%s" (cond
  12525. ((not org-log-note-state) "")
  12526. ((org-string-match-p org-ts-regexp
  12527. org-log-note-state)
  12528. (format "\"[%s]\""
  12529. (substring org-log-note-state 1 -1)))
  12530. (t (format "\"%s\"" org-log-note-state))))
  12531. (cons "%S"
  12532. (cond
  12533. ((not org-log-note-previous-state) "")
  12534. ((org-string-match-p org-ts-regexp
  12535. org-log-note-previous-state)
  12536. (format "\"[%s]\""
  12537. (substring
  12538. org-log-note-previous-state 1 -1)))
  12539. (t (format "\"%s\""
  12540. org-log-note-previous-state)))))))
  12541. (when lines (setq note (concat note " \\\\")))
  12542. (push note lines))
  12543. (when (or current-prefix-arg org-note-abort)
  12544. (when (org-log-into-drawer)
  12545. (org-remove-empty-drawer-at org-log-note-marker))
  12546. (setq lines nil))
  12547. (when lines
  12548. (with-current-buffer (marker-buffer org-log-note-marker)
  12549. (org-with-wide-buffer
  12550. (goto-char org-log-note-marker)
  12551. (move-marker org-log-note-marker nil)
  12552. ;; Make sure point is at the beginning of an empty line.
  12553. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12554. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12555. ;; In an existing list, add a new item at the top level.
  12556. ;; Otherwise, indent line like a regular one.
  12557. (let ((itemp (org-in-item-p)))
  12558. (if itemp
  12559. (org-indent-line-to
  12560. (let ((struct (save-excursion
  12561. (goto-char itemp) (org-list-struct))))
  12562. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12563. (org-indent-line)))
  12564. (insert (org-list-bullet-string "-") (pop lines))
  12565. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12566. (dolist (line lines)
  12567. (insert "\n")
  12568. (org-indent-line-to ind)
  12569. (insert line)))
  12570. (message "Note stored")
  12571. (org-back-to-heading t)
  12572. (org-cycle-hide-drawers 'children))
  12573. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12574. ;; from within `org-add-log-note' because `buffer-undo-list'
  12575. ;; is then modified outside of `org-with-remote-undo'.
  12576. (when (eq this-command 'org-agenda-todo)
  12577. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12578. ;; Don't add undo information when called from `org-agenda-todo'
  12579. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12580. (set-window-configuration org-log-note-window-configuration)
  12581. (with-current-buffer (marker-buffer org-log-note-return-to)
  12582. (goto-char org-log-note-return-to))
  12583. (move-marker org-log-note-return-to nil)
  12584. (and org-log-post-message (message "%s" org-log-post-message))))
  12585. (defun org-remove-empty-drawer-at (pos)
  12586. "Remove an empty drawer at position POS.
  12587. POS may also be a marker."
  12588. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12589. (org-with-wide-buffer
  12590. (goto-char pos)
  12591. (let ((drawer (org-element-at-point)))
  12592. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12593. (not (org-element-property :contents-begin drawer)))
  12594. (delete-region (org-element-property :begin drawer)
  12595. (progn (goto-char (org-element-property :end drawer))
  12596. (skip-chars-backward " \r\t\n")
  12597. (forward-line)
  12598. (point))))))))
  12599. (defvar org-ts-type nil)
  12600. (defun org-sparse-tree (&optional arg type)
  12601. "Create a sparse tree, prompt for the details.
  12602. This command can create sparse trees. You first need to select the type
  12603. of match used to create the tree:
  12604. t Show all TODO entries.
  12605. T Show entries with a specific TODO keyword.
  12606. m Show entries selected by a tags/property match.
  12607. p Enter a property name and its value (both with completion on existing
  12608. names/values) and show entries with that property.
  12609. r Show entries matching a regular expression (`/' can be used as well).
  12610. b Show deadlines and scheduled items before a date.
  12611. a Show deadlines and scheduled items after a date.
  12612. d Show deadlines due within `org-deadline-warning-days'.
  12613. D Show deadlines and scheduled items between a date range."
  12614. (interactive "P")
  12615. (setq type (or type org-sparse-tree-default-date-type))
  12616. (setq org-ts-type type)
  12617. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12618. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12619. [c]ycle through date types: %s"
  12620. (case type
  12621. (all "all timestamps")
  12622. (scheduled "only scheduled")
  12623. (deadline "only deadline")
  12624. (active "only active timestamps")
  12625. (inactive "only inactive timestamps")
  12626. (closed "with a closed time-stamp")
  12627. (otherwise "scheduled/deadline")))
  12628. (let ((answer (read-char-exclusive)))
  12629. (case answer
  12630. (?c
  12631. (org-sparse-tree
  12632. arg
  12633. (cadr
  12634. (memq type '(nil all scheduled deadline active inactive closed)))))
  12635. (?d (call-interactively 'org-check-deadlines))
  12636. (?b (call-interactively 'org-check-before-date))
  12637. (?a (call-interactively 'org-check-after-date))
  12638. (?D (call-interactively 'org-check-dates-range))
  12639. (?t (call-interactively 'org-show-todo-tree))
  12640. (?T (org-show-todo-tree '(4)))
  12641. (?m (call-interactively 'org-match-sparse-tree))
  12642. ((?p ?P)
  12643. (let* ((kwd (org-icompleting-read
  12644. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12645. (value (org-icompleting-read
  12646. "Value: " (mapcar 'list (org-property-values kwd)))))
  12647. (unless (string-match "\\`{.*}\\'" value)
  12648. (setq value (concat "\"" value "\"")))
  12649. (org-match-sparse-tree arg (concat kwd "=" value))))
  12650. ((?r ?R ?/) (call-interactively 'org-occur))
  12651. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12652. (defvar org-occur-highlights nil
  12653. "List of overlays used for occur matches.")
  12654. (make-variable-buffer-local 'org-occur-highlights)
  12655. (defvar org-occur-parameters nil
  12656. "Parameters of the active org-occur calls.
  12657. This is a list, each call to org-occur pushes as cons cell,
  12658. containing the regular expression and the callback, onto the list.
  12659. The list can contain several entries if `org-occur' has been called
  12660. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12661. will only contain one set of parameters. When the highlights are
  12662. removed (for example with `C-c C-c', or with the next edit (depending
  12663. on `org-remove-highlights-with-change'), this variable is emptied
  12664. as well.")
  12665. (make-variable-buffer-local 'org-occur-parameters)
  12666. (defun org-occur (regexp &optional keep-previous callback)
  12667. "Make a compact tree which shows all matches of REGEXP.
  12668. The tree will show the lines where the regexp matches, and all higher
  12669. headlines above the match. It will also show the heading after the match,
  12670. to make sure editing the matching entry is easy.
  12671. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12672. call to `org-occur' will be kept, to allow stacking of calls to this
  12673. command.
  12674. If CALLBACK is non-nil, it is a function which is called to confirm
  12675. that the match should indeed be shown."
  12676. (interactive "sRegexp: \nP")
  12677. (when (equal regexp "")
  12678. (user-error "Regexp cannot be empty"))
  12679. (unless keep-previous
  12680. (org-remove-occur-highlights nil nil t))
  12681. (push (cons regexp callback) org-occur-parameters)
  12682. (let ((cnt 0))
  12683. (save-excursion
  12684. (goto-char (point-min))
  12685. (if (or (not keep-previous) ; do not want to keep
  12686. (not org-occur-highlights)) ; no previous matches
  12687. ;; hide everything
  12688. (org-overview))
  12689. (while (re-search-forward regexp nil t)
  12690. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12691. (when (or (not callback)
  12692. (save-match-data (funcall callback)))
  12693. (setq cnt (1+ cnt))
  12694. (when org-highlight-sparse-tree-matches
  12695. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12696. (org-show-context 'occur-tree))))
  12697. (when org-remove-highlights-with-change
  12698. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12699. nil 'local))
  12700. (unless org-sparse-tree-open-archived-trees
  12701. (org-hide-archived-subtrees (point-min) (point-max)))
  12702. (run-hooks 'org-occur-hook)
  12703. (if (org-called-interactively-p 'interactive)
  12704. (message "%d match(es) for regexp %s" cnt regexp))
  12705. cnt))
  12706. (defun org-occur-next-match (&optional n reset)
  12707. "Function for `next-error-function' to find sparse tree matches.
  12708. N is the number of matches to move, when negative move backwards.
  12709. RESET is entirely ignored - this function always goes back to the
  12710. starting point when no match is found."
  12711. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12712. (search-func (if (< n 0)
  12713. 'previous-single-char-property-change
  12714. 'next-single-char-property-change))
  12715. (n (abs n))
  12716. (pos (point))
  12717. p1)
  12718. (catch 'exit
  12719. (while (setq p1 (funcall search-func (point) 'org-type))
  12720. (when (equal p1 limit)
  12721. (goto-char pos)
  12722. (user-error "No more matches"))
  12723. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12724. (setq n (1- n))
  12725. (when (= n 0)
  12726. (goto-char p1)
  12727. (throw 'exit (point))))
  12728. (goto-char p1))
  12729. (goto-char p1)
  12730. (user-error "No more matches"))))
  12731. (defun org-show-context (&optional key)
  12732. "Make sure point and context are visible.
  12733. Optional argument KEY, when non-nil, is a symbol. See
  12734. `org-show-context-detail' for allowed values and how much is to
  12735. be shown."
  12736. (org-show-set-visibility
  12737. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12738. ((cdr (assq key org-show-context-detail)))
  12739. (t (cdr (assq 'default org-show-context-detail))))))
  12740. (defun org-show-set-visibility (detail)
  12741. "Set visibility around point according to DETAIL.
  12742. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12743. `tree', `canonical' or t. See `org-show-context-detail' for more
  12744. information."
  12745. (unless (org-before-first-heading-p)
  12746. ;; Show current heading and possibly its entry, following headline
  12747. ;; or all children.
  12748. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12749. (org-flag-heading nil)
  12750. (org-show-entry)
  12751. (org-with-limited-levels
  12752. (case detail
  12753. ((tree canonical t) (outline-show-children))
  12754. ((nil minimal ancestors))
  12755. (t (save-excursion
  12756. (outline-next-heading)
  12757. (org-flag-heading nil))))))
  12758. ;; Show all siblings.
  12759. (when (eq detail 'lineage) (org-show-siblings))
  12760. ;; Show ancestors, possibly with their children.
  12761. (when (memq detail '(ancestors lineage tree canonical t))
  12762. (save-excursion
  12763. (while (org-up-heading-safe)
  12764. (org-flag-heading nil)
  12765. (when (memq detail '(canonical t)) (org-show-entry))
  12766. (when (memq detail '(tree canonical t)) (outline-show-children)))))))
  12767. (defvar org-reveal-start-hook nil
  12768. "Hook run before revealing a location.")
  12769. (defun org-reveal (&optional siblings)
  12770. "Show current entry, hierarchy above it, and the following headline.
  12771. This can be used to show a consistent set of context around
  12772. locations exposed with `org-show-context'.
  12773. With optional argument SIBLINGS, on each level of the hierarchy all
  12774. siblings are shown. This repairs the tree structure to what it would
  12775. look like when opened with hierarchical calls to `org-cycle'.
  12776. With double optional argument \\[universal-argument] \\[universal-argument], \
  12777. go to the parent and show the
  12778. entire tree."
  12779. (interactive "P")
  12780. (run-hooks 'org-reveal-start-hook)
  12781. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12782. ((equal siblings '(16))
  12783. (save-excursion
  12784. (when (org-up-heading-safe)
  12785. (org-show-subtree)
  12786. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12787. (t (org-show-set-visibility 'lineage))))
  12788. (defun org-highlight-new-match (beg end)
  12789. "Highlight from BEG to END and mark the highlight is an occur headline."
  12790. (let ((ov (make-overlay beg end)))
  12791. (overlay-put ov 'face 'secondary-selection)
  12792. (overlay-put ov 'org-type 'org-occur)
  12793. (push ov org-occur-highlights)))
  12794. (defun org-remove-occur-highlights (&optional beg end noremove)
  12795. "Remove the occur highlights from the buffer.
  12796. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12797. from the `before-change-functions' in the current buffer."
  12798. (interactive)
  12799. (unless org-inhibit-highlight-removal
  12800. (mapc 'delete-overlay org-occur-highlights)
  12801. (setq org-occur-highlights nil)
  12802. (setq org-occur-parameters nil)
  12803. (unless noremove
  12804. (remove-hook 'before-change-functions
  12805. 'org-remove-occur-highlights 'local))))
  12806. ;;;; Priorities
  12807. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12808. "Regular expression matching the priority indicator.")
  12809. (defvar org-remove-priority-next-time nil)
  12810. (defun org-priority-up ()
  12811. "Increase the priority of the current item."
  12812. (interactive)
  12813. (org-priority 'up))
  12814. (defun org-priority-down ()
  12815. "Decrease the priority of the current item."
  12816. (interactive)
  12817. (org-priority 'down))
  12818. (defun org-priority (&optional action show)
  12819. "Change the priority of an item.
  12820. ACTION can be `set', `up', `down', or a character."
  12821. (interactive "P")
  12822. (if (equal action '(4))
  12823. (org-show-priority)
  12824. (unless org-enable-priority-commands
  12825. (user-error "Priority commands are disabled"))
  12826. (setq action (or action 'set))
  12827. (let (current new news have remove)
  12828. (save-excursion
  12829. (org-back-to-heading t)
  12830. (if (looking-at org-priority-regexp)
  12831. (setq current (string-to-char (match-string 2))
  12832. have t))
  12833. (cond
  12834. ((eq action 'remove)
  12835. (setq remove t new ?\ ))
  12836. ((or (eq action 'set)
  12837. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12838. (if (not (eq action 'set))
  12839. (setq new action)
  12840. (message "Priority %c-%c, SPC to remove: "
  12841. org-highest-priority org-lowest-priority)
  12842. (save-match-data
  12843. (setq new (read-char-exclusive))))
  12844. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12845. (= (upcase org-lowest-priority) org-lowest-priority))
  12846. (setq new (upcase new)))
  12847. (cond ((equal new ?\ ) (setq remove t))
  12848. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12849. (user-error "Priority must be between `%c' and `%c'"
  12850. org-highest-priority org-lowest-priority))))
  12851. ((eq action 'up)
  12852. (setq new (if have
  12853. (1- current) ; normal cycling
  12854. ;; last priority was empty
  12855. (if (eq last-command this-command)
  12856. org-lowest-priority ; wrap around empty to lowest
  12857. ;; default
  12858. (if org-priority-start-cycle-with-default
  12859. org-default-priority
  12860. (1- org-default-priority))))))
  12861. ((eq action 'down)
  12862. (setq new (if have
  12863. (1+ current) ; normal cycling
  12864. ;; last priority was empty
  12865. (if (eq last-command this-command)
  12866. org-highest-priority ; wrap around empty to highest
  12867. ;; default
  12868. (if org-priority-start-cycle-with-default
  12869. org-default-priority
  12870. (1+ org-default-priority))))))
  12871. (t (user-error "Invalid action")))
  12872. (if (or (< (upcase new) org-highest-priority)
  12873. (> (upcase new) org-lowest-priority))
  12874. (if (and (memq action '(up down))
  12875. (not have) (not (eq last-command this-command)))
  12876. ;; `new' is from default priority
  12877. (error
  12878. "The default can not be set, see `org-default-priority' why")
  12879. ;; normal cycling: `new' is beyond highest/lowest priority
  12880. ;; and is wrapped around to the empty priority
  12881. (setq remove t)))
  12882. (setq news (format "%c" new))
  12883. (if have
  12884. (if remove
  12885. (replace-match "" t t nil 1)
  12886. (replace-match news t t nil 2))
  12887. (if remove
  12888. (user-error "No priority cookie found in line")
  12889. (let ((case-fold-search nil))
  12890. (looking-at org-todo-line-regexp))
  12891. (if (match-end 2)
  12892. (progn
  12893. (goto-char (match-end 2))
  12894. (insert " [#" news "]"))
  12895. (goto-char (match-beginning 3))
  12896. (insert "[#" news "] "))))
  12897. (org-set-tags nil 'align))
  12898. (if remove
  12899. (message "Priority removed")
  12900. (message "Priority of current item set to %s" news)))))
  12901. (defun org-show-priority ()
  12902. "Show the priority of the current item.
  12903. This priority is composed of the main priority given with the [#A] cookies,
  12904. and by additional input from the age of a schedules or deadline entry."
  12905. (interactive)
  12906. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12907. (org-get-at-bol 'priority)
  12908. (save-excursion
  12909. (save-match-data
  12910. (beginning-of-line)
  12911. (and (looking-at org-heading-regexp)
  12912. (org-get-priority (match-string 0))))))))
  12913. (message "Priority is %d" (if pri pri -1000))))
  12914. (defun org-get-priority (s)
  12915. "Find priority cookie and return priority."
  12916. (save-match-data
  12917. (if (functionp org-get-priority-function)
  12918. (funcall org-get-priority-function)
  12919. (if (not (string-match org-priority-regexp s))
  12920. (* 1000 (- org-lowest-priority org-default-priority))
  12921. (* 1000 (- org-lowest-priority
  12922. (string-to-char (match-string 2 s))))))))
  12923. ;;;; Tags
  12924. (defvar org-agenda-archives-mode)
  12925. (defvar org-map-continue-from nil
  12926. "Position from where mapping should continue.
  12927. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12928. (defvar org-scanner-tags nil
  12929. "The current tag list while the tags scanner is running.")
  12930. (defvar org-trust-scanner-tags nil
  12931. "Should `org-get-tags-at' use the tags for the scanner.
  12932. This is for internal dynamical scoping only.
  12933. When this is non-nil, the function `org-get-tags-at' will return the value
  12934. of `org-scanner-tags' instead of building the list by itself. This
  12935. can lead to large speed-ups when the tags scanner is used in a file with
  12936. many entries, and when the list of tags is retrieved, for example to
  12937. obtain a list of properties. Building the tags list for each entry in such
  12938. a file becomes an N^2 operation - but with this variable set, it scales
  12939. as N.")
  12940. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12941. "Scan headline tags with inheritance and produce output ACTION.
  12942. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12943. or `agenda' to produce an entry list for an agenda view. It can also be
  12944. a Lisp form or a function that should be called at each matched headline, in
  12945. this case the return value is a list of all return values from these calls.
  12946. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12947. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12948. only lines with a not-done TODO keyword are included in the output.
  12949. This should be the same variable that was scoped into
  12950. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12951. START-LEVEL can be a string with asterisks, reducing the scope to
  12952. headlines matching this string."
  12953. (require 'org-agenda)
  12954. (let* ((re (concat "^"
  12955. (if start-level
  12956. ;; Get the correct level to match
  12957. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12958. org-outline-regexp)
  12959. " *\\(\\<\\("
  12960. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12961. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12962. (props (list 'face 'default
  12963. 'done-face 'org-agenda-done
  12964. 'undone-face 'default
  12965. 'mouse-face 'highlight
  12966. 'org-not-done-regexp org-not-done-regexp
  12967. 'org-todo-regexp org-todo-regexp
  12968. 'org-complex-heading-regexp org-complex-heading-regexp
  12969. 'help-echo
  12970. (format "mouse-2 or RET jump to org file %s"
  12971. (abbreviate-file-name
  12972. (or (buffer-file-name (buffer-base-buffer))
  12973. (buffer-name (buffer-base-buffer)))))))
  12974. (org-map-continue-from nil)
  12975. lspos tags tags-list
  12976. (tags-alist (list (cons 0 org-file-tags)))
  12977. (llast 0) rtn rtn1 level category i txt
  12978. todo marker entry priority
  12979. ts-date ts-date-type ts-date-pair)
  12980. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12981. (setq action (list 'lambda nil action)))
  12982. (save-excursion
  12983. (goto-char (point-min))
  12984. (when (eq action 'sparse-tree)
  12985. (org-overview)
  12986. (org-remove-occur-highlights))
  12987. (while (let (case-fold-search)
  12988. (re-search-forward re nil t))
  12989. (setq org-map-continue-from nil)
  12990. (catch :skip
  12991. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12992. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12993. (goto-char (setq lspos (match-beginning 0)))
  12994. (setq level (org-reduced-level (org-outline-level))
  12995. category (org-get-category))
  12996. (when (eq action 'agenda)
  12997. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12998. ts-date (car ts-date-pair)
  12999. ts-date-type (cdr ts-date-pair)))
  13000. (setq i llast llast level)
  13001. ;; remove tag lists from same and sublevels
  13002. (while (>= i level)
  13003. (when (setq entry (assoc i tags-alist))
  13004. (setq tags-alist (delete entry tags-alist)))
  13005. (setq i (1- i)))
  13006. ;; add the next tags
  13007. (when tags
  13008. (setq tags (org-split-string tags ":")
  13009. tags-alist
  13010. (cons (cons level tags) tags-alist)))
  13011. ;; compile tags for current headline
  13012. (setq tags-list
  13013. (if org-use-tag-inheritance
  13014. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  13015. tags)
  13016. org-scanner-tags tags-list)
  13017. (when org-use-tag-inheritance
  13018. (setcdr (car tags-alist)
  13019. (mapcar (lambda (x)
  13020. (setq x (copy-sequence x))
  13021. (org-add-prop-inherited x))
  13022. (cdar tags-alist))))
  13023. (when (and tags org-use-tag-inheritance
  13024. (or (not (eq t org-use-tag-inheritance))
  13025. org-tags-exclude-from-inheritance))
  13026. ;; selective inheritance, remove uninherited ones
  13027. (setcdr (car tags-alist)
  13028. (org-remove-uninherited-tags (cdar tags-alist))))
  13029. (when (and
  13030. ;; eval matcher only when the todo condition is OK
  13031. (and (or (not todo-only) (member todo org-not-done-keywords))
  13032. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13033. (eval matcher)))
  13034. ;; Call the skipper, but return t if it does not skip,
  13035. ;; so that the `and' form continues evaluating
  13036. (progn
  13037. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13038. t)
  13039. ;; Check if timestamps are deselecting this entry
  13040. (or (not todo-only)
  13041. (and (member todo org-not-done-keywords)
  13042. (or (not org-agenda-tags-todo-honor-ignore-options)
  13043. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13044. ;; select this headline
  13045. (cond
  13046. ((eq action 'sparse-tree)
  13047. (and org-highlight-sparse-tree-matches
  13048. (org-get-heading) (match-end 0)
  13049. (org-highlight-new-match
  13050. (match-beginning 1) (match-end 1)))
  13051. (org-show-context 'tags-tree))
  13052. ((eq action 'agenda)
  13053. (setq txt (org-agenda-format-item
  13054. ""
  13055. (concat
  13056. (if (eq org-tags-match-list-sublevels 'indented)
  13057. (make-string (1- level) ?.) "")
  13058. (org-get-heading))
  13059. (make-string level ?\s)
  13060. category
  13061. tags-list)
  13062. priority (org-get-priority txt))
  13063. (goto-char lspos)
  13064. (setq marker (org-agenda-new-marker))
  13065. (org-add-props txt props
  13066. 'org-marker marker 'org-hd-marker marker 'org-category category
  13067. 'todo-state todo
  13068. 'ts-date ts-date
  13069. 'priority priority
  13070. 'type (concat "tagsmatch" ts-date-type))
  13071. (push txt rtn))
  13072. ((functionp action)
  13073. (setq org-map-continue-from nil)
  13074. (save-excursion
  13075. (setq rtn1 (funcall action))
  13076. (push rtn1 rtn)))
  13077. (t (user-error "Invalid action")))
  13078. ;; if we are to skip sublevels, jump to end of subtree
  13079. (unless org-tags-match-list-sublevels
  13080. (org-end-of-subtree t)
  13081. (backward-char 1))))
  13082. ;; Get the correct position from where to continue
  13083. (if org-map-continue-from
  13084. (goto-char org-map-continue-from)
  13085. (and (= (point) lspos) (end-of-line 1)))))
  13086. (when (and (eq action 'sparse-tree)
  13087. (not org-sparse-tree-open-archived-trees))
  13088. (org-hide-archived-subtrees (point-min) (point-max)))
  13089. (nreverse rtn)))
  13090. (defun org-remove-uninherited-tags (tags)
  13091. "Remove all tags that are not inherited from the list TAGS."
  13092. (cond
  13093. ((eq org-use-tag-inheritance t)
  13094. (if org-tags-exclude-from-inheritance
  13095. (org-delete-all org-tags-exclude-from-inheritance tags)
  13096. tags))
  13097. ((not org-use-tag-inheritance) nil)
  13098. ((stringp org-use-tag-inheritance)
  13099. (delq nil (mapcar
  13100. (lambda (x)
  13101. (if (and (string-match org-use-tag-inheritance x)
  13102. (not (member x org-tags-exclude-from-inheritance)))
  13103. x nil))
  13104. tags)))
  13105. ((listp org-use-tag-inheritance)
  13106. (delq nil (mapcar
  13107. (lambda (x)
  13108. (if (member x org-use-tag-inheritance) x nil))
  13109. tags)))))
  13110. (defun org-match-sparse-tree (&optional todo-only match)
  13111. "Create a sparse tree according to tags string MATCH.
  13112. MATCH can contain positive and negative selection of tags, like
  13113. \"+WORK+URGENT-WITHBOSS\".
  13114. If optional argument TODO-ONLY is non-nil, only select lines that are
  13115. also TODO lines."
  13116. (interactive "P")
  13117. (org-agenda-prepare-buffers (list (current-buffer)))
  13118. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13119. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13120. (defvar org-cached-props nil)
  13121. (defun org-cached-entry-get (pom property)
  13122. (if (or (eq t org-use-property-inheritance)
  13123. (and (stringp org-use-property-inheritance)
  13124. (let ((case-fold-search t))
  13125. (org-string-match-p org-use-property-inheritance property)))
  13126. (and (listp org-use-property-inheritance)
  13127. (member-ignore-case property org-use-property-inheritance)))
  13128. ;; Caching is not possible, check it directly.
  13129. (org-entry-get pom property 'inherit)
  13130. ;; Get all properties, so we can do complicated checks easily.
  13131. (cdr (assoc-string property
  13132. (or org-cached-props
  13133. (setq org-cached-props (org-entry-properties pom)))
  13134. t))))
  13135. (defun org-global-tags-completion-table (&optional files)
  13136. "Return the list of all tags in all agenda buffer/files.
  13137. Optional FILES argument is a list of files which can be used
  13138. instead of the agenda files."
  13139. (save-excursion
  13140. (org-uniquify
  13141. (delq nil
  13142. (apply 'append
  13143. (mapcar
  13144. (lambda (file)
  13145. (set-buffer (find-file-noselect file))
  13146. (append (org-get-buffer-tags)
  13147. (mapcar (lambda (x) (if (stringp (car-safe x))
  13148. (list (car-safe x)) nil))
  13149. org-tag-alist)))
  13150. (if (and files (car files))
  13151. files
  13152. (org-agenda-files))))))))
  13153. (defun org-make-tags-matcher (match)
  13154. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13155. The variable `todo-only' is scoped dynamically into this function.
  13156. It will be set to t if the matcher restricts matching to TODO entries,
  13157. otherwise will not be touched.
  13158. Returns a cons of the selection string MATCH and the constructed
  13159. lisp form implementing the matcher. The matcher is to be evaluated
  13160. at an Org entry, with point on the headline, and returns t if the
  13161. entry matches the selection string MATCH. The returned lisp form
  13162. references two variables with information about the entry, which
  13163. must be bound around the form's evaluation: todo, the TODO keyword
  13164. at the entry (or nil of none); and tags-list, the list of all tags
  13165. at the entry including inherited ones. Additionally, the category
  13166. of the entry (if any) must be specified as the text property
  13167. `org-category' on the headline.
  13168. See also `org-scan-tags'.
  13169. "
  13170. (declare (special todo-only))
  13171. (unless (boundp 'todo-only)
  13172. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13173. (unless match
  13174. ;; Get a new match request, with completion against the global
  13175. ;; tags table and the local tags in current buffer
  13176. (let ((org-last-tags-completion-table
  13177. (org-uniquify
  13178. (delq nil (append (org-get-buffer-tags)
  13179. (org-global-tags-completion-table))))))
  13180. (setq match (org-completing-read-no-i
  13181. "Match: " 'org-tags-completion-function nil nil nil
  13182. 'org-tags-history))))
  13183. ;; Parse the string and create a lisp form
  13184. (let ((match0 match)
  13185. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13186. minus tag mm
  13187. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13188. orterms term orlist re-p str-p level-p level-op time-p
  13189. prop-p pn pv po gv rest (start 0) (ss 0))
  13190. ;; Expand group tags
  13191. (setq match (org-tags-expand match))
  13192. ;; Check if there is a TODO part of this match, which would be the
  13193. ;; part after a "/". TO make sure that this slash is not part of
  13194. ;; a property value to be matched against, we also check that there
  13195. ;; is no " after that slash.
  13196. ;; First, find the last slash
  13197. (while (string-match "/+" match ss)
  13198. (setq start (match-beginning 0) ss (match-end 0)))
  13199. (if (and (string-match "/+" match start)
  13200. (not (save-match-data (string-match "\"" match start))))
  13201. ;; match contains also a todo-matching request
  13202. (progn
  13203. (setq tagsmatch (substring match 0 (match-beginning 0))
  13204. todomatch (substring match (match-end 0)))
  13205. (if (string-match "^!" todomatch)
  13206. (setq todo-only t todomatch (substring todomatch 1)))
  13207. (if (string-match "^\\s-*$" todomatch)
  13208. (setq todomatch nil)))
  13209. ;; only matching tags
  13210. (setq tagsmatch match todomatch nil))
  13211. ;; Make the tags matcher
  13212. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13213. (setq tagsmatcher t)
  13214. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13215. (while (setq term (pop orterms))
  13216. (while (and (equal (substring term -1) "\\") orterms)
  13217. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13218. (while (string-match re term)
  13219. (setq rest (substring term (match-end 0))
  13220. minus (and (match-end 1)
  13221. (equal (match-string 1 term) "-"))
  13222. tag (save-match-data (replace-regexp-in-string
  13223. "\\\\-" "-"
  13224. (match-string 2 term)))
  13225. re-p (equal (string-to-char tag) ?{)
  13226. level-p (match-end 4)
  13227. prop-p (match-end 5)
  13228. mm (cond
  13229. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13230. (level-p
  13231. (setq level-op (org-op-to-function (match-string 3 term)))
  13232. `(,level-op level ,(string-to-number
  13233. (match-string 4 term))))
  13234. (prop-p
  13235. (setq pn (match-string 5 term)
  13236. po (match-string 6 term)
  13237. pv (match-string 7 term)
  13238. re-p (equal (string-to-char pv) ?{)
  13239. str-p (equal (string-to-char pv) ?\")
  13240. time-p (save-match-data
  13241. (string-match "^\"[[<].*[]>]\"$" pv))
  13242. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13243. (if time-p (setq pv (org-matcher-time pv)))
  13244. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13245. (cond
  13246. ((equal pn "CATEGORY")
  13247. (setq gv '(get-text-property (point) 'org-category)))
  13248. ((equal pn "TODO")
  13249. (setq gv 'todo))
  13250. (t
  13251. (setq gv `(org-cached-entry-get nil ,pn))))
  13252. (if re-p
  13253. (if (eq po 'org<>)
  13254. `(not (string-match ,pv (or ,gv "")))
  13255. `(string-match ,pv (or ,gv "")))
  13256. (if str-p
  13257. `(,po (or ,gv "") ,pv)
  13258. `(,po (string-to-number (or ,gv ""))
  13259. ,(string-to-number pv) ))))
  13260. (t `(member ,tag tags-list)))
  13261. mm (if minus (list 'not mm) mm)
  13262. term rest)
  13263. (push mm tagsmatcher))
  13264. (push (if (> (length tagsmatcher) 1)
  13265. (cons 'and tagsmatcher)
  13266. (car tagsmatcher))
  13267. orlist)
  13268. (setq tagsmatcher nil))
  13269. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13270. (setq tagsmatcher
  13271. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13272. ;; Make the todo matcher
  13273. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13274. (setq todomatcher t)
  13275. (setq orterms (org-split-string todomatch "|") orlist nil)
  13276. (dolist (term orterms)
  13277. (while (string-match re term)
  13278. (setq minus (and (match-end 1)
  13279. (equal (match-string 1 term) "-"))
  13280. kwd (match-string 2 term)
  13281. re-p (equal (string-to-char kwd) ?{)
  13282. term (substring term (match-end 0))
  13283. mm (if re-p
  13284. `(string-match ,(substring kwd 1 -1) todo)
  13285. (list 'equal 'todo kwd))
  13286. mm (if minus (list 'not mm) mm))
  13287. (push mm todomatcher))
  13288. (push (if (> (length todomatcher) 1)
  13289. (cons 'and todomatcher)
  13290. (car todomatcher))
  13291. orlist)
  13292. (setq todomatcher nil))
  13293. (setq todomatcher (if (> (length orlist) 1)
  13294. (cons 'or orlist) (car orlist))))
  13295. ;; Return the string and lisp forms of the matcher
  13296. (setq matcher (if todomatcher
  13297. (list 'and tagsmatcher todomatcher)
  13298. tagsmatcher))
  13299. (when todo-only
  13300. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13301. matcher)))
  13302. (cons match0 matcher)))
  13303. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13304. "Expand group tags in MATCH.
  13305. This replaces every group tag in MATCH with a regexp tag search.
  13306. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13307. will be replaced like this:
  13308. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13309. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13310. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13311. Replacing by a regexp preserves the structure of the match.
  13312. E.g., this expansion
  13313. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13314. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13315. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13316. A group tag in MATCH can contain regular expressions of its own.
  13317. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13318. will be replaced like this:
  13319. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13320. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13321. assumed to be a single group tag, and the function will return
  13322. the list of tags in this group.
  13323. When DOWNCASE is non-nil, expand downcased TAGS."
  13324. (if org-group-tags
  13325. (let* ((case-fold-search t)
  13326. (stable org-mode-syntax-table)
  13327. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13328. (taggroups (if downcased
  13329. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13330. taggroups)
  13331. taggroups))
  13332. (taggroups-keys (mapcar #'car taggroups))
  13333. (return-match (if downcased (downcase match) match))
  13334. (count 0)
  13335. (work-already-expanded tags-already-expanded)
  13336. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13337. ;; @ and _ are allowed as word-components in tags.
  13338. (modify-syntax-entry ?@ "w" stable)
  13339. (modify-syntax-entry ?_ "w" stable)
  13340. ;; Temporarily replace regexp-expressions in the match-expression.
  13341. (while (string-match "{.+?}" return-match)
  13342. (incf count)
  13343. (push (match-string 0 return-match) regexps-in-match)
  13344. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13345. (while (and taggroups-keys
  13346. (with-syntax-table stable
  13347. (string-match
  13348. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13349. (regexp-opt taggroups-keys) "\\>\\)")
  13350. return-match)))
  13351. (let* ((dir (match-string 1 return-match))
  13352. (tag (match-string 2 return-match))
  13353. (tag (if downcased (downcase tag) tag)))
  13354. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13355. (member tag work-already-expanded))
  13356. (setq tags-in-group (assoc tag taggroups))
  13357. (push tag work-already-expanded)
  13358. ;; Recursively expand each tag in the group, if the tag hasn't
  13359. ;; already been expanded. Restore the match-data after all recursive calls.
  13360. (save-match-data
  13361. (let (tags-expanded)
  13362. (dolist (x (cdr tags-in-group))
  13363. (if (and (member x taggroups-keys)
  13364. (not (member x work-already-expanded)))
  13365. (setq tags-expanded
  13366. (delete-dups
  13367. (append
  13368. (org-tags-expand x t downcased
  13369. work-already-expanded)
  13370. tags-expanded)))
  13371. (setq tags-expanded
  13372. (append (list x) tags-expanded)))
  13373. (setq work-already-expanded
  13374. (delete-dups
  13375. (append tags-expanded
  13376. work-already-expanded))))
  13377. (setq tags-in-group
  13378. (delete-dups (cons (car tags-in-group)
  13379. tags-expanded)))))
  13380. ;; Filter tag-regexps from tags.
  13381. (setq regexp-in-group-escaped
  13382. (delq nil (mapcar (lambda (x)
  13383. (if (stringp x)
  13384. (and (equal "{" (substring x 0 1))
  13385. (equal "}" (substring x -1))
  13386. x)
  13387. x))
  13388. tags-in-group))
  13389. regexp-in-group
  13390. (mapcar (lambda (x)
  13391. (substring x 1 -1))
  13392. regexp-in-group-escaped)
  13393. tags-in-group
  13394. (delq nil (mapcar (lambda (x)
  13395. (if (stringp x)
  13396. (and (not (equal "{" (substring x 0 1)))
  13397. (not (equal "}" (substring x -1)))
  13398. x)
  13399. x))
  13400. tags-in-group)))
  13401. ;; If single-as-list, do no more in the while-loop.
  13402. (if (not single-as-list)
  13403. (progn
  13404. (when regexp-in-group
  13405. (setq regexp-in-group
  13406. (concat "\\|"
  13407. (mapconcat 'identity regexp-in-group
  13408. "\\|"))))
  13409. (setq tags-in-group
  13410. (concat dir
  13411. "{\\<"
  13412. (regexp-opt tags-in-group)
  13413. "\\>"
  13414. regexp-in-group
  13415. "}"))
  13416. (when (stringp tags-in-group)
  13417. (org-add-props tags-in-group '(grouptag t)))
  13418. (setq return-match
  13419. (replace-match tags-in-group t t return-match)))
  13420. (setq tags-in-group
  13421. (append regexp-in-group-escaped tags-in-group))))
  13422. (setq taggroups-keys (delete tag taggroups-keys))))
  13423. ;; Add the regular expressions back into the match-expression again.
  13424. (while regexps-in-match
  13425. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13426. (pop regexps-in-match)
  13427. return-match t t))
  13428. (decf count))
  13429. (if single-as-list
  13430. (if tags-in-group tags-in-group (list return-match))
  13431. return-match))
  13432. (if single-as-list
  13433. (list (if downcased (downcase match) match))
  13434. match)))
  13435. (defun org-op-to-function (op &optional stringp)
  13436. "Turn an operator into the appropriate function."
  13437. (setq op
  13438. (cond
  13439. ((equal op "<" ) '(< string< org-time<))
  13440. ((equal op ">" ) '(> org-string> org-time>))
  13441. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13442. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13443. ((member op '("=" "==")) '(= string= org-time=))
  13444. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13445. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13446. (defun org<> (a b) (not (= a b)))
  13447. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13448. (defun org-string>= (a b) (not (string< a b)))
  13449. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13450. (defun org-string<> (a b) (not (string= a b)))
  13451. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13452. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13453. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13454. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13455. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13456. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13457. (defun org-2ft (s)
  13458. "Convert S to a floating point time.
  13459. If S is already a number, just return it. If it is a string, parse
  13460. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13461. (cond
  13462. ((numberp s) s)
  13463. ((stringp s)
  13464. (condition-case nil
  13465. (float-time (apply 'encode-time (org-parse-time-string s)))
  13466. (error 0.)))
  13467. (t 0.)))
  13468. (defun org-time-today ()
  13469. "Time in seconds today at 0:00.
  13470. Returns the float number of seconds since the beginning of the
  13471. epoch to the beginning of today (00:00)."
  13472. (float-time (apply 'encode-time
  13473. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13474. (defun org-matcher-time (s)
  13475. "Interpret a time comparison value."
  13476. (save-match-data
  13477. (cond
  13478. ((string= s "<now>") (float-time))
  13479. ((string= s "<today>") (org-time-today))
  13480. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13481. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13482. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13483. (+ (org-time-today)
  13484. (* (string-to-number (match-string 1 s))
  13485. (cdr (assoc (match-string 2 s)
  13486. '(("d" . 86400.0) ("w" . 604800.0)
  13487. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13488. (t (org-2ft s)))))
  13489. (defun org-match-any-p (re list)
  13490. "Does re match any element of list?"
  13491. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13492. (delq nil list))
  13493. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13494. (defvar org-tags-overlay (make-overlay 1 1))
  13495. (org-detach-overlay org-tags-overlay)
  13496. (defun org-get-local-tags-at (&optional pos)
  13497. "Get a list of tags defined in the current headline."
  13498. (org-get-tags-at pos 'local))
  13499. (defun org-get-local-tags ()
  13500. "Get a list of tags defined in the current headline."
  13501. (org-get-tags-at nil 'local))
  13502. (defun org-get-tags-at (&optional pos local)
  13503. "Get a list of all headline tags applicable at POS.
  13504. POS defaults to point. If tags are inherited, the list contains
  13505. the targets in the same sequence as the headlines appear, i.e.
  13506. the tags of the current headline come last.
  13507. When LOCAL is non-nil, only return tags from the current headline,
  13508. ignore inherited ones."
  13509. (interactive)
  13510. (if (and org-trust-scanner-tags
  13511. (or (not pos) (equal pos (point)))
  13512. (not local))
  13513. org-scanner-tags
  13514. (let (tags ltags lastpos parent)
  13515. (save-excursion
  13516. (save-restriction
  13517. (widen)
  13518. (goto-char (or pos (point)))
  13519. (save-match-data
  13520. (catch 'done
  13521. (condition-case nil
  13522. (progn
  13523. (org-back-to-heading t)
  13524. (while (not (equal lastpos (point)))
  13525. (setq lastpos (point))
  13526. (when (looking-at
  13527. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13528. (setq ltags (org-split-string
  13529. (org-match-string-no-properties 1) ":"))
  13530. (when parent
  13531. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13532. (setq tags (append
  13533. (if parent
  13534. (org-remove-uninherited-tags ltags)
  13535. ltags)
  13536. tags)))
  13537. (or org-use-tag-inheritance (throw 'done t))
  13538. (if local (throw 'done t))
  13539. (or (org-up-heading-safe) (error nil))
  13540. (setq parent t)))
  13541. (error nil)))))
  13542. (if local
  13543. tags
  13544. (reverse (delete-dups
  13545. (reverse (append
  13546. (org-remove-uninherited-tags
  13547. org-file-tags)
  13548. tags)))))))))
  13549. (defun org-add-prop-inherited (s)
  13550. (add-text-properties 0 (length s) '(inherited t) s)
  13551. s)
  13552. (defun org-toggle-tag (tag &optional onoff)
  13553. "Toggle the tag TAG for the current line.
  13554. If ONOFF is `on' or `off', don't toggle but set to this state."
  13555. (let (res current)
  13556. (save-excursion
  13557. (org-back-to-heading t)
  13558. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13559. (point-at-eol) t)
  13560. (progn
  13561. (setq current (match-string 1))
  13562. (replace-match ""))
  13563. (setq current ""))
  13564. (setq current (nreverse (org-split-string current ":")))
  13565. (cond
  13566. ((eq onoff 'on)
  13567. (setq res t)
  13568. (or (member tag current) (push tag current)))
  13569. ((eq onoff 'off)
  13570. (or (not (member tag current)) (setq current (delete tag current))))
  13571. (t (if (member tag current)
  13572. (setq current (delete tag current))
  13573. (setq res t)
  13574. (push tag current))))
  13575. (end-of-line 1)
  13576. (if current
  13577. (progn
  13578. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13579. (org-set-tags nil t))
  13580. (delete-horizontal-space))
  13581. (run-hooks 'org-after-tags-change-hook))
  13582. res))
  13583. (defun org-align-tags-here (to-col)
  13584. ;; Assumes that this is a headline
  13585. "Align tags on the current headline to TO-COL."
  13586. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13587. (beginning-of-line 1)
  13588. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13589. (< pos (match-beginning 2)))
  13590. (progn
  13591. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13592. (goto-char (match-beginning 1))
  13593. (insert " ")
  13594. (delete-region (point) (1+ (match-beginning 2)))
  13595. (setq ncol (max (current-column)
  13596. (1+ col)
  13597. (if (> to-col 0)
  13598. to-col
  13599. (- (abs to-col) tags-l))))
  13600. (setq p (point))
  13601. (insert (make-string (- ncol (current-column)) ?\ ))
  13602. (setq ncol (current-column))
  13603. (when indent-tabs-mode (tabify p (point-at-eol)))
  13604. (org-move-to-column (min ncol col)))
  13605. (goto-char pos))))
  13606. (defun org-set-tags-command (&optional arg just-align)
  13607. "Call the set-tags command for the current entry."
  13608. (interactive "P")
  13609. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13610. (org-set-tags arg just-align)
  13611. (save-excursion
  13612. (unless (and (org-region-active-p)
  13613. org-loop-over-headlines-in-active-region)
  13614. (org-back-to-heading t))
  13615. (org-set-tags arg just-align))))
  13616. (defun org-set-tags-to (data)
  13617. "Set the tags of the current entry to DATA, replacing the current tags.
  13618. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13619. If DATA is nil or the empty string, any tags will be removed."
  13620. (interactive "sTags: ")
  13621. (setq data
  13622. (cond
  13623. ((eq data nil) "")
  13624. ((equal data "") "")
  13625. ((stringp data)
  13626. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13627. ":"))
  13628. ((listp data)
  13629. (concat ":" (mapconcat 'identity data ":") ":"))))
  13630. (when data
  13631. (save-excursion
  13632. (org-back-to-heading t)
  13633. (when (looking-at org-complex-heading-regexp)
  13634. (if (match-end 5)
  13635. (progn
  13636. (goto-char (match-beginning 5))
  13637. (insert data)
  13638. (delete-region (point) (point-at-eol))
  13639. (org-set-tags nil 'align))
  13640. (goto-char (point-at-eol))
  13641. (insert " " data)
  13642. (org-set-tags nil 'align)))
  13643. (beginning-of-line 1)
  13644. (if (looking-at ".*?\\([ \t]+\\)$")
  13645. (delete-region (match-beginning 1) (match-end 1))))))
  13646. (defun org-align-all-tags ()
  13647. "Align the tags i all headings."
  13648. (interactive)
  13649. (save-excursion
  13650. (or (ignore-errors (org-back-to-heading t))
  13651. (outline-next-heading))
  13652. (if (org-at-heading-p)
  13653. (org-set-tags t)
  13654. (message "No headings"))))
  13655. (defvar org-indent-indentation-per-level)
  13656. (defun org-set-tags (&optional arg just-align)
  13657. "Set the tags for the current headline.
  13658. With prefix ARG, realign all tags in headings in the current buffer.
  13659. When JUST-ALIGN is non-nil, only align tags."
  13660. (interactive "P")
  13661. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13662. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13663. 'region-start-level
  13664. 'region))
  13665. org-loop-over-headlines-in-active-region)
  13666. (org-map-entries
  13667. ;; We don't use ARG and JUST-ALIGN here because these args
  13668. ;; are not useful when looping over headlines.
  13669. #'org-set-tags
  13670. org-loop-over-headlines-in-active-region
  13671. cl
  13672. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13673. (let ((org-setting-tags t))
  13674. (if arg
  13675. (save-excursion
  13676. (goto-char (point-min))
  13677. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13678. (while (re-search-forward org-outline-regexp-bol nil t)
  13679. (org-set-tags nil t)
  13680. (end-of-line)))
  13681. (message "All tags realigned to column %d" org-tags-column))
  13682. (let* ((current (org-get-tags-string))
  13683. (col (current-column))
  13684. (tags
  13685. (if just-align current
  13686. ;; Get a new set of tags from the user.
  13687. (save-excursion
  13688. (let* ((table
  13689. (setq
  13690. org-last-tags-completion-table
  13691. (append
  13692. org-tag-persistent-alist
  13693. (or org-tag-alist (org-get-buffer-tags))
  13694. (and
  13695. org-complete-tags-always-offer-all-agenda-tags
  13696. (org-global-tags-completion-table
  13697. (org-agenda-files))))))
  13698. (current-tags (org-split-string current ":"))
  13699. (inherited-tags
  13700. (nreverse (nthcdr (length current-tags)
  13701. (nreverse (org-get-tags-at))))))
  13702. (replace-regexp-in-string
  13703. "\\([-+&]+\\|,\\)"
  13704. ":"
  13705. (if (or (eq t org-use-fast-tag-selection)
  13706. (and org-use-fast-tag-selection
  13707. (delq nil (mapcar #'cdr table))))
  13708. (org-fast-tag-selection
  13709. current-tags inherited-tags table
  13710. (and org-fast-tag-selection-include-todo
  13711. org-todo-key-alist))
  13712. (let ((org-add-colon-after-tag-completion
  13713. (< 1 (length table))))
  13714. (org-trim
  13715. (completing-read
  13716. "Tags: "
  13717. #'org-tags-completion-function
  13718. nil nil current 'org-tags-history))))))))))
  13719. (when org-tags-sort-function
  13720. (setq tags
  13721. (mapconcat
  13722. #'identity
  13723. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13724. org-tags-sort-function)
  13725. ":")))
  13726. (if (not (org-string-nw-p tags)) (setq tags "")
  13727. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13728. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13729. ;; Insert new tags at the correct column
  13730. (beginning-of-line)
  13731. (let ((level (if (looking-at org-outline-regexp)
  13732. (- (match-end 0) (point) 1)
  13733. 1)))
  13734. (cond
  13735. ((and (equal current "") (equal tags "")))
  13736. ((re-search-forward
  13737. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13738. (line-end-position)
  13739. t)
  13740. (if (equal tags "") (replace-match "" t t)
  13741. (goto-char (match-beginning 0))
  13742. (let* ((c0 (current-column))
  13743. ;; Compute offset for the case of org-indent-mode
  13744. ;; active.
  13745. (di (if (org-bound-and-true-p org-indent-mode)
  13746. (* (1- org-indent-indentation-per-level)
  13747. (1- level))
  13748. 0))
  13749. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13750. (tc (+ org-tags-column
  13751. (if (> org-tags-column 0) (- di) di)))
  13752. (c1 (max (1+ c0)
  13753. (if (> tc 0) tc
  13754. (- (- tc) (string-width tags)))))
  13755. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13756. (replace-match rpl t t)
  13757. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13758. (tabify p0 (point))))))
  13759. (t (error "Tags alignment failed"))))
  13760. (org-move-to-column col))
  13761. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13762. (defun org-change-tag-in-region (beg end tag off)
  13763. "Add or remove TAG for each entry in the region.
  13764. This works in the agenda, and also in an org-mode buffer."
  13765. (interactive
  13766. (list (region-beginning) (region-end)
  13767. (let ((org-last-tags-completion-table
  13768. (if (derived-mode-p 'org-mode)
  13769. (org-uniquify
  13770. (delq nil (append (org-get-buffer-tags)
  13771. (org-global-tags-completion-table))))
  13772. (org-global-tags-completion-table))))
  13773. (org-icompleting-read
  13774. "Tag: " 'org-tags-completion-function nil nil nil
  13775. 'org-tags-history))
  13776. (progn
  13777. (message "[s]et or [r]emove? ")
  13778. (equal (read-char-exclusive) ?r))))
  13779. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13780. (let ((agendap (equal major-mode 'org-agenda-mode))
  13781. l1 l2 m buf pos newhead (cnt 0))
  13782. (goto-char end)
  13783. (setq l2 (1- (org-current-line)))
  13784. (goto-char beg)
  13785. (setq l1 (org-current-line))
  13786. (loop for l from l1 to l2 do
  13787. (org-goto-line l)
  13788. (setq m (get-text-property (point) 'org-hd-marker))
  13789. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13790. (and agendap m))
  13791. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13792. pos (if agendap m (point)))
  13793. (with-current-buffer buf
  13794. (save-excursion
  13795. (save-restriction
  13796. (goto-char pos)
  13797. (setq cnt (1+ cnt))
  13798. (org-toggle-tag tag (if off 'off 'on))
  13799. (setq newhead (org-get-heading)))))
  13800. (and agendap (org-agenda-change-all-lines newhead m))))
  13801. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13802. (defun org-tags-completion-function (string predicate &optional flag)
  13803. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13804. (confirm (lambda (x) (stringp (car x)))))
  13805. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13806. (setq s1 (match-string 1 string)
  13807. s2 (match-string 2 string))
  13808. (setq s1 "" s2 string))
  13809. (cond
  13810. ((eq flag nil)
  13811. ;; try completion
  13812. (setq rtn (try-completion s2 ctable confirm))
  13813. (if (stringp rtn)
  13814. (setq rtn
  13815. (concat s1 s2 (substring rtn (length s2))
  13816. (if (and org-add-colon-after-tag-completion
  13817. (assoc rtn ctable))
  13818. ":" ""))))
  13819. rtn)
  13820. ((eq flag t)
  13821. ;; all-completions
  13822. (all-completions s2 ctable confirm))
  13823. ((eq flag 'lambda)
  13824. ;; exact match?
  13825. (assoc s2 ctable)))))
  13826. (defun org-fast-tag-insert (kwd tags face &optional end)
  13827. "Insert KDW, and the TAGS, the latter with face FACE.
  13828. Also insert END."
  13829. (insert (format "%-12s" (concat kwd ":"))
  13830. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13831. (or end "")))
  13832. (defun org-fast-tag-show-exit (flag)
  13833. (save-excursion
  13834. (org-goto-line 3)
  13835. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13836. (replace-match ""))
  13837. (when flag
  13838. (end-of-line 1)
  13839. (org-move-to-column (- (window-width) 19) t)
  13840. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13841. (defun org-set-current-tags-overlay (current prefix)
  13842. "Add an overlay to CURRENT tag with PREFIX."
  13843. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13844. (if (featurep 'xemacs)
  13845. (org-overlay-display org-tags-overlay (concat prefix s)
  13846. 'secondary-selection)
  13847. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13848. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13849. (defvar org-last-tag-selection-key nil)
  13850. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13851. "Fast tag selection with single keys.
  13852. CURRENT is the current list of tags in the headline, INHERITED is the
  13853. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13854. possibly with grouping information. TODO-TABLE is a similar table with
  13855. TODO keywords, should these have keys assigned to them.
  13856. If the keys are nil, a-z are automatically assigned.
  13857. Returns the new tags string, or nil to not change the current settings."
  13858. (let* ((fulltable (append table todo-table))
  13859. (maxlen (apply 'max (mapcar
  13860. (lambda (x)
  13861. (if (stringp (car x)) (string-width (car x)) 0))
  13862. fulltable)))
  13863. (buf (current-buffer))
  13864. (expert (eq org-fast-tag-selection-single-key 'expert))
  13865. (buffer-tags nil)
  13866. (fwidth (+ maxlen 3 1 3))
  13867. (ncol (/ (- (window-width) 4) fwidth))
  13868. (i-face 'org-done)
  13869. (c-face 'org-todo)
  13870. tg cnt e c char c1 c2 ntable tbl rtn
  13871. ov-start ov-end ov-prefix
  13872. (exit-after-next org-fast-tag-selection-single-key)
  13873. (done-keywords org-done-keywords)
  13874. groups ingroup intaggroup)
  13875. (save-excursion
  13876. (beginning-of-line 1)
  13877. (if (looking-at
  13878. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13879. (setq ov-start (match-beginning 1)
  13880. ov-end (match-end 1)
  13881. ov-prefix "")
  13882. (setq ov-start (1- (point-at-eol))
  13883. ov-end (1+ ov-start))
  13884. (skip-chars-forward "^\n\r")
  13885. (setq ov-prefix
  13886. (concat
  13887. (buffer-substring (1- (point)) (point))
  13888. (if (> (current-column) org-tags-column)
  13889. " "
  13890. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13891. (move-overlay org-tags-overlay ov-start ov-end)
  13892. (save-window-excursion
  13893. (if expert
  13894. (set-buffer (get-buffer-create " *Org tags*"))
  13895. (delete-other-windows)
  13896. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13897. (org-switch-to-buffer-other-window " *Org tags*"))
  13898. (erase-buffer)
  13899. (org-set-local 'org-done-keywords done-keywords)
  13900. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13901. (org-fast-tag-insert "Current" current c-face "\n\n")
  13902. (org-fast-tag-show-exit exit-after-next)
  13903. (org-set-current-tags-overlay current ov-prefix)
  13904. (setq tbl fulltable char ?a cnt 0)
  13905. (while (setq e (pop tbl))
  13906. (cond
  13907. ((eq (car e) :startgroup)
  13908. (push '() groups) (setq ingroup t)
  13909. (unless (zerop cnt)
  13910. (setq cnt 0)
  13911. (insert "\n"))
  13912. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13913. ((eq (car e) :endgroup)
  13914. (setq ingroup nil cnt 0)
  13915. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13916. ((eq (car e) :startgrouptag)
  13917. (setq intaggroup t)
  13918. (unless (zerop cnt)
  13919. (setq cnt 0)
  13920. (insert "\n"))
  13921. (insert "[ "))
  13922. ((eq (car e) :endgrouptag)
  13923. (setq intaggroup nil cnt 0)
  13924. (insert "]\n"))
  13925. ((equal e '(:newline))
  13926. (unless (zerop cnt)
  13927. (setq cnt 0)
  13928. (insert "\n")
  13929. (setq e (car tbl))
  13930. (while (equal (car tbl) '(:newline))
  13931. (insert "\n")
  13932. (setq tbl (cdr tbl)))))
  13933. ((equal e '(:grouptags)) (insert " : "))
  13934. (t
  13935. (setq tg (copy-sequence (car e)) c2 nil)
  13936. (if (cdr e)
  13937. (setq c (cdr e))
  13938. ;; automatically assign a character.
  13939. (setq c1 (string-to-char
  13940. (downcase (substring
  13941. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13942. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13943. (while (or (rassoc char ntable) (rassoc char table))
  13944. (setq char (1+ char)))
  13945. (setq c2 c1))
  13946. (setq c (or c2 char)))
  13947. (when ingroup (push tg (car groups)))
  13948. (setq tg (org-add-props tg nil 'face
  13949. (cond
  13950. ((not (assoc tg table))
  13951. (org-get-todo-face tg))
  13952. ((member tg current) c-face)
  13953. ((member tg inherited) i-face))))
  13954. (when (equal (caar tbl) :grouptags)
  13955. (org-add-props tg nil 'face 'org-tag-group))
  13956. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13957. (insert "[" c "] " tg (make-string
  13958. (- fwidth 4 (length tg)) ?\ ))
  13959. (push (cons tg c) ntable)
  13960. (when (= (incf cnt) ncol)
  13961. (insert "\n")
  13962. (when (or ingroup intaggroup) (insert " "))
  13963. (setq cnt 0)))))
  13964. (setq ntable (nreverse ntable))
  13965. (insert "\n")
  13966. (goto-char (point-min))
  13967. (unless expert (org-fit-window-to-buffer))
  13968. (setq rtn
  13969. (catch 'exit
  13970. (while t
  13971. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13972. (if (not groups) "no " "")
  13973. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13974. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13975. (setq org-last-tag-selection-key c)
  13976. (cond
  13977. ((= c ?\r) (throw 'exit t))
  13978. ((= c ?!)
  13979. (setq groups (not groups))
  13980. (goto-char (point-min))
  13981. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13982. ((= c ?\C-c)
  13983. (if (not expert)
  13984. (org-fast-tag-show-exit
  13985. (setq exit-after-next (not exit-after-next)))
  13986. (setq expert nil)
  13987. (delete-other-windows)
  13988. (set-window-buffer (split-window-vertically) " *Org tags*")
  13989. (org-switch-to-buffer-other-window " *Org tags*")
  13990. (org-fit-window-to-buffer)))
  13991. ((or (= c ?\C-g)
  13992. (and (= c ?q) (not (rassoc c ntable))))
  13993. (org-detach-overlay org-tags-overlay)
  13994. (setq quit-flag t))
  13995. ((= c ?\ )
  13996. (setq current nil)
  13997. (when exit-after-next (setq exit-after-next 'now)))
  13998. ((= c ?\t)
  13999. (condition-case nil
  14000. (setq tg (org-icompleting-read
  14001. "Tag: "
  14002. (or buffer-tags
  14003. (with-current-buffer buf
  14004. (setq buffer-tags
  14005. (org-get-buffer-tags))))))
  14006. (quit (setq tg "")))
  14007. (when (string-match "\\S-" tg)
  14008. (add-to-list 'buffer-tags (list tg))
  14009. (if (member tg current)
  14010. (setq current (delete tg current))
  14011. (push tg current)))
  14012. (when exit-after-next (setq exit-after-next 'now)))
  14013. ((setq e (rassoc c todo-table) tg (car e))
  14014. (with-current-buffer buf
  14015. (save-excursion (org-todo tg)))
  14016. (when exit-after-next (setq exit-after-next 'now)))
  14017. ((setq e (rassoc c ntable) tg (car e))
  14018. (if (member tg current)
  14019. (setq current (delete tg current))
  14020. (loop for g in groups do
  14021. (when (member tg g)
  14022. (dolist (x g) (setq current (delete x current)))))
  14023. (push tg current))
  14024. (when exit-after-next (setq exit-after-next 'now))))
  14025. ;; Create a sorted list
  14026. (setq current
  14027. (sort current
  14028. (lambda (a b)
  14029. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14030. (when (eq exit-after-next 'now) (throw 'exit t))
  14031. (goto-char (point-min))
  14032. (beginning-of-line 2)
  14033. (delete-region (point) (point-at-eol))
  14034. (org-fast-tag-insert "Current" current c-face)
  14035. (org-set-current-tags-overlay current ov-prefix)
  14036. (while (re-search-forward
  14037. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14038. (setq tg (match-string 1))
  14039. (add-text-properties
  14040. (match-beginning 1) (match-end 1)
  14041. (list 'face
  14042. (cond
  14043. ((member tg current) c-face)
  14044. ((member tg inherited) i-face)
  14045. (t (get-text-property (match-beginning 1) 'face))))))
  14046. (goto-char (point-min)))))
  14047. (org-detach-overlay org-tags-overlay)
  14048. (if rtn
  14049. (mapconcat 'identity current ":")
  14050. nil))))
  14051. (defun org-get-tags-string ()
  14052. "Get the TAGS string in the current headline."
  14053. (unless (org-at-heading-p t)
  14054. (user-error "Not on a heading"))
  14055. (save-excursion
  14056. (beginning-of-line 1)
  14057. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14058. (org-match-string-no-properties 1)
  14059. "")))
  14060. (defun org-get-tags ()
  14061. "Get the list of tags specified in the current headline."
  14062. (org-split-string (org-get-tags-string) ":"))
  14063. (defun org-get-buffer-tags ()
  14064. "Get a table of all tags used in the buffer, for completion."
  14065. (org-with-wide-buffer
  14066. (goto-char (point-min))
  14067. (let ((tag-re (concat org-outline-regexp-bol
  14068. "\\(?:.*?[ \t]\\)?"
  14069. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14070. tags)
  14071. (while (re-search-forward tag-re nil t)
  14072. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14073. (push tag tags)))
  14074. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14075. ;;;; The mapping API
  14076. (defun org-map-entries (func &optional match scope &rest skip)
  14077. "Call FUNC at each headline selected by MATCH in SCOPE.
  14078. FUNC is a function or a lisp form. The function will be called without
  14079. arguments, with the cursor positioned at the beginning of the headline.
  14080. The return values of all calls to the function will be collected and
  14081. returned as a list.
  14082. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14083. does not need to preserve point. After evaluation, the cursor will be
  14084. moved to the end of the line (presumably of the headline of the
  14085. processed entry) and search continues from there. Under some
  14086. circumstances, this may not produce the wanted results. For example,
  14087. if you have removed (e.g. archived) the current (sub)tree it could
  14088. mean that the next entry will be skipped entirely. In such cases, you
  14089. can specify the position from where search should continue by making
  14090. FUNC set the variable `org-map-continue-from' to the desired buffer
  14091. position.
  14092. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14093. Only headlines that are matched by this query will be considered during
  14094. the iteration. When MATCH is nil or t, all headlines will be
  14095. visited by the iteration.
  14096. SCOPE determines the scope of this command. It can be any of:
  14097. nil The current buffer, respecting the restriction if any
  14098. tree The subtree started with the entry at point
  14099. region The entries within the active region, if any
  14100. region-start-level
  14101. The entries within the active region, but only those at
  14102. the same level than the first one.
  14103. file The current buffer, without restriction
  14104. file-with-archives
  14105. The current buffer, and any archives associated with it
  14106. agenda All agenda files
  14107. agenda-with-archives
  14108. All agenda files with any archive files associated with them
  14109. \(file1 file2 ...)
  14110. If this is a list, all files in the list will be scanned
  14111. The remaining args are treated as settings for the skipping facilities of
  14112. the scanner. The following items can be given here:
  14113. archive skip trees with the archive tag
  14114. comment skip trees with the COMMENT keyword
  14115. function or Emacs Lisp form:
  14116. will be used as value for `org-agenda-skip-function', so
  14117. whenever the function returns a position, FUNC will not be
  14118. called for that entry and search will continue from the
  14119. position returned
  14120. If your function needs to retrieve the tags including inherited tags
  14121. at the *current* entry, you can use the value of the variable
  14122. `org-scanner-tags' which will be much faster than getting the value
  14123. with `org-get-tags-at'. If your function gets properties with
  14124. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14125. to t around the call to `org-entry-properties' to get the same speedup.
  14126. Note that if your function moves around to retrieve tags and properties at
  14127. a *different* entry, you cannot use these techniques."
  14128. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14129. (not (org-region-active-p)))
  14130. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14131. (org-agenda-skip-archived-trees (memq 'archive skip))
  14132. (org-agenda-skip-comment-trees (memq 'comment skip))
  14133. (org-agenda-skip-function
  14134. (car (org-delete-all '(comment archive) skip)))
  14135. (org-tags-match-list-sublevels t)
  14136. (start-level (eq scope 'region-start-level))
  14137. matcher file res
  14138. org-todo-keywords-for-agenda
  14139. org-done-keywords-for-agenda
  14140. org-todo-keyword-alist-for-agenda
  14141. org-tag-alist-for-agenda
  14142. todo-only)
  14143. (cond
  14144. ((eq match t) (setq matcher t))
  14145. ((eq match nil) (setq matcher t))
  14146. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14147. (save-excursion
  14148. (save-restriction
  14149. (cond ((eq scope 'tree)
  14150. (org-back-to-heading t)
  14151. (org-narrow-to-subtree)
  14152. (setq scope nil))
  14153. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14154. (org-region-active-p))
  14155. ;; If needed, set start-level to a string like "2"
  14156. (when start-level
  14157. (save-excursion
  14158. (goto-char (region-beginning))
  14159. (unless (org-at-heading-p) (outline-next-heading))
  14160. (setq start-level (org-current-level))))
  14161. (narrow-to-region (region-beginning)
  14162. (save-excursion
  14163. (goto-char (region-end))
  14164. (unless (and (bolp) (org-at-heading-p))
  14165. (outline-next-heading))
  14166. (point)))
  14167. (setq scope nil)))
  14168. (if (not scope)
  14169. (progn
  14170. (org-agenda-prepare-buffers
  14171. (and buffer-file-name (list buffer-file-name)))
  14172. (setq res (org-scan-tags func matcher todo-only start-level)))
  14173. ;; Get the right scope
  14174. (cond
  14175. ((and scope (listp scope) (symbolp (car scope)))
  14176. (setq scope (eval scope)))
  14177. ((eq scope 'agenda)
  14178. (setq scope (org-agenda-files t)))
  14179. ((eq scope 'agenda-with-archives)
  14180. (setq scope (org-agenda-files t))
  14181. (setq scope (org-add-archive-files scope)))
  14182. ((eq scope 'file)
  14183. (setq scope (and buffer-file-name (list buffer-file-name))))
  14184. ((eq scope 'file-with-archives)
  14185. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14186. (org-agenda-prepare-buffers scope)
  14187. (dolist (file scope)
  14188. (with-current-buffer (org-find-base-buffer-visiting file)
  14189. (save-excursion
  14190. (save-restriction
  14191. (widen)
  14192. (goto-char (point-min))
  14193. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14194. res)))
  14195. ;;; Properties API
  14196. (defconst org-special-properties
  14197. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14198. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14199. "The special properties valid in Org mode.
  14200. These are properties that are not defined in the property drawer,
  14201. but in some other way.")
  14202. (defconst org-default-properties
  14203. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14204. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14205. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14206. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14207. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14208. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14209. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14210. "Some properties that are used by Org mode for various purposes.
  14211. Being in this list makes sure that they are offered for completion.")
  14212. (defun org--valid-property-p (property)
  14213. "Non nil when string PROPERTY is a valid property name."
  14214. (not
  14215. (or (equal property "")
  14216. (org-string-match-p "\\s-" property))))
  14217. (defun org--update-property-plist (key val props)
  14218. "Associate KEY to VAL in alist PROPS.
  14219. Modifications are made by side-effect. Return new alist."
  14220. (let* ((appending (string= (substring key -1) "+"))
  14221. (key (if appending (substring key 0 -1) key))
  14222. (old (assoc-string key props t)))
  14223. (if (not old) (cons (cons key val) props)
  14224. (setcdr old (if appending (concat (cdr old) " " val) val))
  14225. props)))
  14226. (defun org-get-property-block (&optional beg force)
  14227. "Return the (beg . end) range of the body of the property drawer.
  14228. BEG is the beginning of the current subtree, or of the part
  14229. before the first headline. If it is not given, it will be found.
  14230. If the drawer does not exist, create it if FORCE is non-nil, or
  14231. return nil."
  14232. (org-with-wide-buffer
  14233. (when beg (goto-char beg))
  14234. (unless (org-before-first-heading-p)
  14235. (let ((beg (cond (beg)
  14236. ((or (not (featurep 'org-inlinetask))
  14237. (org-inlinetask-in-task-p))
  14238. (org-back-to-heading t))
  14239. (t (org-with-limited-levels (org-back-to-heading t))))))
  14240. (forward-line)
  14241. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14242. (cond ((looking-at org-property-drawer-re)
  14243. (forward-line)
  14244. (cons (point) (progn (goto-char (match-end 0))
  14245. (line-beginning-position))))
  14246. (force
  14247. (goto-char beg)
  14248. (org-insert-property-drawer)
  14249. (let ((pos (save-excursion (search-forward ":END:")
  14250. (line-beginning-position))))
  14251. (cons pos pos))))))))
  14252. (defun org-at-property-p ()
  14253. "Non-nil when point is inside a property drawer.
  14254. See `org-property-re' for match data, if applicable."
  14255. (save-excursion
  14256. (beginning-of-line)
  14257. (and (looking-at org-property-re)
  14258. (let ((property-drawer (save-match-data (org-get-property-block))))
  14259. (and property-drawer
  14260. (>= (point) (car property-drawer))
  14261. (< (point) (cdr property-drawer)))))))
  14262. (defun org-property-action ()
  14263. "Do an action on properties."
  14264. (interactive)
  14265. (unless (org-at-property-p) (user-error "Not at a property"))
  14266. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14267. (let ((c (read-char-exclusive)))
  14268. (case c
  14269. (?s (call-interactively #'org-set-property))
  14270. (?d (call-interactively #'org-delete-property))
  14271. (?D (call-interactively #'org-delete-property-globally))
  14272. (?c (call-interactively #'org-compute-property-at-point))
  14273. (otherwise (user-error "No such property action %c" c)))))
  14274. (defun org-inc-effort ()
  14275. "Increment the value of the effort property in the current entry."
  14276. (interactive)
  14277. (org-set-effort nil t))
  14278. (defvar org-clock-effort) ; Defined in org-clock.el.
  14279. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14280. (defun org-set-effort (&optional value increment)
  14281. "Set the effort property of the current entry.
  14282. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14283. 10th allowed value.
  14284. When INCREMENT is non-nil, set the property to the next allowed value."
  14285. (interactive "P")
  14286. (if (equal value 0) (setq value 10))
  14287. (let* ((completion-ignore-case t)
  14288. (prop org-effort-property)
  14289. (cur (org-entry-get nil prop))
  14290. (allowed (org-property-get-allowed-values nil prop 'table))
  14291. (existing (mapcar 'list (org-property-values prop)))
  14292. (heading (nth 4 (org-heading-components)))
  14293. rpl
  14294. (val (cond
  14295. ((stringp value) value)
  14296. ((and allowed (integerp value))
  14297. (or (car (nth (1- value) allowed))
  14298. (car (org-last allowed))))
  14299. ((and allowed increment)
  14300. (or (caadr (member (list cur) allowed))
  14301. (user-error "Allowed effort values are not set")))
  14302. (allowed
  14303. (message "Select 1-9,0, [RET%s]: %s"
  14304. (if cur (concat "=" cur) "")
  14305. (mapconcat 'car allowed " "))
  14306. (setq rpl (read-char-exclusive))
  14307. (if (equal rpl ?\r)
  14308. cur
  14309. (setq rpl (- rpl ?0))
  14310. (if (equal rpl 0) (setq rpl 10))
  14311. (if (and (> rpl 0) (<= rpl (length allowed)))
  14312. (car (nth (1- rpl) allowed))
  14313. (org-completing-read "Effort: " allowed nil))))
  14314. (t
  14315. (let (org-completion-use-ido org-completion-use-iswitchb)
  14316. (org-completing-read
  14317. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14318. (concat "[" cur "]") "")
  14319. ": ")
  14320. existing nil nil "" nil cur))))))
  14321. (unless (equal (org-entry-get nil prop) val)
  14322. (org-entry-put nil prop val))
  14323. (org-refresh-property
  14324. '((effort . identity)
  14325. (effort-minutes . org-duration-string-to-minutes))
  14326. val)
  14327. (when (string= heading org-clock-current-task)
  14328. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14329. (org-clock-update-mode-line))
  14330. (message "%s is now %s" prop val)))
  14331. (defun org-entry-properties (&optional pom which)
  14332. "Get all properties of the current entry.
  14333. When POM is a buffer position, get all properties from the entry
  14334. there instead.
  14335. This includes the TODO keyword, the tags, time strings for
  14336. deadline, scheduled, and clocking, and any additional properties
  14337. defined in the entry.
  14338. If WHICH is nil or `all', get all properties. If WHICH is
  14339. `special' or `standard', only get that subclass. If WHICH is
  14340. a string, only get that property.
  14341. Return value is an alist. Keys are properties, as upcased
  14342. strings."
  14343. (org-with-point-at pom
  14344. (when (and (derived-mode-p 'org-mode)
  14345. (ignore-errors (org-back-to-heading t)))
  14346. (catch 'exit
  14347. (let* ((beg (point))
  14348. (specific (and (stringp which) (upcase which)))
  14349. (which (cond ((not specific) which)
  14350. ((member specific org-special-properties) 'special)
  14351. (t 'standard)))
  14352. props)
  14353. ;; Get the special properties, like TODO and TAGS.
  14354. (when (memq which '(nil all special))
  14355. (when (or (not specific) (string= specific "CLOCKSUM"))
  14356. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14357. (when clocksum
  14358. (push (cons "CLOCKSUM"
  14359. (org-columns-number-to-string
  14360. (/ (float clocksum) 60.) 'add_times))
  14361. props)))
  14362. (when specific (throw 'exit props)))
  14363. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14364. (let ((clocksumt (get-text-property (point)
  14365. :org-clock-minutes-today)))
  14366. (when clocksumt
  14367. (push (cons "CLOCKSUM_T"
  14368. (org-columns-number-to-string
  14369. (/ (float clocksumt) 60.) 'add_times))
  14370. props)))
  14371. (when specific (throw 'exit props)))
  14372. (when (or (not specific) (string= specific "ITEM"))
  14373. (when (looking-at org-complex-heading-regexp)
  14374. (push (cons "ITEM"
  14375. (concat
  14376. (org-match-string-no-properties 1)
  14377. (let ((title (org-match-string-no-properties 4)))
  14378. (when (org-string-nw-p title)
  14379. (concat " " (org-remove-tabs title))))))
  14380. props))
  14381. (when specific (throw 'exit props)))
  14382. (when (or (not specific) (string= specific "TODO"))
  14383. (let ((case-fold-search nil))
  14384. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14385. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14386. (when specific (throw 'exit props)))
  14387. (when (or (not specific) (string= specific "PRIORITY"))
  14388. (push (cons "PRIORITY"
  14389. (if (looking-at org-priority-regexp)
  14390. (org-match-string-no-properties 2)
  14391. (char-to-string org-default-priority)))
  14392. props)
  14393. (when specific (throw 'exit props)))
  14394. (when (or (not specific) (string= specific "FILE"))
  14395. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14396. props)
  14397. (when specific (throw 'exit props)))
  14398. (when (or (not specific) (string= specific "TAGS"))
  14399. (let ((value (org-string-nw-p (org-get-tags-string))))
  14400. (when value (push (cons "TAGS" value) props)))
  14401. (when specific (throw 'exit props)))
  14402. (when (or (not specific) (string= specific "ALLTAGS"))
  14403. (let ((value (org-get-tags-at)))
  14404. (when value
  14405. (push (cons "ALLTAGS"
  14406. (format ":%s:" (mapconcat #'identity value ":")))
  14407. props)))
  14408. (when specific (throw 'exit props)))
  14409. (when (or (not specific) (string= specific "BLOCKED"))
  14410. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14411. (when specific (throw 'exit props)))
  14412. (when (or (not specific)
  14413. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14414. (forward-line)
  14415. (when (org-looking-at-p org-planning-line-re)
  14416. (end-of-line)
  14417. (let ((bol (line-beginning-position))
  14418. ;; Backward compatibility: time keywords used to
  14419. ;; be configurable (before 8.3). Make sure we
  14420. ;; get the correct keyword.
  14421. (key-assoc `(("CLOSED" . ,org-closed-string)
  14422. ("DEADLINE" . ,org-deadline-string)
  14423. ("SCHEDULED" . ,org-scheduled-string))))
  14424. (dolist (pair (if specific (list (assoc specific key-assoc))
  14425. key-assoc))
  14426. (save-excursion
  14427. (when (search-backward (cdr pair) bol t)
  14428. (goto-char (match-end 0))
  14429. (skip-chars-forward " \t")
  14430. (and (looking-at org-ts-regexp-both)
  14431. (push (cons (car pair)
  14432. (org-match-string-no-properties 0))
  14433. props)))))))
  14434. (when specific (throw 'exit props)))
  14435. (when (or (not specific)
  14436. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14437. (let ((find-ts
  14438. (lambda (end ts)
  14439. (let ((regexp (cond
  14440. ((string= specific "TIMESTAMP")
  14441. org-ts-regexp)
  14442. ((string= specific "TIMESTAMP_IA")
  14443. org-ts-regexp-inactive)
  14444. ((assoc "TIMESTAMP_IA" ts)
  14445. org-ts-regexp)
  14446. ((assoc "TIMESTAMP" ts)
  14447. org-ts-regexp-inactive)
  14448. (t org-ts-regexp-both))))
  14449. (catch 'next
  14450. (while (re-search-forward regexp end t)
  14451. (backward-char)
  14452. (let ((object (org-element-context)))
  14453. ;; Accept to match timestamps in node
  14454. ;; properties, too.
  14455. (when (memq (org-element-type object)
  14456. '(node-property timestamp))
  14457. (let ((type
  14458. (org-element-property :type object)))
  14459. (cond
  14460. ((and (memq type '(active active-range))
  14461. (not (equal specific "TIMESTAMP_IA")))
  14462. (unless (assoc "TIMESTAMP" ts)
  14463. (push (cons "TIMESTAMP"
  14464. (org-element-property
  14465. :raw-value object))
  14466. ts)
  14467. (when specific (throw 'exit ts))))
  14468. ((and (memq type '(inactive inactive-range))
  14469. (not (string= specific "TIMESTAMP")))
  14470. (unless (assoc "TIMESTAMP_IA" ts)
  14471. (push (cons "TIMESTAMP_IA"
  14472. (org-element-property
  14473. :raw-value object))
  14474. ts)
  14475. (when specific (throw 'exit ts))))))
  14476. ;; Both timestamp types are found,
  14477. ;; move to next part.
  14478. (when (= (length ts) 2) (throw 'next ts)))))
  14479. ts)))))
  14480. (goto-char beg)
  14481. ;; First look for timestamps within headline.
  14482. (let ((ts (funcall find-ts (line-end-position) nil)))
  14483. (if (= (length ts) 2) (setq props (nconc ts props))
  14484. (forward-line)
  14485. ;; Then find timestamps in the section, skipping
  14486. ;; planning line.
  14487. (when (org-looking-at-p org-planning-line-re)
  14488. (forward-line))
  14489. (let ((end (save-excursion (outline-next-heading))))
  14490. (setq props (nconc (funcall find-ts end ts) props))))))))
  14491. ;; Get the standard properties, like :PROP:.
  14492. (when (memq which '(nil all standard))
  14493. ;; If we are looking after a specific property, delegate
  14494. ;; to `org-entry-get', which is faster. However, make an
  14495. ;; exception for "CATEGORY", since it can be also set
  14496. ;; through keywords (i.e. #+CATEGORY).
  14497. (if (and specific (not (equal specific "CATEGORY")))
  14498. (let ((value (org-entry-get beg specific nil t)))
  14499. (throw 'exit (and value (list (cons specific value)))))
  14500. (let ((range (org-get-property-block beg)))
  14501. (when range
  14502. (let ((end (cdr range)) seen-base)
  14503. (goto-char (car range))
  14504. ;; Unlike to `org--update-property-plist', we
  14505. ;; handle the case where base values is found
  14506. ;; after its extension. We also forbid standard
  14507. ;; properties to be named as special properties.
  14508. (while (re-search-forward org-property-re end t)
  14509. (let* ((key (upcase (org-match-string-no-properties 2)))
  14510. (extendp (org-string-match-p "\\+\\'" key))
  14511. (key-base (if extendp (substring key 0 -1) key))
  14512. (value (org-match-string-no-properties 3)))
  14513. (cond
  14514. ((member-ignore-case key-base org-special-properties))
  14515. (extendp
  14516. (setq props
  14517. (org--update-property-plist key value props)))
  14518. ((member key seen-base))
  14519. (t (push key seen-base)
  14520. (let ((p (assoc-string key props t)))
  14521. (if p (setcdr p (concat value " " (cdr p)))
  14522. (push (cons key value) props))))))))))))
  14523. (unless (assoc "CATEGORY" props)
  14524. (push (cons "CATEGORY" (org-get-category beg)) props)
  14525. (when (string= specific "CATEGORY") (throw 'exit props)))
  14526. ;; Return value.
  14527. props)))))
  14528. (defun org-property--local-values (property literal-nil)
  14529. "Return value for PROPERTY in current entry.
  14530. Value is a list whose care is the base value for PROPERTY and cdr
  14531. a list of accumulated values. Return nil if neither is found in
  14532. the entry. Also return nil when PROPERTY is set to \"nil\",
  14533. unless LITERAL-NIL is non-nil."
  14534. (let ((range (org-get-property-block)))
  14535. (when range
  14536. (goto-char (car range))
  14537. (let* ((case-fold-search t)
  14538. (end (cdr range))
  14539. (value
  14540. ;; Base value.
  14541. (save-excursion
  14542. (let ((v (and (re-search-forward
  14543. (org-re-property property nil t) end t)
  14544. (org-match-string-no-properties 3))))
  14545. (list (if literal-nil v (org-not-nil v)))))))
  14546. ;; Find additional values.
  14547. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14548. (while (re-search-forward property+ end t)
  14549. (push (org-match-string-no-properties 3) value)))
  14550. ;; Return final values.
  14551. (and (not (equal value '(nil))) (nreverse value))))))
  14552. (defun org-entry-get (pom property &optional inherit literal-nil)
  14553. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14554. If INHERIT is non-nil and the entry does not have the property,
  14555. then also check higher levels of the hierarchy. If INHERIT is
  14556. the symbol `selective', use inheritance only if the setting in
  14557. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14558. If the property is present but empty, the return value is the
  14559. empty string. If the property is not present at all, nil is
  14560. returned. In any other case, return the value as a string.
  14561. Search is case-insensitive.
  14562. If LITERAL-NIL is set, return the string value \"nil\" as
  14563. a string, do not interpret it as the list atom nil. This is used
  14564. for inheritance when a \"nil\" value can supersede a non-nil
  14565. value higher up the hierarchy."
  14566. (org-with-point-at pom
  14567. (cond
  14568. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14569. ;; We need a special property. Use `org-entry-properties' to
  14570. ;; retrieve it, but specify the wanted property.
  14571. (cdr (assoc-string property (org-entry-properties nil property))))
  14572. ((and inherit
  14573. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14574. (org-entry-get-with-inheritance property literal-nil))
  14575. (t
  14576. (let* ((local (org-property--local-values property literal-nil))
  14577. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14578. (if literal-nil value (org-not-nil value)))))))
  14579. (defun org-property-or-variable-value (var &optional inherit)
  14580. "Check if there is a property fixing the value of VAR.
  14581. If yes, return this value. If not, return the current value of the variable."
  14582. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14583. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14584. (read prop)
  14585. (symbol-value var))))
  14586. (defun org-entry-delete (pom property)
  14587. "Delete PROPERTY from entry at point-or-marker POM.
  14588. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14589. non-nil when a property was removed."
  14590. (unless (member property org-special-properties)
  14591. (org-with-point-at pom
  14592. (let ((range (org-get-property-block)))
  14593. (when range
  14594. (let* ((begin (car range))
  14595. (origin (cdr range))
  14596. (end (copy-marker origin))
  14597. (re (org-re-property
  14598. (concat (regexp-quote property) "\\+?") t t)))
  14599. (goto-char begin)
  14600. (while (re-search-forward re end t)
  14601. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14602. ;; If drawer is empty, remove it altogether.
  14603. (when (= begin end)
  14604. (delete-region (line-beginning-position 0)
  14605. (line-beginning-position 2)))
  14606. ;; Return non-nil if some property was removed.
  14607. (prog1 (/= end origin) (set-marker end nil))))))))
  14608. ;; Multi-values properties are properties that contain multiple values
  14609. ;; These values are assumed to be single words, separated by whitespace.
  14610. (defun org-entry-add-to-multivalued-property (pom property value)
  14611. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14612. (let* ((old (org-entry-get pom property))
  14613. (values (and old (org-split-string old "[ \t]"))))
  14614. (setq value (org-entry-protect-space value))
  14615. (unless (member value values)
  14616. (setq values (append values (list value)))
  14617. (org-entry-put pom property
  14618. (mapconcat 'identity values " ")))))
  14619. (defun org-entry-remove-from-multivalued-property (pom property value)
  14620. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14621. (let* ((old (org-entry-get pom property))
  14622. (values (and old (org-split-string old "[ \t]"))))
  14623. (setq value (org-entry-protect-space value))
  14624. (when (member value values)
  14625. (setq values (delete value values))
  14626. (org-entry-put pom property
  14627. (mapconcat 'identity values " ")))))
  14628. (defun org-entry-member-in-multivalued-property (pom property value)
  14629. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14630. (let* ((old (org-entry-get pom property))
  14631. (values (and old (org-split-string old "[ \t]"))))
  14632. (setq value (org-entry-protect-space value))
  14633. (member value values)))
  14634. (defun org-entry-get-multivalued-property (pom property)
  14635. "Return a list of values in a multivalued property."
  14636. (let* ((value (org-entry-get pom property))
  14637. (values (and value (org-split-string value "[ \t]"))))
  14638. (mapcar 'org-entry-restore-space values)))
  14639. (defun org-entry-put-multivalued-property (pom property &rest values)
  14640. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14641. VALUES should be a list of strings. Spaces will be protected."
  14642. (org-entry-put pom property
  14643. (mapconcat 'org-entry-protect-space values " "))
  14644. (let* ((value (org-entry-get pom property))
  14645. (values (and value (org-split-string value "[ \t]"))))
  14646. (mapcar 'org-entry-restore-space values)))
  14647. (defun org-entry-protect-space (s)
  14648. "Protect spaces and newline in string S."
  14649. (while (string-match " " s)
  14650. (setq s (replace-match "%20" t t s)))
  14651. (while (string-match "\n" s)
  14652. (setq s (replace-match "%0A" t t s)))
  14653. s)
  14654. (defun org-entry-restore-space (s)
  14655. "Restore spaces and newline in string S."
  14656. (while (string-match "%20" s)
  14657. (setq s (replace-match " " t t s)))
  14658. (while (string-match "%0A" s)
  14659. (setq s (replace-match "\n" t t s)))
  14660. s)
  14661. (defvar org-entry-property-inherited-from (make-marker)
  14662. "Marker pointing to the entry from where a property was inherited.
  14663. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14664. location of the entry where the inheritance search matched. If there was
  14665. no match, the marker will point nowhere.
  14666. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14667. is set.")
  14668. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14669. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14670. The search will stop at the first ancestor which has the property defined.
  14671. If the value found is \"nil\", return nil to show that the property
  14672. should be considered as undefined (this is the meaning of nil here).
  14673. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14674. (move-marker org-entry-property-inherited-from nil)
  14675. (org-with-wide-buffer
  14676. (let (value)
  14677. (catch 'exit
  14678. (while t
  14679. (let ((v (org-property--local-values property literal-nil)))
  14680. (when v
  14681. (setq value
  14682. (concat (mapconcat #'identity (delq nil v) " ")
  14683. (and value " ")
  14684. value)))
  14685. (cond
  14686. ((car v)
  14687. (org-back-to-heading t)
  14688. (move-marker org-entry-property-inherited-from (point))
  14689. (throw 'exit nil))
  14690. ((org-up-heading-safe))
  14691. (t
  14692. (let ((global
  14693. (cdr (or (assoc-string property org-file-properties t)
  14694. (assoc-string property org-global-properties t)
  14695. (assoc-string property org-global-properties-fixed t)))))
  14696. (cond ((not global))
  14697. (value (setq value (concat global " " value)))
  14698. (t (setq value global))))
  14699. (throw 'exit nil))))))
  14700. (if literal-nil value (org-not-nil value)))))
  14701. (defvar org-property-changed-functions nil
  14702. "Hook called when the value of a property has changed.
  14703. Each hook function should accept two arguments, the name of the property
  14704. and the new value.")
  14705. (defun org-entry-put (pom property value)
  14706. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14707. If the value is nil, it is converted to the empty string. If it
  14708. is not a string, an error is raised. Also raise an error on
  14709. invalid property names.
  14710. PROPERTY can be any regular property (see
  14711. `org-special-properties'). It can also be \"TODO\",
  14712. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14713. For the last two properties, VALUE may have any of the special
  14714. values \"earlier\" and \"later\". The function then increases or
  14715. decreases scheduled or deadline date by one day."
  14716. (cond ((null value) (setq value ""))
  14717. ((not (stringp value)) (error "Properties values should be strings"))
  14718. ((not (org--valid-property-p property))
  14719. (user-error "Invalid property name: \"%s\"" property)))
  14720. (org-with-point-at pom
  14721. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14722. (org-back-to-heading t)
  14723. (org-with-limited-levels (org-back-to-heading t)))
  14724. (let ((beg (point)))
  14725. (cond
  14726. ((equal property "TODO")
  14727. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14728. ((not (member value org-todo-keywords-1))
  14729. (user-error "\"%s\" is not a valid TODO state" value)))
  14730. (org-todo value)
  14731. (org-set-tags nil 'align))
  14732. ((equal property "PRIORITY")
  14733. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14734. (org-set-tags nil 'align))
  14735. ((equal property "SCHEDULED")
  14736. (forward-line)
  14737. (if (and (org-looking-at-p org-planning-line-re)
  14738. (re-search-forward
  14739. org-scheduled-time-regexp (line-end-position) t))
  14740. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14741. ((string= value "later") (org-timestamp-change 1 'day))
  14742. ((string= value "") (org-schedule '(4)))
  14743. (t (org-schedule nil value)))
  14744. (if (member value '("earlier" "later" ""))
  14745. (call-interactively #'org-schedule)
  14746. (org-schedule nil value))))
  14747. ((equal property "DEADLINE")
  14748. (forward-line)
  14749. (if (and (org-looking-at-p org-planning-line-re)
  14750. (re-search-forward
  14751. org-deadline-time-regexp (line-end-position) t))
  14752. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14753. ((string= value "later") (org-timestamp-change 1 'day))
  14754. ((string= value "") (org-deadline '(4)))
  14755. (t (org-deadline nil value)))
  14756. (if (member value '("earlier" "later" ""))
  14757. (call-interactively #'org-deadline)
  14758. (org-deadline nil value))))
  14759. ((member property org-special-properties)
  14760. (error "The %s property cannot be set with `org-entry-put'" property))
  14761. (t
  14762. (let* ((range (org-get-property-block beg 'force))
  14763. (end (cdr range))
  14764. (case-fold-search t))
  14765. (goto-char (car range))
  14766. (if (re-search-forward (org-re-property property nil t) end t)
  14767. (progn (delete-region (match-beginning 0) (match-end 0))
  14768. (goto-char (match-beginning 0)))
  14769. (goto-char end)
  14770. (insert "\n")
  14771. (backward-char))
  14772. (insert ":" property ":")
  14773. (when value (insert " " value))
  14774. (org-indent-line)))))
  14775. (run-hook-with-args 'org-property-changed-functions property value)))
  14776. (defun org-buffer-property-keys (&optional specials defaults columns)
  14777. "Get all property keys in the current buffer.
  14778. When SPECIALS is non-nil, also list the special properties that
  14779. reflect things like tags and TODO state.
  14780. When DEFAULTS is non-nil, also include properties that has
  14781. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14782. DESCRIPTION, LOCATION, and LOGGING and others.
  14783. When COLUMNS in non-nil, also include property names given in
  14784. COLUMN formats in the current buffer."
  14785. (let ((case-fold-search t)
  14786. (props (append
  14787. (and specials org-special-properties)
  14788. (and defaults (cons org-effort-property org-default-properties))
  14789. nil)))
  14790. (org-with-wide-buffer
  14791. (goto-char (point-min))
  14792. (while (re-search-forward org-property-start-re nil t)
  14793. (let ((range (org-get-property-block)))
  14794. (catch 'skip
  14795. (unless range
  14796. (when (and (not (org-before-first-heading-p))
  14797. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14798. (line-beginning-position))))
  14799. (org-get-property-block nil t))
  14800. (throw 'skip nil))
  14801. (goto-char (car range))
  14802. (let ((begin (car range))
  14803. (end (cdr range)))
  14804. ;; Make sure that found property block is not located
  14805. ;; before current point, as it would generate an infloop.
  14806. ;; It can happen, for example, in the following
  14807. ;; situation:
  14808. ;;
  14809. ;; * Headline
  14810. ;; :PROPERTIES:
  14811. ;; ...
  14812. ;; :END:
  14813. ;; *************** Inlinetask
  14814. ;; #+BEGIN_EXAMPLE
  14815. ;; :PROPERTIES:
  14816. ;; #+END_EXAMPLE
  14817. ;;
  14818. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14819. (while (< (point) end)
  14820. (let ((p (progn (looking-at org-property-re)
  14821. (org-match-string-no-properties 2))))
  14822. ;; Only add true property name, not extension symbol.
  14823. (add-to-list 'props
  14824. (if (not (org-string-match-p "\\+\\'" p)) p
  14825. (substring p 0 -1))))
  14826. (forward-line))))
  14827. (outline-next-heading)))
  14828. (when columns
  14829. (goto-char (point-min))
  14830. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14831. (let ((element (org-element-at-point)))
  14832. (when (memq (org-element-type element) '(keyword node-property))
  14833. (let ((value (org-element-property :value element))
  14834. (start 0))
  14835. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14836. (setq start (match-end 0))
  14837. (let ((p (org-match-string-no-properties 1 value)))
  14838. (unless (member-ignore-case p org-special-properties)
  14839. (add-to-list 'props p))))))))))
  14840. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14841. (defun org-property-values (key)
  14842. "List all non-nil values of property KEY in current buffer."
  14843. (org-with-wide-buffer
  14844. (goto-char (point-min))
  14845. (let ((case-fold-search t)
  14846. (re (org-re-property key))
  14847. values)
  14848. (while (re-search-forward re nil t)
  14849. (add-to-list 'values (org-entry-get (point) key)))
  14850. values)))
  14851. (defun org-insert-property-drawer ()
  14852. "Insert a property drawer into the current entry."
  14853. (org-with-wide-buffer
  14854. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14855. (org-back-to-heading t)
  14856. (org-with-limited-levels (org-back-to-heading t)))
  14857. (forward-line)
  14858. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14859. (unless (org-looking-at-p org-property-drawer-re)
  14860. ;; Make sure we start editing a line from current entry, not from
  14861. ;; next one. It prevents extending text properties or overlays
  14862. ;; belonging to the latter.
  14863. (when (bolp) (backward-char))
  14864. (let ((begin (1+ (point)))
  14865. (inhibit-read-only t))
  14866. (insert "\n:PROPERTIES:\n:END:")
  14867. (when (eobp) (insert "\n"))
  14868. (org-indent-region begin (point))))))
  14869. (defun org-insert-drawer (&optional arg drawer)
  14870. "Insert a drawer at point.
  14871. When optional argument ARG is non-nil, insert a property drawer.
  14872. Optional argument DRAWER, when non-nil, is a string representing
  14873. drawer's name. Otherwise, the user is prompted for a name.
  14874. If a region is active, insert the drawer around that region
  14875. instead.
  14876. Point is left between drawer's boundaries."
  14877. (interactive "P")
  14878. (let* ((drawer (if arg "PROPERTIES"
  14879. (or drawer (read-from-minibuffer "Drawer: ")))))
  14880. (cond
  14881. ;; With C-u, fall back on `org-insert-property-drawer'
  14882. (arg (org-insert-property-drawer))
  14883. ;; Check validity of suggested drawer's name.
  14884. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14885. (user-error "Invalid drawer name"))
  14886. ;; With an active region, insert a drawer at point.
  14887. ((not (org-region-active-p))
  14888. (progn
  14889. (unless (bolp) (insert "\n"))
  14890. (insert (format ":%s:\n\n:END:\n" drawer))
  14891. (forward-line -2)))
  14892. ;; Otherwise, insert the drawer at point
  14893. (t
  14894. (let ((rbeg (region-beginning))
  14895. (rend (copy-marker (region-end))))
  14896. (unwind-protect
  14897. (progn
  14898. (goto-char rbeg)
  14899. (beginning-of-line)
  14900. (when (save-excursion
  14901. (re-search-forward org-outline-regexp-bol rend t))
  14902. (user-error "Drawers cannot contain headlines"))
  14903. ;; Position point at the beginning of the first
  14904. ;; non-blank line in region. Insert drawer's opening
  14905. ;; there, then indent it.
  14906. (org-skip-whitespace)
  14907. (beginning-of-line)
  14908. (insert ":" drawer ":\n")
  14909. (forward-line -1)
  14910. (indent-for-tab-command)
  14911. ;; Move point to the beginning of the first blank line
  14912. ;; after the last non-blank line in region. Insert
  14913. ;; drawer's closing, then indent it.
  14914. (goto-char rend)
  14915. (skip-chars-backward " \r\t\n")
  14916. (insert "\n:END:")
  14917. (deactivate-mark t)
  14918. (indent-for-tab-command)
  14919. (unless (eolp) (insert "\n")))
  14920. ;; Clear marker, whatever the outcome of insertion is.
  14921. (set-marker rend nil)))))))
  14922. (defvar org-property-set-functions-alist nil
  14923. "Property set function alist.
  14924. Each entry should have the following format:
  14925. (PROPERTY . READ-FUNCTION)
  14926. The read function will be called with the same argument as
  14927. `org-completing-read'.")
  14928. (defun org-set-property-function (property)
  14929. "Get the function that should be used to set PROPERTY.
  14930. This is computed according to `org-property-set-functions-alist'."
  14931. (or (cdr (assoc property org-property-set-functions-alist))
  14932. 'org-completing-read))
  14933. (defun org-read-property-value (property)
  14934. "Read PROPERTY value from user."
  14935. (let* ((completion-ignore-case t)
  14936. (allowed (org-property-get-allowed-values nil property 'table))
  14937. (cur (org-entry-get nil property))
  14938. (prompt (concat property " value"
  14939. (if (and cur (string-match "\\S-" cur))
  14940. (concat " [" cur "]") "") ": "))
  14941. (set-function (org-set-property-function property))
  14942. (val (if allowed
  14943. (funcall set-function prompt allowed nil
  14944. (not (get-text-property 0 'org-unrestricted
  14945. (caar allowed))))
  14946. (let (org-completion-use-ido org-completion-use-iswitchb)
  14947. (funcall set-function prompt
  14948. (mapcar 'list (org-property-values property))
  14949. nil nil "" nil cur)))))
  14950. (org-trim val)))
  14951. (defvar org-last-set-property nil)
  14952. (defvar org-last-set-property-value nil)
  14953. (defun org-read-property-name ()
  14954. "Read a property name."
  14955. (let ((completion-ignore-case t)
  14956. (default-prop (or (and (org-at-property-p)
  14957. (org-match-string-no-properties 2))
  14958. org-last-set-property)))
  14959. (org-completing-read
  14960. (concat "Property"
  14961. (if default-prop (concat " [" default-prop "]") "")
  14962. ": ")
  14963. (mapcar #'list (org-buffer-property-keys nil t t))
  14964. nil nil nil nil default-prop)))
  14965. (defun org-set-property-and-value (use-last)
  14966. "Allow to set [PROPERTY]: [value] direction from prompt.
  14967. When use-default, don't even ask, just use the last
  14968. \"[PROPERTY]: [value]\" string from the history."
  14969. (interactive "P")
  14970. (let* ((completion-ignore-case t)
  14971. (pv (or (and use-last org-last-set-property-value)
  14972. (org-completing-read
  14973. "Enter a \"[Property]: [value]\" pair: "
  14974. nil nil nil nil nil
  14975. org-last-set-property-value)))
  14976. prop val)
  14977. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14978. (setq prop (match-string 1 pv)
  14979. val (match-string 2 pv))
  14980. (org-set-property prop val))))
  14981. (defun org-set-property (property value)
  14982. "In the current entry, set PROPERTY to VALUE.
  14983. When called interactively, this will prompt for a property name, offering
  14984. completion on existing and default properties. And then it will prompt
  14985. for a value, offering completion either on allowed values (via an inherited
  14986. xxx_ALL property) or on existing values in other instances of this property
  14987. in the current file.
  14988. Throw an error when trying to set a property with an invalid name."
  14989. (interactive (list nil nil))
  14990. (let ((property (or property (org-read-property-name))))
  14991. ;; `org-entry-put' also makes the following check, but this one
  14992. ;; avoids polluting `org-last-set-property' and
  14993. ;; `org-last-set-property-value' needlessly.
  14994. (unless (org--valid-property-p property)
  14995. (user-error "Invalid property name: \"%s\"" property))
  14996. (let ((value (or value (org-read-property-value property)))
  14997. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14998. (setq org-last-set-property property)
  14999. (setq org-last-set-property-value (concat property ": " value))
  15000. ;; Possibly postprocess the inserted value:
  15001. (when fn (setq value (funcall fn value)))
  15002. (unless (equal (org-entry-get nil property) value)
  15003. (org-entry-put nil property value)))))
  15004. (defun org-find-property (property &optional value)
  15005. "Find first entry in buffer that sets PROPERTY.
  15006. When optional argument VALUE is non-nil, only consider an entry
  15007. if it contains PROPERTY set to this value. If PROPERTY should be
  15008. explicitly set to nil, use string \"nil\" for VALUE.
  15009. Return position where the entry begins, or nil if there is no
  15010. such entry. If narrowing is in effect, only search the visible
  15011. part of the buffer."
  15012. (save-excursion
  15013. (goto-char (point-min))
  15014. (let ((case-fold-search t)
  15015. (re (org-re-property property nil (not value) value)))
  15016. (catch 'exit
  15017. (while (re-search-forward re nil t)
  15018. (when (if value (org-at-property-p)
  15019. (org-entry-get (point) property nil t))
  15020. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  15021. (defun org-delete-property (property)
  15022. "In the current entry, delete PROPERTY."
  15023. (interactive
  15024. (let* ((completion-ignore-case t)
  15025. (cat (org-entry-get (point) "CATEGORY"))
  15026. (props0 (org-entry-properties nil 'standard))
  15027. (props (if cat props0
  15028. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  15029. (prop (if (< 1 (length props))
  15030. (org-icompleting-read "Property: " props nil t)
  15031. (caar props))))
  15032. (list prop)))
  15033. (if (not property)
  15034. (message "No property to delete in this entry")
  15035. (org-entry-delete nil property)
  15036. (message "Property \"%s\" deleted" property)))
  15037. (defun org-delete-property-globally (property)
  15038. "Remove PROPERTY globally, from all entries.
  15039. This function ignores narrowing, if any."
  15040. (interactive
  15041. (let* ((completion-ignore-case t)
  15042. (prop (org-icompleting-read
  15043. "Globally remove property: "
  15044. (mapcar #'list (org-buffer-property-keys)))))
  15045. (list prop)))
  15046. (org-with-wide-buffer
  15047. (goto-char (point-min))
  15048. (let ((count 0)
  15049. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15050. (while (re-search-forward re nil t)
  15051. (when (org-entry-delete (point) property) (incf count)))
  15052. (message "Property \"%s\" removed from %d entries" property count))))
  15053. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15054. (defun org-compute-property-at-point ()
  15055. "Compute the property at point.
  15056. This looks for an enclosing column format, extracts the operator and
  15057. then applies it to the property in the column format's scope."
  15058. (interactive)
  15059. (unless (org-at-property-p)
  15060. (user-error "Not at a property"))
  15061. (let ((prop (org-match-string-no-properties 2)))
  15062. (org-columns-get-format-and-top-level)
  15063. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15064. (user-error "No operator defined for property %s" prop))
  15065. (org-columns-compute prop)))
  15066. (defvar org-property-allowed-value-functions nil
  15067. "Hook for functions supplying allowed values for a specific property.
  15068. The functions must take a single argument, the name of the property, and
  15069. return a flat list of allowed values. If \":ETC\" is one of
  15070. the values, this means that these values are intended as defaults for
  15071. completion, but that other values should be allowed too.
  15072. The functions must return nil if they are not responsible for this
  15073. property.")
  15074. (defun org-property-get-allowed-values (pom property &optional table)
  15075. "Get allowed values for the property PROPERTY.
  15076. When TABLE is non-nil, return an alist that can directly be used for
  15077. completion."
  15078. (let (vals)
  15079. (cond
  15080. ((equal property "TODO")
  15081. (setq vals (org-with-point-at pom
  15082. (append org-todo-keywords-1 '("")))))
  15083. ((equal property "PRIORITY")
  15084. (let ((n org-lowest-priority))
  15085. (while (>= n org-highest-priority)
  15086. (push (char-to-string n) vals)
  15087. (setq n (1- n)))))
  15088. ((equal property "CATEGORY"))
  15089. ((member property org-special-properties))
  15090. ((setq vals (run-hook-with-args-until-success
  15091. 'org-property-allowed-value-functions property)))
  15092. (t
  15093. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15094. (when (and vals (string-match "\\S-" vals))
  15095. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15096. (setq vals (mapcar (lambda (x)
  15097. (cond ((stringp x) x)
  15098. ((numberp x) (number-to-string x))
  15099. ((symbolp x) (symbol-name x))
  15100. (t "???")))
  15101. vals)))))
  15102. (when (member ":ETC" vals)
  15103. (setq vals (remove ":ETC" vals))
  15104. (org-add-props (car vals) '(org-unrestricted t)))
  15105. (if table (mapcar 'list vals) vals)))
  15106. (defun org-property-previous-allowed-value (&optional previous)
  15107. "Switch to the next allowed value for this property."
  15108. (interactive)
  15109. (org-property-next-allowed-value t))
  15110. (defun org-property-next-allowed-value (&optional previous)
  15111. "Switch to the next allowed value for this property."
  15112. (interactive)
  15113. (unless (org-at-property-p)
  15114. (user-error "Not at a property"))
  15115. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15116. (key (match-string 2))
  15117. (value (match-string 3))
  15118. (allowed (or (org-property-get-allowed-values (point) key)
  15119. (and (member value '("[ ]" "[-]" "[X]"))
  15120. '("[ ]" "[X]"))))
  15121. (heading (save-match-data (nth 4 (org-heading-components))))
  15122. nval)
  15123. (unless allowed
  15124. (user-error "Allowed values for this property have not been defined"))
  15125. (if previous (setq allowed (reverse allowed)))
  15126. (if (member value allowed)
  15127. (setq nval (car (cdr (member value allowed)))))
  15128. (setq nval (or nval (car allowed)))
  15129. (if (equal nval value)
  15130. (user-error "Only one allowed value for this property"))
  15131. (org-at-property-p)
  15132. (replace-match (concat " :" key ": " nval) t t)
  15133. (org-indent-line)
  15134. (beginning-of-line 1)
  15135. (skip-chars-forward " \t")
  15136. (when (equal prop org-effort-property)
  15137. (org-refresh-property
  15138. '((effort . identity)
  15139. (effort-minutes . org-duration-string-to-minutes))
  15140. nval)
  15141. (when (string= org-clock-current-task heading)
  15142. (setq org-clock-effort nval)
  15143. (org-clock-update-mode-line)))
  15144. (run-hook-with-args 'org-property-changed-functions key nval)))
  15145. (defun org-find-olp (path &optional this-buffer)
  15146. "Return a marker pointing to the entry at outline path OLP.
  15147. If anything goes wrong, throw an error.
  15148. You can wrap this call to catch the error like this:
  15149. (condition-case msg
  15150. (org-mobile-locate-entry (match-string 4))
  15151. (error (nth 1 msg)))
  15152. The return value will then be either a string with the error message,
  15153. or a marker if everything is OK.
  15154. If THIS-BUFFER is set, the outline path does not contain a file,
  15155. only headings."
  15156. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15157. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15158. (level 1)
  15159. (lmin 1)
  15160. (lmax 1)
  15161. limit re end found pos heading cnt flevel)
  15162. (unless buffer (error "File not found :%s" file))
  15163. (with-current-buffer buffer
  15164. (save-excursion
  15165. (save-restriction
  15166. (widen)
  15167. (setq limit (point-max))
  15168. (goto-char (point-min))
  15169. (dolist (heading path)
  15170. (setq re (format org-complex-heading-regexp-format
  15171. (regexp-quote heading)))
  15172. (setq cnt 0 pos (point))
  15173. (while (re-search-forward re end t)
  15174. (setq level (- (match-end 1) (match-beginning 1)))
  15175. (if (and (>= level lmin) (<= level lmax))
  15176. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15177. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15178. lmax heading))
  15179. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15180. lmax heading))
  15181. (goto-char found)
  15182. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15183. (setq end (save-excursion (org-end-of-subtree t t))))
  15184. (when (org-at-heading-p)
  15185. (point-marker)))))))
  15186. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15187. "Find node HEADING in BUFFER.
  15188. Return a marker to the heading if it was found, or nil if not.
  15189. If POS-ONLY is set, return just the position instead of a marker.
  15190. The heading text must match exact, but it may have a TODO keyword,
  15191. a priority cookie and tags in the standard locations."
  15192. (with-current-buffer (or buffer (current-buffer))
  15193. (save-excursion
  15194. (save-restriction
  15195. (widen)
  15196. (goto-char (point-min))
  15197. (let (case-fold-search)
  15198. (if (re-search-forward
  15199. (format org-complex-heading-regexp-format
  15200. (regexp-quote heading)) nil t)
  15201. (if pos-only
  15202. (match-beginning 0)
  15203. (move-marker (make-marker) (match-beginning 0)))))))))
  15204. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15205. "Find Org node headline HEADING in all .org files in directory DIR.
  15206. When the target headline is found, return a marker to this location."
  15207. (let ((files (directory-files (or dir default-directory)
  15208. t "\\`[^.#].*\\.org\\'"))
  15209. visiting m buffer)
  15210. (catch 'found
  15211. (dolist (file files)
  15212. (message "trying %s" file)
  15213. (setq visiting (org-find-base-buffer-visiting file))
  15214. (setq buffer (or visiting (find-file-noselect file)))
  15215. (setq m (org-find-exact-headline-in-buffer
  15216. heading buffer))
  15217. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15218. (and m (throw 'found m))))))
  15219. (defun org-find-entry-with-id (ident)
  15220. "Locate the entry that contains the ID property with exact value IDENT.
  15221. IDENT can be a string, a symbol or a number, this function will search for
  15222. the string representation of it.
  15223. Return the position where this entry starts, or nil if there is no such entry."
  15224. (interactive "sID: ")
  15225. (let ((id (cond
  15226. ((stringp ident) ident)
  15227. ((symbolp ident) (symbol-name ident))
  15228. ((numberp ident) (number-to-string ident))
  15229. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15230. (org-with-wide-buffer (org-find-property "ID" id))))
  15231. ;;;; Timestamps
  15232. (defvar org-last-changed-timestamp nil)
  15233. (defvar org-last-inserted-timestamp nil
  15234. "The last time stamp inserted with `org-insert-time-stamp'.")
  15235. (defvar org-ts-what) ; dynamically scoped parameter
  15236. (defun org-time-stamp (arg &optional inactive)
  15237. "Prompt for a date/time and insert a time stamp.
  15238. If the user specifies a time like HH:MM or if this command is
  15239. called with at least one prefix argument, the time stamp contains
  15240. the date and the time. Otherwise, only the date is included.
  15241. All parts of a date not specified by the user are filled in from
  15242. the timestamp at point, if any, or the current date/time
  15243. otherwise.
  15244. If there is already a timestamp at the cursor, it is replaced.
  15245. With two universal prefix arguments, insert an active timestamp
  15246. with the current time without prompting the user.
  15247. When called from lisp, the timestamp is inactive if INACTIVE is
  15248. non-nil."
  15249. (interactive "P")
  15250. (let* ((ts (cond
  15251. ((org-at-date-range-p t)
  15252. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15253. ((org-at-timestamp-p t) (match-string 0))))
  15254. ;; Default time is either the timestamp at point or today.
  15255. ;; When entering a range, only the range start is considered.
  15256. (default-time (if (not ts) (current-time)
  15257. (apply #'encode-time (org-parse-time-string ts))))
  15258. (default-input (and ts (org-get-compact-tod ts)))
  15259. (repeater (and ts
  15260. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15261. (match-string 0 ts)))
  15262. org-time-was-given
  15263. org-end-time-was-given
  15264. (time
  15265. (and (if (equal arg '(16)) (current-time)
  15266. ;; Preserve `this-command' and `last-command'.
  15267. (let ((this-command this-command)
  15268. (last-command last-command))
  15269. (org-read-date
  15270. arg 'totime nil nil default-time default-input
  15271. inactive))))))
  15272. (cond
  15273. ((and ts
  15274. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15275. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15276. (insert "--")
  15277. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15278. (ts
  15279. ;; Make sure we're on a timestamp. When in the middle of a date
  15280. ;; range, move arbitrarily to range end.
  15281. (unless (org-at-timestamp-p t)
  15282. (skip-chars-forward "-")
  15283. (org-at-timestamp-p t))
  15284. (replace-match "")
  15285. (setq org-last-changed-timestamp
  15286. (org-insert-time-stamp
  15287. time (or org-time-was-given arg)
  15288. inactive nil nil (list org-end-time-was-given)))
  15289. (when repeater
  15290. (backward-char)
  15291. (insert " " repeater)
  15292. (setq org-last-changed-timestamp
  15293. (concat (substring org-last-inserted-timestamp 0 -1)
  15294. " " repeater ">")))
  15295. (message "Timestamp updated"))
  15296. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15297. (t (org-insert-time-stamp
  15298. time (or org-time-was-given arg) inactive nil nil
  15299. (list org-end-time-was-given))))))
  15300. ;; FIXME: can we use this for something else, like computing time differences?
  15301. (defun org-get-compact-tod (s)
  15302. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15303. (let* ((t1 (match-string 1 s))
  15304. (h1 (string-to-number (match-string 2 s)))
  15305. (m1 (string-to-number (match-string 3 s)))
  15306. (t2 (and (match-end 4) (match-string 5 s)))
  15307. (h2 (and t2 (string-to-number (match-string 6 s))))
  15308. (m2 (and t2 (string-to-number (match-string 7 s))))
  15309. dh dm)
  15310. (if (not t2)
  15311. t1
  15312. (setq dh (- h2 h1) dm (- m2 m1))
  15313. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15314. (concat t1 "+" (number-to-string dh)
  15315. (and (/= 0 dm) (format ":%02d" dm)))))))
  15316. (defun org-time-stamp-inactive (&optional arg)
  15317. "Insert an inactive time stamp.
  15318. An inactive time stamp is enclosed in square brackets instead of angle
  15319. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15320. does not link to the calendar and cannot be changed with the S-cursor keys.
  15321. So these are more for recording a certain time/date."
  15322. (interactive "P")
  15323. (org-time-stamp arg 'inactive))
  15324. (defvar org-date-ovl (make-overlay 1 1))
  15325. (overlay-put org-date-ovl 'face 'org-date-selected)
  15326. (org-detach-overlay org-date-ovl)
  15327. (defvar org-ans1) ; dynamically scoped parameter
  15328. (defvar org-ans2) ; dynamically scoped parameter
  15329. (defvar org-plain-time-of-day-regexp) ; defined below
  15330. (defvar org-overriding-default-time nil) ; dynamically scoped
  15331. (defvar org-read-date-overlay nil)
  15332. (defvar org-dcst nil) ; dynamically scoped
  15333. (defvar org-read-date-history nil)
  15334. (defvar org-read-date-final-answer nil)
  15335. (defvar org-read-date-analyze-futurep nil)
  15336. (defvar org-read-date-analyze-forced-year nil)
  15337. (defvar org-read-date-inactive)
  15338. (defvar org-read-date-minibuffer-local-map
  15339. (let* ((map (make-sparse-keymap)))
  15340. (set-keymap-parent map minibuffer-local-map)
  15341. (org-defkey map (kbd ".")
  15342. (lambda () (interactive)
  15343. ;; Are we at the beginning of the prompt?
  15344. (if (org-looking-back "^[^:]+: "
  15345. (let ((inhibit-field-text-motion t))
  15346. (line-beginning-position)))
  15347. (org-eval-in-calendar '(calendar-goto-today))
  15348. (insert "."))))
  15349. (org-defkey map (kbd "C-.")
  15350. (lambda () (interactive)
  15351. (org-eval-in-calendar '(calendar-goto-today))))
  15352. (org-defkey map [(meta shift left)]
  15353. (lambda () (interactive)
  15354. (org-eval-in-calendar '(calendar-backward-month 1))))
  15355. (org-defkey map [(meta shift right)]
  15356. (lambda () (interactive)
  15357. (org-eval-in-calendar '(calendar-forward-month 1))))
  15358. (org-defkey map [(meta shift up)]
  15359. (lambda () (interactive)
  15360. (org-eval-in-calendar '(calendar-backward-year 1))))
  15361. (org-defkey map [(meta shift down)]
  15362. (lambda () (interactive)
  15363. (org-eval-in-calendar '(calendar-forward-year 1))))
  15364. (org-defkey map [?\e (shift left)]
  15365. (lambda () (interactive)
  15366. (org-eval-in-calendar '(calendar-backward-month 1))))
  15367. (org-defkey map [?\e (shift right)]
  15368. (lambda () (interactive)
  15369. (org-eval-in-calendar '(calendar-forward-month 1))))
  15370. (org-defkey map [?\e (shift up)]
  15371. (lambda () (interactive)
  15372. (org-eval-in-calendar '(calendar-backward-year 1))))
  15373. (org-defkey map [?\e (shift down)]
  15374. (lambda () (interactive)
  15375. (org-eval-in-calendar '(calendar-forward-year 1))))
  15376. (org-defkey map [(shift up)]
  15377. (lambda () (interactive)
  15378. (org-eval-in-calendar '(calendar-backward-week 1))))
  15379. (org-defkey map [(shift down)]
  15380. (lambda () (interactive)
  15381. (org-eval-in-calendar '(calendar-forward-week 1))))
  15382. (org-defkey map [(shift left)]
  15383. (lambda () (interactive)
  15384. (org-eval-in-calendar '(calendar-backward-day 1))))
  15385. (org-defkey map [(shift right)]
  15386. (lambda () (interactive)
  15387. (org-eval-in-calendar '(calendar-forward-day 1))))
  15388. (org-defkey map "!"
  15389. (lambda () (interactive)
  15390. (org-eval-in-calendar '(diary-view-entries))
  15391. (message "")))
  15392. (org-defkey map ">"
  15393. (lambda () (interactive)
  15394. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15395. (org-defkey map "<"
  15396. (lambda () (interactive)
  15397. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15398. (org-defkey map "\C-v"
  15399. (lambda () (interactive)
  15400. (org-eval-in-calendar
  15401. '(calendar-scroll-left-three-months 1))))
  15402. (org-defkey map "\M-v"
  15403. (lambda () (interactive)
  15404. (org-eval-in-calendar
  15405. '(calendar-scroll-right-three-months 1))))
  15406. map)
  15407. "Keymap for minibuffer commands when using `org-read-date'.")
  15408. (defvar org-def)
  15409. (defvar org-defdecode)
  15410. (defvar org-with-time)
  15411. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15412. default-time default-input inactive)
  15413. "Read a date, possibly a time, and make things smooth for the user.
  15414. The prompt will suggest to enter an ISO date, but you can also enter anything
  15415. which will at least partially be understood by `parse-time-string'.
  15416. Unrecognized parts of the date will default to the current day, month, year,
  15417. hour and minute. If this command is called to replace a timestamp at point,
  15418. or to enter the second timestamp of a range, the default time is taken
  15419. from the existing stamp. Furthermore, the command prefers the future,
  15420. so if you are giving a date where the year is not given, and the day-month
  15421. combination is already past in the current year, it will assume you
  15422. mean next year. For details, see the manual. A few examples:
  15423. 3-2-5 --> 2003-02-05
  15424. feb 15 --> currentyear-02-15
  15425. 2/15 --> currentyear-02-15
  15426. sep 12 9 --> 2009-09-12
  15427. 12:45 --> today 12:45
  15428. 22 sept 0:34 --> currentyear-09-22 0:34
  15429. 12 --> currentyear-currentmonth-12
  15430. Fri --> nearest Friday after today
  15431. -Tue --> last Tuesday
  15432. etc.
  15433. Furthermore you can specify a relative date by giving, as the *first* thing
  15434. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15435. change in days weeks, months, years.
  15436. With a single plus or minus, the date is relative to today. With a double
  15437. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15438. +4d --> four days from today
  15439. +4 --> same as above
  15440. +2w --> two weeks from today
  15441. ++5 --> five days from default date
  15442. The function understands only English month and weekday abbreviations.
  15443. While prompting, a calendar is popped up - you can also select the
  15444. date with the mouse (button 1). The calendar shows a period of three
  15445. months. To scroll it to other months, use the keys `>' and `<'.
  15446. If you don't like the calendar, turn it off with
  15447. (setq org-read-date-popup-calendar nil)
  15448. With optional argument TO-TIME, the date will immediately be converted
  15449. to an internal time.
  15450. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15451. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15452. still enter a time, and this function will inform the calling routine
  15453. about this change. The calling routine may then choose to change the
  15454. format used to insert the time stamp into the buffer to include the time.
  15455. With optional argument FROM-STRING, read from this string instead from
  15456. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15457. the time/date that is used for everything that is not specified by the
  15458. user."
  15459. (require 'parse-time)
  15460. (let* ((org-time-stamp-rounding-minutes
  15461. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15462. (org-dcst org-display-custom-times)
  15463. (ct (org-current-time))
  15464. (org-def (or org-overriding-default-time default-time ct))
  15465. (org-defdecode (decode-time org-def))
  15466. (dummy (progn
  15467. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15468. (setcar (nthcdr 2 org-defdecode) -1)
  15469. (setcar (nthcdr 1 org-defdecode) 59)
  15470. (setq org-def (apply 'encode-time org-defdecode)
  15471. org-defdecode (decode-time org-def)))))
  15472. (cur-frame (selected-frame))
  15473. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15474. (calendar-frame-setup nil)
  15475. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15476. (calendar-move-hook nil)
  15477. (calendar-view-diary-initially-flag nil)
  15478. (calendar-view-holidays-initially-flag nil)
  15479. (timestr (format-time-string
  15480. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15481. (prompt (concat (if prompt (concat prompt " ") "")
  15482. (format "Date+time [%s]: " timestr)))
  15483. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15484. (cond
  15485. (from-string (setq ans from-string))
  15486. (org-read-date-popup-calendar
  15487. (save-excursion
  15488. (save-window-excursion
  15489. (calendar)
  15490. (when (eq calendar-setup 'calendar-only)
  15491. (setq cal-frame
  15492. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15493. (select-frame cal-frame))
  15494. (org-eval-in-calendar '(setq cursor-type nil) t)
  15495. (unwind-protect
  15496. (progn
  15497. (calendar-forward-day (- (time-to-days org-def)
  15498. (calendar-absolute-from-gregorian
  15499. (calendar-current-date))))
  15500. (org-eval-in-calendar nil t)
  15501. (let* ((old-map (current-local-map))
  15502. (map (copy-keymap calendar-mode-map))
  15503. (minibuffer-local-map
  15504. (copy-keymap org-read-date-minibuffer-local-map)))
  15505. (org-defkey map (kbd "RET") 'org-calendar-select)
  15506. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15507. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15508. (unwind-protect
  15509. (progn
  15510. (use-local-map map)
  15511. (setq org-read-date-inactive inactive)
  15512. (add-hook 'post-command-hook 'org-read-date-display)
  15513. (setq org-ans0 (read-string prompt default-input
  15514. 'org-read-date-history nil))
  15515. ;; org-ans0: from prompt
  15516. ;; org-ans1: from mouse click
  15517. ;; org-ans2: from calendar motion
  15518. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15519. (remove-hook 'post-command-hook 'org-read-date-display)
  15520. (use-local-map old-map)
  15521. (when org-read-date-overlay
  15522. (delete-overlay org-read-date-overlay)
  15523. (setq org-read-date-overlay nil)))))
  15524. (bury-buffer "*Calendar*")
  15525. (when cal-frame
  15526. (delete-frame cal-frame)
  15527. (select-frame-set-input-focus cur-frame))))))
  15528. (t ; Naked prompt only
  15529. (unwind-protect
  15530. (setq ans (read-string prompt default-input
  15531. 'org-read-date-history timestr))
  15532. (when org-read-date-overlay
  15533. (delete-overlay org-read-date-overlay)
  15534. (setq org-read-date-overlay nil)))))
  15535. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15536. (when org-read-date-analyze-forced-year
  15537. (message "Year was forced into %s"
  15538. (if org-read-date-force-compatible-dates
  15539. "compatible range (1970-2037)"
  15540. "range representable on this machine"))
  15541. (ding))
  15542. ;; One round trip to get rid of 34th of August and stuff like that....
  15543. (setq final (decode-time (apply 'encode-time final)))
  15544. (setq org-read-date-final-answer ans)
  15545. (if to-time
  15546. (apply 'encode-time final)
  15547. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15548. (format "%04d-%02d-%02d %02d:%02d"
  15549. (nth 5 final) (nth 4 final) (nth 3 final)
  15550. (nth 2 final) (nth 1 final))
  15551. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15552. (defun org-read-date-display ()
  15553. "Display the current date prompt interpretation in the minibuffer."
  15554. (when org-read-date-display-live
  15555. (when org-read-date-overlay
  15556. (delete-overlay org-read-date-overlay))
  15557. (when (minibufferp (current-buffer))
  15558. (save-excursion
  15559. (end-of-line 1)
  15560. (while (not (equal (buffer-substring
  15561. (max (point-min) (- (point) 4)) (point))
  15562. " "))
  15563. (insert " ")))
  15564. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15565. " " (or org-ans1 org-ans2)))
  15566. (org-end-time-was-given nil)
  15567. (f (org-read-date-analyze ans org-def org-defdecode))
  15568. (fmts (if org-dcst
  15569. org-time-stamp-custom-formats
  15570. org-time-stamp-formats))
  15571. (fmt (if (or org-with-time
  15572. (and (boundp 'org-time-was-given) org-time-was-given))
  15573. (cdr fmts)
  15574. (car fmts)))
  15575. (txt (format-time-string fmt (apply 'encode-time f)))
  15576. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15577. (txt (concat "=> " txt)))
  15578. (when (and org-end-time-was-given
  15579. (string-match org-plain-time-of-day-regexp txt))
  15580. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15581. org-end-time-was-given
  15582. (substring txt (match-end 0)))))
  15583. (when org-read-date-analyze-futurep
  15584. (setq txt (concat txt " (=>F)")))
  15585. (setq org-read-date-overlay
  15586. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15587. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15588. (defun org-read-date-analyze (ans org-def org-defdecode)
  15589. "Analyze the combined answer of the date prompt."
  15590. ;; FIXME: cleanup and comment
  15591. ;; Pass `current-time' result to `decode-time' (instead of calling
  15592. ;; without arguments) so that only `current-time' has to be
  15593. ;; overriden in tests.
  15594. (let ((nowdecode (decode-time (current-time)))
  15595. delta deltan deltaw deltadef year month day
  15596. hour minute second wday pm h2 m2 tl wday1
  15597. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15598. (setq org-read-date-analyze-futurep nil
  15599. org-read-date-analyze-forced-year nil)
  15600. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15601. (setq ans "+0"))
  15602. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15603. (setq ans (replace-match "" t t ans)
  15604. deltan (car delta)
  15605. deltaw (nth 1 delta)
  15606. deltadef (nth 2 delta)))
  15607. ;; Check if there is an iso week date in there. If yes, store the
  15608. ;; info and postpone interpreting it until the rest of the parsing
  15609. ;; is done.
  15610. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15611. (setq iso-year (if (match-end 1)
  15612. (org-small-year-to-year
  15613. (string-to-number (match-string 1 ans))))
  15614. iso-weekday (if (match-end 3)
  15615. (string-to-number (match-string 3 ans)))
  15616. iso-week (string-to-number (match-string 2 ans)))
  15617. (setq ans (replace-match "" t t ans)))
  15618. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15619. (when (string-match
  15620. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15621. (setq year (if (match-end 2)
  15622. (string-to-number (match-string 2 ans))
  15623. (progn (setq kill-year t)
  15624. (string-to-number (format-time-string "%Y"))))
  15625. month (string-to-number (match-string 3 ans))
  15626. day (string-to-number (match-string 4 ans)))
  15627. (if (< year 100) (setq year (+ 2000 year)))
  15628. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15629. t nil ans)))
  15630. ;; Help matching dotted european dates
  15631. (when (string-match
  15632. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15633. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15634. (setq kill-year t)
  15635. (string-to-number (format-time-string "%Y")))
  15636. day (string-to-number (match-string 1 ans))
  15637. month (string-to-number (match-string 2 ans))
  15638. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15639. t nil ans)))
  15640. ;; Help matching american dates, like 5/30 or 5/30/7
  15641. (when (string-match
  15642. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15643. (setq year (if (match-end 4)
  15644. (string-to-number (match-string 4 ans))
  15645. (progn (setq kill-year t)
  15646. (string-to-number (format-time-string "%Y"))))
  15647. month (string-to-number (match-string 1 ans))
  15648. day (string-to-number (match-string 2 ans)))
  15649. (if (< year 100) (setq year (+ 2000 year)))
  15650. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15651. t nil ans)))
  15652. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15653. ;; If there is a time with am/pm, and *no* time without it, we convert
  15654. ;; so that matching will be successful.
  15655. (loop for i from 1 to 2 do ; twice, for end time as well
  15656. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15657. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15658. (setq hour (string-to-number (match-string 1 ans))
  15659. minute (if (match-end 3)
  15660. (string-to-number (match-string 3 ans))
  15661. 0)
  15662. pm (equal ?p
  15663. (string-to-char (downcase (match-string 4 ans)))))
  15664. (if (and (= hour 12) (not pm))
  15665. (setq hour 0)
  15666. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15667. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15668. t t ans))))
  15669. ;; Check if a time range is given as a duration
  15670. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15671. (setq hour (string-to-number (match-string 1 ans))
  15672. h2 (+ hour (string-to-number (match-string 3 ans)))
  15673. minute (string-to-number (match-string 2 ans))
  15674. m2 (+ minute (if (match-end 5) (string-to-number
  15675. (match-string 5 ans))0)))
  15676. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15677. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15678. t t ans)))
  15679. ;; Check if there is a time range
  15680. (when (boundp 'org-end-time-was-given)
  15681. (setq org-time-was-given nil)
  15682. (when (and (string-match org-plain-time-of-day-regexp ans)
  15683. (match-end 8))
  15684. (setq org-end-time-was-given (match-string 8 ans))
  15685. (setq ans (concat (substring ans 0 (match-beginning 7))
  15686. (substring ans (match-end 7))))))
  15687. (setq tl (parse-time-string ans)
  15688. day (or (nth 3 tl) (nth 3 org-defdecode))
  15689. month
  15690. (cond ((nth 4 tl))
  15691. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15692. ;; Day was specified. Make sure DAY+MONTH
  15693. ;; combination happens in the future.
  15694. ((nth 3 tl)
  15695. (setq futurep t)
  15696. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15697. (nth 4 nowdecode)))
  15698. (t (nth 4 org-defdecode)))
  15699. year
  15700. (cond ((and (not kill-year) (nth 5 tl)))
  15701. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15702. ;; Month was guessed in the future and is at least
  15703. ;; equal to NOWDECODE's. Fix year accordingly.
  15704. (futurep
  15705. (if (or (> month (nth 4 nowdecode))
  15706. (>= day (nth 3 nowdecode)))
  15707. (nth 5 nowdecode)
  15708. (1+ (nth 5 nowdecode))))
  15709. ;; Month was specified. Make sure MONTH+YEAR
  15710. ;; combination happens in the future.
  15711. ((nth 4 tl)
  15712. (setq futurep t)
  15713. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15714. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15715. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15716. (t (nth 5 nowdecode))))
  15717. (t (nth 5 org-defdecode)))
  15718. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15719. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15720. second (or (nth 0 tl) 0)
  15721. wday (nth 6 tl))
  15722. (when (and (eq org-read-date-prefer-future 'time)
  15723. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15724. (equal day (nth 3 nowdecode))
  15725. (equal month (nth 4 nowdecode))
  15726. (equal year (nth 5 nowdecode))
  15727. (nth 2 tl)
  15728. (or (< (nth 2 tl) (nth 2 nowdecode))
  15729. (and (= (nth 2 tl) (nth 2 nowdecode))
  15730. (nth 1 tl)
  15731. (< (nth 1 tl) (nth 1 nowdecode)))))
  15732. (setq day (1+ day)
  15733. futurep t))
  15734. ;; Special date definitions below
  15735. (cond
  15736. (iso-week
  15737. ;; There was an iso week
  15738. (require 'cal-iso)
  15739. (setq futurep nil)
  15740. (setq year (or iso-year year)
  15741. day (or iso-weekday wday 1)
  15742. wday nil ; to make sure that the trigger below does not match
  15743. iso-date (calendar-gregorian-from-absolute
  15744. (calendar-iso-to-absolute
  15745. (list iso-week day year))))
  15746. ; FIXME: Should we also push ISO weeks into the future?
  15747. ; (when (and org-read-date-prefer-future
  15748. ; (not iso-year)
  15749. ; (< (calendar-absolute-from-gregorian iso-date)
  15750. ; (time-to-days (current-time))))
  15751. ; (setq year (1+ year)
  15752. ; iso-date (calendar-gregorian-from-absolute
  15753. ; (calendar-iso-to-absolute
  15754. ; (list iso-week day year)))))
  15755. (setq month (car iso-date)
  15756. year (nth 2 iso-date)
  15757. day (nth 1 iso-date)))
  15758. (deltan
  15759. (setq futurep nil)
  15760. (unless deltadef
  15761. ;; Pass `current-time' result to `decode-time' (instead of
  15762. ;; calling without arguments) so that only `current-time' has
  15763. ;; to be overriden in tests.
  15764. (let ((now (decode-time (current-time))))
  15765. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15766. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15767. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15768. ((equal deltaw "m") (setq month (+ month deltan)))
  15769. ((equal deltaw "y") (setq year (+ year deltan)))))
  15770. ((and wday (not (nth 3 tl)))
  15771. ;; Weekday was given, but no day, so pick that day in the week
  15772. ;; on or after the derived date.
  15773. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15774. (unless (equal wday wday1)
  15775. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15776. (if (and (boundp 'org-time-was-given)
  15777. (nth 2 tl))
  15778. (setq org-time-was-given t))
  15779. (if (< year 100) (setq year (+ 2000 year)))
  15780. ;; Check of the date is representable
  15781. (if org-read-date-force-compatible-dates
  15782. (progn
  15783. (if (< year 1970)
  15784. (setq year 1970 org-read-date-analyze-forced-year t))
  15785. (if (> year 2037)
  15786. (setq year 2037 org-read-date-analyze-forced-year t)))
  15787. (condition-case nil
  15788. (ignore (encode-time second minute hour day month year))
  15789. (error
  15790. (setq year (nth 5 org-defdecode))
  15791. (setq org-read-date-analyze-forced-year t))))
  15792. (setq org-read-date-analyze-futurep futurep)
  15793. (list second minute hour day month year)))
  15794. (defvar parse-time-weekdays)
  15795. (defun org-read-date-get-relative (s today default)
  15796. "Check string S for special relative date string.
  15797. TODAY and DEFAULT are internal times, for today and for a default.
  15798. Return shift list (N what def-flag)
  15799. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15800. N is the number of WHATs to shift.
  15801. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15802. the DEFAULT date rather than TODAY."
  15803. (require 'parse-time)
  15804. (when (and
  15805. (string-match
  15806. (concat
  15807. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15808. "\\([0-9]+\\)?"
  15809. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15810. "\\([ \t]\\|$\\)") s)
  15811. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15812. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15813. (string-to-char (substring (match-string 1 s) -1))
  15814. ?+))
  15815. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15816. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15817. (what (if (match-end 3) (match-string 3 s) "d"))
  15818. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15819. (date (if rel default today))
  15820. (wday (nth 6 (decode-time date)))
  15821. delta)
  15822. (if wday1
  15823. (progn
  15824. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15825. (if (= delta 0) (setq delta 7))
  15826. (if (= dir ?-)
  15827. (progn
  15828. (setq delta (- delta 7))
  15829. (if (= delta 0) (setq delta -7))))
  15830. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15831. (list delta "d" rel))
  15832. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15833. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15834. "Turn a user-specified date into the internal representation.
  15835. The internal representation needed by the calendar is (month day year).
  15836. This is a wrapper to handle the brain-dead convention in calendar that
  15837. user function argument order change dependent on argument order."
  15838. (if (boundp 'calendar-date-style)
  15839. (cond
  15840. ((eq calendar-date-style 'american)
  15841. (list arg1 arg2 arg3))
  15842. ((eq calendar-date-style 'european)
  15843. (list arg2 arg1 arg3))
  15844. ((eq calendar-date-style 'iso)
  15845. (list arg2 arg3 arg1)))
  15846. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15847. (if (org-bound-and-true-p european-calendar-style)
  15848. (list arg2 arg1 arg3)
  15849. (list arg1 arg2 arg3)))))
  15850. (defun org-eval-in-calendar (form &optional keepdate)
  15851. "Eval FORM in the calendar window and return to current window.
  15852. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15853. (let ((sf (selected-frame))
  15854. (sw (selected-window)))
  15855. (select-window (get-buffer-window "*Calendar*" t))
  15856. (eval form)
  15857. (when (and (not keepdate) (calendar-cursor-to-date))
  15858. (let* ((date (calendar-cursor-to-date))
  15859. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15860. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15861. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15862. (select-window sw)
  15863. (org-select-frame-set-input-focus sf)))
  15864. (defun org-calendar-select ()
  15865. "Return to `org-read-date' with the date currently selected.
  15866. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15867. (interactive)
  15868. (when (calendar-cursor-to-date)
  15869. (let* ((date (calendar-cursor-to-date))
  15870. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15871. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15872. (if (active-minibuffer-window) (exit-minibuffer))))
  15873. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15874. "Insert a date stamp for the date given by the internal TIME.
  15875. See `format-time-string' for the format of TIME.
  15876. WITH-HM means use the stamp format that includes the time of the day.
  15877. INACTIVE means use square brackets instead of angular ones, so that the
  15878. stamp will not contribute to the agenda.
  15879. PRE and POST are optional strings to be inserted before and after the
  15880. stamp.
  15881. The command returns the inserted time stamp."
  15882. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15883. stamp)
  15884. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15885. (insert-before-markers (or pre ""))
  15886. (when (listp extra)
  15887. (setq extra (car extra))
  15888. (if (and (stringp extra)
  15889. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15890. (setq extra (format "-%02d:%02d"
  15891. (string-to-number (match-string 1 extra))
  15892. (string-to-number (match-string 2 extra))))
  15893. (setq extra nil)))
  15894. (when extra
  15895. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15896. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15897. (insert-before-markers (or post ""))
  15898. (setq org-last-inserted-timestamp stamp)))
  15899. (defun org-toggle-time-stamp-overlays ()
  15900. "Toggle the use of custom time stamp formats."
  15901. (interactive)
  15902. (setq org-display-custom-times (not org-display-custom-times))
  15903. (unless org-display-custom-times
  15904. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15905. (while (setq p (next-single-property-change p 'display))
  15906. (if (and (get-text-property p 'display)
  15907. (eq (get-text-property p 'face) 'org-date))
  15908. (remove-text-properties
  15909. p (setq p (next-single-property-change p 'display))
  15910. '(display t))))
  15911. (set-buffer-modified-p bmp)))
  15912. (if (featurep 'xemacs)
  15913. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15914. (org-restart-font-lock)
  15915. (setq org-table-may-need-update t)
  15916. (if org-display-custom-times
  15917. (message "Time stamps are overlaid with custom format")
  15918. (message "Time stamp overlays removed")))
  15919. (defun org-display-custom-time (beg end)
  15920. "Overlay modified time stamp format over timestamp between BEG and END."
  15921. (let* ((ts (buffer-substring beg end))
  15922. t1 w1 with-hm tf time str w2 (off 0))
  15923. (save-match-data
  15924. (setq t1 (org-parse-time-string ts t))
  15925. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15926. (setq off (- (match-end 0) (match-beginning 0)))))
  15927. (setq end (- end off))
  15928. (setq w1 (- end beg)
  15929. with-hm (and (nth 1 t1) (nth 2 t1))
  15930. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15931. time (org-fix-decoded-time t1)
  15932. str (org-add-props
  15933. (format-time-string
  15934. (substring tf 1 -1) (apply 'encode-time time))
  15935. nil 'mouse-face 'highlight)
  15936. w2 (length str))
  15937. (if (not (= w2 w1))
  15938. (add-text-properties (1+ beg) (+ 2 beg)
  15939. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15940. (if (featurep 'xemacs)
  15941. (progn
  15942. (put-text-property beg end 'invisible t)
  15943. (put-text-property beg end 'end-glyph (make-glyph str)))
  15944. (put-text-property beg end 'display str))))
  15945. (defun org-fix-decoded-time (time)
  15946. "Set 0 instead of nil for the first 6 elements of time.
  15947. Don't touch the rest."
  15948. (let ((n 0))
  15949. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15950. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15951. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15952. "Difference between TIMESTAMP-STRING and now in days.
  15953. If SECONDS is non-nil, return the difference in seconds."
  15954. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15955. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15956. (funcall fdiff (current-time)))))
  15957. (defun org-deadline-close (timestamp-string &optional ndays)
  15958. "Is the time in TIMESTAMP-STRING close to the current date?"
  15959. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15960. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15961. (not (org-entry-is-done-p))))
  15962. (defun org-get-wdays (ts &optional delay zero-delay)
  15963. "Get the deadline lead time appropriate for timestring TS.
  15964. When DELAY is non-nil, get the delay time for scheduled items
  15965. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15966. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15967. don't try to find the delay cookie in the scheduled timestamp."
  15968. (let ((tv (if delay org-scheduled-delay-days
  15969. org-deadline-warning-days)))
  15970. (cond
  15971. ((or (and delay (< tv 0))
  15972. (and delay zero-delay (<= tv 0))
  15973. (and (not delay) (<= tv 0)))
  15974. ;; Enforce this value no matter what
  15975. (- tv))
  15976. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15977. ;; lead time is specified.
  15978. (floor (* (string-to-number (match-string 1 ts))
  15979. (cdr (assoc (match-string 2 ts)
  15980. '(("d" . 1) ("w" . 7)
  15981. ("m" . 30.4) ("y" . 365.25)
  15982. ("h" . 0.041667)))))))
  15983. ;; go for the default.
  15984. (t tv))))
  15985. (defun org-calendar-select-mouse (ev)
  15986. "Return to `org-read-date' with the date currently selected.
  15987. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15988. (interactive "e")
  15989. (mouse-set-point ev)
  15990. (when (calendar-cursor-to-date)
  15991. (let* ((date (calendar-cursor-to-date))
  15992. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15993. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15994. (if (active-minibuffer-window) (exit-minibuffer))))
  15995. (defun org-check-deadlines (ndays)
  15996. "Check if there are any deadlines due or past due.
  15997. A deadline is considered due if it happens within `org-deadline-warning-days'
  15998. days from today's date. If the deadline appears in an entry marked DONE,
  15999. it is not shown. The prefix arg NDAYS can be used to test that many
  16000. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16001. (interactive "P")
  16002. (let* ((org-warn-days
  16003. (cond
  16004. ((equal ndays '(4)) 100000)
  16005. (ndays (prefix-numeric-value ndays))
  16006. (t (abs org-deadline-warning-days))))
  16007. (case-fold-search nil)
  16008. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16009. (callback
  16010. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16011. (message "%d deadlines past-due or due within %d days"
  16012. (org-occur regexp nil callback)
  16013. org-warn-days)))
  16014. (defsubst org-re-timestamp (type)
  16015. "Return a regexp for timestamp TYPE.
  16016. Allowed values for TYPE are:
  16017. all: all timestamps
  16018. active: only active timestamps (<...>)
  16019. inactive: only inactive timestamps ([...])
  16020. scheduled: only scheduled timestamps
  16021. deadline: only deadline timestamps
  16022. closed: only closed time-stamps
  16023. When TYPE is nil, fall back on returning a regexp that matches
  16024. both scheduled and deadline timestamps."
  16025. (case type
  16026. (all org-ts-regexp-both)
  16027. (active org-ts-regexp)
  16028. (inactive org-ts-regexp-inactive)
  16029. (scheduled org-scheduled-time-regexp)
  16030. (deadline org-deadline-time-regexp)
  16031. (closed org-closed-time-regexp)
  16032. (otherwise
  16033. (concat "\\<"
  16034. (regexp-opt (list org-deadline-string org-scheduled-string))
  16035. " *<\\([^>]+\\)>"))))
  16036. (defun org-check-before-date (date)
  16037. "Check if there are deadlines or scheduled entries before DATE."
  16038. (interactive (list (org-read-date)))
  16039. (let ((case-fold-search nil)
  16040. (regexp (org-re-timestamp org-ts-type))
  16041. (callback
  16042. `(lambda ()
  16043. (let ((match (match-string 1)))
  16044. (and ,(if (memq org-ts-type '(active inactive all))
  16045. '(eq (org-element-type (org-element-context)) 'timestamp)
  16046. '(org-at-planning-p))
  16047. (time-less-p
  16048. (org-time-string-to-time match)
  16049. (org-time-string-to-time date)))))))
  16050. (message "%d entries before %s"
  16051. (org-occur regexp nil callback) date)))
  16052. (defun org-check-after-date (date)
  16053. "Check if there are deadlines or scheduled entries after DATE."
  16054. (interactive (list (org-read-date)))
  16055. (let ((case-fold-search nil)
  16056. (regexp (org-re-timestamp org-ts-type))
  16057. (callback
  16058. `(lambda ()
  16059. (let ((match (match-string 1)))
  16060. (and ,(if (memq org-ts-type '(active inactive all))
  16061. '(eq (org-element-type (org-element-context)) 'timestamp)
  16062. '(org-at-planning-p))
  16063. (not (time-less-p
  16064. (org-time-string-to-time match)
  16065. (org-time-string-to-time date))))))))
  16066. (message "%d entries after %s"
  16067. (org-occur regexp nil callback) date)))
  16068. (defun org-check-dates-range (start-date end-date)
  16069. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16070. (interactive (list (org-read-date nil nil nil "Range starts")
  16071. (org-read-date nil nil nil "Range end")))
  16072. (let ((case-fold-search nil)
  16073. (regexp (org-re-timestamp org-ts-type))
  16074. (callback
  16075. `(lambda ()
  16076. (let ((match (match-string 1)))
  16077. (and
  16078. ,(if (memq org-ts-type '(active inactive all))
  16079. '(eq (org-element-type (org-element-context)) 'timestamp)
  16080. '(org-at-planning-p))
  16081. (not (time-less-p
  16082. (org-time-string-to-time match)
  16083. (org-time-string-to-time start-date)))
  16084. (time-less-p
  16085. (org-time-string-to-time match)
  16086. (org-time-string-to-time end-date)))))))
  16087. (message "%d entries between %s and %s"
  16088. (org-occur regexp nil callback) start-date end-date)))
  16089. (defun org-evaluate-time-range (&optional to-buffer)
  16090. "Evaluate a time range by computing the difference between start and end.
  16091. Normally the result is just printed in the echo area, but with prefix arg
  16092. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16093. If the time range is actually in a table, the result is inserted into the
  16094. next column.
  16095. For time difference computation, a year is assumed to be exactly 365
  16096. days in order to avoid rounding problems."
  16097. (interactive "P")
  16098. (or
  16099. (org-clock-update-time-maybe)
  16100. (save-excursion
  16101. (unless (org-at-date-range-p t)
  16102. (goto-char (point-at-bol))
  16103. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16104. (if (not (org-at-date-range-p t))
  16105. (user-error "Not at a time-stamp range, and none found in current line")))
  16106. (let* ((ts1 (match-string 1))
  16107. (ts2 (match-string 2))
  16108. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16109. (match-end (match-end 0))
  16110. (time1 (org-time-string-to-time ts1))
  16111. (time2 (org-time-string-to-time ts2))
  16112. (t1 (org-float-time time1))
  16113. (t2 (org-float-time time2))
  16114. (diff (abs (- t2 t1)))
  16115. (negative (< (- t2 t1) 0))
  16116. ;; (ys (floor (* 365 24 60 60)))
  16117. (ds (* 24 60 60))
  16118. (hs (* 60 60))
  16119. (fy "%dy %dd %02d:%02d")
  16120. (fy1 "%dy %dd")
  16121. (fd "%dd %02d:%02d")
  16122. (fd1 "%dd")
  16123. (fh "%02d:%02d")
  16124. y d h m align)
  16125. (if havetime
  16126. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16127. y 0
  16128. d (floor (/ diff ds)) diff (mod diff ds)
  16129. h (floor (/ diff hs)) diff (mod diff hs)
  16130. m (floor (/ diff 60)))
  16131. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16132. y 0
  16133. d (floor (+ (/ diff ds) 0.5))
  16134. h 0 m 0))
  16135. (if (not to-buffer)
  16136. (message "%s" (org-make-tdiff-string y d h m))
  16137. (if (org-at-table-p)
  16138. (progn
  16139. (goto-char match-end)
  16140. (setq align t)
  16141. (and (looking-at " *|") (goto-char (match-end 0))))
  16142. (goto-char match-end))
  16143. (if (looking-at
  16144. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16145. (replace-match ""))
  16146. (if negative (insert " -"))
  16147. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16148. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16149. (insert " " (format fh h m))))
  16150. (if align (org-table-align))
  16151. (message "Time difference inserted")))))
  16152. (defun org-make-tdiff-string (y d h m)
  16153. (let ((fmt "")
  16154. (l nil))
  16155. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16156. l (push y l)))
  16157. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16158. l (push d l)))
  16159. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16160. l (push h l)))
  16161. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16162. l (push m l)))
  16163. (apply 'format fmt (nreverse l))))
  16164. (defun org-time-string-to-time (s &optional buffer pos)
  16165. "Convert a timestamp string into internal time."
  16166. (condition-case errdata
  16167. (apply 'encode-time (org-parse-time-string s))
  16168. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16169. s (if (not (and buffer pos))
  16170. ""
  16171. (format-message " at %d in buffer `%s'" pos buffer))
  16172. (cdr errdata)))))
  16173. (defun org-time-string-to-seconds (s)
  16174. "Convert a timestamp string to a number of seconds."
  16175. (org-float-time (org-time-string-to-time s)))
  16176. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16177. "Convert a time stamp to an absolute day number.
  16178. If there is a specifier for a cyclic time stamp, get the closest
  16179. date to DAYNR.
  16180. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16181. The variable `date' is bound by the calendar when this is called."
  16182. (cond
  16183. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16184. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16185. daynr
  16186. (+ daynr 1000)))
  16187. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16188. (> (string-to-number (match-string 1 s)) 0))
  16189. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16190. (time-to-days (current-time))) (match-string 0 s)
  16191. prefer show-all))
  16192. (t (time-to-days
  16193. (condition-case errdata
  16194. (apply 'encode-time (org-parse-time-string s))
  16195. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16196. s (if (not (and buffer pos))
  16197. ""
  16198. (format-message " at %d in buffer `%s'" pos buffer))
  16199. (cdr errdata))))))))
  16200. (defun org-days-to-iso-week (days)
  16201. "Return the iso week number."
  16202. (require 'cal-iso)
  16203. (car (calendar-iso-from-absolute days)))
  16204. (defun org-small-year-to-year (year)
  16205. "Convert 2-digit years into 4-digit years.
  16206. YEAR is expanded into one of the 30 next years, if possible, or
  16207. into a past one. Any year larger than 99 is returned unchanged."
  16208. (if (>= year 100) year
  16209. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16210. (century (/ current 100))
  16211. (offset (- year (% current 100))))
  16212. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16213. ((> offset -70) (+ (* century 100) year))
  16214. (t (+ (* (1+ century) 100) year))))))
  16215. (defun org-time-from-absolute (d)
  16216. "Return the time corresponding to date D.
  16217. D may be an absolute day number, or a calendar-type list (month day year)."
  16218. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16219. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16220. (defun org-calendar-holiday ()
  16221. "List of holidays, for Diary display in Org-mode."
  16222. (require 'holidays)
  16223. (let ((hl (funcall
  16224. (if (fboundp 'calendar-check-holidays)
  16225. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16226. (if hl (mapconcat 'identity hl "; "))))
  16227. (defun org-diary-sexp-entry (sexp entry date)
  16228. "Process a SEXP diary ENTRY for DATE."
  16229. (require 'diary-lib)
  16230. (let ((result (if calendar-debug-sexp
  16231. (let ((stack-trace-on-error t))
  16232. (eval (car (read-from-string sexp))))
  16233. (condition-case nil
  16234. (eval (car (read-from-string sexp)))
  16235. (error
  16236. (beep)
  16237. (message "Bad sexp at line %d in %s: %s"
  16238. (org-current-line)
  16239. (buffer-file-name) sexp)
  16240. (sleep-for 2))))))
  16241. (cond ((stringp result) (split-string result "; "))
  16242. ((and (consp result)
  16243. (not (consp (cdr result)))
  16244. (stringp (cdr result))) (cdr result))
  16245. ((and (consp result)
  16246. (stringp (car result))) result)
  16247. (result entry))))
  16248. (defun org-diary-to-ical-string (frombuf)
  16249. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16250. This uses the icalendar.el library."
  16251. (let* ((tmpdir (if (featurep 'xemacs)
  16252. (temp-directory)
  16253. temporary-file-directory))
  16254. (tmpfile (make-temp-name
  16255. (expand-file-name "orgics" tmpdir)))
  16256. buf rtn b e)
  16257. (with-current-buffer frombuf
  16258. (icalendar-export-region (point-min) (point-max) tmpfile)
  16259. (setq buf (find-buffer-visiting tmpfile))
  16260. (set-buffer buf)
  16261. (goto-char (point-min))
  16262. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16263. (setq b (match-beginning 0)))
  16264. (goto-char (point-max))
  16265. (if (re-search-backward "^END:VEVENT" nil t)
  16266. (setq e (match-end 0)))
  16267. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16268. (kill-buffer buf)
  16269. (delete-file tmpfile)
  16270. rtn))
  16271. (defun org-closest-date (start current change prefer show-all)
  16272. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16273. When PREFER is `past', return a date that is either CURRENT or past.
  16274. When PREFER is `future', return a date that is either CURRENT or future.
  16275. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16276. ;; Make the proper lists from the dates
  16277. (catch 'exit
  16278. (let ((a1 '(("h" . hour)
  16279. ("d" . day)
  16280. ("w" . week)
  16281. ("m" . month)
  16282. ("y" . year)))
  16283. (shour (nth 2 (org-parse-time-string start)))
  16284. dn dw sday cday n1 n2 n0
  16285. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16286. (setq start (org-date-to-gregorian start)
  16287. current (org-date-to-gregorian
  16288. (if show-all
  16289. current
  16290. (time-to-days (current-time))))
  16291. sday (calendar-absolute-from-gregorian start)
  16292. cday (calendar-absolute-from-gregorian current))
  16293. (if (<= cday sday) (throw 'exit sday))
  16294. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16295. (setq dn (string-to-number (match-string 1 change))
  16296. dw (cdr (assoc (match-string 2 change) a1))))
  16297. (unless (and dn (> dn 0))
  16298. (user-error "Invalid change specifier: %s" change))
  16299. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16300. (cond
  16301. ((eq dw 'hour)
  16302. (let ((missing-hours
  16303. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16304. dn)))
  16305. (setq n1 (if (zerop missing-hours) cday
  16306. (- cday (1+ (floor (/ missing-hours 24)))))
  16307. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16308. ((eq dw 'day)
  16309. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16310. n2 (+ n1 dn)))
  16311. ((eq dw 'year)
  16312. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16313. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16314. (setq date1 (list m d y1)
  16315. n1 (calendar-absolute-from-gregorian date1)
  16316. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16317. n2 (calendar-absolute-from-gregorian date2)))
  16318. ((eq dw 'month)
  16319. ;; approx number of month between the two dates
  16320. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16321. ;; How often does dn fit in there?
  16322. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16323. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16324. m (+ m nm)
  16325. ny (floor (/ m 12))
  16326. y (+ y ny)
  16327. m (- m (* ny 12)))
  16328. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16329. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16330. (setq m2 (+ m dn) y2 y)
  16331. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16332. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16333. (while (<= n2 cday)
  16334. (setq n1 n2 m m2 y y2)
  16335. (setq m2 (+ m dn) y2 y)
  16336. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16337. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16338. ;; Make sure n1 is the earlier date
  16339. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16340. (if show-all
  16341. (cond
  16342. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16343. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16344. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16345. (cond
  16346. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16347. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16348. (t (if (= cday n1) n1 n2)))))))
  16349. (defun org-date-to-gregorian (date)
  16350. "Turn any specification of DATE into a Gregorian date for the calendar."
  16351. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16352. ((and (listp date) (= (length date) 3)) date)
  16353. ((stringp date)
  16354. (setq date (org-parse-time-string date))
  16355. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16356. ((listp date)
  16357. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16358. (defun org-parse-time-string (s &optional nodefault)
  16359. "Parse the standard Org-mode time string.
  16360. This should be a lot faster than the normal `parse-time-string'.
  16361. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16362. hour and minute fields will be nil if not given."
  16363. (cond ((string-match org-ts-regexp0 s)
  16364. (list 0
  16365. (if (or (match-beginning 8) (not nodefault))
  16366. (string-to-number (or (match-string 8 s) "0")))
  16367. (if (or (match-beginning 7) (not nodefault))
  16368. (string-to-number (or (match-string 7 s) "0")))
  16369. (string-to-number (match-string 4 s))
  16370. (string-to-number (match-string 3 s))
  16371. (string-to-number (match-string 2 s))
  16372. nil nil nil))
  16373. ((string-match "^<[^>]+>$" s)
  16374. (decode-time (seconds-to-time (org-matcher-time s))))
  16375. (t (error "Not a standard Org-mode time string: %s" s))))
  16376. (defun org-timestamp-up (&optional arg)
  16377. "Increase the date item at the cursor by one.
  16378. If the cursor is on the year, change the year. If it is on the month,
  16379. the day or the time, change that.
  16380. With prefix ARG, change by that many units."
  16381. (interactive "p")
  16382. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16383. (defun org-timestamp-down (&optional arg)
  16384. "Decrease the date item at the cursor by one.
  16385. If the cursor is on the year, change the year. If it is on the month,
  16386. the day or the time, change that.
  16387. With prefix ARG, change by that many units."
  16388. (interactive "p")
  16389. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16390. (defun org-timestamp-up-day (&optional arg)
  16391. "Increase the date in the time stamp by one day.
  16392. With prefix ARG, change that many days."
  16393. (interactive "p")
  16394. (if (and (not (org-at-timestamp-p t))
  16395. (org-at-heading-p))
  16396. (org-todo 'up)
  16397. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16398. (defun org-timestamp-down-day (&optional arg)
  16399. "Decrease the date in the time stamp by one day.
  16400. With prefix ARG, change that many days."
  16401. (interactive "p")
  16402. (if (and (not (org-at-timestamp-p t))
  16403. (org-at-heading-p))
  16404. (org-todo 'down)
  16405. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16406. (defun org-at-timestamp-p (&optional inactive-ok)
  16407. "Non-nil if point is inside a timestamp.
  16408. When optional argument INACTIVE-OK is non-nil, also consider
  16409. inactive timestamps.
  16410. When this function returns a non-nil value, match data is set
  16411. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16412. INACTIVE-OK."
  16413. (interactive)
  16414. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16415. (pos (point))
  16416. (ans (or (looking-at tsr)
  16417. (save-excursion
  16418. (skip-chars-backward "^[<\n\r\t")
  16419. (if (> (point) (point-min)) (backward-char 1))
  16420. (and (looking-at tsr)
  16421. (> (- (match-end 0) pos) -1))))))
  16422. (and ans
  16423. (boundp 'org-ts-what)
  16424. (setq org-ts-what
  16425. (cond
  16426. ((= pos (match-beginning 0)) 'bracket)
  16427. ;; Point is considered to be "on the bracket" whether
  16428. ;; it's really on it or right after it.
  16429. ((= pos (1- (match-end 0))) 'bracket)
  16430. ((= pos (match-end 0)) 'after)
  16431. ((org-pos-in-match-range pos 2) 'year)
  16432. ((org-pos-in-match-range pos 3) 'month)
  16433. ((org-pos-in-match-range pos 7) 'hour)
  16434. ((org-pos-in-match-range pos 8) 'minute)
  16435. ((or (org-pos-in-match-range pos 4)
  16436. (org-pos-in-match-range pos 5)) 'day)
  16437. ((and (> pos (or (match-end 8) (match-end 5)))
  16438. (< pos (match-end 0)))
  16439. (- pos (or (match-end 8) (match-end 5))))
  16440. (t 'day))))
  16441. ans))
  16442. (defun org-toggle-timestamp-type ()
  16443. "Toggle the type (<active> or [inactive]) of a time stamp."
  16444. (interactive)
  16445. (when (org-at-timestamp-p t)
  16446. (let ((beg (match-beginning 0)) (end (match-end 0))
  16447. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16448. (save-excursion
  16449. (goto-char beg)
  16450. (while (re-search-forward "[][<>]" end t)
  16451. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16452. t t)))
  16453. (message "Timestamp is now %sactive"
  16454. (if (equal (char-after beg) ?<) "" "in")))))
  16455. (defun org-at-clock-log-p nil
  16456. "Is the cursor on the clock log line?"
  16457. (save-excursion
  16458. (move-beginning-of-line 1)
  16459. (looking-at org-clock-line-re)))
  16460. (defvar org-clock-history) ; defined in org-clock.el
  16461. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16462. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16463. "Change the date in the time stamp at point.
  16464. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16465. `year', `minute', `second'. If WHAT is not given, the cursor position
  16466. in the timestamp determines what will be changed.
  16467. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16468. (let ((origin (point)) origin-cat
  16469. with-hm inactive
  16470. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16471. org-ts-what
  16472. extra rem
  16473. ts time time0 fixnext clrgx)
  16474. (if (not (org-at-timestamp-p t))
  16475. (user-error "Not at a timestamp"))
  16476. (if (and (not what) (eq org-ts-what 'bracket))
  16477. (org-toggle-timestamp-type)
  16478. ;; Point isn't on brackets. Remember the part of the time-stamp
  16479. ;; the point was in. Indeed, size of time-stamps may change,
  16480. ;; but point must be kept in the same category nonetheless.
  16481. (setq origin-cat org-ts-what)
  16482. (if (and (not what) (not (eq org-ts-what 'day))
  16483. org-display-custom-times
  16484. (get-text-property (point) 'display)
  16485. (not (get-text-property (1- (point)) 'display)))
  16486. (setq org-ts-what 'day))
  16487. (setq org-ts-what (or what org-ts-what)
  16488. inactive (= (char-after (match-beginning 0)) ?\[)
  16489. ts (match-string 0))
  16490. (replace-match "")
  16491. (when (string-match
  16492. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16493. ts)
  16494. (setq extra (match-string 1 ts))
  16495. (if suppress-tmp-delay
  16496. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16497. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16498. (setq with-hm t))
  16499. (setq time0 (org-parse-time-string ts))
  16500. (when (and updown
  16501. (eq org-ts-what 'minute)
  16502. (not current-prefix-arg))
  16503. ;; This looks like s-up and s-down. Change by one rounding step.
  16504. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16505. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16506. (setcar (cdr time0) (+ (nth 1 time0)
  16507. (if (> n 0) (- rem) (- dm rem))))))
  16508. (setq time
  16509. (encode-time (or (car time0) 0)
  16510. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16511. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16512. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16513. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16514. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16515. (nthcdr 6 time0)))
  16516. (when (and (member org-ts-what '(hour minute))
  16517. extra
  16518. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16519. (setq extra (org-modify-ts-extra
  16520. extra
  16521. (if (eq org-ts-what 'hour) 2 5)
  16522. n dm)))
  16523. (when (integerp org-ts-what)
  16524. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16525. (if (eq what 'calendar)
  16526. (let ((cal-date (org-get-date-from-calendar)))
  16527. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16528. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16529. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16530. (setcar time0 (or (car time0) 0))
  16531. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16532. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16533. (setq time (apply 'encode-time time0))))
  16534. ;; Insert the new time-stamp, and ensure point stays in the same
  16535. ;; category as before (i.e. not after the last position in that
  16536. ;; category).
  16537. (let ((pos (point)))
  16538. ;; Stay before inserted string. `save-excursion' is of no use.
  16539. (setq org-last-changed-timestamp
  16540. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16541. (goto-char pos))
  16542. (save-match-data
  16543. (looking-at org-ts-regexp3)
  16544. (goto-char (cond
  16545. ;; `day' category ends before `hour' if any, or at
  16546. ;; the end of the day name.
  16547. ((eq origin-cat 'day)
  16548. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16549. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16550. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16551. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16552. ;; `year' and `month' have both fixed size: point
  16553. ;; couldn't have moved into another part.
  16554. (t origin))))
  16555. ;; Update clock if on a CLOCK line.
  16556. (org-clock-update-time-maybe)
  16557. ;; Maybe adjust the closest clock in `org-clock-history'
  16558. (when org-clock-adjust-closest
  16559. (if (not (and (org-at-clock-log-p)
  16560. (< 1 (length (delq nil (mapcar 'marker-position
  16561. org-clock-history))))))
  16562. (message "No clock to adjust")
  16563. (cond ((save-excursion ; fix previous clock?
  16564. (re-search-backward org-ts-regexp0 nil t)
  16565. (org-looking-back (concat org-clock-string " \\[")
  16566. (line-beginning-position)))
  16567. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16568. ((save-excursion ; fix next clock?
  16569. (re-search-backward org-ts-regexp0 nil t)
  16570. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16571. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16572. (save-window-excursion
  16573. ;; Find closest clock to point, adjust the previous/next one in history
  16574. (let* ((p (save-excursion (org-back-to-heading t)))
  16575. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16576. (clfixnth
  16577. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16578. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16579. (if (not clfixpos)
  16580. (message "No clock to adjust")
  16581. (save-excursion
  16582. (org-goto-marker-or-bmk clfixpos)
  16583. (org-show-subtree)
  16584. (when (re-search-forward clrgx nil t)
  16585. (goto-char (match-beginning 1))
  16586. (let (org-clock-adjust-closest)
  16587. (org-timestamp-change n org-ts-what updown))
  16588. (message "Clock adjusted in %s for heading: %s"
  16589. (file-name-nondirectory (buffer-file-name))
  16590. (org-get-heading t t)))))))))
  16591. ;; Try to recenter the calendar window, if any.
  16592. (if (and org-calendar-follow-timestamp-change
  16593. (get-buffer-window "*Calendar*" t)
  16594. (memq org-ts-what '(day month year)))
  16595. (org-recenter-calendar (time-to-days time))))))
  16596. (defun org-modify-ts-extra (s pos n dm)
  16597. "Change the different parts of the lead-time and repeat fields in timestamp."
  16598. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16599. ng h m new rem)
  16600. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16601. (cond
  16602. ((or (org-pos-in-match-range pos 2)
  16603. (org-pos-in-match-range pos 3))
  16604. (setq m (string-to-number (match-string 3 s))
  16605. h (string-to-number (match-string 2 s)))
  16606. (if (org-pos-in-match-range pos 2)
  16607. (setq h (+ h n))
  16608. (setq n (* dm (org-no-warnings (signum n))))
  16609. (when (not (= 0 (setq rem (% m dm))))
  16610. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16611. (setq m (+ m n)))
  16612. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16613. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16614. (setq h (mod h 24))
  16615. (setq ng 1 new (format "-%02d:%02d" h m)))
  16616. ((org-pos-in-match-range pos 6)
  16617. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16618. ((org-pos-in-match-range pos 5)
  16619. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16620. ((org-pos-in-match-range pos 9)
  16621. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16622. ((org-pos-in-match-range pos 8)
  16623. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16624. (when ng
  16625. (setq s (concat
  16626. (substring s 0 (match-beginning ng))
  16627. new
  16628. (substring s (match-end ng))))))
  16629. s))
  16630. (defun org-recenter-calendar (date)
  16631. "If the calendar is visible, recenter it to DATE."
  16632. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16633. (when cwin
  16634. (let ((calendar-move-hook nil))
  16635. (with-selected-window cwin
  16636. (calendar-goto-date (if (listp date) date
  16637. (calendar-gregorian-from-absolute date))))))))
  16638. (defun org-goto-calendar (&optional arg)
  16639. "Go to the Emacs calendar at the current date.
  16640. If there is a time stamp in the current line, go to that date.
  16641. A prefix ARG can be used to force the current date."
  16642. (interactive "P")
  16643. (let ((tsr org-ts-regexp) diff
  16644. (calendar-move-hook nil)
  16645. (calendar-view-holidays-initially-flag nil)
  16646. (calendar-view-diary-initially-flag nil))
  16647. (if (or (org-at-timestamp-p)
  16648. (save-excursion
  16649. (beginning-of-line 1)
  16650. (looking-at (concat ".*" tsr))))
  16651. (let ((d1 (time-to-days (current-time)))
  16652. (d2 (time-to-days
  16653. (org-time-string-to-time (match-string 1)))))
  16654. (setq diff (- d2 d1))))
  16655. (calendar)
  16656. (calendar-goto-today)
  16657. (if (and diff (not arg)) (calendar-forward-day diff))))
  16658. (defun org-get-date-from-calendar ()
  16659. "Return a list (month day year) of date at point in calendar."
  16660. (with-current-buffer "*Calendar*"
  16661. (save-match-data
  16662. (calendar-cursor-to-date))))
  16663. (defun org-date-from-calendar ()
  16664. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16665. If there is already a time stamp at the cursor position, update it."
  16666. (interactive)
  16667. (if (org-at-timestamp-p t)
  16668. (org-timestamp-change 0 'calendar)
  16669. (let ((cal-date (org-get-date-from-calendar)))
  16670. (org-insert-time-stamp
  16671. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16672. (defcustom org-effort-durations
  16673. `(("min" . 1)
  16674. ("h" . 60)
  16675. ("d" . ,(* 60 8))
  16676. ("w" . ,(* 60 8 5))
  16677. ("m" . ,(* 60 8 5 4))
  16678. ("y" . ,(* 60 8 5 40)))
  16679. "Conversion factor to minutes for an effort modifier.
  16680. Each entry has the form (MODIFIER . MINUTES).
  16681. In an effort string, a number followed by MODIFIER is multiplied
  16682. by the specified number of MINUTES to obtain an effort in
  16683. minutes.
  16684. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16685. effort string \"2hours\" is equivalent to 120 minutes."
  16686. :group 'org-agenda
  16687. :version "25.1"
  16688. :package-version '(Org . "8.3")
  16689. :type '(alist :key-type (string :tag "Modifier")
  16690. :value-type (number :tag "Minutes")))
  16691. (defun org-minutes-to-clocksum-string (m)
  16692. "Format number of minutes as a clocksum string.
  16693. The format is determined by `org-time-clocksum-format',
  16694. `org-time-clocksum-use-fractional' and
  16695. `org-time-clocksum-fractional-format' and
  16696. `org-time-clocksum-use-effort-durations'."
  16697. (let ((clocksum "")
  16698. (m (round m)) ; Don't allow fractions of minutes
  16699. h d w mo y fmt n)
  16700. (setq h (if org-time-clocksum-use-effort-durations
  16701. (cdr (assoc "h" org-effort-durations)) 60)
  16702. d (if org-time-clocksum-use-effort-durations
  16703. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16704. w (if org-time-clocksum-use-effort-durations
  16705. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16706. mo (if org-time-clocksum-use-effort-durations
  16707. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16708. y (if org-time-clocksum-use-effort-durations
  16709. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16710. ;; fractional format
  16711. (if org-time-clocksum-use-fractional
  16712. (cond
  16713. ;; single format string
  16714. ((stringp org-time-clocksum-fractional-format)
  16715. (format org-time-clocksum-fractional-format (/ m (float h))))
  16716. ;; choice of fractional formats for different time units
  16717. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16718. (> (/ (truncate m) (* y d h)) 0))
  16719. (format fmt (/ m (* y d (float h)))))
  16720. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16721. (> (/ (truncate m) (* mo d h)) 0))
  16722. (format fmt (/ m (* mo d (float h)))))
  16723. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16724. (> (/ (truncate m) (* w d h)) 0))
  16725. (format fmt (/ m (* w d (float h)))))
  16726. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16727. (> (/ (truncate m) (* d h)) 0))
  16728. (format fmt (/ m (* d (float h)))))
  16729. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16730. (> (/ (truncate m) h) 0))
  16731. (format fmt (/ m (float h))))
  16732. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16733. (format fmt m))
  16734. ;; fall back to smallest time unit with a format
  16735. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16736. (format fmt (/ m (float h))))
  16737. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16738. (format fmt (/ m (* d (float h)))))
  16739. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16740. (format fmt (/ m (* w d (float h)))))
  16741. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16742. (format fmt (/ m (* mo d (float h)))))
  16743. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16744. (format fmt (/ m (* y d (float h))))))
  16745. ;; standard (non-fractional) format, with single format string
  16746. (if (stringp org-time-clocksum-format)
  16747. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16748. ;; separate formats components
  16749. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16750. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16751. (plist-get org-time-clocksum-format :require-years))
  16752. (setq clocksum (concat clocksum (format fmt n))
  16753. m (- m (* n y d h))))
  16754. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16755. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16756. (plist-get org-time-clocksum-format :require-months))
  16757. (setq clocksum (concat clocksum (format fmt n))
  16758. m (- m (* n mo d h))))
  16759. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16760. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16761. (plist-get org-time-clocksum-format :require-weeks))
  16762. (setq clocksum (concat clocksum (format fmt n))
  16763. m (- m (* n w d h))))
  16764. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16765. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16766. (plist-get org-time-clocksum-format :require-days))
  16767. (setq clocksum (concat clocksum (format fmt n))
  16768. m (- m (* n d h))))
  16769. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16770. (or (> (setq n (/ (truncate m) h)) 0)
  16771. (plist-get org-time-clocksum-format :require-hours))
  16772. (setq clocksum (concat clocksum (format fmt n))
  16773. m (- m (* n h))))
  16774. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16775. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16776. (setq clocksum (concat clocksum (format fmt m))))
  16777. ;; return formatted time duration
  16778. clocksum))))
  16779. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16780. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16781. "Org mode version 8.0")
  16782. (defun org-hours-to-clocksum-string (n)
  16783. (org-minutes-to-clocksum-string (* n 60)))
  16784. (defun org-hh:mm-string-to-minutes (s)
  16785. "Convert a string H:MM to a number of minutes.
  16786. If the string is just a number, interpret it as minutes.
  16787. In fact, the first hh:mm or number in the string will be taken,
  16788. there can be extra stuff in the string.
  16789. If no number is found, the return value is 0."
  16790. (cond
  16791. ((integerp s) s)
  16792. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16793. (+ (* (string-to-number (match-string 1 s)) 60)
  16794. (string-to-number (match-string 2 s))))
  16795. ((string-match "\\([0-9]+\\)" s)
  16796. (string-to-number (match-string 1 s)))
  16797. (t 0)))
  16798. (defcustom org-image-actual-width t
  16799. "Should we use the actual width of images when inlining them?
  16800. When set to t, always use the image width.
  16801. When set to a number, use imagemagick (when available) to set
  16802. the image's width to this value.
  16803. When set to a number in a list, try to get the width from any
  16804. #+ATTR.* keyword if it matches a width specification like
  16805. #+ATTR_HTML: :width 300px
  16806. and fall back on that number if none is found.
  16807. When set to nil, try to get the width from an #+ATTR.* keyword
  16808. and fall back on the original width if none is found.
  16809. This requires Emacs >= 24.1, build with imagemagick support."
  16810. :group 'org-appearance
  16811. :version "24.4"
  16812. :package-version '(Org . "8.0")
  16813. :type '(choice
  16814. (const :tag "Use the image width" t)
  16815. (integer :tag "Use a number of pixels")
  16816. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16817. (const :tag "Use #+ATTR* or don't resize" nil)))
  16818. (defcustom org-agenda-inhibit-startup nil
  16819. "Inhibit startup when preparing agenda buffers.
  16820. When this variable is t, the initialization of the Org agenda
  16821. buffers is inhibited: e.g. the visibility state is not set, the
  16822. tables are not re-aligned, etc."
  16823. :type 'boolean
  16824. :version "24.3"
  16825. :group 'org-agenda)
  16826. (define-obsolete-variable-alias
  16827. 'org-agenda-ignore-drawer-properties
  16828. 'org-agenda-ignore-properties "25.1")
  16829. (defcustom org-agenda-ignore-properties nil
  16830. "Avoid updating text properties when building the agenda.
  16831. Properties are used to prepare buffers for effort estimates,
  16832. appointments, statistics and subtree-local categories.
  16833. If you don't use these in the agenda, you can add them to this
  16834. list and agenda building will be a bit faster.
  16835. The value is a list, with zero or more of the symbols `effort', `appt',
  16836. `stats' or `category'."
  16837. :type '(set :greedy t
  16838. (const effort)
  16839. (const appt)
  16840. (const stats)
  16841. (const category))
  16842. :version "25.1"
  16843. :package-version '(Org . "8.3")
  16844. :group 'org-agenda)
  16845. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16846. "Convert a duration string S to minutes.
  16847. A bare number is interpreted as minutes, modifiers can be set by
  16848. customizing `org-effort-durations' (which see).
  16849. Entries containing a colon are interpreted as H:MM by
  16850. `org-hh:mm-string-to-minutes'."
  16851. (let ((result 0)
  16852. (re (concat "\\([0-9.]+\\) *\\("
  16853. (regexp-opt (mapcar 'car org-effort-durations))
  16854. "\\)")))
  16855. (while (string-match re s)
  16856. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16857. (string-to-number (match-string 1 s))))
  16858. (setq s (replace-match "" nil t s)))
  16859. (setq result (floor result))
  16860. (incf result (org-hh:mm-string-to-minutes s))
  16861. (if output-to-string (number-to-string result) result)))
  16862. ;;;; Files
  16863. (defun org-save-all-org-buffers ()
  16864. "Save all Org-mode buffers without user confirmation."
  16865. (interactive)
  16866. (message "Saving all Org-mode buffers...")
  16867. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16868. (when (featurep 'org-id) (org-id-locations-save))
  16869. (message "Saving all Org-mode buffers... done"))
  16870. (defun org-revert-all-org-buffers ()
  16871. "Revert all Org-mode buffers.
  16872. Prompt for confirmation when there are unsaved changes.
  16873. Be sure you know what you are doing before letting this function
  16874. overwrite your changes.
  16875. This function is useful in a setup where one tracks org files
  16876. with a version control system, to revert on one machine after pulling
  16877. changes from another. I believe the procedure must be like this:
  16878. 1. M-x org-save-all-org-buffers
  16879. 2. Pull changes from the other machine, resolve conflicts
  16880. 3. M-x org-revert-all-org-buffers"
  16881. (interactive)
  16882. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16883. (user-error "Abort"))
  16884. (save-excursion
  16885. (save-window-excursion
  16886. (mapc
  16887. (lambda (b)
  16888. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16889. (with-current-buffer b buffer-file-name))
  16890. (org-pop-to-buffer-same-window b)
  16891. (revert-buffer t 'no-confirm)))
  16892. (buffer-list))
  16893. (when (and (featurep 'org-id) org-id-track-globally)
  16894. (org-id-locations-load)))))
  16895. ;;;; Agenda files
  16896. ;;;###autoload
  16897. (defun org-switchb (&optional arg)
  16898. "Switch between Org buffers.
  16899. With one prefix argument, restrict available buffers to files.
  16900. With two prefix arguments, restrict available buffers to agenda files.
  16901. Defaults to `iswitchb' for buffer name completion.
  16902. Set `org-completion-use-ido' to make it use ido instead."
  16903. (interactive "P")
  16904. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16905. ((equal arg '(16)) (org-buffer-list 'agenda))
  16906. (t (org-buffer-list))))
  16907. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16908. (org-completion-use-ido org-completion-use-ido))
  16909. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16910. (setq org-completion-use-iswitchb t))
  16911. (org-pop-to-buffer-same-window
  16912. (org-icompleting-read "Org buffer: "
  16913. (mapcar 'list (mapcar 'buffer-name blist))
  16914. nil t))))
  16915. ;;; Define some older names previously used for this functionality
  16916. ;;;###autoload
  16917. (defalias 'org-ido-switchb 'org-switchb)
  16918. ;;;###autoload
  16919. (defalias 'org-iswitchb 'org-switchb)
  16920. (defun org-buffer-list (&optional predicate exclude-tmp)
  16921. "Return a list of Org buffers.
  16922. PREDICATE can be `export', `files' or `agenda'.
  16923. export restrict the list to Export buffers.
  16924. files restrict the list to buffers visiting Org files.
  16925. agenda restrict the list to buffers visiting agenda files.
  16926. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16927. (let* ((bfn nil)
  16928. (agenda-files (and (eq predicate 'agenda)
  16929. (mapcar 'file-truename (org-agenda-files t))))
  16930. (filter
  16931. (cond
  16932. ((eq predicate 'files)
  16933. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16934. ((eq predicate 'export)
  16935. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16936. ((eq predicate 'agenda)
  16937. (lambda (b)
  16938. (with-current-buffer b
  16939. (and (derived-mode-p 'org-mode)
  16940. (setq bfn (buffer-file-name b))
  16941. (member (file-truename bfn) agenda-files)))))
  16942. (t (lambda (b) (with-current-buffer b
  16943. (or (derived-mode-p 'org-mode)
  16944. (string-match "\\*Org .*Export"
  16945. (buffer-name b)))))))))
  16946. (delq nil
  16947. (mapcar
  16948. (lambda(b)
  16949. (if (and (funcall filter b)
  16950. (or (not exclude-tmp)
  16951. (not (string-match "tmp" (buffer-name b)))))
  16952. b
  16953. nil))
  16954. (buffer-list)))))
  16955. (defun org-agenda-files (&optional unrestricted archives)
  16956. "Get the list of agenda files.
  16957. Optional UNRESTRICTED means return the full list even if a restriction
  16958. is currently in place.
  16959. When ARCHIVES is t, include all archive files that are really being
  16960. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16961. `org-agenda-archives-mode' is t."
  16962. (let ((files
  16963. (cond
  16964. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16965. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16966. ((listp org-agenda-files) org-agenda-files)
  16967. (t (error "Invalid value of `org-agenda-files'")))))
  16968. (setq files (apply 'append
  16969. (mapcar (lambda (f)
  16970. (if (file-directory-p f)
  16971. (directory-files
  16972. f t org-agenda-file-regexp)
  16973. (list f)))
  16974. files)))
  16975. (when org-agenda-skip-unavailable-files
  16976. (setq files (delq nil
  16977. (mapcar (function
  16978. (lambda (file)
  16979. (and (file-readable-p file) file)))
  16980. files))))
  16981. (when (or (eq archives t)
  16982. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16983. (setq files (org-add-archive-files files)))
  16984. files))
  16985. (defun org-agenda-file-p (&optional file)
  16986. "Return non-nil, if FILE is an agenda file.
  16987. If FILE is omitted, use the file associated with the current
  16988. buffer."
  16989. (let ((fname (or file (buffer-file-name))))
  16990. (and fname
  16991. (member (file-truename fname)
  16992. (mapcar #'file-truename (org-agenda-files t))))))
  16993. (defun org-edit-agenda-file-list ()
  16994. "Edit the list of agenda files.
  16995. Depending on setup, this either uses customize to edit the variable
  16996. `org-agenda-files', or it visits the file that is holding the list. In the
  16997. latter case, the buffer is set up in a way that saving it automatically kills
  16998. the buffer and restores the previous window configuration."
  16999. (interactive)
  17000. (if (stringp org-agenda-files)
  17001. (let ((cw (current-window-configuration)))
  17002. (find-file org-agenda-files)
  17003. (org-set-local 'org-window-configuration cw)
  17004. (org-add-hook 'after-save-hook
  17005. (lambda ()
  17006. (set-window-configuration
  17007. (prog1 org-window-configuration
  17008. (kill-buffer (current-buffer))))
  17009. (org-install-agenda-files-menu)
  17010. (message "New agenda file list installed"))
  17011. nil 'local)
  17012. (message "%s" (substitute-command-keys
  17013. "Edit list and finish with \\[save-buffer]")))
  17014. (customize-variable 'org-agenda-files)))
  17015. (defun org-store-new-agenda-file-list (list)
  17016. "Set new value for the agenda file list and save it correctly."
  17017. (if (stringp org-agenda-files)
  17018. (let ((fe (org-read-agenda-file-list t)) b u)
  17019. (while (setq b (find-buffer-visiting org-agenda-files))
  17020. (kill-buffer b))
  17021. (with-temp-file org-agenda-files
  17022. (insert
  17023. (mapconcat
  17024. (lambda (f) ;; Keep un-expanded entries.
  17025. (if (setq u (assoc f fe))
  17026. (cdr u)
  17027. f))
  17028. list "\n")
  17029. "\n")))
  17030. (let ((org-mode-hook nil) (org-inhibit-startup t)
  17031. (org-insert-mode-line-in-empty-file nil))
  17032. (setq org-agenda-files list)
  17033. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17034. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  17035. "Read the list of agenda files from a file.
  17036. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  17037. filenames, used by `org-store-new-agenda-file-list' to write back
  17038. un-expanded file names."
  17039. (when (file-directory-p org-agenda-files)
  17040. (error "`org-agenda-files' cannot be a single directory"))
  17041. (when (stringp org-agenda-files)
  17042. (with-temp-buffer
  17043. (insert-file-contents org-agenda-files)
  17044. (mapcar
  17045. (lambda (f)
  17046. (let ((e (expand-file-name (substitute-in-file-name f)
  17047. org-directory)))
  17048. (if pair-with-expansion
  17049. (cons e f)
  17050. e)))
  17051. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17052. ;;;###autoload
  17053. (defun org-cycle-agenda-files ()
  17054. "Cycle through the files in `org-agenda-files'.
  17055. If the current buffer visits an agenda file, find the next one in the list.
  17056. If the current buffer does not, find the first agenda file."
  17057. (interactive)
  17058. (let* ((fs (or (org-agenda-files t)
  17059. (user-error "No agenda files")))
  17060. (files (copy-sequence fs))
  17061. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17062. file)
  17063. (when tcf
  17064. (while (and (setq file (pop files))
  17065. (not (equal (file-truename file) tcf)))))
  17066. (find-file (car (or files fs)))
  17067. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17068. (defun org-agenda-file-to-front (&optional to-end)
  17069. "Move/add the current file to the top of the agenda file list.
  17070. If the file is not present in the list, it is added to the front. If it is
  17071. present, it is moved there. With optional argument TO-END, add/move to the
  17072. end of the list."
  17073. (interactive "P")
  17074. (let ((org-agenda-skip-unavailable-files nil)
  17075. (file-alist (mapcar (lambda (x)
  17076. (cons (file-truename x) x))
  17077. (org-agenda-files t)))
  17078. (ctf (file-truename
  17079. (or buffer-file-name
  17080. (user-error "Please save the current buffer to a file"))))
  17081. x had)
  17082. (setq x (assoc ctf file-alist) had x)
  17083. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17084. (if to-end
  17085. (setq file-alist (append (delq x file-alist) (list x)))
  17086. (setq file-alist (cons x (delq x file-alist))))
  17087. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17088. (org-install-agenda-files-menu)
  17089. (message "File %s to %s of agenda file list"
  17090. (if had "moved" "added") (if to-end "end" "front"))))
  17091. (defun org-remove-file (&optional file)
  17092. "Remove current file from the list of files in variable `org-agenda-files'.
  17093. These are the files which are being checked for agenda entries.
  17094. Optional argument FILE means use this file instead of the current."
  17095. (interactive)
  17096. (let* ((org-agenda-skip-unavailable-files nil)
  17097. (file (or file buffer-file-name
  17098. (user-error "Current buffer does not visit a file")))
  17099. (true-file (file-truename file))
  17100. (afile (abbreviate-file-name file))
  17101. (files (delq nil (mapcar
  17102. (lambda (x)
  17103. (if (equal true-file
  17104. (file-truename x))
  17105. nil x))
  17106. (org-agenda-files t)))))
  17107. (if (not (= (length files) (length (org-agenda-files t))))
  17108. (progn
  17109. (org-store-new-agenda-file-list files)
  17110. (org-install-agenda-files-menu)
  17111. (message "Removed from Org Agenda list: %s" afile))
  17112. (message "File was not in list: %s (not removed)" afile))))
  17113. (defun org-file-menu-entry (file)
  17114. (vector file (list 'find-file file) t))
  17115. (defun org-check-agenda-file (file)
  17116. "Make sure FILE exists. If not, ask user what to do."
  17117. (when (not (file-exists-p file))
  17118. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17119. (abbreviate-file-name file))
  17120. (let ((r (downcase (read-char-exclusive))))
  17121. (cond
  17122. ((equal r ?r)
  17123. (org-remove-file file)
  17124. (throw 'nextfile t))
  17125. (t (user-error "Abort"))))))
  17126. (defun org-get-agenda-file-buffer (file)
  17127. "Get an agenda buffer visiting FILE.
  17128. If the buffer needs to be created, add it to the list of buffers
  17129. which might be released later."
  17130. (let ((buf (org-find-base-buffer-visiting file)))
  17131. (if buf
  17132. buf ; just return it
  17133. ;; Make a new buffer and remember it
  17134. (setq buf (find-file-noselect file))
  17135. (if buf (push buf org-agenda-new-buffers))
  17136. buf)))
  17137. (defun org-release-buffers (blist)
  17138. "Release all buffers in list, asking the user for confirmation when needed.
  17139. When a buffer is unmodified, it is just killed. When modified, it is saved
  17140. \(if the user agrees) and then killed."
  17141. (let (file)
  17142. (dolist (buf blist)
  17143. (setq file (buffer-file-name buf))
  17144. (when (and (buffer-modified-p buf)
  17145. file
  17146. (y-or-n-p (format "Save file %s? " file)))
  17147. (with-current-buffer buf (save-buffer)))
  17148. (kill-buffer buf))))
  17149. (defun org-agenda-prepare-buffers (files)
  17150. "Create buffers for all agenda files, protect archived trees and comments."
  17151. (interactive)
  17152. (let ((pa '(:org-archived t))
  17153. (pc '(:org-comment t))
  17154. (pall '(:org-archived t :org-comment t))
  17155. (inhibit-read-only t)
  17156. (org-inhibit-startup org-agenda-inhibit-startup)
  17157. (rea (concat ":" org-archive-tag ":"))
  17158. file re pos)
  17159. (setq org-tag-alist-for-agenda nil
  17160. org-tag-groups-alist-for-agenda nil)
  17161. (save-excursion
  17162. (save-restriction
  17163. (dolist (file files)
  17164. (catch 'nextfile
  17165. (if (bufferp file)
  17166. (set-buffer file)
  17167. (org-check-agenda-file file)
  17168. (set-buffer (org-get-agenda-file-buffer file)))
  17169. (widen)
  17170. (org-set-regexps-and-options 'tags-only)
  17171. (setq pos (point))
  17172. (or (memq 'category org-agenda-ignore-properties)
  17173. (org-refresh-category-properties))
  17174. (or (memq 'stats org-agenda-ignore-properties)
  17175. (org-refresh-stats-properties))
  17176. (or (memq 'effort org-agenda-ignore-properties)
  17177. (org-refresh-effort-properties))
  17178. (or (memq 'appt org-agenda-ignore-properties)
  17179. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17180. (setq org-todo-keywords-for-agenda
  17181. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17182. (setq org-done-keywords-for-agenda
  17183. (append org-done-keywords-for-agenda org-done-keywords))
  17184. (setq org-todo-keyword-alist-for-agenda
  17185. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17186. (setq org-tag-alist-for-agenda
  17187. (org-uniquify
  17188. (append org-tag-alist-for-agenda
  17189. org-tag-alist
  17190. org-tag-persistent-alist)))
  17191. ;; Merge current file's tag groups into global
  17192. ;; `org-tag-groups-alist-for-agenda'.
  17193. (when org-group-tags
  17194. (dolist (alist org-tag-groups-alist)
  17195. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  17196. (if old
  17197. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  17198. (push alist org-tag-groups-alist-for-agenda)))))
  17199. (org-with-silent-modifications
  17200. (save-excursion
  17201. (remove-text-properties (point-min) (point-max) pall)
  17202. (when org-agenda-skip-archived-trees
  17203. (goto-char (point-min))
  17204. (while (re-search-forward rea nil t)
  17205. (if (org-at-heading-p t)
  17206. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17207. (goto-char (point-min))
  17208. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17209. (while (re-search-forward re nil t)
  17210. (when (save-match-data (org-in-commented-heading-p t))
  17211. (add-text-properties
  17212. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17213. (goto-char pos)))))
  17214. (setq org-todo-keywords-for-agenda
  17215. (org-uniquify org-todo-keywords-for-agenda))
  17216. (setq org-todo-keyword-alist-for-agenda
  17217. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17218. ;;;; CDLaTeX minor mode
  17219. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17220. "Keymap for the minor `org-cdlatex-mode'.")
  17221. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17222. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17223. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17224. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17225. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17226. (defvar org-cdlatex-texmathp-advice-is-done nil
  17227. "Flag remembering if we have applied the advice to texmathp already.")
  17228. (define-minor-mode org-cdlatex-mode
  17229. "Toggle the minor `org-cdlatex-mode'.
  17230. This mode supports entering LaTeX environment and math in LaTeX fragments
  17231. in Org-mode.
  17232. \\{org-cdlatex-mode-map}"
  17233. nil " OCDL" nil
  17234. (when org-cdlatex-mode
  17235. (require 'cdlatex)
  17236. (run-hooks 'cdlatex-mode-hook)
  17237. (cdlatex-compute-tables))
  17238. (unless org-cdlatex-texmathp-advice-is-done
  17239. (setq org-cdlatex-texmathp-advice-is-done t)
  17240. (defadvice texmathp (around org-math-always-on activate)
  17241. "Always return t in org-mode buffers.
  17242. This is because we want to insert math symbols without dollars even outside
  17243. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17244. an embedded LaTeX fragment, let texmathp do its job.
  17245. \\[org-cdlatex-mode-map]"
  17246. (interactive)
  17247. (let (p)
  17248. (cond
  17249. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17250. ((eq this-command 'cdlatex-math-symbol)
  17251. (setq ad-return-value t
  17252. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17253. (t
  17254. (let ((p (org-inside-LaTeX-fragment-p)))
  17255. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17256. (setq ad-return-value t
  17257. texmathp-why '("Org-mode embedded math" . 0))
  17258. (if p ad-do-it)))))))))
  17259. (defun turn-on-org-cdlatex ()
  17260. "Unconditionally turn on `org-cdlatex-mode'."
  17261. (org-cdlatex-mode 1))
  17262. (defun org-try-cdlatex-tab ()
  17263. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17264. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17265. - inside a LaTeX fragment, or
  17266. - after the first word in a line, where an abbreviation expansion could
  17267. insert a LaTeX environment."
  17268. (when org-cdlatex-mode
  17269. (cond
  17270. ;; Before any word on the line: No expansion possible.
  17271. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17272. ;; Just after first word on the line: Expand it. Make sure it
  17273. ;; cannot happen on headlines, though.
  17274. ((save-excursion
  17275. (skip-chars-backward "a-zA-Z0-9*")
  17276. (skip-chars-backward " \t")
  17277. (and (bolp) (not (org-at-heading-p))))
  17278. (cdlatex-tab) t)
  17279. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17280. (defun org-cdlatex-underscore-caret (&optional arg)
  17281. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17282. Revert to the normal definition outside of these fragments."
  17283. (interactive "P")
  17284. (if (org-inside-LaTeX-fragment-p)
  17285. (call-interactively 'cdlatex-sub-superscript)
  17286. (let (org-cdlatex-mode)
  17287. (call-interactively (key-binding (vector last-input-event))))))
  17288. (defun org-cdlatex-math-modify (&optional arg)
  17289. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17290. Revert to the normal definition outside of these fragments."
  17291. (interactive "P")
  17292. (if (org-inside-LaTeX-fragment-p)
  17293. (call-interactively 'cdlatex-math-modify)
  17294. (let (org-cdlatex-mode)
  17295. (call-interactively (key-binding (vector last-input-event))))))
  17296. (defun org-cdlatex-environment-indent (&optional environment item)
  17297. "Execute `cdlatex-environment' and indent the inserted environment.
  17298. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17299. The inserted environment is indented to current indentation
  17300. unless point is at the beginning of the line, in which the
  17301. environment remains unintended."
  17302. (interactive)
  17303. ;; cdlatex-environment always return nil. Therefore, capture output
  17304. ;; first and determine if an environment was selected.
  17305. (let* ((beg (point-marker))
  17306. (end (copy-marker (point) t))
  17307. (inserted (progn
  17308. (ignore-errors (cdlatex-environment environment item))
  17309. (< beg end)))
  17310. ;; Figure out how many lines to move forward after the
  17311. ;; environment has been inserted.
  17312. (lines (when inserted
  17313. (save-excursion
  17314. (- (loop while (< beg (point))
  17315. with x = 0
  17316. do (forward-line -1)
  17317. (incf x)
  17318. finally return x)
  17319. (if (progn (goto-char beg)
  17320. (and (progn (skip-chars-forward " \t") (eolp))
  17321. (progn (skip-chars-backward " \t") (bolp))))
  17322. 1 0)))))
  17323. (env (org-trim (delete-and-extract-region beg end))))
  17324. (when inserted
  17325. ;; Get indentation of next line unless at column 0.
  17326. (let ((ind (if (bolp) 0
  17327. (save-excursion
  17328. (org-return-indent)
  17329. (prog1 (org-get-indentation)
  17330. (when (progn (skip-chars-forward " \t") (eolp))
  17331. (delete-region beg (point)))))))
  17332. (bol (progn (skip-chars-backward " \t") (bolp))))
  17333. ;; Insert a newline before environment unless at column zero
  17334. ;; to "escape" the current line. Insert a newline if
  17335. ;; something is one the same line as \end{ENVIRONMENT}.
  17336. (insert
  17337. (concat (unless bol "\n") env
  17338. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17339. (unless (zerop ind)
  17340. (save-excursion
  17341. (goto-char beg)
  17342. (while (< (point) end)
  17343. (unless (eolp) (org-indent-line-to ind))
  17344. (forward-line))))
  17345. (goto-char beg)
  17346. (forward-line lines)
  17347. (org-indent-line-to ind)))
  17348. (set-marker beg nil)
  17349. (set-marker end nil)))
  17350. ;;;; LaTeX fragments
  17351. (defun org-inside-LaTeX-fragment-p ()
  17352. "Test if point is inside a LaTeX fragment.
  17353. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17354. sequence appearing also before point.
  17355. Even though the matchers for math are configurable, this function assumes
  17356. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17357. delimiters are skipped when they have been removed by customization.
  17358. The return value is nil, or a cons cell with the delimiter and the
  17359. position of this delimiter.
  17360. This function does a reasonably good job, but can locally be fooled by
  17361. for example currency specifications. For example it will assume being in
  17362. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17363. fragments that are properly closed, but during editing, we have to live
  17364. with the uncertainty caused by missing closing delimiters. This function
  17365. looks only before point, not after."
  17366. (catch 'exit
  17367. (let ((pos (point))
  17368. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17369. (lim (progn
  17370. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17371. (point)))
  17372. dd-on str (start 0) m re)
  17373. (goto-char pos)
  17374. (when dodollar
  17375. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17376. re (nth 1 (assoc "$" org-latex-regexps)))
  17377. (while (string-match re str start)
  17378. (cond
  17379. ((= (match-end 0) (length str))
  17380. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17381. ((= (match-end 0) (- (length str) 5))
  17382. (throw 'exit nil))
  17383. (t (setq start (match-end 0))))))
  17384. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17385. (goto-char pos)
  17386. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17387. (and (match-beginning 2) (throw 'exit nil))
  17388. ;; count $$
  17389. (while (re-search-backward "\\$\\$" lim t)
  17390. (setq dd-on (not dd-on)))
  17391. (goto-char pos)
  17392. (if dd-on (cons "$$" m))))))
  17393. (defun org-inside-latex-macro-p ()
  17394. "Is point inside a LaTeX macro or its arguments?"
  17395. (save-match-data
  17396. (org-in-regexp
  17397. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17398. (defun org--format-latex-make-overlay (beg end image)
  17399. "Build an overlay between BEG and END using IMAGE file."
  17400. (let ((ov (make-overlay beg end)))
  17401. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17402. (overlay-put ov 'evaporate t)
  17403. (overlay-put ov
  17404. 'modification-hooks
  17405. (list (lambda (o _flag _beg _end &optional _l)
  17406. (delete-overlay o))))
  17407. (if (featurep 'xemacs)
  17408. (progn
  17409. (overlay-put ov 'invisible t)
  17410. (overlay-put ov 'end-glyph (make-glyph (vector 'png :file image))))
  17411. (overlay-put ov
  17412. 'display
  17413. (list 'image :type 'png :file image :ascent 'center)))))
  17414. (defun org--list-latex-overlays (&optional beg end)
  17415. "List all Org LaTeX overlays in current buffer.
  17416. Limit to overlays between BEG and END when those are provided."
  17417. (org-remove-if-not
  17418. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17419. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17420. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17421. "Remove all overlays with LaTeX fragment images in current buffer.
  17422. When optional arguments BEG and END are non-nil, remove all
  17423. overlays between them instead. Return a non-nil value when some
  17424. overlays were removed, nil otherwise."
  17425. (let ((overlays (org--list-latex-overlays beg end)))
  17426. (mapc #'delete-overlay overlays)
  17427. overlays))
  17428. (define-obsolete-function-alias
  17429. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17430. (defun org-toggle-latex-fragment (&optional arg)
  17431. "Preview the LaTeX fragment at point, or all locally or globally.
  17432. If the cursor is on a LaTeX fragment, create the image and overlay
  17433. it over the source code, if there is none. Remove it otherwise.
  17434. If there is no fragment at point, display all fragments in the
  17435. current section.
  17436. With prefix ARG, preview or clear image for all fragments in the
  17437. current subtree or in the whole buffer when used before the first
  17438. headline. With a double prefix ARG \\[universal-argument] \
  17439. \\[universal-argument] preview or clear images
  17440. for all fragments in the buffer."
  17441. (interactive "P")
  17442. (unless (buffer-file-name (buffer-base-buffer))
  17443. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17444. (when (display-graphic-p)
  17445. (catch 'exit
  17446. (save-excursion
  17447. (let ((window-start (window-start)) msg)
  17448. (save-restriction
  17449. (cond
  17450. ((or (equal arg '(16))
  17451. (and (equal arg '(4))
  17452. (org-with-limited-levels (org-before-first-heading-p))))
  17453. (if (org-remove-latex-fragment-image-overlays)
  17454. (progn (message "LaTeX fragments images removed from buffer")
  17455. (throw 'exit nil))
  17456. (setq msg "Creating images for buffer...")))
  17457. ((equal arg '(4))
  17458. (org-with-limited-levels (org-back-to-heading t))
  17459. (let ((beg (point))
  17460. (end (progn (org-end-of-subtree t) (point))))
  17461. (if (org-remove-latex-fragment-image-overlays beg end)
  17462. (progn
  17463. (message "LaTeX fragment images removed from subtree")
  17464. (throw 'exit nil))
  17465. (setq msg "Creating images for subtree...")
  17466. (narrow-to-region beg end))))
  17467. ((let ((datum (org-element-context)))
  17468. (when (memq (org-element-type datum)
  17469. '(latex-environment latex-fragment))
  17470. (let* ((beg (org-element-property :begin datum))
  17471. (end (org-element-property :end datum)))
  17472. (if (org-remove-latex-fragment-image-overlays beg end)
  17473. (progn (message "LaTeX fragment image removed")
  17474. (throw 'exit nil))
  17475. (narrow-to-region beg end)
  17476. (setq msg "Creating image..."))))))
  17477. (t
  17478. (org-with-limited-levels
  17479. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17480. (outline-previous-heading)
  17481. (point)))
  17482. (end (progn (outline-next-heading) (point))))
  17483. (if (org-remove-latex-fragment-image-overlays beg end)
  17484. (progn
  17485. (message "LaTeX fragment images removed from section")
  17486. (throw 'exit nil))
  17487. (setq msg "Creating images for section...")
  17488. (narrow-to-region beg end))))))
  17489. (let ((file (buffer-file-name (buffer-base-buffer))))
  17490. (org-format-latex
  17491. (concat org-latex-preview-ltxpng-directory
  17492. (file-name-sans-extension (file-name-nondirectory file)))
  17493. ;; Emacs cannot overlay images from remote hosts.
  17494. ;; Create it in `temporary-file-directory' instead.
  17495. (if (file-remote-p file) temporary-file-directory
  17496. default-directory)
  17497. 'overlays msg 'forbuffer
  17498. org-latex-create-formula-image-program)))
  17499. ;; Work around a bug that doesn't restore window's start
  17500. ;; when widening back the buffer.
  17501. (set-window-start nil window-start)
  17502. (message (concat msg "done")))))))
  17503. (defun org-format-latex
  17504. (prefix &optional dir overlays msg forbuffer processing-type)
  17505. "Replace LaTeX fragments with links to an image, and produce images.
  17506. When optional argument OVERLAYS is non-nil, display the image on
  17507. top of the fragment instead of replacing it.
  17508. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17509. Some of the options can be changed using the variable
  17510. `org-format-latex-options', which see."
  17511. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17512. (unless (eq processing-type 'verbatim)
  17513. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17514. (cnt 0)
  17515. checkdir-flag)
  17516. (goto-char (point-min))
  17517. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17518. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17519. (overlay-recenter (point-max)))
  17520. (while (re-search-forward math-regexp nil t)
  17521. (unless (and overlays
  17522. (eq (get-char-property (point) 'org-overlay-type)
  17523. 'org-latex-overlay))
  17524. (let* ((context (org-element-context))
  17525. (type (org-element-type context)))
  17526. (when (memq type '(latex-environment latex-fragment))
  17527. (let ((block-type (eq type 'latex-environment))
  17528. (value (org-element-property :value context))
  17529. (beg (org-element-property :begin context))
  17530. (end (save-excursion
  17531. (goto-char (org-element-property :end context))
  17532. (skip-chars-backward " \r\t\n")
  17533. (point))))
  17534. (case processing-type
  17535. (mathjax
  17536. ;; Prepare for MathJax processing.
  17537. (if (not (string-match "\\`\\$\\$?" value))
  17538. (goto-char end)
  17539. (delete-region beg end)
  17540. (if (string= (match-string 0 value) "$$")
  17541. (insert "\\[" (substring value 2 -2) "\\]")
  17542. (insert "\\(" (substring value 1 -1) "\\)"))))
  17543. ((dvipng imagemagick)
  17544. ;; Process to an image.
  17545. (incf cnt)
  17546. (goto-char beg)
  17547. (let* ((face (face-at-point))
  17548. ;; Get the colors from the face at point.
  17549. (fg
  17550. (let ((color (plist-get org-format-latex-options
  17551. :foreground)))
  17552. (if (and forbuffer (eq color 'auto))
  17553. (face-attribute face :foreground nil 'default)
  17554. color)))
  17555. (bg
  17556. (let ((color (plist-get org-format-latex-options
  17557. :background)))
  17558. (if (and forbuffer (eq color 'auto))
  17559. (face-attribute face :background nil 'default)
  17560. color)))
  17561. (hash (sha1 (prin1-to-string
  17562. (list org-format-latex-header
  17563. org-latex-default-packages-alist
  17564. org-latex-packages-alist
  17565. org-format-latex-options
  17566. forbuffer value fg bg))))
  17567. (absprefix (expand-file-name prefix dir))
  17568. (linkfile (format "%s_%s.png" prefix hash))
  17569. (movefile (format "%s_%s.png" absprefix hash))
  17570. (sep (and block-type "\n\n"))
  17571. (link (concat sep "[[file:" linkfile "]]" sep))
  17572. (options
  17573. (org-combine-plists
  17574. org-format-latex-options
  17575. `(:foreground ,fg :background ,bg))))
  17576. (when msg (message msg cnt))
  17577. (unless checkdir-flag ; Ensure the directory exists.
  17578. (setq checkdir-flag t)
  17579. (let ((todir (file-name-directory absprefix)))
  17580. (unless (file-directory-p todir)
  17581. (make-directory todir t))))
  17582. (unless (file-exists-p movefile)
  17583. (org-create-formula-image
  17584. value movefile options forbuffer processing-type))
  17585. (if overlays
  17586. (progn
  17587. (dolist (o (overlays-in beg end))
  17588. (when (eq (overlay-get o 'org-overlay-type)
  17589. 'org-latex-overlay)
  17590. (delete-overlay o)))
  17591. (org--format-latex-make-overlay beg end movefile)
  17592. (goto-char end))
  17593. (delete-region beg end)
  17594. (insert
  17595. (org-add-props link
  17596. (list 'org-latex-src
  17597. (replace-regexp-in-string "\"" "" value)
  17598. 'org-latex-src-embed-type
  17599. (if block-type 'paragraph 'character)))))))
  17600. (mathml
  17601. ;; Process to MathML.
  17602. (unless (org-format-latex-mathml-available-p)
  17603. (user-error "LaTeX to MathML converter not configured"))
  17604. (incf cnt)
  17605. (when msg (message msg cnt))
  17606. (goto-char beg)
  17607. (delete-region beg end)
  17608. (insert (org-format-latex-as-mathml
  17609. value block-type prefix dir)))
  17610. (otherwise
  17611. (error "Unknown conversion type %s for LaTeX fragments"
  17612. processing-type)))))))))))
  17613. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17614. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17615. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17616. sucessful, return the portion between \"<math...> </math>\"
  17617. elements otherwise return nil. When MATHML-FILE is specified,
  17618. write the results in to that file. When invoked as an
  17619. interactive command, prompt for LATEX-FRAG, with initial value
  17620. set to the current active region and echo the results for user
  17621. inspection."
  17622. (interactive (list (let ((frag (when (org-region-active-p)
  17623. (buffer-substring-no-properties
  17624. (region-beginning) (region-end)))))
  17625. (read-string "LaTeX Fragment: " frag nil frag))))
  17626. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17627. (let* ((tmp-in-file (file-relative-name
  17628. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17629. (ignore (write-region latex-frag nil tmp-in-file))
  17630. (tmp-out-file (file-relative-name
  17631. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17632. (cmd (format-spec
  17633. org-latex-to-mathml-convert-command
  17634. `((?j . ,(and org-latex-to-mathml-jar-file
  17635. (shell-quote-argument
  17636. (expand-file-name
  17637. org-latex-to-mathml-jar-file))))
  17638. (?I . ,(shell-quote-argument tmp-in-file))
  17639. (?i . ,latex-frag)
  17640. (?o . ,(shell-quote-argument tmp-out-file)))))
  17641. mathml shell-command-output)
  17642. (when (org-called-interactively-p 'any)
  17643. (unless (org-format-latex-mathml-available-p)
  17644. (user-error "LaTeX to MathML converter not configured")))
  17645. (message "Running %s" cmd)
  17646. (setq shell-command-output (shell-command-to-string cmd))
  17647. (setq mathml
  17648. (when (file-readable-p tmp-out-file)
  17649. (with-current-buffer (find-file-noselect tmp-out-file t)
  17650. (goto-char (point-min))
  17651. (when (re-search-forward
  17652. (concat
  17653. (regexp-quote
  17654. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17655. "[^>]*?>"
  17656. "\\(.\\|\n\\)*"
  17657. "</math>")
  17658. nil t)
  17659. (prog1 (match-string 0) (kill-buffer))))))
  17660. (cond
  17661. (mathml
  17662. (setq mathml
  17663. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17664. (when mathml-file
  17665. (write-region mathml nil mathml-file))
  17666. (when (org-called-interactively-p 'any)
  17667. (message mathml)))
  17668. ((message "LaTeX to MathML conversion failed")
  17669. (message shell-command-output)))
  17670. (delete-file tmp-in-file)
  17671. (when (file-exists-p tmp-out-file)
  17672. (delete-file tmp-out-file))
  17673. mathml))
  17674. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17675. prefix &optional dir)
  17676. "Use `org-create-math-formula' but check local cache first."
  17677. (let* ((absprefix (expand-file-name prefix dir))
  17678. (print-length nil) (print-level nil)
  17679. (formula-id (concat
  17680. "formula-"
  17681. (sha1
  17682. (prin1-to-string
  17683. (list latex-frag
  17684. org-latex-to-mathml-convert-command)))))
  17685. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17686. (formula-cache-dir (file-name-directory formula-cache)))
  17687. (unless (file-directory-p formula-cache-dir)
  17688. (make-directory formula-cache-dir t))
  17689. (unless (file-exists-p formula-cache)
  17690. (org-create-math-formula latex-frag formula-cache))
  17691. (if (file-exists-p formula-cache)
  17692. ;; Successful conversion. Return the link to MathML file.
  17693. (org-add-props
  17694. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17695. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17696. 'org-latex-src-embed-type (if latex-frag-type
  17697. 'paragraph 'character)))
  17698. ;; Failed conversion. Return the LaTeX fragment verbatim
  17699. latex-frag)))
  17700. (defun org-create-formula-image (string tofile options buffer &optional type)
  17701. "Create an image from LaTeX source using dvipng or convert.
  17702. This function calls either `org-create-formula-image-with-dvipng'
  17703. or `org-create-formula-image-with-imagemagick' depending on the
  17704. value of `org-latex-create-formula-image-program' or on the value
  17705. of the optional TYPE variable.
  17706. Note: ultimately these two function should be combined as they
  17707. share a good deal of logic."
  17708. (org-check-external-command
  17709. "latex" "needed to convert LaTeX fragments to images")
  17710. (funcall
  17711. (case (or type org-latex-create-formula-image-program)
  17712. (dvipng
  17713. (org-check-external-command
  17714. "dvipng" "needed to convert LaTeX fragments to images")
  17715. #'org-create-formula-image-with-dvipng)
  17716. (imagemagick
  17717. (org-check-external-command
  17718. "convert" "you need to install imagemagick")
  17719. #'org-create-formula-image-with-imagemagick)
  17720. (t (error
  17721. "Invalid value of `org-latex-create-formula-image-program'")))
  17722. string tofile options buffer))
  17723. (declare-function org-export-get-backend "ox" (name))
  17724. (declare-function org-export--get-global-options "ox" (&optional backend))
  17725. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17726. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17727. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17728. (defun org-create-formula--latex-header ()
  17729. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17730. (let ((info (org-combine-plists (org-export--get-global-options
  17731. (org-export-get-backend 'latex))
  17732. (org-export--get-inbuffer-options
  17733. (org-export-get-backend 'latex)))))
  17734. (org-latex-guess-babel-language
  17735. (org-latex-guess-inputenc
  17736. (org-splice-latex-header
  17737. org-format-latex-header
  17738. org-latex-default-packages-alist
  17739. org-latex-packages-alist t
  17740. (plist-get info :latex-header)))
  17741. info)))
  17742. (defun org--get-display-dpi ()
  17743. "Get the DPI of the display.
  17744. Assumes that the display has the same pixel width in the
  17745. horizontal and vertical directions."
  17746. (if (display-graphic-p)
  17747. (round (/ (display-pixel-height)
  17748. (/ (display-mm-height) 25.4)))
  17749. (error "Attempt to calculate the dpi of a non-graphic display")))
  17750. ;; This function borrows from Ganesh Swami's latex2png.el
  17751. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17752. "This calls dvipng."
  17753. (require 'ox-latex)
  17754. (let* ((tmpdir (if (featurep 'xemacs)
  17755. (temp-directory)
  17756. temporary-file-directory))
  17757. (texfilebase (make-temp-name
  17758. (expand-file-name "orgtex" tmpdir)))
  17759. (texfile (concat texfilebase ".tex"))
  17760. (dvifile (concat texfilebase ".dvi"))
  17761. (pngfile (concat texfilebase ".png"))
  17762. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17763. ;; This assumes that the display has the same pixel width in
  17764. ;; the horizontal and vertical directions
  17765. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17766. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17767. "Black"))
  17768. (bg (or (plist-get options (if buffer :background :html-background))
  17769. "Transparent")))
  17770. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17771. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17772. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17773. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17774. (let ((latex-header (org-create-formula--latex-header)))
  17775. (with-temp-file texfile
  17776. (insert latex-header)
  17777. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17778. (let ((dir default-directory))
  17779. (ignore-errors
  17780. (cd tmpdir)
  17781. (call-process "latex" nil nil nil texfile))
  17782. (cd dir))
  17783. (if (not (file-exists-p dvifile))
  17784. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17785. (ignore-errors
  17786. (if (featurep 'xemacs)
  17787. (call-process "dvipng" nil nil nil
  17788. "-fg" fg "-bg" bg
  17789. "-T" "tight"
  17790. "-o" pngfile
  17791. dvifile)
  17792. (call-process "dvipng" nil nil nil
  17793. "-fg" fg "-bg" bg
  17794. "-D" dpi
  17795. ;;"-x" scale "-y" scale
  17796. "-T" "tight"
  17797. "-o" pngfile
  17798. dvifile)))
  17799. (if (not (file-exists-p pngfile))
  17800. (if org-format-latex-signal-error
  17801. (error "Failed to create png file from %s" texfile)
  17802. (message "Failed to create png file from %s" texfile)
  17803. nil)
  17804. ;; Use the requested file name and clean up
  17805. (copy-file pngfile tofile 'replace)
  17806. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17807. (if (file-exists-p (concat texfilebase e))
  17808. (delete-file (concat texfilebase e))))
  17809. pngfile))))
  17810. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17811. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17812. "This calls convert, which is included into imagemagick."
  17813. (require 'ox-latex)
  17814. (let* ((tmpdir (if (featurep 'xemacs)
  17815. (temp-directory)
  17816. temporary-file-directory))
  17817. (texfilebase (make-temp-name
  17818. (expand-file-name "orgtex" tmpdir)))
  17819. (texfile (concat texfilebase ".tex"))
  17820. (pdffile (concat texfilebase ".pdf"))
  17821. (pngfile (concat texfilebase ".png"))
  17822. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17823. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17824. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17825. "black"))
  17826. (bg (or (plist-get options (if buffer :background :html-background))
  17827. "white")))
  17828. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17829. (setq fg (org-latex-color-format fg)))
  17830. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17831. (setq bg (org-latex-color-format
  17832. (if (string= bg "Transparent") "white" bg))))
  17833. (let ((latex-header (org-create-formula--latex-header)))
  17834. (with-temp-file texfile
  17835. (insert latex-header)
  17836. (insert "\n\\begin{document}\n"
  17837. "\\definecolor{fg}{rgb}{" fg "}\n"
  17838. "\\definecolor{bg}{rgb}{" bg "}\n"
  17839. "\n\\pagecolor{bg}\n"
  17840. "\n{\\color{fg}\n"
  17841. string
  17842. "\n}\n"
  17843. "\n\\end{document}\n")))
  17844. (org-latex-compile texfile t)
  17845. (if (not (file-exists-p pdffile))
  17846. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17847. (ignore-errors
  17848. (if (featurep 'xemacs)
  17849. (call-process "convert" nil nil nil
  17850. "-density" "96"
  17851. "-trim"
  17852. "-antialias"
  17853. pdffile
  17854. "-quality" "100"
  17855. ;; "-sharpen" "0x1.0"
  17856. pngfile)
  17857. (call-process "convert" nil nil nil
  17858. "-density" dpi
  17859. "-trim"
  17860. "-antialias"
  17861. pdffile
  17862. "-quality" "100"
  17863. ;; "-sharpen" "0x1.0"
  17864. pngfile)))
  17865. (if (not (file-exists-p pngfile))
  17866. (if org-format-latex-signal-error
  17867. (error "Failed to create png file from %s" texfile)
  17868. (message "Failed to create png file from %s" texfile)
  17869. nil)
  17870. ;; Use the requested file name and clean up
  17871. (copy-file pngfile tofile 'replace)
  17872. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17873. (if (file-exists-p (concat texfilebase e))
  17874. (delete-file (concat texfilebase e))))
  17875. pngfile))))
  17876. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17877. "Fill a LaTeX header template TPL.
  17878. In the template, the following place holders will be recognized:
  17879. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17880. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17881. [PACKAGES] \\usepackage statements for PKG
  17882. [NO-PACKAGES] do not include PKG
  17883. [EXTRA] the string EXTRA
  17884. [NO-EXTRA] do not include EXTRA
  17885. For backward compatibility, if both the positive and the negative place
  17886. holder is missing, the positive one (without the \"NO-\") will be
  17887. assumed to be present at the end of the template.
  17888. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17889. EXTRA is a string.
  17890. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17891. (let (rpl (end ""))
  17892. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17893. (setq rpl (if (or (match-end 1) (not def-pkg))
  17894. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17895. tpl (replace-match rpl t t tpl))
  17896. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17897. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17898. (setq rpl (if (or (match-end 1) (not pkg))
  17899. "" (org-latex-packages-to-string pkg snippets-p t))
  17900. tpl (replace-match rpl t t tpl))
  17901. (if pkg (setq end
  17902. (concat end "\n"
  17903. (org-latex-packages-to-string pkg snippets-p)))))
  17904. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17905. (setq rpl (if (or (match-end 1) (not extra))
  17906. "" (concat extra "\n"))
  17907. tpl (replace-match rpl t t tpl))
  17908. (if (and extra (string-match "\\S-" extra))
  17909. (setq end (concat end "\n" extra))))
  17910. (if (string-match "\\S-" end)
  17911. (concat tpl "\n" end)
  17912. tpl)))
  17913. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17914. "Turn an alist of packages into a string with the \\usepackage macros."
  17915. (setq pkg (mapconcat (lambda(p)
  17916. (cond
  17917. ((stringp p) p)
  17918. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17919. (format "%% Package %s omitted" (cadr p)))
  17920. ((equal "" (car p))
  17921. (format "\\usepackage{%s}" (cadr p)))
  17922. (t
  17923. (format "\\usepackage[%s]{%s}"
  17924. (car p) (cadr p)))))
  17925. pkg
  17926. "\n"))
  17927. (if newline (concat pkg "\n") pkg))
  17928. (defun org-dvipng-color (attr)
  17929. "Return a RGB color specification for dvipng."
  17930. (apply 'format "rgb %s %s %s"
  17931. (mapcar 'org-normalize-color
  17932. (if (featurep 'xemacs)
  17933. (color-rgb-components
  17934. (face-property 'default
  17935. (cond ((eq attr :foreground) 'foreground)
  17936. ((eq attr :background) 'background))))
  17937. (color-values (face-attribute 'default attr nil))))))
  17938. (defun org-dvipng-color-format (color-name)
  17939. "Convert COLOR-NAME to a RGB color value for dvipng."
  17940. (apply 'format "rgb %s %s %s"
  17941. (mapcar 'org-normalize-color
  17942. (color-values color-name))))
  17943. (defun org-latex-color (attr)
  17944. "Return a RGB color for the LaTeX color package."
  17945. (apply 'format "%s,%s,%s"
  17946. (mapcar 'org-normalize-color
  17947. (if (featurep 'xemacs)
  17948. (color-rgb-components
  17949. (face-property 'default
  17950. (cond ((eq attr :foreground) 'foreground)
  17951. ((eq attr :background) 'background))))
  17952. (color-values (face-attribute 'default attr nil))))))
  17953. (defun org-latex-color-format (color-name)
  17954. "Convert COLOR-NAME to a RGB color value."
  17955. (apply 'format "%s,%s,%s"
  17956. (mapcar 'org-normalize-color
  17957. (color-values color-name))))
  17958. (defun org-normalize-color (value)
  17959. "Return string to be used as color value for an RGB component."
  17960. (format "%g" (/ value 65535.0)))
  17961. ;; Image display
  17962. (defvar org-inline-image-overlays nil)
  17963. (make-variable-buffer-local 'org-inline-image-overlays)
  17964. (defun org-toggle-inline-images (&optional include-linked)
  17965. "Toggle the display of inline images.
  17966. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17967. (interactive "P")
  17968. (if org-inline-image-overlays
  17969. (progn
  17970. (org-remove-inline-images)
  17971. (when (org-called-interactively-p 'interactive)
  17972. (message "Inline image display turned off")))
  17973. (org-display-inline-images include-linked)
  17974. (when (org-called-interactively-p 'interactive)
  17975. (message (if org-inline-image-overlays
  17976. (format "%d images displayed inline"
  17977. (length org-inline-image-overlays))
  17978. "No images to display inline")))))
  17979. (defun org-redisplay-inline-images ()
  17980. "Refresh the display of inline images."
  17981. (interactive)
  17982. (if (not org-inline-image-overlays)
  17983. (org-toggle-inline-images)
  17984. (org-toggle-inline-images)
  17985. (org-toggle-inline-images)))
  17986. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17987. "Display inline images.
  17988. An inline image is a link which follows either of these
  17989. conventions:
  17990. 1. Its path is a file with an extension matching return value
  17991. from `image-file-name-regexp' and it has no contents.
  17992. 2. Its description consists in a single link of the previous
  17993. type.
  17994. When optional argument INCLUDE-LINKED is non-nil, also links with
  17995. a text description part will be inlined. This can be nice for
  17996. a quick look at those images, but it does not reflect what
  17997. exported files will look like.
  17998. When optional argument REFRESH is non-nil, refresh existing
  17999. images between BEG and END. This will create new image displays
  18000. only if necessary. BEG and END default to the buffer
  18001. boundaries."
  18002. (interactive "P")
  18003. (when (display-graphic-p)
  18004. (unless refresh
  18005. (org-remove-inline-images)
  18006. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  18007. (org-with-wide-buffer
  18008. (goto-char (or beg (point-min)))
  18009. (let ((case-fold-search t)
  18010. (file-extension-re (org-image-file-name-regexp)))
  18011. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  18012. (let ((link (save-match-data (org-element-context))))
  18013. ;; Check if we're at an inline image.
  18014. (when (and (equal (org-element-property :type link) "file")
  18015. (or include-linked
  18016. (not (org-element-property :contents-begin link)))
  18017. (let ((parent (org-element-property :parent link)))
  18018. (or (not (eq (org-element-type parent) 'link))
  18019. (not (cdr (org-element-contents parent)))))
  18020. (org-string-match-p file-extension-re
  18021. (org-element-property :path link)))
  18022. (let ((file (expand-file-name
  18023. (org-link-unescape
  18024. (org-element-property :path link)))))
  18025. (when (file-exists-p file)
  18026. (let ((width
  18027. ;; Apply `org-image-actual-width' specifications.
  18028. (cond
  18029. ((not (image-type-available-p 'imagemagick)) nil)
  18030. ((eq org-image-actual-width t) nil)
  18031. ((listp org-image-actual-width)
  18032. (or
  18033. ;; First try to find a width among
  18034. ;; attributes associated to the paragraph
  18035. ;; containing link.
  18036. (let ((paragraph
  18037. (let ((e link))
  18038. (while (and (setq e (org-element-property
  18039. :parent e))
  18040. (not (eq (org-element-type e)
  18041. 'paragraph))))
  18042. e)))
  18043. (when paragraph
  18044. (save-excursion
  18045. (goto-char (org-element-property :begin paragraph))
  18046. (when
  18047. (re-search-forward
  18048. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18049. (org-element-property
  18050. :post-affiliated paragraph)
  18051. t)
  18052. (string-to-number (match-string 1))))))
  18053. ;; Otherwise, fall-back to provided number.
  18054. (car org-image-actual-width)))
  18055. ((numberp org-image-actual-width)
  18056. org-image-actual-width)))
  18057. (old (get-char-property-and-overlay
  18058. (org-element-property :begin link)
  18059. 'org-image-overlay)))
  18060. (if (and (car-safe old) refresh)
  18061. (image-refresh (overlay-get (cdr old) 'display))
  18062. (let ((image (create-image file
  18063. (and width 'imagemagick)
  18064. nil
  18065. :width width)))
  18066. (when image
  18067. (let* ((link
  18068. ;; If inline image is the description
  18069. ;; of another link, be sure to
  18070. ;; consider the latter as the one to
  18071. ;; apply the overlay on.
  18072. (let ((parent
  18073. (org-element-property :parent link)))
  18074. (if (eq (org-element-type parent) 'link)
  18075. parent
  18076. link)))
  18077. (ov (make-overlay
  18078. (org-element-property :begin link)
  18079. (progn
  18080. (goto-char
  18081. (org-element-property :end link))
  18082. (skip-chars-backward " \t")
  18083. (point)))))
  18084. (overlay-put ov 'display image)
  18085. (overlay-put ov 'face 'default)
  18086. (overlay-put ov 'org-image-overlay t)
  18087. (overlay-put
  18088. ov 'modification-hooks
  18089. (list 'org-display-inline-remove-overlay))
  18090. (push ov org-inline-image-overlays)))))))))))))))
  18091. (define-obsolete-function-alias
  18092. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18093. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18094. "Remove inline-display overlay if a corresponding region is modified."
  18095. (let ((inhibit-modification-hooks t))
  18096. (when (and ov after)
  18097. (delete ov org-inline-image-overlays)
  18098. (delete-overlay ov))))
  18099. (defun org-remove-inline-images ()
  18100. "Remove inline display of images."
  18101. (interactive)
  18102. (mapc 'delete-overlay org-inline-image-overlays)
  18103. (setq org-inline-image-overlays nil))
  18104. ;;;; Key bindings
  18105. ;; Outline functions from `outline-mode-prefix-map'
  18106. ;; that can be remapped in Org:
  18107. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18108. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18109. (define-key org-mode-map [remap outline-forward-same-level]
  18110. 'org-forward-heading-same-level)
  18111. (define-key org-mode-map [remap outline-backward-same-level]
  18112. 'org-backward-heading-same-level)
  18113. (define-key org-mode-map [remap outline-show-branches]
  18114. 'org-kill-note-or-show-branches)
  18115. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18116. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18117. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18118. (define-key org-mode-map [remap outline-next-visible-heading]
  18119. 'org-next-visible-heading)
  18120. (define-key org-mode-map [remap outline-previous-visible-heading]
  18121. 'org-previous-visible-heading)
  18122. ;; Outline functions from `outline-mode-prefix-map' that can not
  18123. ;; be remapped in Org:
  18124. ;; - the column "key binding" shows whether the Outline function is still
  18125. ;; available in Org mode on the same key that it has been bound to in
  18126. ;; Outline mode:
  18127. ;; - "overridden": key used for a different functionality in Org mode
  18128. ;; - else: key still bound to the same Outline function in Org mode
  18129. ;; | Outline function | key binding | Org replacement |
  18130. ;; |------------------------------------+-------------+--------------------------|
  18131. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18132. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18133. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18134. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18135. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18136. ;; | `show-entry' | overridden | no replacement |
  18137. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18138. ;; | `show-branches' | `C-c C-k' | still same function |
  18139. ;; | `show-subtree' | overridden | visibility cycling |
  18140. ;; | `show-all' | overridden | no replacement |
  18141. ;; | `hide-subtree' | overridden | visibility cycling |
  18142. ;; | `hide-body' | overridden | no replacement |
  18143. ;; | `hide-entry' | overridden | visibility cycling |
  18144. ;; | `hide-leaves' | overridden | no replacement |
  18145. ;; | `hide-sublevels' | overridden | no replacement |
  18146. ;; | `hide-other' | overridden | no replacement |
  18147. ;; Make `C-c C-x' a prefix key
  18148. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18149. ;; TAB key with modifiers
  18150. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18151. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18152. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18153. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18154. ;; The following line is necessary under Suse GNU/Linux
  18155. (unless (featurep 'xemacs)
  18156. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18157. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18158. (define-key org-mode-map [backtab] 'org-shifttab)
  18159. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18160. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18161. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18162. ;; Cursor keys with modifiers
  18163. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18164. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18165. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18166. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18167. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18168. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18169. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18170. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18171. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18172. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18173. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18174. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18175. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18176. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18177. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18178. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18179. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18180. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18181. ;; Babel keys
  18182. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18183. (mapc (lambda (pair)
  18184. (define-key org-babel-map (car pair) (cdr pair)))
  18185. org-babel-key-bindings)
  18186. ;;; Extra keys for tty access.
  18187. ;; We only set them when really needed because otherwise the
  18188. ;; menus don't show the simple keys
  18189. (when (or org-use-extra-keys
  18190. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18191. (not window-system))
  18192. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18193. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18194. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18195. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18196. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18197. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18198. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18199. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18200. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18201. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18202. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18203. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18204. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18205. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18206. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18207. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18208. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18209. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18210. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18211. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18212. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18213. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18214. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18215. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18216. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18217. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18218. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18219. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18220. ;; All the other keys
  18221. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18222. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18223. (if (boundp 'narrow-map)
  18224. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18225. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18226. (if (boundp 'narrow-map)
  18227. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18228. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18229. (if (boundp 'narrow-map)
  18230. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18231. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18232. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18233. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18234. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18235. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18236. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18237. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18238. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18239. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18240. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18241. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18242. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18243. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18244. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18245. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18246. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18247. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18248. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18249. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18250. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18251. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18252. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18253. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18254. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18255. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18256. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18257. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18258. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18259. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18260. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18261. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18262. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18263. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18264. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18265. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18266. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18267. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18268. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18269. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18270. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18271. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18272. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18273. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18274. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18275. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18276. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18277. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18278. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18279. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18280. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18281. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18282. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18283. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18284. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18285. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18286. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18287. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18288. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18289. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18290. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18291. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18292. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18293. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18294. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18295. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18296. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18297. (org-defkey org-mode-map "\C-m" 'org-return)
  18298. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18299. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18300. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18301. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18302. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18303. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18304. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18305. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18306. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18307. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18308. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18309. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18310. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18311. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18312. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18313. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18314. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18315. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18316. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18317. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18318. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18319. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18320. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18321. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18322. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18323. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18324. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18325. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18326. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18327. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18328. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18329. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18330. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18331. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18332. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18333. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18334. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18335. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18336. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18337. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18338. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18339. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18340. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18341. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18342. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18343. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18344. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18345. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18346. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18347. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18348. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18349. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18350. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18351. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18352. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18353. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18354. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18355. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18356. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18357. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18358. (when (featurep 'xemacs)
  18359. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18360. (defconst org-speed-commands-default
  18361. '(
  18362. ("Outline Navigation")
  18363. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18364. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18365. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18366. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18367. ("F" . org-next-block)
  18368. ("B" . org-previous-block)
  18369. ("u" . (org-speed-move-safe 'outline-up-heading))
  18370. ("j" . org-goto)
  18371. ("g" . (org-refile t))
  18372. ("Outline Visibility")
  18373. ("c" . org-cycle)
  18374. ("C" . org-shifttab)
  18375. (" " . org-display-outline-path)
  18376. ("s" . org-narrow-to-subtree)
  18377. ("=" . org-columns)
  18378. ("Outline Structure Editing")
  18379. ("U" . org-metaup)
  18380. ("D" . org-metadown)
  18381. ("r" . org-metaright)
  18382. ("l" . org-metaleft)
  18383. ("R" . org-shiftmetaright)
  18384. ("L" . org-shiftmetaleft)
  18385. ("i" . (progn (forward-char 1) (call-interactively
  18386. 'org-insert-heading-respect-content)))
  18387. ("^" . org-sort)
  18388. ("w" . org-refile)
  18389. ("a" . org-archive-subtree-default-with-confirmation)
  18390. ("@" . org-mark-subtree)
  18391. ("#" . org-toggle-comment)
  18392. ("Clock Commands")
  18393. ("I" . org-clock-in)
  18394. ("O" . org-clock-out)
  18395. ("Meta Data Editing")
  18396. ("t" . org-todo)
  18397. ("," . (org-priority))
  18398. ("0" . (org-priority ?\ ))
  18399. ("1" . (org-priority ?A))
  18400. ("2" . (org-priority ?B))
  18401. ("3" . (org-priority ?C))
  18402. (":" . org-set-tags-command)
  18403. ("e" . org-set-effort)
  18404. ("E" . org-inc-effort)
  18405. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18406. (org-entry-put (point) "APPT_WARNTIME" m)))
  18407. ("Agenda Views etc")
  18408. ("v" . org-agenda)
  18409. ("/" . org-sparse-tree)
  18410. ("Misc")
  18411. ("o" . org-open-at-point)
  18412. ("?" . org-speed-command-help)
  18413. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18414. (">" . (org-agenda-remove-restriction-lock))
  18415. )
  18416. "The default speed commands.")
  18417. (defun org-print-speed-command (e)
  18418. (if (> (length (car e)) 1)
  18419. (progn
  18420. (princ "\n")
  18421. (princ (car e))
  18422. (princ "\n")
  18423. (princ (make-string (length (car e)) ?-))
  18424. (princ "\n"))
  18425. (princ (car e))
  18426. (princ " ")
  18427. (if (symbolp (cdr e))
  18428. (princ (symbol-name (cdr e)))
  18429. (prin1 (cdr e)))
  18430. (princ "\n")))
  18431. (defun org-speed-command-help ()
  18432. "Show the available speed commands."
  18433. (interactive)
  18434. (if (not org-use-speed-commands)
  18435. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18436. (with-output-to-temp-buffer "*Help*"
  18437. (princ "User-defined Speed commands\n===========================\n")
  18438. (mapc 'org-print-speed-command org-speed-commands-user)
  18439. (princ "\n")
  18440. (princ "Built-in Speed commands\n=======================\n")
  18441. (mapc 'org-print-speed-command org-speed-commands-default))
  18442. (with-current-buffer "*Help*"
  18443. (setq truncate-lines t))))
  18444. (defun org-speed-move-safe (cmd)
  18445. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18446. If not, return to the original position and throw an error."
  18447. (interactive)
  18448. (let ((pos (point)))
  18449. (call-interactively cmd)
  18450. (unless (and (bolp) (org-at-heading-p))
  18451. (goto-char pos)
  18452. (error "Boundary reached while executing %s" cmd))))
  18453. (defvar org-self-insert-command-undo-counter 0)
  18454. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18455. (defvar org-speed-command nil)
  18456. (define-obsolete-function-alias
  18457. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18458. (defun org-speed-command-activate (keys)
  18459. "Hook for activating single-letter speed commands.
  18460. `org-speed-commands-default' specifies a minimal command set.
  18461. Use `org-speed-commands-user' for further customization."
  18462. (when (or (and (bolp) (looking-at org-outline-regexp))
  18463. (and (functionp org-use-speed-commands)
  18464. (funcall org-use-speed-commands)))
  18465. (cdr (assoc keys (append org-speed-commands-user
  18466. org-speed-commands-default)))))
  18467. (define-obsolete-function-alias
  18468. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18469. (defun org-babel-speed-command-activate (keys)
  18470. "Hook for activating single-letter code block commands."
  18471. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18472. (cdr (assoc keys org-babel-key-bindings))))
  18473. (defcustom org-speed-command-hook
  18474. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18475. "Hook for activating speed commands at strategic locations.
  18476. Hook functions are called in sequence until a valid handler is
  18477. found.
  18478. Each hook takes a single argument, a user-pressed command key
  18479. which is also a `self-insert-command' from the global map.
  18480. Within the hook, examine the cursor position and the command key
  18481. and return nil or a valid handler as appropriate. Handler could
  18482. be one of an interactive command, a function, or a form.
  18483. Set `org-use-speed-commands' to non-nil value to enable this
  18484. hook. The default setting is `org-speed-command-activate'."
  18485. :group 'org-structure
  18486. :version "24.1"
  18487. :type 'hook)
  18488. (defun org-self-insert-command (N)
  18489. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18490. If the cursor is in a table looking at whitespace, the whitespace is
  18491. overwritten, and the table is not marked as requiring realignment."
  18492. (interactive "p")
  18493. (org-check-before-invisible-edit 'insert)
  18494. (cond
  18495. ((and org-use-speed-commands
  18496. (let ((kv (this-command-keys-vector)))
  18497. (setq org-speed-command
  18498. (run-hook-with-args-until-success
  18499. 'org-speed-command-hook
  18500. (make-string 1 (aref kv (1- (length kv))))))))
  18501. (cond
  18502. ((commandp org-speed-command)
  18503. (setq this-command org-speed-command)
  18504. (call-interactively org-speed-command))
  18505. ((functionp org-speed-command)
  18506. (funcall org-speed-command))
  18507. ((and org-speed-command (listp org-speed-command))
  18508. (eval org-speed-command))
  18509. (t (let (org-use-speed-commands)
  18510. (call-interactively 'org-self-insert-command)))))
  18511. ((and
  18512. (org-table-p)
  18513. (progn
  18514. ;; Check if we blank the field, and if that triggers align.
  18515. (and (featurep 'org-table) org-table-auto-blank-field
  18516. (memq last-command
  18517. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18518. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18519. ;; Got extra space, this field does not determine
  18520. ;; column width.
  18521. (let (org-table-may-need-update) (org-table-blank-field))
  18522. ;; No extra space, this field may determine column
  18523. ;; width.
  18524. (org-table-blank-field)))
  18525. t)
  18526. (eq N 1)
  18527. (looking-at "[^|\n]* \\( \\)|"))
  18528. ;; There is room for insertion without re-aligning the table.
  18529. (delete-region (match-beginning 1) (match-end 1))
  18530. (self-insert-command N))
  18531. (t
  18532. (setq org-table-may-need-update t)
  18533. (self-insert-command N)
  18534. (org-fix-tags-on-the-fly)
  18535. (if org-self-insert-cluster-for-undo
  18536. (if (not (eq last-command 'org-self-insert-command))
  18537. (setq org-self-insert-command-undo-counter 1)
  18538. (if (>= org-self-insert-command-undo-counter 20)
  18539. (setq org-self-insert-command-undo-counter 1)
  18540. (and (> org-self-insert-command-undo-counter 0)
  18541. buffer-undo-list (listp buffer-undo-list)
  18542. (not (cadr buffer-undo-list)) ; remove nil entry
  18543. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18544. (setq org-self-insert-command-undo-counter
  18545. (1+ org-self-insert-command-undo-counter))))))))
  18546. (defun org-check-before-invisible-edit (kind)
  18547. "Check is editing if kind KIND would be dangerous with invisible text around.
  18548. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18549. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18550. (if (and org-catch-invisible-edits
  18551. (or (not (boundp 'visible-mode)) (not visible-mode))
  18552. (or (get-char-property (point) 'invisible)
  18553. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18554. ;; OK, we need to take a closer look
  18555. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18556. (invisible-before-point (if (bobp) nil (get-char-property
  18557. (1- (point)) 'invisible)))
  18558. (border-and-ok-direction
  18559. (or
  18560. ;; Check if we are acting predictably before invisible text
  18561. (and invisible-at-point (not invisible-before-point)
  18562. (memq kind '(insert delete-backward)))
  18563. ;; Check if we are acting predictably after invisible text
  18564. ;; This works not well, and I have turned it off. It seems
  18565. ;; better to always show and stop after invisible text.
  18566. ;; (and (not invisible-at-point) invisible-before-point
  18567. ;; (memq kind '(insert delete)))
  18568. )))
  18569. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18570. (memq invisible-before-point '(outline org-hide-block t)))
  18571. (if (eq org-catch-invisible-edits 'error)
  18572. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18573. (if (and org-custom-properties-overlays
  18574. (y-or-n-p "Display invisible properties in this buffer? "))
  18575. (org-toggle-custom-properties-visibility)
  18576. ;; Make the area visible
  18577. (save-excursion
  18578. (if invisible-before-point
  18579. (goto-char (previous-single-char-property-change
  18580. (point) 'invisible)))
  18581. (outline-show-subtree))
  18582. (cond
  18583. ((eq org-catch-invisible-edits 'show)
  18584. ;; That's it, we do the edit after showing
  18585. (message
  18586. "Unfolding invisible region around point before editing")
  18587. (sit-for 1))
  18588. ((and (eq org-catch-invisible-edits 'smart)
  18589. border-and-ok-direction)
  18590. (message "Unfolding invisible region around point before editing"))
  18591. (t
  18592. ;; Don't do the edit, make the user repeat it in full visibility
  18593. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18594. (defun org-fix-tags-on-the-fly ()
  18595. (when (and (equal (char-after (point-at-bol)) ?*)
  18596. (org-at-heading-p))
  18597. (org-align-tags-here org-tags-column)))
  18598. (defun org-delete-backward-char (N)
  18599. "Like `delete-backward-char', insert whitespace at field end in tables.
  18600. When deleting backwards, in tables this function will insert whitespace in
  18601. front of the next \"|\" separator, to keep the table aligned. The table will
  18602. still be marked for re-alignment if the field did fill the entire column,
  18603. because, in this case the deletion might narrow the column."
  18604. (interactive "p")
  18605. (save-match-data
  18606. (org-check-before-invisible-edit 'delete-backward)
  18607. (if (and (org-table-p)
  18608. (eq N 1)
  18609. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18610. (looking-at ".*?|"))
  18611. (let ((pos (point))
  18612. (noalign (looking-at "[^|\n\r]* |"))
  18613. (c org-table-may-need-update))
  18614. (backward-delete-char N)
  18615. (if (not overwrite-mode)
  18616. (progn
  18617. (skip-chars-forward "^|")
  18618. (insert " ")
  18619. (goto-char (1- pos))))
  18620. ;; noalign: if there were two spaces at the end, this field
  18621. ;; does not determine the width of the column.
  18622. (if noalign (setq org-table-may-need-update c)))
  18623. (backward-delete-char N)
  18624. (org-fix-tags-on-the-fly))))
  18625. (defun org-delete-char (N)
  18626. "Like `delete-char', but insert whitespace at field end in tables.
  18627. When deleting characters, in tables this function will insert whitespace in
  18628. front of the next \"|\" separator, to keep the table aligned. The table will
  18629. still be marked for re-alignment if the field did fill the entire column,
  18630. because, in this case the deletion might narrow the column."
  18631. (interactive "p")
  18632. (save-match-data
  18633. (org-check-before-invisible-edit 'delete)
  18634. (if (and (org-table-p)
  18635. (not (bolp))
  18636. (not (= (char-after) ?|))
  18637. (eq N 1))
  18638. (if (looking-at ".*?|")
  18639. (let ((pos (point))
  18640. (noalign (looking-at "[^|\n\r]* |"))
  18641. (c org-table-may-need-update))
  18642. (replace-match
  18643. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18644. (goto-char pos)
  18645. ;; noalign: if there were two spaces at the end, this field
  18646. ;; does not determine the width of the column.
  18647. (if noalign (setq org-table-may-need-update c)))
  18648. (delete-char N))
  18649. (delete-char N)
  18650. (org-fix-tags-on-the-fly))))
  18651. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18652. (put 'org-self-insert-command 'delete-selection
  18653. (lambda ()
  18654. (not (run-hook-with-args-until-success
  18655. 'self-insert-uses-region-functions))))
  18656. (put 'orgtbl-self-insert-command 'delete-selection
  18657. (lambda ()
  18658. (not (run-hook-with-args-until-success
  18659. 'self-insert-uses-region-functions))))
  18660. (put 'org-delete-char 'delete-selection 'supersede)
  18661. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18662. (put 'org-yank 'delete-selection 'yank)
  18663. ;; Make `flyspell-mode' delay after some commands
  18664. (put 'org-self-insert-command 'flyspell-delayed t)
  18665. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18666. (put 'org-delete-char 'flyspell-delayed t)
  18667. (put 'org-delete-backward-char 'flyspell-delayed t)
  18668. ;; Make pabbrev-mode expand after org-mode commands
  18669. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18670. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18671. (defun org-remap (map &rest commands)
  18672. "In MAP, remap the functions given in COMMANDS.
  18673. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18674. (let (new old)
  18675. (while commands
  18676. (setq old (pop commands) new (pop commands))
  18677. (if (fboundp 'command-remapping)
  18678. (org-defkey map (vector 'remap old) new)
  18679. (substitute-key-definition old new map global-map)))))
  18680. (defun org-transpose-words ()
  18681. "Transpose words for Org.
  18682. This uses the `org-mode-transpose-word-syntax-table' syntax
  18683. table, which interprets characters in `org-emphasis-alist' as
  18684. word constituents."
  18685. (interactive)
  18686. (with-syntax-table org-mode-transpose-word-syntax-table
  18687. (call-interactively 'transpose-words)))
  18688. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18689. (when (eq org-enable-table-editor 'optimized)
  18690. ;; If the user wants maximum table support, we need to hijack
  18691. ;; some standard editing functions
  18692. (org-remap org-mode-map
  18693. 'self-insert-command 'org-self-insert-command
  18694. 'delete-char 'org-delete-char
  18695. 'delete-backward-char 'org-delete-backward-char)
  18696. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18697. (defvar org-ctrl-c-ctrl-c-hook nil
  18698. "Hook for functions attaching themselves to `C-c C-c'.
  18699. This can be used to add additional functionality to the C-c C-c
  18700. key which executes context-dependent commands. This hook is run
  18701. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18702. run after the last test.
  18703. Each function will be called with no arguments. The function
  18704. must check if the context is appropriate for it to act. If yes,
  18705. it should do its thing and then return a non-nil value. If the
  18706. context is wrong, just do nothing and return nil.")
  18707. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18708. "Hook for functions attaching themselves to `C-c C-c'.
  18709. This can be used to add additional functionality to the C-c C-c
  18710. key which executes context-dependent commands. This hook is run
  18711. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18712. before the first test.
  18713. Each function will be called with no arguments. The function
  18714. must check if the context is appropriate for it to act. If yes,
  18715. it should do its thing and then return a non-nil value. If the
  18716. context is wrong, just do nothing and return nil.")
  18717. (defvar org-tab-first-hook nil
  18718. "Hook for functions to attach themselves to TAB.
  18719. See `org-ctrl-c-ctrl-c-hook' for more information.
  18720. This hook runs as the first action when TAB is pressed, even before
  18721. `org-cycle' messes around with the `outline-regexp' to cater for
  18722. inline tasks and plain list item folding.
  18723. If any function in this hook returns t, any other actions that
  18724. would have been caused by TAB (such as table field motion or visibility
  18725. cycling) will not occur.")
  18726. (defvar org-tab-after-check-for-table-hook nil
  18727. "Hook for functions to attach themselves to TAB.
  18728. See `org-ctrl-c-ctrl-c-hook' for more information.
  18729. This hook runs after it has been established that the cursor is not in a
  18730. table, but before checking if the cursor is in a headline or if global cycling
  18731. should be done.
  18732. If any function in this hook returns t, not other actions like visibility
  18733. cycling will be done.")
  18734. (defvar org-tab-after-check-for-cycling-hook nil
  18735. "Hook for functions to attach themselves to TAB.
  18736. See `org-ctrl-c-ctrl-c-hook' for more information.
  18737. This hook runs after it has been established that not table field motion and
  18738. not visibility should be done because of current context. This is probably
  18739. the place where a package like yasnippets can hook in.")
  18740. (defvar org-tab-before-tab-emulation-hook nil
  18741. "Hook for functions to attach themselves to TAB.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.
  18743. This hook runs after every other options for TAB have been exhausted, but
  18744. before indentation and \t insertion takes place.")
  18745. (defvar org-metaleft-hook nil
  18746. "Hook for functions attaching themselves to `M-left'.
  18747. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18748. (defvar org-metaright-hook nil
  18749. "Hook for functions attaching themselves to `M-right'.
  18750. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18751. (defvar org-metaup-hook nil
  18752. "Hook for functions attaching themselves to `M-up'.
  18753. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18754. (defvar org-metadown-hook nil
  18755. "Hook for functions attaching themselves to `M-down'.
  18756. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18757. (defvar org-shiftmetaleft-hook nil
  18758. "Hook for functions attaching themselves to `M-S-left'.
  18759. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18760. (defvar org-shiftmetaright-hook nil
  18761. "Hook for functions attaching themselves to `M-S-right'.
  18762. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18763. (defvar org-shiftmetaup-hook nil
  18764. "Hook for functions attaching themselves to `M-S-up'.
  18765. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18766. (defvar org-shiftmetadown-hook nil
  18767. "Hook for functions attaching themselves to `M-S-down'.
  18768. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18769. (defvar org-metareturn-hook nil
  18770. "Hook for functions attaching themselves to `M-RET'.
  18771. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18772. (defvar org-shiftup-hook nil
  18773. "Hook for functions attaching themselves to `S-up'.
  18774. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18775. (defvar org-shiftup-final-hook nil
  18776. "Hook for functions attaching themselves to `S-up'.
  18777. This one runs after all other options except shift-select have been excluded.
  18778. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18779. (defvar org-shiftdown-hook nil
  18780. "Hook for functions attaching themselves to `S-down'.
  18781. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18782. (defvar org-shiftdown-final-hook nil
  18783. "Hook for functions attaching themselves to `S-down'.
  18784. This one runs after all other options except shift-select have been excluded.
  18785. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18786. (defvar org-shiftleft-hook nil
  18787. "Hook for functions attaching themselves to `S-left'.
  18788. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18789. (defvar org-shiftleft-final-hook nil
  18790. "Hook for functions attaching themselves to `S-left'.
  18791. This one runs after all other options except shift-select have been excluded.
  18792. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18793. (defvar org-shiftright-hook nil
  18794. "Hook for functions attaching themselves to `S-right'.
  18795. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18796. (defvar org-shiftright-final-hook nil
  18797. "Hook for functions attaching themselves to `S-right'.
  18798. This one runs after all other options except shift-select have been excluded.
  18799. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18800. (defun org-modifier-cursor-error ()
  18801. "Throw an error, a modified cursor command was applied in wrong context."
  18802. (user-error "This command is active in special context like tables, headlines or items"))
  18803. (defun org-shiftselect-error ()
  18804. "Throw an error because Shift-Cursor command was applied in wrong context."
  18805. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18806. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18807. (user-error "This command works only in special context like headlines or timestamps")))
  18808. (defun org-call-for-shift-select (cmd)
  18809. (let ((this-command-keys-shift-translated t))
  18810. (call-interactively cmd)))
  18811. (defun org-shifttab (&optional arg)
  18812. "Global visibility cycling or move to previous table field.
  18813. Call `org-table-previous-field' within a table.
  18814. When ARG is nil, cycle globally through visibility states.
  18815. When ARG is a numeric prefix, show contents of this level."
  18816. (interactive "P")
  18817. (cond
  18818. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18819. ((integerp arg)
  18820. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18821. (message "Content view to level: %d" arg)
  18822. (org-content (prefix-numeric-value arg2))
  18823. (org-cycle-show-empty-lines t)
  18824. (setq org-cycle-global-status 'overview)))
  18825. (t (call-interactively 'org-global-cycle))))
  18826. (defun org-shiftmetaleft ()
  18827. "Promote subtree or delete table column.
  18828. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18829. `org-table-delete-column', depending on context. See the
  18830. individual commands for more information."
  18831. (interactive)
  18832. (cond
  18833. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18834. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18835. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18836. ((if (not (org-region-active-p)) (org-at-item-p)
  18837. (save-excursion (goto-char (region-beginning))
  18838. (org-at-item-p)))
  18839. (call-interactively 'org-outdent-item-tree))
  18840. (t (org-modifier-cursor-error))))
  18841. (defun org-shiftmetaright ()
  18842. "Demote subtree or insert table column.
  18843. Calls `org-demote-subtree', `org-indent-item-tree', or
  18844. `org-table-insert-column', depending on context. See the
  18845. individual commands for more information."
  18846. (interactive)
  18847. (cond
  18848. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18849. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18850. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18851. ((if (not (org-region-active-p)) (org-at-item-p)
  18852. (save-excursion (goto-char (region-beginning))
  18853. (org-at-item-p)))
  18854. (call-interactively 'org-indent-item-tree))
  18855. (t (org-modifier-cursor-error))))
  18856. (defun org-shiftmetaup (&optional arg)
  18857. "Drag the line at point up.
  18858. In a table, kill the current row.
  18859. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18860. but also adjust the previous clocked item in the clock history.
  18861. Everywhere else, drag the line at point up."
  18862. (interactive "P")
  18863. (cond
  18864. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18865. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18866. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18867. (call-interactively 'org-timestamp-up)))
  18868. (t (call-interactively 'org-drag-line-backward))))
  18869. (defun org-shiftmetadown (&optional arg)
  18870. "Drag the line at point down.
  18871. In a table, insert an empty row at the current line.
  18872. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18873. but also adjust the previous clocked item in the clock history.
  18874. Everywhere else, drag the line at point down."
  18875. (interactive "P")
  18876. (cond
  18877. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18878. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18879. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18880. (call-interactively 'org-timestamp-down)))
  18881. (t (call-interactively 'org-drag-line-forward))))
  18882. (defsubst org-hidden-tree-error ()
  18883. (user-error
  18884. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18885. (defun org-metaleft (&optional arg)
  18886. "Promote heading, list item at point or move table column left.
  18887. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18888. depending on context. With no specific context, calls the Emacs
  18889. default `backward-word'. See the individual commands for more
  18890. information.
  18891. This function runs the hook `org-metaleft-hook' as a first step,
  18892. and returns at first non-nil value."
  18893. (interactive "P")
  18894. (cond
  18895. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18896. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18897. ((org-with-limited-levels
  18898. (or (org-at-heading-p)
  18899. (and (org-region-active-p)
  18900. (save-excursion
  18901. (goto-char (region-beginning))
  18902. (org-at-heading-p)))))
  18903. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18904. (call-interactively 'org-do-promote))
  18905. ;; At an inline task.
  18906. ((org-at-heading-p)
  18907. (call-interactively 'org-inlinetask-promote))
  18908. ((or (org-at-item-p)
  18909. (and (org-region-active-p)
  18910. (save-excursion
  18911. (goto-char (region-beginning))
  18912. (org-at-item-p))))
  18913. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18914. (call-interactively 'org-outdent-item))
  18915. (t (call-interactively 'backward-word))))
  18916. (defun org-metaright (&optional arg)
  18917. "Demote heading, list item at point or move table column right.
  18918. In front of a drawer or a block keyword, indent it correctly.
  18919. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18920. `org-indnet-drawer' or `org-indent-block' depending on context.
  18921. With no specific context, calls the Emacs default `forward-word'.
  18922. See the individual commands for more information.
  18923. This function runs the hook `org-metaright-hook' as a first step,
  18924. and returns at first non-nil value."
  18925. (interactive "P")
  18926. (cond
  18927. ((run-hook-with-args-until-success 'org-metaright-hook))
  18928. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18929. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18930. ((org-at-block-p) (call-interactively 'org-indent-block))
  18931. ((org-with-limited-levels
  18932. (or (org-at-heading-p)
  18933. (and (org-region-active-p)
  18934. (save-excursion
  18935. (goto-char (region-beginning))
  18936. (org-at-heading-p)))))
  18937. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18938. (call-interactively 'org-do-demote))
  18939. ;; At an inline task.
  18940. ((org-at-heading-p)
  18941. (call-interactively 'org-inlinetask-demote))
  18942. ((or (org-at-item-p)
  18943. (and (org-region-active-p)
  18944. (save-excursion
  18945. (goto-char (region-beginning))
  18946. (org-at-item-p))))
  18947. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18948. (call-interactively 'org-indent-item))
  18949. (t (call-interactively 'forward-word))))
  18950. (defun org-check-for-hidden (what)
  18951. "Check if there are hidden headlines/items in the current visual line.
  18952. WHAT can be either `headlines' or `items'. If the current line is
  18953. an outline or item heading and it has a folded subtree below it,
  18954. this function returns t, nil otherwise."
  18955. (let ((re (cond
  18956. ((eq what 'headlines) org-outline-regexp-bol)
  18957. ((eq what 'items) (org-item-beginning-re))
  18958. (t (error "This should not happen"))))
  18959. beg end)
  18960. (save-excursion
  18961. (catch 'exit
  18962. (unless (org-region-active-p)
  18963. (setq beg (point-at-bol))
  18964. (beginning-of-line 2)
  18965. (while (and (not (eobp)) ;; this is like `next-line'
  18966. (get-char-property (1- (point)) 'invisible))
  18967. (beginning-of-line 2))
  18968. (setq end (point))
  18969. (goto-char beg)
  18970. (goto-char (point-at-eol))
  18971. (setq end (max end (point)))
  18972. (while (re-search-forward re end t)
  18973. (if (get-char-property (match-beginning 0) 'invisible)
  18974. (throw 'exit t))))
  18975. nil))))
  18976. (defun org-metaup (&optional arg)
  18977. "Move subtree up or move table row up.
  18978. Calls `org-move-subtree-up' or `org-table-move-row' or
  18979. `org-move-item-up', depending on context. See the individual commands
  18980. for more information."
  18981. (interactive "P")
  18982. (cond
  18983. ((run-hook-with-args-until-success 'org-metaup-hook))
  18984. ((org-region-active-p)
  18985. (let* ((a (min (region-beginning) (region-end)))
  18986. (b (1- (max (region-beginning) (region-end))))
  18987. (c (save-excursion (goto-char a)
  18988. (move-beginning-of-line 0)))
  18989. (d (save-excursion (goto-char a)
  18990. (move-end-of-line 0) (point))))
  18991. (transpose-regions a b c d)
  18992. (goto-char c)))
  18993. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18994. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18995. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18996. (t (org-drag-element-backward))))
  18997. (defun org-metadown (&optional arg)
  18998. "Move subtree down or move table row down.
  18999. Calls `org-move-subtree-down' or `org-table-move-row' or
  19000. `org-move-item-down', depending on context. See the individual
  19001. commands for more information."
  19002. (interactive "P")
  19003. (cond
  19004. ((run-hook-with-args-until-success 'org-metadown-hook))
  19005. ((org-region-active-p)
  19006. (let* ((a (min (region-beginning) (region-end)))
  19007. (b (max (region-beginning) (region-end)))
  19008. (c (save-excursion (goto-char b)
  19009. (move-beginning-of-line 1)))
  19010. (d (save-excursion (goto-char b)
  19011. (move-end-of-line 1) (1+ (point)))))
  19012. (transpose-regions a b c d)
  19013. (goto-char d)))
  19014. ((org-at-table-p) (call-interactively 'org-table-move-row))
  19015. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  19016. ((org-at-item-p) (call-interactively 'org-move-item-down))
  19017. (t (org-drag-element-forward))))
  19018. (defun org-shiftup (&optional arg)
  19019. "Increase item in timestamp or increase priority of current headline.
  19020. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  19021. depending on context. See the individual commands for more information."
  19022. (interactive "P")
  19023. (cond
  19024. ((run-hook-with-args-until-success 'org-shiftup-hook))
  19025. ((and org-support-shift-select (org-region-active-p))
  19026. (org-call-for-shift-select 'previous-line))
  19027. ((org-at-timestamp-p t)
  19028. (call-interactively (if org-edit-timestamp-down-means-later
  19029. 'org-timestamp-down 'org-timestamp-up)))
  19030. ((and (not (eq org-support-shift-select 'always))
  19031. org-enable-priority-commands
  19032. (org-at-heading-p))
  19033. (call-interactively 'org-priority-up))
  19034. ((and (not org-support-shift-select) (org-at-item-p))
  19035. (call-interactively 'org-previous-item))
  19036. ((org-clocktable-try-shift 'up arg))
  19037. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  19038. (org-support-shift-select
  19039. (org-call-for-shift-select 'previous-line))
  19040. (t (org-shiftselect-error))))
  19041. (defun org-shiftdown (&optional arg)
  19042. "Decrease item in timestamp or decrease priority of current headline.
  19043. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19044. depending on context. See the individual commands for more information."
  19045. (interactive "P")
  19046. (cond
  19047. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19048. ((and org-support-shift-select (org-region-active-p))
  19049. (org-call-for-shift-select 'next-line))
  19050. ((org-at-timestamp-p t)
  19051. (call-interactively (if org-edit-timestamp-down-means-later
  19052. 'org-timestamp-up 'org-timestamp-down)))
  19053. ((and (not (eq org-support-shift-select 'always))
  19054. org-enable-priority-commands
  19055. (org-at-heading-p))
  19056. (call-interactively 'org-priority-down))
  19057. ((and (not org-support-shift-select) (org-at-item-p))
  19058. (call-interactively 'org-next-item))
  19059. ((org-clocktable-try-shift 'down arg))
  19060. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19061. (org-support-shift-select
  19062. (org-call-for-shift-select 'next-line))
  19063. (t (org-shiftselect-error))))
  19064. (defun org-shiftright (&optional arg)
  19065. "Cycle the thing at point or in the current line, depending on context.
  19066. Depending on context, this does one of the following:
  19067. - switch a timestamp at point one day into the future
  19068. - on a headline, switch to the next TODO keyword.
  19069. - on an item, switch entire list to the next bullet type
  19070. - on a property line, switch to the next allowed value
  19071. - on a clocktable definition line, move time block into the future"
  19072. (interactive "P")
  19073. (cond
  19074. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19075. ((and org-support-shift-select (org-region-active-p))
  19076. (org-call-for-shift-select 'forward-char))
  19077. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19078. ((and (not (eq org-support-shift-select 'always))
  19079. (org-at-heading-p))
  19080. (let ((org-inhibit-logging
  19081. (not org-treat-S-cursor-todo-selection-as-state-change))
  19082. (org-inhibit-blocking
  19083. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19084. (org-call-with-arg 'org-todo 'right)))
  19085. ((or (and org-support-shift-select
  19086. (not (eq org-support-shift-select 'always))
  19087. (org-at-item-bullet-p))
  19088. (and (not org-support-shift-select) (org-at-item-p)))
  19089. (org-call-with-arg 'org-cycle-list-bullet nil))
  19090. ((and (not (eq org-support-shift-select 'always))
  19091. (org-at-property-p))
  19092. (call-interactively 'org-property-next-allowed-value))
  19093. ((org-clocktable-try-shift 'right arg))
  19094. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19095. (org-support-shift-select
  19096. (org-call-for-shift-select 'forward-char))
  19097. (t (org-shiftselect-error))))
  19098. (defun org-shiftleft (&optional arg)
  19099. "Cycle the thing at point or in the current line, depending on context.
  19100. Depending on context, this does one of the following:
  19101. - switch a timestamp at point one day into the past
  19102. - on a headline, switch to the previous TODO keyword.
  19103. - on an item, switch entire list to the previous bullet type
  19104. - on a property line, switch to the previous allowed value
  19105. - on a clocktable definition line, move time block into the past"
  19106. (interactive "P")
  19107. (cond
  19108. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19109. ((and org-support-shift-select (org-region-active-p))
  19110. (org-call-for-shift-select 'backward-char))
  19111. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19112. ((and (not (eq org-support-shift-select 'always))
  19113. (org-at-heading-p))
  19114. (let ((org-inhibit-logging
  19115. (not org-treat-S-cursor-todo-selection-as-state-change))
  19116. (org-inhibit-blocking
  19117. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19118. (org-call-with-arg 'org-todo 'left)))
  19119. ((or (and org-support-shift-select
  19120. (not (eq org-support-shift-select 'always))
  19121. (org-at-item-bullet-p))
  19122. (and (not org-support-shift-select) (org-at-item-p)))
  19123. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19124. ((and (not (eq org-support-shift-select 'always))
  19125. (org-at-property-p))
  19126. (call-interactively 'org-property-previous-allowed-value))
  19127. ((org-clocktable-try-shift 'left arg))
  19128. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19129. (org-support-shift-select
  19130. (org-call-for-shift-select 'backward-char))
  19131. (t (org-shiftselect-error))))
  19132. (defun org-shiftcontrolright ()
  19133. "Switch to next TODO set."
  19134. (interactive)
  19135. (cond
  19136. ((and org-support-shift-select (org-region-active-p))
  19137. (org-call-for-shift-select 'forward-word))
  19138. ((and (not (eq org-support-shift-select 'always))
  19139. (org-at-heading-p))
  19140. (org-call-with-arg 'org-todo 'nextset))
  19141. (org-support-shift-select
  19142. (org-call-for-shift-select 'forward-word))
  19143. (t (org-shiftselect-error))))
  19144. (defun org-shiftcontrolleft ()
  19145. "Switch to previous TODO set."
  19146. (interactive)
  19147. (cond
  19148. ((and org-support-shift-select (org-region-active-p))
  19149. (org-call-for-shift-select 'backward-word))
  19150. ((and (not (eq org-support-shift-select 'always))
  19151. (org-at-heading-p))
  19152. (org-call-with-arg 'org-todo 'previousset))
  19153. (org-support-shift-select
  19154. (org-call-for-shift-select 'backward-word))
  19155. (t (org-shiftselect-error))))
  19156. (defun org-shiftcontrolup (&optional n)
  19157. "Change timestamps synchronously up in CLOCK log lines.
  19158. Optional argument N tells to change by that many units."
  19159. (interactive "P")
  19160. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19161. (let (org-support-shift-select)
  19162. (org-clock-timestamps-up n))
  19163. (user-error "Not at a clock log")))
  19164. (defun org-shiftcontroldown (&optional n)
  19165. "Change timestamps synchronously down in CLOCK log lines.
  19166. Optional argument N tells to change by that many units."
  19167. (interactive "P")
  19168. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19169. (let (org-support-shift-select)
  19170. (org-clock-timestamps-down n))
  19171. (user-error "Not at a clock log")))
  19172. (defun org-increase-number-at-point (&optional inc)
  19173. "Increment the number at point.
  19174. With an optional prefix numeric argument INC, increment using
  19175. this numeric value."
  19176. (interactive "p")
  19177. (if (not (number-at-point))
  19178. (user-error "Not on a number")
  19179. (unless inc (setq inc 1))
  19180. (let ((pos (point))
  19181. (beg (skip-chars-backward "-+^/*0-9eE."))
  19182. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19183. (setq nap (buffer-substring-no-properties
  19184. (+ pos beg) (+ pos beg end)))
  19185. (delete-region (+ pos beg) (+ pos beg end))
  19186. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19187. (when (org-at-table-p)
  19188. (org-table-align)
  19189. (org-table-end-of-field 1))))
  19190. (defun org-decrease-number-at-point (&optional inc)
  19191. "Decrement the number at point.
  19192. With an optional prefix numeric argument INC, decrement using
  19193. this numeric value."
  19194. (interactive "p")
  19195. (org-increase-number-at-point (- (or inc 1))))
  19196. (defun org-ctrl-c-ret ()
  19197. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19198. (interactive)
  19199. (cond
  19200. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19201. (t (call-interactively 'org-insert-heading))))
  19202. (defun org-find-visible ()
  19203. (let ((s (point)))
  19204. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19205. (get-char-property s 'invisible)))
  19206. s))
  19207. (defun org-find-invisible ()
  19208. (let ((s (point)))
  19209. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19210. (not (get-char-property s 'invisible))))
  19211. s))
  19212. (defun org-copy-visible (beg end)
  19213. "Copy the visible parts of the region."
  19214. (interactive "r")
  19215. (let (snippets s)
  19216. (save-excursion
  19217. (save-restriction
  19218. (narrow-to-region beg end)
  19219. (setq s (goto-char (point-min)))
  19220. (while (not (= (point) (point-max)))
  19221. (goto-char (org-find-invisible))
  19222. (push (buffer-substring s (point)) snippets)
  19223. (setq s (goto-char (org-find-visible))))))
  19224. (kill-new (apply 'concat (nreverse snippets)))))
  19225. (defun org-copy-special ()
  19226. "Copy region in table or copy current subtree.
  19227. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19228. See the individual commands for more information."
  19229. (interactive)
  19230. (call-interactively
  19231. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19232. (defun org-cut-special ()
  19233. "Cut region in table or cut current subtree.
  19234. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19235. See the individual commands for more information."
  19236. (interactive)
  19237. (call-interactively
  19238. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19239. (defun org-paste-special (arg)
  19240. "Paste rectangular region into table, or past subtree relative to level.
  19241. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19242. See the individual commands for more information."
  19243. (interactive "P")
  19244. (if (org-at-table-p)
  19245. (org-table-paste-rectangle)
  19246. (org-paste-subtree arg)))
  19247. (defsubst org-in-fixed-width-region-p ()
  19248. "Is point in a fixed-width region?"
  19249. (save-match-data
  19250. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19251. (defun org-edit-special (&optional arg)
  19252. "Call a special editor for the element at point.
  19253. When at a table, call the formula editor with `org-table-edit-formulas'.
  19254. When in a source code block, call `org-edit-src-code'.
  19255. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19256. When in an export block, call `org-edit-export-block'.
  19257. When at an #+INCLUDE keyword, visit the included file.
  19258. When at a footnote reference, call `org-edit-footnote-reference'
  19259. On a link, call `ffap' to visit the link at point.
  19260. Otherwise, return a user error."
  19261. (interactive "P")
  19262. (let ((element (org-element-at-point)))
  19263. (assert (not buffer-read-only) nil
  19264. "Buffer is read-only: %s" (buffer-name))
  19265. (case (org-element-type element)
  19266. (src-block
  19267. (if (not arg) (org-edit-src-code)
  19268. (let* ((info (org-babel-get-src-block-info))
  19269. (lang (nth 0 info))
  19270. (params (nth 2 info))
  19271. (session (cdr (assq :session params))))
  19272. (if (not session) (org-edit-src-code)
  19273. ;; At a src-block with a session and function called with
  19274. ;; an ARG: switch to the buffer related to the inferior
  19275. ;; process.
  19276. (switch-to-buffer
  19277. (funcall (intern (concat "org-babel-prep-session:" lang))
  19278. session params))))))
  19279. (keyword
  19280. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19281. (org-open-link-from-string
  19282. (format "[[%s]]"
  19283. (expand-file-name
  19284. (let ((value (org-element-property :value element)))
  19285. (cond ((not (org-string-nw-p value))
  19286. (user-error "No file to edit"))
  19287. ((string-match "\\`\"\\(.*?\\)\"" value)
  19288. (match-string 1 value))
  19289. ((string-match "\\`[^ \t\"]\\S-*" value)
  19290. (match-string 0 value))
  19291. (t (user-error "No valid file specified")))))))
  19292. (user-error "No special environment to edit here")))
  19293. (table
  19294. (if (eq (org-element-property :type element) 'table.el)
  19295. (org-edit-table.el)
  19296. (call-interactively 'org-table-edit-formulas)))
  19297. ;; Only Org tables contain `table-row' type elements.
  19298. (table-row (call-interactively 'org-table-edit-formulas))
  19299. (example-block (org-edit-src-code))
  19300. (export-block (org-edit-export-block))
  19301. (fixed-width (org-edit-fixed-width-region))
  19302. (otherwise
  19303. ;; No notable element at point. Though, we may be at a link or
  19304. ;; a footnote reference, which are objects. Thus, scan deeper.
  19305. (let ((context (org-element-context element)))
  19306. (case (org-element-type context)
  19307. (link (call-interactively #'ffap))
  19308. (footnote-reference (org-edit-footnote-reference))
  19309. (t (user-error "No special environment to edit here"))))))))
  19310. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19311. (defun org-ctrl-c-ctrl-c (&optional arg)
  19312. "Set tags in headline, or update according to changed information at point.
  19313. This command does many different things, depending on context:
  19314. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19315. this is what we do.
  19316. - If the cursor is on a statistics cookie, update it.
  19317. - If the cursor is in a headline, prompt for tags and insert them
  19318. into the current line, aligned to `org-tags-column'. When called
  19319. with prefix arg, realign all tags in the current buffer.
  19320. - If the cursor is in one of the special #+KEYWORD lines, this
  19321. triggers scanning the buffer for these lines and updating the
  19322. information.
  19323. - If the cursor is inside a table, realign the table. This command
  19324. works even if the automatic table editor has been turned off.
  19325. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19326. the entire table.
  19327. - If the cursor is at a footnote reference or definition, jump to
  19328. the corresponding definition or references, respectively.
  19329. - If the cursor is a the beginning of a dynamic block, update it.
  19330. - If the current buffer is a capture buffer, close note and file it.
  19331. - If the cursor is on a <<<target>>>, update radio targets and
  19332. corresponding links in this buffer.
  19333. - If the cursor is on a numbered item in a plain list, renumber the
  19334. ordered list.
  19335. - If the cursor is on a checkbox, toggle it.
  19336. - If the cursor is on a code block, evaluate it. The variable
  19337. `org-confirm-babel-evaluate' can be used to control prompting
  19338. before code block evaluation, by default every code block
  19339. evaluation requires confirmation. Code block evaluation can be
  19340. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19341. (interactive "P")
  19342. (cond
  19343. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19344. org-occur-highlights)
  19345. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19346. (org-remove-occur-highlights)
  19347. (message "Temporary highlights/overlays removed from current buffer"))
  19348. ((and (local-variable-p 'org-finish-function (current-buffer))
  19349. (fboundp org-finish-function))
  19350. (funcall org-finish-function))
  19351. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19352. (t
  19353. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19354. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19355. (user-error "C-c C-c can do nothing useful at this location"))
  19356. (let* ((context (org-element-context))
  19357. (type (org-element-type context)))
  19358. (case type
  19359. ;; When at a link, act according to the parent instead.
  19360. (link (setq context (org-element-property :parent context))
  19361. (setq type (org-element-type context)))
  19362. ;; Unsupported object types: refer to the first supported
  19363. ;; element or object containing it.
  19364. ((bold code entity export-snippet inline-babel-call inline-src-block
  19365. italic latex-fragment line-break macro strike-through subscript
  19366. superscript underline verbatim)
  19367. (setq context
  19368. (org-element-lineage
  19369. context '(radio-target paragraph verse-block table-cell)))))
  19370. ;; For convenience: at the first line of a paragraph on the
  19371. ;; same line as an item, apply function on that item instead.
  19372. (when (eq type 'paragraph)
  19373. (let ((parent (org-element-property :parent context)))
  19374. (when (and (eq (org-element-type parent) 'item)
  19375. (= (line-beginning-position)
  19376. (org-element-property :begin parent)))
  19377. (setq context parent type 'item))))
  19378. ;; Act according to type of element or object at point.
  19379. (case type
  19380. (clock (org-clock-update-time-maybe))
  19381. (dynamic-block
  19382. (save-excursion
  19383. (goto-char (org-element-property :post-affiliated context))
  19384. (org-update-dblock)))
  19385. (footnote-definition
  19386. (goto-char (org-element-property :post-affiliated context))
  19387. (call-interactively 'org-footnote-action))
  19388. (footnote-reference (call-interactively 'org-footnote-action))
  19389. ((headline inlinetask)
  19390. (save-excursion (goto-char (org-element-property :begin context))
  19391. (call-interactively 'org-set-tags)))
  19392. (item
  19393. ;; At an item: a double C-u set checkbox to "[-]"
  19394. ;; unconditionally, whereas a single one will toggle its
  19395. ;; presence. Without a universal argument, if the item
  19396. ;; has a checkbox, toggle it. Otherwise repair the list.
  19397. (let* ((box (org-element-property :checkbox context))
  19398. (struct (org-element-property :structure context))
  19399. (old-struct (copy-tree struct))
  19400. (parents (org-list-parents-alist struct))
  19401. (prevs (org-list-prevs-alist struct))
  19402. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19403. (org-list-set-checkbox
  19404. (org-element-property :begin context) struct
  19405. (cond ((equal arg '(16)) "[-]")
  19406. ((and (not box) (equal arg '(4))) "[ ]")
  19407. ((or (not box) (equal arg '(4))) nil)
  19408. ((eq box 'on) "[ ]")
  19409. (t "[X]")))
  19410. ;; Mimic `org-list-write-struct' but with grabbing
  19411. ;; a return value from `org-list-struct-fix-box'.
  19412. (org-list-struct-fix-ind struct parents 2)
  19413. (org-list-struct-fix-item-end struct)
  19414. (org-list-struct-fix-bul struct prevs)
  19415. (org-list-struct-fix-ind struct parents)
  19416. (let ((block-item
  19417. (org-list-struct-fix-box struct parents prevs orderedp)))
  19418. (if (and box (equal struct old-struct))
  19419. (if (equal arg '(16))
  19420. (message "Checkboxes already reset")
  19421. (user-error "Cannot toggle this checkbox: %s"
  19422. (if (eq box 'on)
  19423. "all subitems checked"
  19424. "unchecked subitems")))
  19425. (org-list-struct-apply-struct struct old-struct)
  19426. (org-update-checkbox-count-maybe))
  19427. (when block-item
  19428. (message "Checkboxes were removed due to empty box at line %d"
  19429. (org-current-line block-item))))))
  19430. (keyword
  19431. (let ((org-inhibit-startup-visibility-stuff t)
  19432. (org-startup-align-all-tables nil))
  19433. (when (boundp 'org-table-coordinate-overlays)
  19434. (mapc 'delete-overlay org-table-coordinate-overlays)
  19435. (setq org-table-coordinate-overlays nil))
  19436. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19437. (message "Local setup has been refreshed"))
  19438. (plain-list
  19439. ;; At a plain list, with a double C-u argument, set
  19440. ;; checkboxes of each item to "[-]", whereas a single one
  19441. ;; will toggle their presence according to the state of the
  19442. ;; first item in the list. Without an argument, repair the
  19443. ;; list.
  19444. (let* ((begin (org-element-property :contents-begin context))
  19445. (beginm (move-marker (make-marker) begin))
  19446. (struct (org-element-property :structure context))
  19447. (old-struct (copy-tree struct))
  19448. (first-box (save-excursion
  19449. (goto-char begin)
  19450. (looking-at org-list-full-item-re)
  19451. (match-string-no-properties 3)))
  19452. (new-box (cond ((equal arg '(16)) "[-]")
  19453. ((equal arg '(4)) (unless first-box "[ ]"))
  19454. ((equal first-box "[X]") "[ ]")
  19455. (t "[X]"))))
  19456. (cond
  19457. (arg
  19458. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19459. (org-list-get-all-items
  19460. begin struct (org-list-prevs-alist struct))))
  19461. ((and first-box (eq (point) begin))
  19462. ;; For convenience, when point is at bol on the first
  19463. ;; item of the list and no argument is provided, simply
  19464. ;; toggle checkbox of that item, if any.
  19465. (org-list-set-checkbox begin struct new-box)))
  19466. (org-list-write-struct
  19467. struct (org-list-parents-alist struct) old-struct)
  19468. (org-update-checkbox-count-maybe)
  19469. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19470. ((property-drawer node-property)
  19471. (call-interactively 'org-property-action))
  19472. ((radio-target target)
  19473. (call-interactively 'org-update-radio-target-regexp))
  19474. (statistics-cookie
  19475. (call-interactively 'org-update-statistics-cookies))
  19476. ((table table-cell table-row)
  19477. ;; At a table, recalculate every field and align it. Also
  19478. ;; send the table if necessary. If the table has
  19479. ;; a `table.el' type, just give up. At a table row or
  19480. ;; cell, maybe recalculate line but always align table.
  19481. (if (eq (org-element-property :type context) 'table.el)
  19482. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19483. Use \\[org-edit-special] to edit table.el tables"))
  19484. (let ((org-enable-table-editor t))
  19485. (if (or (eq type 'table)
  19486. ;; Check if point is at a TBLFM line.
  19487. (and (eq type 'table-row)
  19488. (= (point) (org-element-property :end context))))
  19489. (save-excursion
  19490. (if (org-at-TBLFM-p)
  19491. (progn (require 'org-table)
  19492. (org-table-calc-current-TBLFM))
  19493. (goto-char (org-element-property :contents-begin context))
  19494. (org-call-with-arg 'org-table-recalculate (or arg t))
  19495. (orgtbl-send-table 'maybe)))
  19496. (org-table-maybe-eval-formula)
  19497. (cond (arg (call-interactively 'org-table-recalculate))
  19498. ((org-table-maybe-recalculate-line))
  19499. (t (org-table-align)))))))
  19500. (timestamp (org-timestamp-change 0 'day))
  19501. (otherwise
  19502. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19503. (user-error
  19504. "C-c C-c can do nothing useful at this location")))))))))
  19505. (defun org-mode-restart ()
  19506. (interactive)
  19507. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19508. (funcall major-mode)
  19509. (hack-local-variables)
  19510. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19511. (org-indent-mode -1)))
  19512. (message "%s restarted" major-mode))
  19513. (defun org-kill-note-or-show-branches ()
  19514. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19515. (interactive)
  19516. (if (not org-finish-function)
  19517. (progn
  19518. (outline-hide-subtree)
  19519. (call-interactively 'outline-show-branches))
  19520. (let ((org-note-abort t))
  19521. (funcall org-finish-function))))
  19522. (defun org-delete-indentation (&optional ARG)
  19523. "Join current line to previous and fix whitespace at join.
  19524. If previous line is a headline add to headline title. Otherwise
  19525. the function calls `delete-indentation'.
  19526. With argument, join this line to following line."
  19527. (interactive "*P")
  19528. (if (save-excursion
  19529. (if ARG (beginning-of-line)
  19530. (forward-line -1))
  19531. (looking-at org-complex-heading-regexp))
  19532. ;; At headline.
  19533. (let ((tags-column (when (match-beginning 5)
  19534. (save-excursion (goto-char (match-beginning 5))
  19535. (current-column))))
  19536. (string (concat " " (progn (when ARG (forward-line 1))
  19537. (org-trim (delete-and-extract-region
  19538. (line-beginning-position)
  19539. (line-end-position)))))))
  19540. (unless (bobp) (delete-region (point) (1- (point))))
  19541. (goto-char (or (match-end 4)
  19542. (match-beginning 5)
  19543. (match-end 0)))
  19544. (skip-chars-backward " \t")
  19545. (save-excursion (insert string))
  19546. ;; Adjust alignment of tags.
  19547. (when tags-column
  19548. (org-align-tags-here (if org-auto-align-tags
  19549. org-tags-column
  19550. tags-column))))
  19551. (delete-indentation ARG)))
  19552. (defun org-open-line (n)
  19553. "Insert a new row in tables, call `open-line' elsewhere.
  19554. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19555. (interactive "*p")
  19556. (cond
  19557. ((not org-special-ctrl-o)
  19558. (open-line n))
  19559. ((org-at-table-p)
  19560. (org-table-insert-row))
  19561. (t
  19562. (open-line n))))
  19563. (defun org-return (&optional indent)
  19564. "Goto next table row or insert a newline.
  19565. Calls `org-table-next-row' or `newline', depending on context.
  19566. When optional INDENT argument is non-nil, call
  19567. `newline-and-indent' instead of `newline'.
  19568. When `org-return-follows-link' is non-nil and point is on
  19569. a timestamp or a link, call `org-open-at-point'. However, it
  19570. will not happen if point is in a table or on a \"dead\"
  19571. object (e.g., within a comment). In these case, you need to use
  19572. `org-open-at-point' directly."
  19573. (interactive)
  19574. (let ((context (if org-return-follows-link (org-element-context)
  19575. (org-element-at-point))))
  19576. (cond
  19577. ;; In a table, call `org-table-next-row'.
  19578. ((or (and (eq (org-element-type context) 'table)
  19579. (>= (point) (org-element-property :contents-begin context))
  19580. (< (point) (org-element-property :contents-end context)))
  19581. (org-element-lineage context '(table-row table-cell) t))
  19582. (org-table-justify-field-maybe)
  19583. (call-interactively #'org-table-next-row))
  19584. ;; On a link or a timestamp, call `org-open-line' if
  19585. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19586. ;; locations, e.g., in a comment, as `org-open-line'.
  19587. ((and org-return-follows-link
  19588. (or (org-at-timestamp-p t)
  19589. (org-at-date-range-p t)
  19590. (org-in-regexp org-any-link-re)))
  19591. (call-interactively #'org-open-at-point))
  19592. ;; Insert newline in heading, but preserve tags.
  19593. ((and (not (bolp))
  19594. (save-excursion (beginning-of-line)
  19595. (looking-at org-complex-heading-regexp)))
  19596. ;; At headline. Split line. However, if point is on keyword,
  19597. ;; priority cookie or tags, do not break any of them: add
  19598. ;; a newline after the headline instead.
  19599. (let ((tags-column (and (match-beginning 5)
  19600. (save-excursion (goto-char (match-beginning 5))
  19601. (current-column))))
  19602. (string
  19603. (when (and (match-end 4)
  19604. (>= (point)
  19605. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19606. (<= (point) (match-end 4)))
  19607. (delete-and-extract-region (point) (match-end 4)))))
  19608. (when (and tags-column string) ; Adjust tag alignment.
  19609. (org-align-tags-here
  19610. (if org-auto-align-tags org-tags-column tags-column)))
  19611. (end-of-line)
  19612. (org-show-entry)
  19613. (if indent (newline-and-indent) (newline))
  19614. (when string (save-excursion (insert (org-trim string))))))
  19615. ;; In a list, make sure indenting keeps trailing text within.
  19616. ((and indent
  19617. (not (eolp))
  19618. (org-element-lineage context '(item)))
  19619. (let ((trailing-data
  19620. (delete-and-extract-region (point) (line-end-position))))
  19621. (newline-and-indent)
  19622. (save-excursion (insert trailing-data))))
  19623. (t (if indent (newline-and-indent) (newline))))))
  19624. (defun org-return-indent ()
  19625. "Goto next table row or insert a newline and indent.
  19626. Calls `org-table-next-row' or `newline-and-indent', depending on
  19627. context. See the individual commands for more information."
  19628. (interactive)
  19629. (org-return t))
  19630. (defun org-ctrl-c-star ()
  19631. "Compute table, or change heading status of lines.
  19632. Calls `org-table-recalculate' or `org-toggle-heading',
  19633. depending on context."
  19634. (interactive)
  19635. (cond
  19636. ((org-at-table-p)
  19637. (call-interactively 'org-table-recalculate))
  19638. (t
  19639. ;; Convert all lines in region to list items
  19640. (call-interactively 'org-toggle-heading))))
  19641. (defun org-ctrl-c-minus ()
  19642. "Insert separator line in table or modify bullet status of line.
  19643. Also turns a plain line or a region of lines into list items.
  19644. Calls `org-table-insert-hline', `org-toggle-item', or
  19645. `org-cycle-list-bullet', depending on context."
  19646. (interactive)
  19647. (cond
  19648. ((org-at-table-p)
  19649. (call-interactively 'org-table-insert-hline))
  19650. ((org-region-active-p)
  19651. (call-interactively 'org-toggle-item))
  19652. ((org-in-item-p)
  19653. (call-interactively 'org-cycle-list-bullet))
  19654. (t
  19655. (call-interactively 'org-toggle-item))))
  19656. (defun org-toggle-item (arg)
  19657. "Convert headings or normal lines to items, items to normal lines.
  19658. If there is no active region, only the current line is considered.
  19659. If the first non blank line in the region is a headline, convert
  19660. all headlines to items, shifting text accordingly.
  19661. If it is an item, convert all items to normal lines.
  19662. If it is normal text, change region into a list of items.
  19663. With a prefix argument ARG, change the region in a single item."
  19664. (interactive "P")
  19665. (let ((shift-text
  19666. (function
  19667. ;; Shift text in current section to IND, from point to END.
  19668. ;; The function leaves point to END line.
  19669. (lambda (ind end)
  19670. (let ((min-i 1000) (end (copy-marker end)))
  19671. ;; First determine the minimum indentation (MIN-I) of
  19672. ;; the text.
  19673. (save-excursion
  19674. (catch 'exit
  19675. (while (< (point) end)
  19676. (let ((i (org-get-indentation)))
  19677. (cond
  19678. ;; Skip blank lines and inline tasks.
  19679. ((looking-at "^[ \t]*$"))
  19680. ((looking-at org-outline-regexp-bol))
  19681. ;; We can't find less than 0 indentation.
  19682. ((zerop i) (throw 'exit (setq min-i 0)))
  19683. ((< i min-i) (setq min-i i))))
  19684. (forward-line))))
  19685. ;; Then indent each line so that a line indented to
  19686. ;; MIN-I becomes indented to IND. Ignore blank lines
  19687. ;; and inline tasks in the process.
  19688. (let ((delta (- ind min-i)))
  19689. (while (< (point) end)
  19690. (unless (or (looking-at "^[ \t]*$")
  19691. (looking-at org-outline-regexp-bol))
  19692. (org-indent-line-to (+ (org-get-indentation) delta)))
  19693. (forward-line)))))))
  19694. (skip-blanks
  19695. (function
  19696. ;; Return beginning of first non-blank line, starting from
  19697. ;; line at POS.
  19698. (lambda (pos)
  19699. (save-excursion
  19700. (goto-char pos)
  19701. (skip-chars-forward " \r\t\n")
  19702. (point-at-bol)))))
  19703. beg end)
  19704. ;; Determine boundaries of changes.
  19705. (if (org-region-active-p)
  19706. (setq beg (funcall skip-blanks (region-beginning))
  19707. end (copy-marker (region-end)))
  19708. (setq beg (funcall skip-blanks (point-at-bol))
  19709. end (copy-marker (point-at-eol))))
  19710. ;; Depending on the starting line, choose an action on the text
  19711. ;; between BEG and END.
  19712. (org-with-limited-levels
  19713. (save-excursion
  19714. (goto-char beg)
  19715. (cond
  19716. ;; Case 1. Start at an item: de-itemize. Note that it only
  19717. ;; happens when a region is active: `org-ctrl-c-minus'
  19718. ;; would call `org-cycle-list-bullet' otherwise.
  19719. ((org-at-item-p)
  19720. (while (< (point) end)
  19721. (when (org-at-item-p)
  19722. (skip-chars-forward " \t")
  19723. (delete-region (point) (match-end 0)))
  19724. (forward-line)))
  19725. ;; Case 2. Start at an heading: convert to items.
  19726. ((org-at-heading-p)
  19727. (let* ((bul (org-list-bullet-string "-"))
  19728. (bul-len (length bul))
  19729. (done (org-entry-is-done-p))
  19730. (todo (org-entry-is-todo-p))
  19731. ;; Indentation of the first heading. It should be
  19732. ;; relative to the indentation of its parent, if any.
  19733. (start-ind (save-excursion
  19734. (cond
  19735. ((not org-adapt-indentation) 0)
  19736. ((not (outline-previous-heading)) 0)
  19737. (t (length (match-string 0))))))
  19738. ;; Level of first heading. Further headings will be
  19739. ;; compared to it to determine hierarchy in the list.
  19740. (ref-level (org-reduced-level (org-outline-level))))
  19741. (when (or done todo) (org-todo ""))
  19742. (while (< (point) end)
  19743. (let* ((level (org-reduced-level (org-outline-level)))
  19744. (delta (max 0 (- level ref-level))))
  19745. ;; If current headline is less indented than the first
  19746. ;; one, set it as reference, in order to preserve
  19747. ;; subtrees.
  19748. (when (< level ref-level) (setq ref-level level))
  19749. (replace-match bul t t)
  19750. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19751. (when (or done todo)
  19752. (let* ((struct (org-list-struct))
  19753. (old (copy-tree struct)))
  19754. (org-list-set-checkbox (line-beginning-position)
  19755. struct
  19756. (if done "[X]" "[ ]"))
  19757. (org-list-write-struct struct
  19758. (org-list-parents-alist struct)
  19759. old)))
  19760. ;; Ensure all text down to END (or SECTION-END) belongs
  19761. ;; to the newly created item.
  19762. (let ((section-end (save-excursion
  19763. (or (outline-next-heading) (point)))))
  19764. (forward-line)
  19765. (funcall shift-text
  19766. (+ start-ind (* (1+ delta) bul-len))
  19767. (min end section-end)))))))
  19768. ;; Case 3. Normal line with ARG: make the first line of region
  19769. ;; an item, and shift indentation of others lines to
  19770. ;; set them as item's body.
  19771. (arg (let* ((bul (org-list-bullet-string "-"))
  19772. (bul-len (length bul))
  19773. (ref-ind (org-get-indentation)))
  19774. (skip-chars-forward " \t")
  19775. (insert bul)
  19776. (forward-line)
  19777. (while (< (point) end)
  19778. ;; Ensure that lines less indented than first one
  19779. ;; still get included in item body.
  19780. (funcall shift-text
  19781. (+ ref-ind bul-len)
  19782. (min end (save-excursion (or (outline-next-heading)
  19783. (point)))))
  19784. (forward-line))))
  19785. ;; Case 4. Normal line without ARG: turn each non-item line
  19786. ;; into an item.
  19787. (t
  19788. (while (< (point) end)
  19789. (unless (or (org-at-heading-p) (org-at-item-p))
  19790. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19791. (replace-match
  19792. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19793. (forward-line))))))))
  19794. (defun org-toggle-heading (&optional nstars)
  19795. "Convert headings to normal text, or items or text to headings.
  19796. If there is no active region, only convert the current line.
  19797. With a \\[universal-argument] prefix, convert the whole list at
  19798. point into heading.
  19799. In a region:
  19800. - If the first non blank line is a headline, remove the stars
  19801. from all headlines in the region.
  19802. - If it is a normal line, turn each and every normal line (i.e.,
  19803. not an heading or an item) in the region into headings. If you
  19804. want to convert only the first line of this region, use one
  19805. universal prefix argument.
  19806. - If it is a plain list item, turn all plain list items into headings.
  19807. When converting a line into a heading, the number of stars is chosen
  19808. such that the lines become children of the current entry. However,
  19809. when a numeric prefix argument is given, its value determines the
  19810. number of stars to add."
  19811. (interactive "P")
  19812. (let ((skip-blanks
  19813. (function
  19814. ;; Return beginning of first non-blank line, starting from
  19815. ;; line at POS.
  19816. (lambda (pos)
  19817. (save-excursion
  19818. (goto-char pos)
  19819. (while (org-at-comment-p) (forward-line))
  19820. (skip-chars-forward " \r\t\n")
  19821. (point-at-bol)))))
  19822. beg end toggled)
  19823. ;; Determine boundaries of changes. If a universal prefix has
  19824. ;; been given, put the list in a region. If region ends at a bol,
  19825. ;; do not consider the last line to be in the region.
  19826. (when (and current-prefix-arg (org-at-item-p))
  19827. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19828. (org-mark-element))
  19829. (if (org-region-active-p)
  19830. (setq beg (funcall skip-blanks (region-beginning))
  19831. end (copy-marker (save-excursion
  19832. (goto-char (region-end))
  19833. (if (bolp) (point) (point-at-eol)))))
  19834. (setq beg (funcall skip-blanks (point-at-bol))
  19835. end (copy-marker (point-at-eol))))
  19836. ;; Ensure inline tasks don't count as headings.
  19837. (org-with-limited-levels
  19838. (save-excursion
  19839. (goto-char beg)
  19840. (cond
  19841. ;; Case 1. Started at an heading: de-star headings.
  19842. ((org-at-heading-p)
  19843. (while (< (point) end)
  19844. (when (org-at-heading-p t)
  19845. (looking-at org-outline-regexp) (replace-match "")
  19846. (setq toggled t))
  19847. (forward-line)))
  19848. ;; Case 2. Started at an item: change items into headlines.
  19849. ;; One star will be added by `org-list-to-subtree'.
  19850. ((org-at-item-p)
  19851. (while (< (point) end)
  19852. (when (org-at-item-p)
  19853. ;; Pay attention to cases when region ends before list.
  19854. (let* ((struct (org-list-struct))
  19855. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19856. (save-restriction
  19857. (narrow-to-region (point) list-end)
  19858. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19859. (setq toggled t))
  19860. (forward-line)))
  19861. ;; Case 3. Started at normal text: make every line an heading,
  19862. ;; skipping headlines and items.
  19863. (t (let* ((stars
  19864. (make-string
  19865. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19866. (add-stars
  19867. (cond (nstars "") ; stars from prefix only
  19868. ((equal stars "") "*") ; before first heading
  19869. (org-odd-levels-only "**") ; inside heading, odd
  19870. (t "*"))) ; inside heading, oddeven
  19871. (rpl (concat stars add-stars " "))
  19872. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19873. (while (< (point) (if (equal nstars '(4)) lend end))
  19874. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19875. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19876. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19877. (forward-line)))))))
  19878. (unless toggled (message "Cannot toggle heading from here"))))
  19879. (defun org-meta-return (&optional _arg)
  19880. "Insert a new heading or wrap a region in a table.
  19881. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19882. on context. See the individual commands for more information."
  19883. (interactive)
  19884. (org-check-before-invisible-edit 'insert)
  19885. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19886. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19887. #'org-insert-heading))))
  19888. ;;; Menu entries
  19889. (defsubst org-in-subtree-not-table-p ()
  19890. "Are we in a subtree and not in a table?"
  19891. (and (not (org-before-first-heading-p))
  19892. (not (org-at-table-p))))
  19893. ;; Define the Org-mode menus
  19894. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19895. '("Tbl"
  19896. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19897. ["Next Field" org-cycle (org-at-table-p)]
  19898. ["Previous Field" org-shifttab (org-at-table-p)]
  19899. ["Next Row" org-return (org-at-table-p)]
  19900. "--"
  19901. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19902. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19903. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19904. "--"
  19905. ("Column"
  19906. ["Move Column Left" org-metaleft (org-at-table-p)]
  19907. ["Move Column Right" org-metaright (org-at-table-p)]
  19908. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19909. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19910. ("Row"
  19911. ["Move Row Up" org-metaup (org-at-table-p)]
  19912. ["Move Row Down" org-metadown (org-at-table-p)]
  19913. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19914. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19915. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19916. "--"
  19917. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19918. ("Rectangle"
  19919. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19920. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19921. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19922. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19923. "--"
  19924. ("Calculate"
  19925. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19926. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19927. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19928. "--"
  19929. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19930. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19931. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19932. "--"
  19933. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19934. "--"
  19935. ["Sum Column/Rectangle" org-table-sum
  19936. (or (org-at-table-p) (org-region-active-p))]
  19937. ["Which Column?" org-table-current-column (org-at-table-p)])
  19938. ["Debug Formulas"
  19939. org-table-toggle-formula-debugger
  19940. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19941. ["Show Col/Row Numbers"
  19942. org-table-toggle-coordinate-overlays
  19943. :style toggle
  19944. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19945. "--"
  19946. ["Create" org-table-create (and (not (org-at-table-p))
  19947. org-enable-table-editor)]
  19948. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19949. ["Import from File" org-table-import (not (org-at-table-p))]
  19950. ["Export to File" org-table-export (org-at-table-p)]
  19951. "--"
  19952. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19953. "--"
  19954. ("Plot"
  19955. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19956. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19957. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19958. '("Org"
  19959. ("Show/Hide"
  19960. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19961. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19962. ["Sparse Tree..." org-sparse-tree t]
  19963. ["Reveal Context" org-reveal t]
  19964. ["Show All" outline-show-all t]
  19965. "--"
  19966. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19967. "--"
  19968. ["New Heading" org-insert-heading t]
  19969. ("Navigate Headings"
  19970. ["Up" outline-up-heading t]
  19971. ["Next" outline-next-visible-heading t]
  19972. ["Previous" outline-previous-visible-heading t]
  19973. ["Next Same Level" outline-forward-same-level t]
  19974. ["Previous Same Level" outline-backward-same-level t]
  19975. "--"
  19976. ["Jump" org-goto t])
  19977. ("Edit Structure"
  19978. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19979. "--"
  19980. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19981. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19982. "--"
  19983. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19984. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19985. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19986. "--"
  19987. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19988. "--"
  19989. ["Copy visible text" org-copy-visible t]
  19990. "--"
  19991. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19992. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19993. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19994. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19995. "--"
  19996. ["Sort Region/Children" org-sort t]
  19997. "--"
  19998. ["Convert to odd levels" org-convert-to-odd-levels t]
  19999. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  20000. ("Editing"
  20001. ["Emphasis..." org-emphasize t]
  20002. ["Edit Source Example" org-edit-special t]
  20003. "--"
  20004. ["Footnote new/jump" org-footnote-action t]
  20005. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  20006. ("Archive"
  20007. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  20008. "--"
  20009. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  20010. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  20011. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  20012. )
  20013. "--"
  20014. ("Hyperlinks"
  20015. ["Store Link (Global)" org-store-link t]
  20016. ["Find existing link to here" org-occur-link-in-agenda-files t]
  20017. ["Insert Link" org-insert-link t]
  20018. ["Follow Link" org-open-at-point t]
  20019. "--"
  20020. ["Next link" org-next-link t]
  20021. ["Previous link" org-previous-link t]
  20022. "--"
  20023. ["Descriptive Links"
  20024. org-toggle-link-display
  20025. :style radio
  20026. :selected org-descriptive-links
  20027. ]
  20028. ["Literal Links"
  20029. org-toggle-link-display
  20030. :style radio
  20031. :selected (not org-descriptive-links)])
  20032. "--"
  20033. ("TODO Lists"
  20034. ["TODO/DONE/-" org-todo t]
  20035. ("Select keyword"
  20036. ["Next keyword" org-shiftright (org-at-heading-p)]
  20037. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  20038. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20039. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20040. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20041. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20042. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20043. "--"
  20044. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20045. :selected org-enforce-todo-dependencies :style toggle :active t]
  20046. "Settings for tree at point"
  20047. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20048. :selected (org-entry-get nil "ORDERED")
  20049. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20050. ["Do Children parallel" org-toggle-ordered-property :style radio
  20051. :selected (not (org-entry-get nil "ORDERED"))
  20052. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20053. "--"
  20054. ["Set Priority" org-priority t]
  20055. ["Priority Up" org-shiftup t]
  20056. ["Priority Down" org-shiftdown t]
  20057. "--"
  20058. ["Get news from all feeds" org-feed-update-all t]
  20059. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20060. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20061. ("TAGS and Properties"
  20062. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20063. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20064. "--"
  20065. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20066. ["Column view of properties" org-columns t]
  20067. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20068. ("Dates and Scheduling"
  20069. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20070. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20071. ("Change Date"
  20072. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20073. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20074. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20075. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20076. ["Compute Time Range" org-evaluate-time-range t]
  20077. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20078. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20079. "--"
  20080. ["Custom time format" org-toggle-time-stamp-overlays
  20081. :style radio :selected org-display-custom-times]
  20082. "--"
  20083. ["Goto Calendar" org-goto-calendar t]
  20084. ["Date from Calendar" org-date-from-calendar t]
  20085. "--"
  20086. ["Start/Restart Timer" org-timer-start t]
  20087. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20088. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20089. ["Insert Timer String" org-timer t]
  20090. ["Insert Timer Item" org-timer-item t])
  20091. ("Logging work"
  20092. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20093. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20094. ["Clock out" org-clock-out t]
  20095. ["Clock cancel" org-clock-cancel t]
  20096. "--"
  20097. ["Mark as default task" org-clock-mark-default-task t]
  20098. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20099. ["Goto running clock" org-clock-goto t]
  20100. "--"
  20101. ["Display times" org-clock-display t]
  20102. ["Create clock table" org-clock-report t]
  20103. "--"
  20104. ["Record DONE time"
  20105. (progn (setq org-log-done (not org-log-done))
  20106. (message "Switching to %s will %s record a timestamp"
  20107. (car org-done-keywords)
  20108. (if org-log-done "automatically" "not")))
  20109. :style toggle :selected org-log-done])
  20110. "--"
  20111. ["Agenda Command..." org-agenda t]
  20112. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20113. ("File List for Agenda")
  20114. ("Special views current file"
  20115. ["TODO Tree" org-show-todo-tree t]
  20116. ["Check Deadlines" org-check-deadlines t]
  20117. ["Timeline" org-timeline t]
  20118. ["Tags/Property tree" org-match-sparse-tree t])
  20119. "--"
  20120. ["Export/Publish..." org-export-dispatch t]
  20121. ("LaTeX"
  20122. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20123. :selected org-cdlatex-mode]
  20124. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20125. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20126. ["Modify math symbol" org-cdlatex-math-modify
  20127. (org-inside-LaTeX-fragment-p)]
  20128. ["Insert citation" org-reftex-citation t]
  20129. "--"
  20130. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20131. "--"
  20132. ("MobileOrg"
  20133. ["Push Files and Views" org-mobile-push t]
  20134. ["Get Captured and Flagged" org-mobile-pull t]
  20135. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20136. "--"
  20137. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20138. "--"
  20139. ("Documentation"
  20140. ["Show Version" org-version t]
  20141. ["Info Documentation" org-info t])
  20142. ("Customize"
  20143. ["Browse Org Group" org-customize t]
  20144. "--"
  20145. ["Expand This Menu" org-create-customize-menu
  20146. (fboundp 'customize-menu-create)])
  20147. ["Send bug report" org-submit-bug-report t]
  20148. "--"
  20149. ("Refresh/Reload"
  20150. ["Refresh setup current buffer" org-mode-restart t]
  20151. ["Reload Org (after update)" org-reload t]
  20152. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20153. ))
  20154. (defun org-info (&optional node)
  20155. "Read documentation for Org-mode in the info system.
  20156. With optional NODE, go directly to that node."
  20157. (interactive)
  20158. (info (format "(org)%s" (or node ""))))
  20159. ;;;###autoload
  20160. (defun org-submit-bug-report ()
  20161. "Submit a bug report on Org-mode via mail.
  20162. Don't hesitate to report any problems or inaccurate documentation.
  20163. If you don't have setup sending mail from (X)Emacs, please copy the
  20164. output buffer into your mail program, as it gives us important
  20165. information about your Org-mode version and configuration."
  20166. (interactive)
  20167. (require 'reporter)
  20168. (defvar reporter-prompt-for-summary-p)
  20169. (org-load-modules-maybe)
  20170. (org-require-autoloaded-modules)
  20171. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20172. (reporter-submit-bug-report
  20173. "emacs-orgmode@gnu.org"
  20174. (org-version nil 'full)
  20175. (let (list)
  20176. (save-window-excursion
  20177. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20178. (delete-other-windows)
  20179. (erase-buffer)
  20180. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20181. We would like to add your full Org-mode and Outline configuration to the
  20182. bug report. This greatly simplifies the work of the maintainer and
  20183. other experts on the mailing list.
  20184. HOWEVER, some variables you have customized may contain private
  20185. information. The names of customers, colleagues, or friends, might
  20186. appear in the form of file names, tags, todo states, or search strings.
  20187. If you answer yes to the prompt, you might want to check and remove
  20188. such private information before sending the email.")
  20189. (add-text-properties (point-min) (point-max) '(face org-warning))
  20190. (when (yes-or-no-p "Include your Org-mode configuration ")
  20191. (mapatoms
  20192. (lambda (v)
  20193. (and (boundp v)
  20194. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20195. (or (and (symbol-value v)
  20196. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20197. (and
  20198. (get v 'custom-type) (get v 'standard-value)
  20199. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20200. (push v list)))))
  20201. (kill-buffer (get-buffer "*Warn about privacy*"))
  20202. list))
  20203. nil nil
  20204. "Remember to cover the basics, that is, what you expected to happen and
  20205. what in fact did happen. You don't know how to make a good report? See
  20206. http://orgmode.org/manual/Feedback.html#Feedback
  20207. Your bug report will be posted to the Org-mode mailing list.
  20208. ------------------------------------------------------------------------")
  20209. (save-excursion
  20210. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20211. (replace-match "\\1Bug: \\3 [\\2]")))))
  20212. (defun org-install-agenda-files-menu ()
  20213. (let ((bl (buffer-list)))
  20214. (save-excursion
  20215. (while bl
  20216. (set-buffer (pop bl))
  20217. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20218. (when (derived-mode-p 'org-mode)
  20219. (easy-menu-change
  20220. '("Org") "File List for Agenda"
  20221. (append
  20222. (list
  20223. ["Edit File List" (org-edit-agenda-file-list) t]
  20224. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20225. ["Remove Current File from List" org-remove-file t]
  20226. ["Cycle through agenda files" org-cycle-agenda-files t]
  20227. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20228. "--")
  20229. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20230. ;;;; Documentation
  20231. (defun org-require-autoloaded-modules ()
  20232. (interactive)
  20233. (mapc 'require
  20234. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20235. org-table org-timer)))
  20236. ;;;###autoload
  20237. (defun org-reload (&optional uncompiled)
  20238. "Reload all org lisp files.
  20239. With prefix arg UNCOMPILED, load the uncompiled versions."
  20240. (interactive "P")
  20241. (require 'loadhist)
  20242. (let* ((org-dir (org-find-library-dir "org"))
  20243. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20244. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20245. (remove-re (mapconcat 'identity
  20246. (mapcar (lambda (f) (concat "^" f "$"))
  20247. (list (if (featurep 'xemacs)
  20248. "org-colview"
  20249. "org-colview-xemacs")
  20250. "org" "org-loaddefs" "org-version"))
  20251. "\\|"))
  20252. (feats (delete-dups
  20253. (mapcar 'file-name-sans-extension
  20254. (mapcar 'file-name-nondirectory
  20255. (delq nil
  20256. (mapcar 'feature-file
  20257. features))))))
  20258. (lfeat (append
  20259. (sort
  20260. (setq feats
  20261. (delq nil (mapcar
  20262. (lambda (f)
  20263. (if (and (string-match feature-re f)
  20264. (not (string-match remove-re f)))
  20265. f nil))
  20266. feats)))
  20267. 'string-lessp)
  20268. (list "org-version" "org")))
  20269. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20270. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20271. load-uncore load-misses)
  20272. (setq load-misses
  20273. (delq 't
  20274. (mapcar (lambda (f)
  20275. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20276. (and (string= org-dir contrib-dir)
  20277. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20278. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20279. (add-to-list 'load-uncore f 'append)
  20280. 't)
  20281. f))
  20282. lfeat)))
  20283. (if load-uncore
  20284. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20285. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20286. (if load-misses
  20287. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20288. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20289. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20290. ;;;###autoload
  20291. (defun org-customize ()
  20292. "Call the customize function with org as argument."
  20293. (interactive)
  20294. (org-load-modules-maybe)
  20295. (org-require-autoloaded-modules)
  20296. (customize-browse 'org))
  20297. (defun org-create-customize-menu ()
  20298. "Create a full customization menu for Org-mode, insert it into the menu."
  20299. (interactive)
  20300. (org-load-modules-maybe)
  20301. (org-require-autoloaded-modules)
  20302. (if (fboundp 'customize-menu-create)
  20303. (progn
  20304. (easy-menu-change
  20305. '("Org") "Customize"
  20306. `(["Browse Org group" org-customize t]
  20307. "--"
  20308. ,(customize-menu-create 'org)
  20309. ["Set" Custom-set t]
  20310. ["Save" Custom-save t]
  20311. ["Reset to Current" Custom-reset-current t]
  20312. ["Reset to Saved" Custom-reset-saved t]
  20313. ["Reset to Standard Settings" Custom-reset-standard t]))
  20314. (message "\"Org\"-menu now contains full customization menu"))
  20315. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20316. ;;;; Miscellaneous stuff
  20317. ;;; Generally useful functions
  20318. (defsubst org-get-at-eol (property n)
  20319. "Get text property PROPERTY at the end of line less N characters."
  20320. (get-text-property (- (point-at-eol) n) property))
  20321. (defun org-find-text-property-in-string (prop s)
  20322. "Return the first non-nil value of property PROP in string S."
  20323. (or (get-text-property 0 prop s)
  20324. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20325. prop s)))
  20326. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20327. "Display the given MESSAGE as a warning."
  20328. (if (fboundp 'display-warning)
  20329. (display-warning 'org message
  20330. (if (featurep 'xemacs) 'warning :warning))
  20331. (let ((buf (get-buffer-create "*Org warnings*")))
  20332. (with-current-buffer buf
  20333. (goto-char (point-max))
  20334. (insert "Warning (Org): " message)
  20335. (unless (bolp)
  20336. (newline)))
  20337. (display-buffer buf)
  20338. (sit-for 0))))
  20339. (defun org-eval (form)
  20340. "Eval FORM and return result."
  20341. (condition-case error
  20342. (eval form)
  20343. (error (format "%%![Error: %s]" error))))
  20344. (defun org-in-clocktable-p ()
  20345. "Check if the cursor is in a clocktable."
  20346. (let ((pos (point)) start)
  20347. (save-excursion
  20348. (end-of-line 1)
  20349. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20350. (setq start (match-beginning 0))
  20351. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20352. (>= (match-end 0) pos)
  20353. start))))
  20354. (defun org-in-verbatim-emphasis ()
  20355. (save-match-data
  20356. (and (org-in-regexp org-emph-re 2)
  20357. (>= (point) (match-beginning 3))
  20358. (<= (point) (match-end 4))
  20359. (member (match-string 3) '("=" "~")))))
  20360. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20361. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20362. (if (and marker (marker-buffer marker)
  20363. (buffer-live-p (marker-buffer marker)))
  20364. (progn
  20365. (org-pop-to-buffer-same-window (marker-buffer marker))
  20366. (if (or (> marker (point-max)) (< marker (point-min)))
  20367. (widen))
  20368. (goto-char marker)
  20369. (org-show-context 'org-goto))
  20370. (if bookmark
  20371. (bookmark-jump bookmark)
  20372. (error "Cannot find location"))))
  20373. (defun org-quote-csv-field (s)
  20374. "Quote field for inclusion in CSV material."
  20375. (if (string-match "[\",]" s)
  20376. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20377. s))
  20378. (defun org-force-self-insert (N)
  20379. "Needed to enforce self-insert under remapping."
  20380. (interactive "p")
  20381. (self-insert-command N))
  20382. (defun org-string-width (s)
  20383. "Compute width of string, ignoring invisible characters.
  20384. This ignores character with invisibility property `org-link', and also
  20385. characters with property `org-cwidth', because these will become invisible
  20386. upon the next fontification round."
  20387. (let (b l)
  20388. (when (or (eq t buffer-invisibility-spec)
  20389. (assq 'org-link buffer-invisibility-spec))
  20390. (while (setq b (text-property-any 0 (length s)
  20391. 'invisible 'org-link s))
  20392. (setq s (concat (substring s 0 b)
  20393. (substring s (or (next-single-property-change
  20394. b 'invisible s)
  20395. (length s)))))))
  20396. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20397. (setq s (concat (substring s 0 b)
  20398. (substring s (or (next-single-property-change
  20399. b 'org-cwidth s)
  20400. (length s))))))
  20401. (setq l (string-width s) b -1)
  20402. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20403. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20404. l))
  20405. (defun org-shorten-string (s maxlength)
  20406. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20407. If the string is shorter or has length MAXLENGTH, just return the
  20408. original string. If it is longer, the functions finds a space in the
  20409. string, breaks this string off at that locations and adds three dots
  20410. as ellipsis. Including the ellipsis, the string will not be longer
  20411. than MAXLENGTH. If finding a good breaking point in the string does
  20412. not work, the string is just chopped off in the middle of a word
  20413. if necessary."
  20414. (if (<= (length s) maxlength)
  20415. s
  20416. (let* ((n (max (- maxlength 4) 1))
  20417. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20418. (if (string-match re s)
  20419. (concat (match-string 1 s) "...")
  20420. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20421. (defun org-get-indentation (&optional line)
  20422. "Get the indentation of the current line, interpreting tabs.
  20423. When LINE is given, assume it represents a line and compute its indentation."
  20424. (if line
  20425. (if (string-match "^ *" (org-remove-tabs line))
  20426. (match-end 0))
  20427. (save-excursion
  20428. (beginning-of-line 1)
  20429. (skip-chars-forward " \t")
  20430. (current-column))))
  20431. (defun org-get-string-indentation (s)
  20432. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20433. (let ((n -1) (i 0) (w tab-width) c)
  20434. (catch 'exit
  20435. (while (< (setq n (1+ n)) (length s))
  20436. (setq c (aref s n))
  20437. (cond ((= c ?\ ) (setq i (1+ i)))
  20438. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20439. (t (throw 'exit t)))))
  20440. i))
  20441. (defun org-remove-tabs (s &optional width)
  20442. "Replace tabulators in S with spaces.
  20443. Assumes that s is a single line, starting in column 0."
  20444. (setq width (or width tab-width))
  20445. (while (string-match "\t" s)
  20446. (setq s (replace-match
  20447. (make-string
  20448. (- (* width (/ (+ (match-beginning 0) width) width))
  20449. (match-beginning 0)) ?\ )
  20450. t t s)))
  20451. s)
  20452. (defun org-fix-indentation (line ind)
  20453. "Fix indentation in LINE.
  20454. IND is a cons cell with target and minimum indentation.
  20455. If the current indentation in LINE is smaller than the minimum,
  20456. leave it alone. If it is larger than ind, set it to the target."
  20457. (let* ((l (org-remove-tabs line))
  20458. (i (org-get-indentation l))
  20459. (i1 (car ind)) (i2 (cdr ind)))
  20460. (if (>= i i2) (setq l (substring line i2)))
  20461. (if (> i1 0)
  20462. (concat (make-string i1 ?\ ) l)
  20463. l)))
  20464. (defun org-remove-indentation (code &optional n)
  20465. "Remove the maximum common indentation from the lines in CODE.
  20466. N may optionally be the number of spaces to remove."
  20467. (with-temp-buffer
  20468. (insert code)
  20469. (org-do-remove-indentation n)
  20470. (buffer-string)))
  20471. (defun org-do-remove-indentation (&optional n)
  20472. "Remove the maximum common indentation from the buffer."
  20473. (untabify (point-min) (point-max))
  20474. (let ((min 10000) re)
  20475. (if n
  20476. (setq min n)
  20477. (goto-char (point-min))
  20478. (while (re-search-forward "^ *[^ \n]" nil t)
  20479. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20480. (unless (or (= min 0) (= min 10000))
  20481. (setq re (format "^ \\{%d\\}" min))
  20482. (goto-char (point-min))
  20483. (while (re-search-forward re nil t)
  20484. (replace-match "")
  20485. (end-of-line 1))
  20486. min)))
  20487. (defun org-fill-template (template alist)
  20488. "Find each %key of ALIST in TEMPLATE and replace it."
  20489. (let ((case-fold-search nil))
  20490. (dolist (entry (sort (copy-sequence alist)
  20491. (lambda (a b) (< (length (car a)) (length (car b))))))
  20492. (setq template
  20493. (replace-regexp-in-string
  20494. (concat "%" (regexp-quote (car entry)))
  20495. (or (cdr entry) "") template t t)))
  20496. template))
  20497. (defun org-base-buffer (buffer)
  20498. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20499. (if (not buffer)
  20500. buffer
  20501. (or (buffer-base-buffer buffer)
  20502. buffer)))
  20503. (defun org-wrap (string &optional width lines)
  20504. "Wrap string to either a number of lines, or a width in characters.
  20505. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20506. that costs. If there is a word longer than WIDTH, the text is actually
  20507. wrapped to the length of that word.
  20508. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20509. many lines, whatever width that takes.
  20510. The return value is a list of lines, without newlines at the end."
  20511. (let* ((words (org-split-string string "[ \t\n]+"))
  20512. (maxword (apply 'max (mapcar 'org-string-width words)))
  20513. w ll)
  20514. (cond (width
  20515. (org-do-wrap words (max maxword width)))
  20516. (lines
  20517. (setq w maxword)
  20518. (setq ll (org-do-wrap words maxword))
  20519. (if (<= (length ll) lines)
  20520. ll
  20521. (setq ll words)
  20522. (while (> (length ll) lines)
  20523. (setq w (1+ w))
  20524. (setq ll (org-do-wrap words w)))
  20525. ll))
  20526. (t (error "Cannot wrap this")))))
  20527. (defun org-do-wrap (words width)
  20528. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20529. (let (lines line)
  20530. (while words
  20531. (setq line (pop words))
  20532. (while (and words (< (+ (length line) (length (car words))) width))
  20533. (setq line (concat line " " (pop words))))
  20534. (setq lines (push line lines)))
  20535. (nreverse lines)))
  20536. (defun org-split-string (string &optional separators)
  20537. "Splits STRING into substrings at SEPARATORS.
  20538. SEPARATORS is a regular expression.
  20539. No empty strings are returned if there are matches at the beginning
  20540. and end of string."
  20541. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20542. (let ((start 0) notfirst list)
  20543. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20544. (if (and notfirst
  20545. (= start (match-beginning 0))
  20546. (< start (length string)))
  20547. (1+ start) start))
  20548. (< (match-beginning 0) (length string)))
  20549. (setq notfirst t)
  20550. (or (eq (match-beginning 0) 0)
  20551. (and (eq (match-beginning 0) (match-end 0))
  20552. (eq (match-beginning 0) start))
  20553. (push (substring string start (match-beginning 0)) list))
  20554. (setq start (match-end 0)))
  20555. (or (eq start (length string))
  20556. (push (substring string start) list))
  20557. (nreverse list)))
  20558. (defun org-quote-vert (s)
  20559. "Replace \"|\" with \"\\vert\"."
  20560. (while (string-match "|" s)
  20561. (setq s (replace-match "\\vert" t t s)))
  20562. s)
  20563. (defun org-uuidgen-p (s)
  20564. "Is S an ID created by UUIDGEN?"
  20565. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20566. (defun org-in-src-block-p (&optional inside)
  20567. "Whether point is in a code source block.
  20568. When INSIDE is non-nil, don't consider we are within a src block
  20569. when point is at #+BEGIN_SRC or #+END_SRC."
  20570. (let ((case-fold-search t) ov)
  20571. (or (and (eq (get-char-property (point) 'src-block) t))
  20572. (and (not inside)
  20573. (save-match-data
  20574. (save-excursion
  20575. (beginning-of-line)
  20576. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20577. (defun org-context ()
  20578. "Return a list of contexts of the current cursor position.
  20579. If several contexts apply, all are returned.
  20580. Each context entry is a list with a symbol naming the context, and
  20581. two positions indicating start and end of the context. Possible
  20582. contexts are:
  20583. :headline anywhere in a headline
  20584. :headline-stars on the leading stars in a headline
  20585. :todo-keyword on a TODO keyword (including DONE) in a headline
  20586. :tags on the TAGS in a headline
  20587. :priority on the priority cookie in a headline
  20588. :item on the first line of a plain list item
  20589. :item-bullet on the bullet/number of a plain list item
  20590. :checkbox on the checkbox in a plain list item
  20591. :table in an org-mode table
  20592. :table-special on a special filed in a table
  20593. :table-table in a table.el table
  20594. :clocktable in a clocktable
  20595. :src-block in a source block
  20596. :link on a hyperlink
  20597. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20598. :target on a <<target>>
  20599. :radio-target on a <<<radio-target>>>
  20600. :latex-fragment on a LaTeX fragment
  20601. :latex-preview on a LaTeX fragment with overlaid preview image
  20602. This function expects the position to be visible because it uses font-lock
  20603. faces as a help to recognize the following contexts: :table-special, :link,
  20604. and :keyword."
  20605. (let* ((f (get-text-property (point) 'face))
  20606. (faces (if (listp f) f (list f)))
  20607. (case-fold-search t)
  20608. (p (point)) clist o)
  20609. ;; First the large context
  20610. (cond
  20611. ((org-at-heading-p t)
  20612. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20613. (when (progn
  20614. (beginning-of-line 1)
  20615. (looking-at org-todo-line-tags-regexp))
  20616. (push (org-point-in-group p 1 :headline-stars) clist)
  20617. (push (org-point-in-group p 2 :todo-keyword) clist)
  20618. (push (org-point-in-group p 4 :tags) clist))
  20619. (goto-char p)
  20620. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20621. (if (looking-at "\\[#[A-Z0-9]\\]")
  20622. (push (org-point-in-group p 0 :priority) clist)))
  20623. ((org-at-item-p)
  20624. (push (org-point-in-group p 2 :item-bullet) clist)
  20625. (push (list :item (point-at-bol)
  20626. (save-excursion (org-end-of-item) (point)))
  20627. clist)
  20628. (and (org-at-item-checkbox-p)
  20629. (push (org-point-in-group p 0 :checkbox) clist)))
  20630. ((org-at-table-p)
  20631. (push (list :table (org-table-begin) (org-table-end)) clist)
  20632. (if (memq 'org-formula faces)
  20633. (push (list :table-special
  20634. (previous-single-property-change p 'face)
  20635. (next-single-property-change p 'face)) clist)))
  20636. ((org-at-table-p 'any)
  20637. (push (list :table-table) clist)))
  20638. (goto-char p)
  20639. (let ((case-fold-search t))
  20640. ;; New the "medium" contexts: clocktables, source blocks
  20641. (cond ((org-in-clocktable-p)
  20642. (push (list :clocktable
  20643. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20644. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20645. (match-beginning 1))
  20646. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20647. (match-end 0))) clist))
  20648. ((org-in-src-block-p)
  20649. (push (list :src-block
  20650. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20651. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20652. (match-beginning 1))
  20653. (and (search-forward "#+END_SRC" nil t)
  20654. (match-beginning 0))) clist))))
  20655. (goto-char p)
  20656. ;; Now the small context
  20657. (cond
  20658. ((org-at-timestamp-p)
  20659. (push (org-point-in-group p 0 :timestamp) clist))
  20660. ((memq 'org-link faces)
  20661. (push (list :link
  20662. (previous-single-property-change p 'face)
  20663. (next-single-property-change p 'face)) clist))
  20664. ((memq 'org-special-keyword faces)
  20665. (push (list :keyword
  20666. (previous-single-property-change p 'face)
  20667. (next-single-property-change p 'face)) clist))
  20668. ((org-at-target-p)
  20669. (push (org-point-in-group p 0 :target) clist)
  20670. (goto-char (1- (match-beginning 0)))
  20671. (if (looking-at org-radio-target-regexp)
  20672. (push (org-point-in-group p 0 :radio-target) clist))
  20673. (goto-char p))
  20674. ((setq o (org-some
  20675. (lambda (o)
  20676. (and (eq (overlay-get o 'org-overlay-type)
  20677. 'org-latex-overlay)
  20678. o))
  20679. (overlays-at (point))))
  20680. (push (list :latex-fragment
  20681. (overlay-start o) (overlay-end o)) clist)
  20682. (push (list :latex-preview
  20683. (overlay-start o) (overlay-end o)) clist))
  20684. ((org-inside-LaTeX-fragment-p)
  20685. ;; FIXME: positions wrong.
  20686. (push (list :latex-fragment (point) (point)) clist)))
  20687. (setq clist (nreverse (delq nil clist)))
  20688. clist))
  20689. (defun org-in-regexp (re &optional nlines visually)
  20690. "Check if point is inside a match of RE.
  20691. Normally only the current line is checked, but you can include
  20692. NLINES extra lines after point into the search. If VISUALLY is
  20693. set, require that the cursor is not after the match but really
  20694. on, so that the block visually is on the match."
  20695. (catch 'exit
  20696. (let ((pos (point))
  20697. (eol (point-at-eol (+ 1 (or nlines 0))))
  20698. (inc (if visually 1 0)))
  20699. (save-excursion
  20700. (beginning-of-line (- 1 (or nlines 0)))
  20701. (while (re-search-forward re eol t)
  20702. (if (and (<= (match-beginning 0) pos)
  20703. (>= (+ inc (match-end 0)) pos))
  20704. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20705. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20706. "Org mode 8.3")
  20707. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20708. "Non-nil when point is between matches of START-RE and END-RE.
  20709. Also return a non-nil value when point is on one of the matches.
  20710. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20711. buffer positions. Default values are the positions of headlines
  20712. surrounding the point.
  20713. The functions returns a cons cell whose car (resp. cdr) is the
  20714. position before START-RE (resp. after END-RE)."
  20715. (save-match-data
  20716. (let ((pos (point))
  20717. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20718. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20719. beg end)
  20720. (save-excursion
  20721. ;; Point is on a block when on START-RE or if START-RE can be
  20722. ;; found before it...
  20723. (and (or (org-in-regexp start-re)
  20724. (re-search-backward start-re limit-up t))
  20725. (setq beg (match-beginning 0))
  20726. ;; ... and END-RE after it...
  20727. (goto-char (match-end 0))
  20728. (re-search-forward end-re limit-down t)
  20729. (> (setq end (match-end 0)) pos)
  20730. ;; ... without another START-RE in-between.
  20731. (goto-char (match-beginning 0))
  20732. (not (re-search-backward start-re (1+ beg) t))
  20733. ;; Return value.
  20734. (cons beg end))))))
  20735. (defun org-in-block-p (names)
  20736. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20737. NAMES is a list of strings containing names of blocks.
  20738. Return first block name matched, or nil. Beware that in case of
  20739. nested blocks, the returned name may not belong to the closest
  20740. block from point."
  20741. (save-match-data
  20742. (catch 'exit
  20743. (let ((case-fold-search t)
  20744. (lim-up (save-excursion (outline-previous-heading)))
  20745. (lim-down (save-excursion (outline-next-heading))))
  20746. (mapc (lambda (name)
  20747. (let ((n (regexp-quote name)))
  20748. (when (org-between-regexps-p
  20749. (concat "^[ \t]*#\\+begin_" n)
  20750. (concat "^[ \t]*#\\+end_" n)
  20751. lim-up lim-down)
  20752. (throw 'exit n))))
  20753. names))
  20754. nil)))
  20755. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20756. "Call `multi-occur' with buffers for all agenda files."
  20757. (interactive "sOrg-files matching: ")
  20758. (let* ((files (org-agenda-files))
  20759. (tnames (mapcar #'file-truename files))
  20760. (extra org-agenda-text-search-extra-files))
  20761. (when (eq (car extra) 'agenda-archives)
  20762. (setq extra (cdr extra))
  20763. (setq files (org-add-archive-files files)))
  20764. (dolist (f extra)
  20765. (unless (member (file-truename f) tnames)
  20766. (unless (member f files) (setq files (append files (list f))))
  20767. (setq tnames (append tnames (list (file-truename f))))))
  20768. (multi-occur
  20769. (mapcar (lambda (x)
  20770. (with-current-buffer
  20771. ;; FIXME: Why not just (find-file-noselect x)?
  20772. ;; Is it to avoid the "revert buffer" prompt?
  20773. (or (get-file-buffer x) (find-file-noselect x))
  20774. (widen)
  20775. (current-buffer)))
  20776. files)
  20777. regexp)))
  20778. (if (boundp 'occur-mode-find-occurrence-hook)
  20779. ;; Emacs 23
  20780. (add-hook 'occur-mode-find-occurrence-hook
  20781. (lambda ()
  20782. (when (derived-mode-p 'org-mode)
  20783. (org-reveal))))
  20784. ;; Emacs 22
  20785. (defadvice occur-mode-goto-occurrence
  20786. (after org-occur-reveal activate)
  20787. (and (derived-mode-p 'org-mode) (org-reveal)))
  20788. (defadvice occur-mode-goto-occurrence-other-window
  20789. (after org-occur-reveal activate)
  20790. (and (derived-mode-p 'org-mode) (org-reveal)))
  20791. (defadvice occur-mode-display-occurrence
  20792. (after org-occur-reveal activate)
  20793. (when (derived-mode-p 'org-mode)
  20794. (let ((pos (occur-mode-find-occurrence)))
  20795. (with-current-buffer (marker-buffer pos)
  20796. (save-excursion
  20797. (goto-char pos)
  20798. (org-reveal)))))))
  20799. (defun org-occur-link-in-agenda-files ()
  20800. "Create a link and search for it in the agendas.
  20801. The link is not stored in `org-stored-links', it is just created
  20802. for the search purpose."
  20803. (interactive)
  20804. (let ((link (condition-case nil
  20805. (org-store-link nil)
  20806. (error "Unable to create a link to here"))))
  20807. (org-occur-in-agenda-files (regexp-quote link))))
  20808. (defun org-reverse-string (string)
  20809. "Return the reverse of STRING."
  20810. (apply 'string (reverse (string-to-list string))))
  20811. ;; defsubst org-uniquify must be defined before first use
  20812. (defun org-uniquify-alist (alist)
  20813. "Merge elements of ALIST with the same key.
  20814. For example, in this alist:
  20815. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20816. => \\='((a 1 3) (b 2))
  20817. merge (a 1) and (a 3) into (a 1 3).
  20818. The function returns the new ALIST."
  20819. (let (rtn)
  20820. (mapc
  20821. (lambda (e)
  20822. (let (n)
  20823. (if (not (assoc (car e) rtn))
  20824. (push e rtn)
  20825. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20826. (setq rtn (assq-delete-all (car e) rtn))
  20827. (push n rtn))))
  20828. alist)
  20829. rtn))
  20830. (defun org-delete-all (elts list)
  20831. "Remove all elements in ELTS from LIST."
  20832. (while elts
  20833. (setq list (delete (pop elts) list)))
  20834. list)
  20835. (defun org-count (cl-item cl-seq)
  20836. "Count the number of occurrences of ITEM in SEQ.
  20837. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20838. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20839. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20840. (while (< cl-start cl-end)
  20841. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20842. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20843. (setq cl-start (1+ cl-start)))
  20844. cl-count))
  20845. (defun org-remove-if (predicate seq)
  20846. "Remove everything from SEQ that fulfills PREDICATE."
  20847. (let (res e)
  20848. (while seq
  20849. (setq e (pop seq))
  20850. (if (not (funcall predicate e)) (push e res)))
  20851. (nreverse res)))
  20852. (defun org-remove-if-not (predicate seq)
  20853. "Remove everything from SEQ that does not fulfill PREDICATE."
  20854. (let (res e)
  20855. (while seq
  20856. (setq e (pop seq))
  20857. (if (funcall predicate e) (push e res)))
  20858. (nreverse res)))
  20859. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20860. "Reduce two-argument FUNCTION across SEQ.
  20861. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20862. \":initial-value\" removed."
  20863. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20864. (cadr (memq :initial-value cl-keys)))
  20865. (cl-seq (pop cl-seq))
  20866. (t (funcall cl-func)))))
  20867. (while cl-seq
  20868. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20869. cl-accum))
  20870. (defun org-every (pred seq)
  20871. "Return true if PREDICATE is true of every element of SEQ.
  20872. Adapted from `every' in cl.el."
  20873. (catch 'org-every
  20874. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20875. t))
  20876. (defun org-some (pred seq)
  20877. "Return true if PREDICATE is true of any element of SEQ.
  20878. Adapted from `some' in cl.el."
  20879. (catch 'org-some
  20880. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20881. nil))
  20882. (defun org-back-over-empty-lines ()
  20883. "Move backwards over whitespace, to the beginning of the first empty line.
  20884. Returns the number of empty lines passed."
  20885. (let ((pos (point)))
  20886. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20887. (skip-chars-backward " \t\n\r")
  20888. (unless (eobp)
  20889. (forward-line -1)))
  20890. (beginning-of-line 2)
  20891. (goto-char (min (point) pos))
  20892. (count-lines (point) pos)))
  20893. (defun org-skip-whitespace ()
  20894. (skip-chars-forward " \t\n\r"))
  20895. (defun org-point-in-group (point group &optional context)
  20896. "Check if POINT is in match-group GROUP.
  20897. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20898. match. If the match group does not exist or point is not inside it,
  20899. return nil."
  20900. (and (match-beginning group)
  20901. (>= point (match-beginning group))
  20902. (<= point (match-end group))
  20903. (if context
  20904. (list context (match-beginning group) (match-end group))
  20905. t)))
  20906. (defun org-switch-to-buffer-other-window (&rest args)
  20907. "Switch to buffer in a second window on the current frame.
  20908. In particular, do not allow pop-up frames.
  20909. Returns the newly created buffer."
  20910. (org-no-popups
  20911. (apply 'switch-to-buffer-other-window args)))
  20912. (defun org-combine-plists (&rest plists)
  20913. "Create a single property list from all plists in PLISTS.
  20914. The process starts by copying the first list, and then setting properties
  20915. from the other lists. Settings in the last list are the most significant
  20916. ones and overrule settings in the other lists."
  20917. (let ((rtn (copy-sequence (pop plists)))
  20918. p v ls)
  20919. (while plists
  20920. (setq ls (pop plists))
  20921. (while ls
  20922. (setq p (pop ls) v (pop ls))
  20923. (setq rtn (plist-put rtn p v))))
  20924. rtn))
  20925. (defun org-replace-escapes (string table)
  20926. "Replace %-escapes in STRING with values in TABLE.
  20927. TABLE is an association list with keys like \"%a\" and string values.
  20928. The sequences in STRING may contain normal field width and padding information,
  20929. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20930. so values can contain further %-escapes if they are define later in TABLE."
  20931. (let ((tbl (copy-alist table))
  20932. (case-fold-search nil)
  20933. (pchg 0)
  20934. e re rpl)
  20935. (dolist (e tbl)
  20936. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20937. (when (and (cdr e) (string-match re (cdr e)))
  20938. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20939. (safe "SREF"))
  20940. (add-text-properties 0 3 (list 'sref sref) safe)
  20941. (setcdr e (replace-match safe t t (cdr e)))))
  20942. (while (string-match re string)
  20943. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20944. (cdr e)))
  20945. (setq string (replace-match rpl t t string))))
  20946. (while (setq pchg (next-property-change pchg string))
  20947. (let ((sref (get-text-property pchg 'sref string)))
  20948. (when (and sref (string-match "SREF" string pchg))
  20949. (setq string (replace-match sref t t string)))))
  20950. string))
  20951. (defun org-sublist (list start end)
  20952. "Return a section of LIST, from START to END.
  20953. Counting starts at 1."
  20954. (let (rtn (c start))
  20955. (setq list (nthcdr (1- start) list))
  20956. (while (and list (<= c end))
  20957. (push (pop list) rtn)
  20958. (setq c (1+ c)))
  20959. (nreverse rtn)))
  20960. (defun org-find-base-buffer-visiting (file)
  20961. "Like `find-buffer-visiting' but always return the base buffer and
  20962. not an indirect buffer."
  20963. (let ((buf (or (get-file-buffer file)
  20964. (find-buffer-visiting file))))
  20965. (if buf
  20966. (or (buffer-base-buffer buf) buf)
  20967. nil)))
  20968. (defun org-image-file-name-regexp (&optional extensions)
  20969. "Return regexp matching the file names of images.
  20970. If EXTENSIONS is given, only match these."
  20971. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20972. (image-file-name-regexp)
  20973. (let ((image-file-name-extensions
  20974. (or extensions
  20975. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20976. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20977. (concat "\\."
  20978. (regexp-opt (nconc (mapcar 'upcase
  20979. image-file-name-extensions)
  20980. image-file-name-extensions)
  20981. t)
  20982. "\\'"))))
  20983. (defun org-file-image-p (file &optional extensions)
  20984. "Return non-nil if FILE is an image."
  20985. (save-match-data
  20986. (string-match (org-image-file-name-regexp extensions) file)))
  20987. (defun org-get-cursor-date (&optional with-time)
  20988. "Return the date at cursor in as a time.
  20989. This works in the calendar and in the agenda, anywhere else it just
  20990. returns the current time.
  20991. If WITH-TIME is non-nil, returns the time of the event at point (in
  20992. the agenda) or the current time of the day."
  20993. (let (date day defd tp hod mod)
  20994. (when with-time
  20995. (setq tp (get-text-property (point) 'time))
  20996. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20997. (setq hod (string-to-number (match-string 1 tp))
  20998. mod (string-to-number (match-string 2 tp))))
  20999. (or tp (let ((now (decode-time)))
  21000. (setq hod (nth 2 now)
  21001. mod (nth 1 now)))))
  21002. (cond
  21003. ((eq major-mode 'calendar-mode)
  21004. (setq date (calendar-cursor-to-date)
  21005. defd (encode-time 0 (or mod 0) (or hod 0)
  21006. (nth 1 date) (nth 0 date) (nth 2 date))))
  21007. ((eq major-mode 'org-agenda-mode)
  21008. (setq day (get-text-property (point) 'day))
  21009. (if day
  21010. (setq date (calendar-gregorian-from-absolute day)
  21011. defd (encode-time 0 (or mod 0) (or hod 0)
  21012. (nth 1 date) (nth 0 date) (nth 2 date))))))
  21013. (or defd (current-time))))
  21014. (defun org-mark-subtree (&optional up)
  21015. "Mark the current subtree.
  21016. This puts point at the start of the current subtree, and mark at
  21017. the end. If a numeric prefix UP is given, move up into the
  21018. hierarchy of headlines by UP levels before marking the subtree."
  21019. (interactive "P")
  21020. (org-with-limited-levels
  21021. (cond ((org-at-heading-p) (beginning-of-line))
  21022. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  21023. (t (outline-previous-visible-heading 1))))
  21024. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  21025. (if (org-called-interactively-p 'any)
  21026. (call-interactively 'org-mark-element)
  21027. (org-mark-element)))
  21028. ;;; Indentation
  21029. (defun org--get-expected-indentation (element contentsp)
  21030. "Expected indentation column for current line, according to ELEMENT.
  21031. ELEMENT is an element containing point. CONTENTSP is non-nil
  21032. when indentation is to be computed according to contents of
  21033. ELEMENT."
  21034. (let ((type (org-element-type element))
  21035. (start (org-element-property :begin element))
  21036. (post-affiliated (org-element-property :post-affiliated element)))
  21037. (org-with-wide-buffer
  21038. (cond
  21039. (contentsp
  21040. (case type
  21041. ((diary-sexp footnote-definition) 0)
  21042. ((headline inlinetask nil)
  21043. (if (not org-adapt-indentation) 0
  21044. (let ((level (org-current-level)))
  21045. (if level (1+ level) 0))))
  21046. ((item plain-list) (org-list-item-body-column post-affiliated))
  21047. (t
  21048. (goto-char start)
  21049. (org-get-indentation))))
  21050. ((memq type '(headline inlinetask nil))
  21051. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21052. (org--get-expected-indentation element t)
  21053. 0))
  21054. ((memq type '(diary-sexp footnote-definition)) 0)
  21055. ;; First paragraph of a footnote definition or an item.
  21056. ;; Indent like parent.
  21057. ((< (line-beginning-position) start)
  21058. (org--get-expected-indentation
  21059. (org-element-property :parent element) t))
  21060. ;; At first line: indent according to previous sibling, if any,
  21061. ;; ignoring footnote definitions and inline tasks, or parent's
  21062. ;; contents.
  21063. ((= (line-beginning-position) start)
  21064. (catch 'exit
  21065. (while t
  21066. (if (= (point-min) start) (throw 'exit 0)
  21067. (goto-char (1- start))
  21068. (let* ((previous (org-element-at-point))
  21069. (parent previous))
  21070. (while (and parent (<= (org-element-property :end parent) start))
  21071. (setq previous parent
  21072. parent (org-element-property :parent parent)))
  21073. (cond
  21074. ((not previous) (throw 'exit 0))
  21075. ((> (org-element-property :end previous) start)
  21076. (throw 'exit (org--get-expected-indentation previous t)))
  21077. ((memq (org-element-type previous)
  21078. '(footnote-definition inlinetask))
  21079. (setq start (org-element-property :begin previous)))
  21080. (t (goto-char (org-element-property :begin previous))
  21081. (throw 'exit
  21082. (if (bolp) (org-get-indentation)
  21083. ;; At first paragraph in an item or
  21084. ;; a footnote definition.
  21085. (org--get-expected-indentation
  21086. (org-element-property :parent previous) t))))))))))
  21087. ;; Otherwise, move to the first non-blank line above.
  21088. (t
  21089. (beginning-of-line)
  21090. (let ((pos (point)))
  21091. (skip-chars-backward " \r\t\n")
  21092. (cond
  21093. ;; Two blank lines end a footnote definition or a plain
  21094. ;; list. When we indent an empty line after them, the
  21095. ;; containing list or footnote definition is over, so it
  21096. ;; qualifies as a previous sibling. Therefore, we indent
  21097. ;; like its first line.
  21098. ((and (memq type '(footnote-definition plain-list))
  21099. (> (count-lines (point) pos) 2))
  21100. (goto-char start)
  21101. (org-get-indentation))
  21102. ;; Line above is the first one of a paragraph at the
  21103. ;; beginning of an item or a footnote definition. Indent
  21104. ;; like parent.
  21105. ((< (line-beginning-position) start)
  21106. (org--get-expected-indentation
  21107. (org-element-property :parent element) t))
  21108. ;; Line above is the beginning of an element, i.e., point
  21109. ;; was originally on the blank lines between element's start
  21110. ;; and contents.
  21111. ((= (line-beginning-position) post-affiliated)
  21112. (org--get-expected-indentation element t))
  21113. ;; POS is after contents in a greater element. Indent like
  21114. ;; the beginning of the element.
  21115. ;;
  21116. ;; As a special case, if point is at the end of a footnote
  21117. ;; definition or an item, indent like the very last element
  21118. ;; within.
  21119. ((and (not (eq type 'paragraph))
  21120. (let ((cend (org-element-property :contents-end element)))
  21121. (and cend (<= cend pos))))
  21122. (if (memq type '(footnote-definition item plain-list))
  21123. (let ((last (org-element-at-point)))
  21124. (org--get-expected-indentation
  21125. last
  21126. (memq (org-element-type last)
  21127. '(footnote-definition item plain-list))))
  21128. (goto-char start)
  21129. (org-get-indentation)))
  21130. ;; In any other case, indent like the current line.
  21131. (t (org-get-indentation)))))))))
  21132. (defun org--align-node-property ()
  21133. "Align node property at point.
  21134. Alignment is done according to `org-property-format', which see."
  21135. (when (save-excursion
  21136. (beginning-of-line)
  21137. (looking-at org-property-re))
  21138. (replace-match
  21139. (concat (match-string 4)
  21140. (org-trim
  21141. (format org-property-format (match-string 1) (match-string 3))))
  21142. t t)))
  21143. (defun org-indent-line ()
  21144. "Indent line depending on context.
  21145. Indentation is done according to the following rules:
  21146. - Footnote definitions, diary sexps, headlines and inline tasks
  21147. have to start at column 0.
  21148. - On the very first line of an element, consider, in order, the
  21149. next rules until one matches:
  21150. 1. If there's a sibling element before, ignoring footnote
  21151. definitions and inline tasks, indent like its first line.
  21152. 2. If element has a parent, indent like its contents. More
  21153. precisely, if parent is an item, indent after the
  21154. description part, if any, or the bullet (see
  21155. `org-list-description-max-indent'). Else, indent like
  21156. parent's first line.
  21157. 3. Otherwise, indent relatively to current level, if
  21158. `org-adapt-indentation' is non-nil, or to left margin.
  21159. - On a blank line at the end of an element, indent according to
  21160. the type of the element. More precisely
  21161. 1. If element is a plain list, an item, or a footnote
  21162. definition, indent like the very last element within.
  21163. 2. If element is a paragraph, indent like its last non blank
  21164. line.
  21165. 3. Otherwise, indent like its very first line.
  21166. - In the code part of a source block, use language major mode
  21167. to indent current line if `org-src-tab-acts-natively' is
  21168. non-nil. If it is nil, do nothing.
  21169. - Otherwise, indent like the first non-blank line above.
  21170. The function doesn't indent an item as it could break the whole
  21171. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21172. \\[org-shiftmetaright].
  21173. Also align node properties according to `org-property-format'."
  21174. (interactive)
  21175. (cond
  21176. (orgstruct-is-++
  21177. (let ((indent-line-function
  21178. (cadadr (assq 'indent-line-function org-fb-vars))))
  21179. (indent-according-to-mode)))
  21180. ((org-at-heading-p) 'noindent)
  21181. (t
  21182. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21183. (type (org-element-type element)))
  21184. (cond ((and (memq type '(plain-list item))
  21185. (= (line-beginning-position)
  21186. (org-element-property :post-affiliated element)))
  21187. 'noindent)
  21188. ((and (eq type 'src-block)
  21189. org-src-tab-acts-natively
  21190. (> (line-beginning-position)
  21191. (org-element-property :post-affiliated element))
  21192. (< (line-beginning-position)
  21193. (org-with-wide-buffer
  21194. (goto-char (org-element-property :end element))
  21195. (skip-chars-backward " \r\t\n")
  21196. (line-beginning-position))))
  21197. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21198. (t
  21199. (let ((column (org--get-expected-indentation element nil)))
  21200. ;; Preserve current column.
  21201. (if (<= (current-column) (current-indentation))
  21202. (org-indent-line-to column)
  21203. (save-excursion (org-indent-line-to column))))
  21204. ;; Align node property. Also preserve current column.
  21205. (when (eq type 'node-property)
  21206. (let ((column (current-column)))
  21207. (org--align-node-property)
  21208. (org-move-to-column column)))))))))
  21209. (defun org-indent-region (start end)
  21210. "Indent each non-blank line in the region.
  21211. Called from a program, START and END specify the region to
  21212. indent. The function will not indent contents of example blocks,
  21213. verse blocks and export blocks as leading white spaces are
  21214. assumed to be significant there."
  21215. (interactive "r")
  21216. (save-excursion
  21217. (goto-char start)
  21218. (skip-chars-forward " \r\t\n")
  21219. (unless (eobp) (beginning-of-line))
  21220. (let ((indent-to
  21221. (lambda (ind pos)
  21222. ;; Set IND as indentation for all lines between point and
  21223. ;; POS or END, whichever comes first. Blank lines are
  21224. ;; ignored. Leave point after POS once done.
  21225. (let ((limit (copy-marker (min end pos))))
  21226. (while (< (point) limit)
  21227. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21228. (forward-line))
  21229. (set-marker limit nil))))
  21230. (end (copy-marker end)))
  21231. (while (< (point) end)
  21232. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21233. (let* ((element (org-element-at-point))
  21234. (type (org-element-type element))
  21235. (element-end (copy-marker (org-element-property :end element)))
  21236. (ind (org--get-expected-indentation element nil)))
  21237. (cond
  21238. ((or (memq type '(paragraph table table-row))
  21239. (not (or (org-element-property :contents-begin element)
  21240. (memq type
  21241. '(example-block export-block src-block)))))
  21242. ;; Elements here are indented as a single block. Also
  21243. ;; align node properties.
  21244. (when (eq type 'node-property)
  21245. (org--align-node-property)
  21246. (beginning-of-line))
  21247. (funcall indent-to ind element-end))
  21248. (t
  21249. ;; Elements in this category consist of three parts:
  21250. ;; before the contents, the contents, and after the
  21251. ;; contents. The contents are treated specially,
  21252. ;; according to the element type, or not indented at
  21253. ;; all. Other parts are indented as a single block.
  21254. (let* ((post (copy-marker
  21255. (org-element-property :post-affiliated element)))
  21256. (cbeg
  21257. (copy-marker
  21258. (cond
  21259. ((not (org-element-property :contents-begin element))
  21260. ;; Fake contents for source blocks.
  21261. (org-with-wide-buffer
  21262. (goto-char post)
  21263. (forward-line)
  21264. (point)))
  21265. ((memq type '(footnote-definition item plain-list))
  21266. ;; Contents in these elements could start on
  21267. ;; the same line as the beginning of the
  21268. ;; element. Make sure we start indenting
  21269. ;; from the second line.
  21270. (org-with-wide-buffer
  21271. (goto-char post)
  21272. (end-of-line)
  21273. (skip-chars-forward " \r\t\n")
  21274. (if (eobp) (point) (line-beginning-position))))
  21275. (t (org-element-property :contents-begin element)))))
  21276. (cend (copy-marker
  21277. (or (org-element-property :contents-end element)
  21278. ;; Fake contents for source blocks.
  21279. (org-with-wide-buffer
  21280. (goto-char element-end)
  21281. (skip-chars-backward " \r\t\n")
  21282. (line-beginning-position)))
  21283. t)))
  21284. ;; Do not change items indentation individually as it
  21285. ;; might break the list as a whole. On the other
  21286. ;; hand, when at a plain list, indent it as a whole.
  21287. (cond ((eq type 'plain-list)
  21288. (let ((offset (- ind (org-get-indentation))))
  21289. (unless (zerop offset)
  21290. (indent-rigidly (org-element-property :begin element)
  21291. (org-element-property :end element)
  21292. offset))
  21293. (goto-char cbeg)))
  21294. ((eq type 'item) (goto-char cbeg))
  21295. (t (funcall indent-to ind cbeg)))
  21296. (when (< (point) end)
  21297. (case type
  21298. ((example-block export-block verse-block))
  21299. (src-block
  21300. ;; In a source block, indent source code
  21301. ;; according to language major mode, but only if
  21302. ;; `org-src-tab-acts-natively' is non-nil.
  21303. (when (and (< (point) end) org-src-tab-acts-natively)
  21304. (ignore-errors
  21305. (org-babel-do-in-edit-buffer
  21306. (indent-region (point-min) (point-max))))))
  21307. (t (org-indent-region (point) (min cend end))))
  21308. (goto-char (min cend end))
  21309. (when (< (point) end) (funcall indent-to ind element-end)))
  21310. (set-marker post nil)
  21311. (set-marker cbeg nil)
  21312. (set-marker cend nil))))
  21313. (set-marker element-end nil))))
  21314. (set-marker end nil))))
  21315. (defun org-indent-drawer ()
  21316. "Indent the drawer at point."
  21317. (interactive)
  21318. (unless (save-excursion
  21319. (beginning-of-line)
  21320. (org-looking-at-p org-drawer-regexp))
  21321. (user-error "Not at a drawer"))
  21322. (let ((element (org-element-at-point)))
  21323. (unless (memq (org-element-type element) '(drawer property-drawer))
  21324. (user-error "Not at a drawer"))
  21325. (org-with-wide-buffer
  21326. (org-indent-region (org-element-property :begin element)
  21327. (org-element-property :end element))))
  21328. (message "Drawer at point indented"))
  21329. (defun org-indent-block ()
  21330. "Indent the block at point."
  21331. (interactive)
  21332. (unless (save-excursion
  21333. (beginning-of-line)
  21334. (let ((case-fold-search t))
  21335. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21336. (user-error "Not at a block"))
  21337. (let ((element (org-element-at-point)))
  21338. (unless (memq (org-element-type element)
  21339. '(comment-block center-block dynamic-block example-block
  21340. export-block quote-block special-block
  21341. src-block verse-block))
  21342. (user-error "Not at a block"))
  21343. (org-with-wide-buffer
  21344. (org-indent-region (org-element-property :begin element)
  21345. (org-element-property :end element))))
  21346. (message "Block at point indented"))
  21347. ;;; Filling
  21348. ;; We use our own fill-paragraph and auto-fill functions.
  21349. ;; `org-fill-paragraph' relies on adaptive filling and context
  21350. ;; checking. Appropriate `fill-prefix' is computed with
  21351. ;; `org-adaptive-fill-function'.
  21352. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21353. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21354. ;; `org-adaptive-fill-function' value. Internally,
  21355. ;; `org-comment-line-break-function' breaks the line.
  21356. ;; `org-setup-filling' installs filling and auto-filling related
  21357. ;; variables during `org-mode' initialization.
  21358. (defvar org-element-paragraph-separate) ; org-element.el
  21359. (defun org-setup-filling ()
  21360. (require 'org-element)
  21361. ;; Prevent auto-fill from inserting unwanted new items.
  21362. (when (boundp 'fill-nobreak-predicate)
  21363. (org-set-local
  21364. 'fill-nobreak-predicate
  21365. (org-uniquify
  21366. (append fill-nobreak-predicate
  21367. '(org-fill-line-break-nobreak-p
  21368. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21369. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21370. (org-set-local 'paragraph-start paragraph-ending)
  21371. (org-set-local 'paragraph-separate paragraph-ending))
  21372. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21373. (org-set-local 'auto-fill-inhibit-regexp nil)
  21374. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21375. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21376. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21377. (defun org-fill-line-break-nobreak-p ()
  21378. "Non-nil when a new line at point would create an Org line break."
  21379. (save-excursion
  21380. (skip-chars-backward "[ \t]")
  21381. (skip-chars-backward "\\\\")
  21382. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21383. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21384. "Non-nil when a new line at point would split a timestamp."
  21385. (and (org-at-timestamp-p t)
  21386. (not (looking-at org-ts-regexp-both))))
  21387. (declare-function message-in-body-p "message" ())
  21388. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21389. (defun org-adaptive-fill-function ()
  21390. "Compute a fill prefix for the current line.
  21391. Return fill prefix, as a string, or nil if current line isn't
  21392. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21393. matches in paragraphs or comments, use it."
  21394. (catch 'exit
  21395. (when (derived-mode-p 'message-mode)
  21396. (save-excursion
  21397. (beginning-of-line)
  21398. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21399. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21400. ((looking-at message-cite-prefix-regexp)
  21401. (throw 'exit (match-string-no-properties 0)))
  21402. ((looking-at org-outline-regexp)
  21403. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21404. (org-with-wide-buffer
  21405. (unless (org-at-heading-p)
  21406. (let* ((p (line-beginning-position))
  21407. (element (save-excursion
  21408. (beginning-of-line)
  21409. (org-element-at-point)))
  21410. (type (org-element-type element))
  21411. (post-affiliated (org-element-property :post-affiliated element)))
  21412. (unless (< p post-affiliated)
  21413. (case type
  21414. (comment
  21415. (save-excursion
  21416. (beginning-of-line)
  21417. (looking-at "[ \t]*")
  21418. (concat (match-string 0) "# ")))
  21419. (footnote-definition "")
  21420. ((item plain-list)
  21421. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21422. (paragraph
  21423. ;; Fill prefix is usually the same as the current line,
  21424. ;; unless the paragraph is at the beginning of an item.
  21425. (let ((parent (org-element-property :parent element)))
  21426. (save-excursion
  21427. (beginning-of-line)
  21428. (cond ((eq (org-element-type parent) 'item)
  21429. (make-string (org-list-item-body-column
  21430. (org-element-property :begin parent))
  21431. ?\s))
  21432. ((and adaptive-fill-regexp
  21433. ;; Locally disable
  21434. ;; `adaptive-fill-function' to let
  21435. ;; `fill-context-prefix' handle
  21436. ;; `adaptive-fill-regexp' variable.
  21437. (let (adaptive-fill-function)
  21438. (fill-context-prefix
  21439. post-affiliated
  21440. (org-element-property :end element)))))
  21441. ((looking-at "[ \t]+") (match-string 0))
  21442. (t "")))))
  21443. (comment-block
  21444. ;; Only fill contents if P is within block boundaries.
  21445. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21446. (forward-line)
  21447. (point)))
  21448. (cend (save-excursion
  21449. (goto-char (org-element-property :end element))
  21450. (skip-chars-backward " \r\t\n")
  21451. (line-beginning-position))))
  21452. (when (and (>= p cbeg) (< p cend))
  21453. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21454. (match-string 0)
  21455. "")))))))))))
  21456. (declare-function message-goto-body "message" ())
  21457. (defvar message-cite-prefix-regexp) ; From message.el
  21458. (defun org-fill-paragraph (&optional justify)
  21459. "Fill element at point, when applicable.
  21460. This function only applies to comment blocks, comments, example
  21461. blocks and paragraphs. Also, as a special case, re-align table
  21462. when point is at one.
  21463. If JUSTIFY is non-nil (interactively, with prefix argument),
  21464. justify as well. If `sentence-end-double-space' is non-nil, then
  21465. period followed by one space does not end a sentence, so don't
  21466. break a line there. The variable `fill-column' controls the
  21467. width for filling.
  21468. For convenience, when point is at a plain list, an item or
  21469. a footnote definition, try to fill the first paragraph within."
  21470. (interactive)
  21471. (if (and (derived-mode-p 'message-mode)
  21472. (or (not (message-in-body-p))
  21473. (save-excursion (move-beginning-of-line 1)
  21474. (looking-at message-cite-prefix-regexp))))
  21475. ;; First ensure filling is correct in message-mode.
  21476. (let ((fill-paragraph-function
  21477. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21478. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21479. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21480. (paragraph-separate
  21481. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21482. (fill-paragraph nil))
  21483. (with-syntax-table org-mode-transpose-word-syntax-table
  21484. ;; Move to end of line in order to get the first paragraph
  21485. ;; within a plain list or a footnote definition.
  21486. (let ((element (save-excursion
  21487. (end-of-line)
  21488. (or (ignore-errors (org-element-at-point))
  21489. (user-error "An element cannot be parsed line %d"
  21490. (line-number-at-pos (point)))))))
  21491. ;; First check if point is in a blank line at the beginning of
  21492. ;; the buffer. In that case, ignore filling.
  21493. (case (org-element-type element)
  21494. ;; Use major mode filling function is src blocks.
  21495. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21496. ;; Align Org tables, leave table.el tables as-is.
  21497. (table-row (org-table-align) t)
  21498. (table
  21499. (when (eq (org-element-property :type element) 'org)
  21500. (save-excursion
  21501. (goto-char (org-element-property :post-affiliated element))
  21502. (org-table-align)))
  21503. t)
  21504. (paragraph
  21505. ;; Paragraphs may contain `line-break' type objects.
  21506. (let ((beg (max (point-min)
  21507. (org-element-property :contents-begin element)))
  21508. (end (min (point-max)
  21509. (org-element-property :contents-end element))))
  21510. ;; Do nothing if point is at an affiliated keyword.
  21511. (if (< (line-end-position) beg) t
  21512. (when (derived-mode-p 'message-mode)
  21513. ;; In `message-mode', do not fill following citation
  21514. ;; in current paragraph nor text before message body.
  21515. (let ((body-start (save-excursion (message-goto-body))))
  21516. (when body-start (setq beg (max body-start beg))))
  21517. (when (save-excursion
  21518. (re-search-forward
  21519. (concat "^" message-cite-prefix-regexp) end t))
  21520. (setq end (match-beginning 0))))
  21521. ;; Fill paragraph, taking line breaks into account.
  21522. (save-excursion
  21523. (goto-char beg)
  21524. (let ((cuts (list beg)))
  21525. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21526. (when (eq 'line-break
  21527. (org-element-type
  21528. (save-excursion (backward-char)
  21529. (org-element-context))))
  21530. (push (point) cuts)))
  21531. (dolist (c (delq end cuts))
  21532. (fill-region-as-paragraph c end justify)
  21533. (setq end c))))
  21534. t)))
  21535. ;; Contents of `comment-block' type elements should be
  21536. ;; filled as plain text, but only if point is within block
  21537. ;; markers.
  21538. (comment-block
  21539. (let* ((case-fold-search t)
  21540. (beg (save-excursion
  21541. (goto-char (org-element-property :begin element))
  21542. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21543. (forward-line)
  21544. (point)))
  21545. (end (save-excursion
  21546. (goto-char (org-element-property :end element))
  21547. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21548. (line-beginning-position))))
  21549. (if (or (< (point) beg) (> (point) end)) t
  21550. (fill-region-as-paragraph
  21551. (save-excursion (end-of-line)
  21552. (re-search-backward "^[ \t]*$" beg 'move)
  21553. (line-beginning-position))
  21554. (save-excursion (beginning-of-line)
  21555. (re-search-forward "^[ \t]*$" end 'move)
  21556. (line-beginning-position))
  21557. justify))))
  21558. ;; Fill comments.
  21559. (comment
  21560. (let ((begin (org-element-property :post-affiliated element))
  21561. (end (org-element-property :end element)))
  21562. (when (and (>= (point) begin) (<= (point) end))
  21563. (let ((begin (save-excursion
  21564. (end-of-line)
  21565. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21566. (progn (forward-line) (point))
  21567. begin)))
  21568. (end (save-excursion
  21569. (end-of-line)
  21570. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21571. (1- (line-beginning-position))
  21572. (skip-chars-backward " \r\t\n")
  21573. (line-end-position)))))
  21574. ;; Do not fill comments when at a blank line.
  21575. (when (> end begin)
  21576. (let ((fill-prefix
  21577. (save-excursion
  21578. (beginning-of-line)
  21579. (looking-at "[ \t]*#")
  21580. (let ((comment-prefix (match-string 0)))
  21581. (goto-char (match-end 0))
  21582. (if (looking-at adaptive-fill-regexp)
  21583. (concat comment-prefix (match-string 0))
  21584. (concat comment-prefix " "))))))
  21585. (save-excursion
  21586. (fill-region-as-paragraph begin end justify))))))
  21587. t))
  21588. ;; Ignore every other element.
  21589. (otherwise t))))))
  21590. (defun org-auto-fill-function ()
  21591. "Auto-fill function."
  21592. ;; Check if auto-filling is meaningful.
  21593. (let ((fc (current-fill-column)))
  21594. (when (and fc (> (current-column) fc))
  21595. (let* ((fill-prefix (org-adaptive-fill-function))
  21596. ;; Enforce empty fill prefix, if required. Otherwise, it
  21597. ;; will be computed again.
  21598. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21599. (when fill-prefix (do-auto-fill))))))
  21600. (defun org-comment-line-break-function (&optional soft)
  21601. "Break line at point and indent, continuing comment if within one.
  21602. The inserted newline is marked hard if variable
  21603. `use-hard-newlines' is true, unless optional argument SOFT is
  21604. non-nil."
  21605. (if soft (insert-and-inherit ?\n) (newline 1))
  21606. (save-excursion (forward-char -1) (delete-horizontal-space))
  21607. (delete-horizontal-space)
  21608. (indent-to-left-margin)
  21609. (insert-before-markers-and-inherit fill-prefix))
  21610. ;;; Fixed Width Areas
  21611. (defun org-toggle-fixed-width ()
  21612. "Toggle fixed-width markup.
  21613. Add or remove fixed-width markup on current line, whenever it
  21614. makes sense. Return an error otherwise.
  21615. If a region is active and if it contains only fixed-width areas
  21616. or blank lines, remove all fixed-width markup in it. If the
  21617. region contains anything else, convert all non-fixed-width lines
  21618. to fixed-width ones.
  21619. Blank lines at the end of the region are ignored unless the
  21620. region only contains such lines."
  21621. (interactive)
  21622. (if (not (org-region-active-p))
  21623. ;; No region:
  21624. ;;
  21625. ;; Remove fixed width marker only in a fixed-with element.
  21626. ;;
  21627. ;; Add fixed width maker in paragraphs, in blank lines after
  21628. ;; elements or at the beginning of a headline or an inlinetask,
  21629. ;; and before any one-line elements (e.g., a clock).
  21630. (progn
  21631. (beginning-of-line)
  21632. (let* ((element (org-element-at-point))
  21633. (type (org-element-type element)))
  21634. (cond
  21635. ((and (eq type 'fixed-width)
  21636. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21637. (replace-match
  21638. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21639. ((and (memq type '(babel-call clock comment diary-sexp headline
  21640. horizontal-rule keyword paragraph
  21641. planning))
  21642. (<= (org-element-property :post-affiliated element) (point)))
  21643. (skip-chars-forward " \t")
  21644. (insert ": "))
  21645. ((and (org-looking-at-p "[ \t]*$")
  21646. (or (eq type 'inlinetask)
  21647. (save-excursion
  21648. (skip-chars-forward " \r\t\n")
  21649. (<= (org-element-property :end element) (point)))))
  21650. (delete-region (point) (line-end-position))
  21651. (org-indent-line)
  21652. (insert ": "))
  21653. (t (user-error "Cannot insert a fixed-width line here")))))
  21654. ;; Region active.
  21655. (let* ((begin (save-excursion
  21656. (goto-char (region-beginning))
  21657. (line-beginning-position)))
  21658. (end (copy-marker
  21659. (save-excursion
  21660. (goto-char (region-end))
  21661. (unless (eolp) (beginning-of-line))
  21662. (if (save-excursion (re-search-backward "\\S-" begin t))
  21663. (progn (skip-chars-backward " \r\t\n") (point))
  21664. (point)))))
  21665. (all-fixed-width-p
  21666. (catch 'not-all-p
  21667. (save-excursion
  21668. (goto-char begin)
  21669. (skip-chars-forward " \r\t\n")
  21670. (when (eobp) (throw 'not-all-p nil))
  21671. (while (< (point) end)
  21672. (let ((element (org-element-at-point)))
  21673. (if (eq (org-element-type element) 'fixed-width)
  21674. (goto-char (org-element-property :end element))
  21675. (throw 'not-all-p nil))))
  21676. t))))
  21677. (if all-fixed-width-p
  21678. (save-excursion
  21679. (goto-char begin)
  21680. (while (< (point) end)
  21681. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21682. (replace-match
  21683. "" nil nil nil
  21684. (if (= (line-end-position) (match-end 0)) 0 1)))
  21685. (forward-line)))
  21686. (let ((min-ind (point-max)))
  21687. ;; Find minimum indentation across all lines.
  21688. (save-excursion
  21689. (goto-char begin)
  21690. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21691. (setq min-ind 0)
  21692. (catch 'zerop
  21693. (while (< (point) end)
  21694. (unless (org-looking-at-p "[ \t]*$")
  21695. (let ((ind (org-get-indentation)))
  21696. (setq min-ind (min min-ind ind))
  21697. (when (zerop ind) (throw 'zerop t))))
  21698. (forward-line)))))
  21699. ;; Loop over all lines and add fixed-width markup everywhere
  21700. ;; but in fixed-width lines.
  21701. (save-excursion
  21702. (goto-char begin)
  21703. (while (< (point) end)
  21704. (cond
  21705. ((org-at-heading-p)
  21706. (insert ": ")
  21707. (forward-line)
  21708. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21709. (insert ":")
  21710. (forward-line)))
  21711. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21712. (let* ((element (org-element-at-point))
  21713. (element-end (org-element-property :end element)))
  21714. (if (eq (org-element-type element) 'fixed-width)
  21715. (progn (goto-char element-end)
  21716. (skip-chars-backward " \r\t\n")
  21717. (forward-line))
  21718. (let ((limit (min end element-end)))
  21719. (while (< (point) limit)
  21720. (org-move-to-column min-ind t)
  21721. (insert ": ")
  21722. (forward-line))))))
  21723. (t
  21724. (org-move-to-column min-ind t)
  21725. (insert ": ")
  21726. (forward-line)))))))
  21727. (set-marker end nil))))
  21728. ;;; Comments
  21729. ;; Org comments syntax is quite complex. It requires the entire line
  21730. ;; to be just a comment. Also, even with the right syntax at the
  21731. ;; beginning of line, some some elements (i.e. verse-block or
  21732. ;; example-block) don't accept comments. Usual Emacs comment commands
  21733. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21734. ;; Org still relies on `comment-dwim', but cannot trust
  21735. ;; `comment-only-p'. So, `comment-region-function' and
  21736. ;; `uncomment-region-function' both point
  21737. ;; to`org-comment-or-uncomment-region'. Eventually,
  21738. ;; `org-insert-comment' takes care of insertion of comments at the
  21739. ;; beginning of line.
  21740. ;; `org-setup-comments-handling' install comments related variables
  21741. ;; during `org-mode' initialization.
  21742. (defun org-setup-comments-handling ()
  21743. (interactive)
  21744. (org-set-local 'comment-use-syntax nil)
  21745. (org-set-local 'comment-start "# ")
  21746. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21747. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21748. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21749. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21750. (defun org-insert-comment ()
  21751. "Insert an empty comment above current line.
  21752. If the line is empty, insert comment at its beginning. When
  21753. point is within a source block, comment according to the related
  21754. major mode."
  21755. (if (let ((element (org-element-at-point)))
  21756. (and (eq (org-element-type element) 'src-block)
  21757. (< (save-excursion
  21758. (goto-char (org-element-property :post-affiliated element))
  21759. (line-end-position))
  21760. (point))
  21761. (> (save-excursion
  21762. (goto-char (org-element-property :end element))
  21763. (skip-chars-backward " \r\t\n")
  21764. (line-beginning-position))
  21765. (point))))
  21766. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21767. (beginning-of-line)
  21768. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21769. (open-line 1))
  21770. (org-indent-line)
  21771. (insert "# ")))
  21772. (defvar comment-empty-lines) ; From newcomment.el.
  21773. (defun org-comment-or-uncomment-region (beg end &rest _)
  21774. "Comment or uncomment each non-blank line in the region.
  21775. Uncomment each non-blank line between BEG and END if it only
  21776. contains commented lines. Otherwise, comment them. If region is
  21777. strictly within a source block, use appropriate comment syntax."
  21778. (if (let ((element (org-element-at-point)))
  21779. (and (eq (org-element-type element) 'src-block)
  21780. (< (save-excursion
  21781. (goto-char (org-element-property :post-affiliated element))
  21782. (line-end-position))
  21783. beg)
  21784. (>= (save-excursion
  21785. (goto-char (org-element-property :end element))
  21786. (skip-chars-backward " \r\t\n")
  21787. (line-beginning-position))
  21788. end)))
  21789. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21790. (save-restriction
  21791. ;; Restrict region
  21792. (narrow-to-region (save-excursion (goto-char beg)
  21793. (skip-chars-forward " \r\t\n" end)
  21794. (line-beginning-position))
  21795. (save-excursion (goto-char end)
  21796. (skip-chars-backward " \r\t\n" beg)
  21797. (line-end-position)))
  21798. (let ((uncommentp
  21799. ;; UNCOMMENTP is non-nil when every non blank line between
  21800. ;; BEG and END is a comment.
  21801. (save-excursion
  21802. (goto-char (point-min))
  21803. (while (and (not (eobp))
  21804. (let ((element (org-element-at-point)))
  21805. (and (eq (org-element-type element) 'comment)
  21806. (goto-char (min (point-max)
  21807. (org-element-property
  21808. :end element)))))))
  21809. (eobp))))
  21810. (if uncommentp
  21811. ;; Only blank lines and comments in region: uncomment it.
  21812. (save-excursion
  21813. (goto-char (point-min))
  21814. (while (not (eobp))
  21815. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21816. (replace-match "" nil nil nil 1))
  21817. (forward-line)))
  21818. ;; Comment each line in region.
  21819. (let ((min-indent (point-max)))
  21820. ;; First find the minimum indentation across all lines.
  21821. (save-excursion
  21822. (goto-char (point-min))
  21823. (while (and (not (eobp)) (not (zerop min-indent)))
  21824. (unless (looking-at "[ \t]*$")
  21825. (setq min-indent (min min-indent (current-indentation))))
  21826. (forward-line)))
  21827. ;; Then loop over all lines.
  21828. (save-excursion
  21829. (goto-char (point-min))
  21830. (while (not (eobp))
  21831. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21832. ;; Don't get fooled by invisible text (e.g. link path)
  21833. ;; when moving to column MIN-INDENT.
  21834. (let ((buffer-invisibility-spec nil))
  21835. (org-move-to-column min-indent t))
  21836. (insert comment-start))
  21837. (forward-line)))))))))
  21838. (defun org-comment-dwim (arg)
  21839. "Call `comment-dwim' within a source edit buffer if needed."
  21840. (interactive "*P")
  21841. (if (org-in-src-block-p)
  21842. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21843. (call-interactively 'comment-dwim)))
  21844. ;;; Timestamps API
  21845. ;; This section contains tools to operate on timestamp objects, as
  21846. ;; returned by, e.g. `org-element-context'.
  21847. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21848. "Encode TIMESTAMP object into Emacs internal time.
  21849. Use end of date range or time range when END is non-nil."
  21850. (apply #'encode-time
  21851. (cons 0
  21852. (mapcar
  21853. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21854. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21855. '(:minute-start :hour-start :day-start :month-start
  21856. :year-start))))))
  21857. (defun org-timestamp-has-time-p (timestamp)
  21858. "Non-nil when TIMESTAMP has a time specified."
  21859. (org-element-property :hour-start timestamp))
  21860. (defun org-timestamp-format (timestamp format &optional end utc)
  21861. "Format a TIMESTAMP object into a string.
  21862. FORMAT is a format specifier to be passed to
  21863. `format-time-string'.
  21864. When optional argument END is non-nil, use end of date-range or
  21865. time-range, if possible.
  21866. When optional argument UTC is non-nil, time will be expressed as
  21867. Universal Time."
  21868. (format-time-string
  21869. format (org-timestamp--to-internal-time timestamp end) utc))
  21870. (defun org-timestamp-split-range (timestamp &optional end)
  21871. "Extract a TIMESTAMP object from a date or time range.
  21872. END, when non-nil, means extract the end of the range.
  21873. Otherwise, extract its start.
  21874. Return a new timestamp object."
  21875. (let ((type (org-element-property :type timestamp)))
  21876. (if (memq type '(active inactive diary)) timestamp
  21877. (let ((split-ts (org-element-copy timestamp)))
  21878. ;; Set new type.
  21879. (org-element-put-property
  21880. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21881. ;; Copy start properties over end properties if END is
  21882. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21883. (let ((p-alist '((:minute-start . :minute-end)
  21884. (:hour-start . :hour-end)
  21885. (:day-start . :day-end)
  21886. (:month-start . :month-end)
  21887. (:year-start . :year-end))))
  21888. (dolist (p-cell p-alist)
  21889. (org-element-put-property
  21890. split-ts
  21891. (funcall (if end #'car #'cdr) p-cell)
  21892. (org-element-property
  21893. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21894. ;; Eventually refresh `:raw-value'.
  21895. (org-element-put-property split-ts :raw-value nil)
  21896. (org-element-put-property
  21897. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21898. (defun org-timestamp-translate (timestamp &optional boundary)
  21899. "Translate TIMESTAMP object to custom format.
  21900. Format string is defined in `org-time-stamp-custom-formats',
  21901. which see.
  21902. When optional argument BOUNDARY is non-nil, it is either the
  21903. symbol `start' or `end'. In this case, only translate the
  21904. starting or ending part of TIMESTAMP if it is a date or time
  21905. range. Otherwise, translate both parts.
  21906. Return timestamp as-is if `org-display-custom-times' is nil or if
  21907. it has a `diary' type."
  21908. (let ((type (org-element-property :type timestamp)))
  21909. (if (or (not org-display-custom-times) (eq type 'diary))
  21910. (org-element-interpret-data timestamp)
  21911. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21912. org-time-stamp-custom-formats)))
  21913. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21914. (concat (org-timestamp-format timestamp fmt)
  21915. "--"
  21916. (org-timestamp-format timestamp fmt t))
  21917. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21918. ;;; Other stuff.
  21919. (defvar reftex-docstruct-symbol)
  21920. (defvar reftex-cite-format)
  21921. (defvar org--rds)
  21922. (defun org-reftex-citation ()
  21923. "Use reftex-citation to insert a citation into the buffer.
  21924. This looks for a line like
  21925. #+BIBLIOGRAPHY: foo plain option:-d
  21926. and derives from it that foo.bib is the bibliography file relevant
  21927. for this document. It then installs the necessary environment for RefTeX
  21928. to work in this buffer and calls `reftex-citation' to insert a citation
  21929. into the buffer.
  21930. Export of such citations to both LaTeX and HTML is handled by the contributed
  21931. package ox-bibtex by Taru Karttunen."
  21932. (interactive)
  21933. (let ((reftex-docstruct-symbol 'org--rds)
  21934. (reftex-cite-format "\\cite{%l}")
  21935. org--rds bib)
  21936. (save-excursion
  21937. (save-restriction
  21938. (widen)
  21939. (let ((case-fold-search t)
  21940. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21941. (if (not (save-excursion
  21942. (or (re-search-forward re nil t)
  21943. (re-search-backward re nil t))))
  21944. (user-error "No bibliography defined in file")
  21945. (setq bib (concat (match-string 1) ".bib")
  21946. org--rds (list (list 'bib bib)))))))
  21947. (call-interactively 'reftex-citation)))
  21948. ;;;; Functions extending outline functionality
  21949. (defun org-beginning-of-line (&optional arg)
  21950. "Go to the beginning of the current line. If that is invisible, continue
  21951. to a visible line beginning. This makes the function of C-a more intuitive.
  21952. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21953. first attempt, and only move to after the tags when the cursor is already
  21954. beyond the end of the headline."
  21955. (interactive "P")
  21956. (let ((pos (point))
  21957. (special (if (consp org-special-ctrl-a/e)
  21958. (car org-special-ctrl-a/e)
  21959. org-special-ctrl-a/e))
  21960. deactivate-mark refpos)
  21961. (if (org-bound-and-true-p visual-line-mode)
  21962. (beginning-of-visual-line 1)
  21963. (beginning-of-line 1))
  21964. (if (and arg (fboundp 'move-beginning-of-line))
  21965. (call-interactively 'move-beginning-of-line)
  21966. (if (bobp)
  21967. nil
  21968. (backward-char 1)
  21969. (if (org-truely-invisible-p)
  21970. (while (and (not (bobp)) (org-truely-invisible-p))
  21971. (backward-char 1)
  21972. (beginning-of-line 1))
  21973. (forward-char 1))))
  21974. (when special
  21975. (cond
  21976. ((and (looking-at org-complex-heading-regexp)
  21977. (eq (char-after (match-end 1)) ?\s))
  21978. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21979. (point-at-eol)))
  21980. (goto-char
  21981. (if (eq special t)
  21982. (cond ((> pos refpos) refpos)
  21983. ((= pos (point)) refpos)
  21984. (t (point)))
  21985. (cond ((> pos (point)) (point))
  21986. ((not (eq last-command this-command)) (point))
  21987. (t refpos)))))
  21988. ((org-at-item-p)
  21989. ;; Being at an item and not looking at an the item means point
  21990. ;; was previously moved to beginning of a visual line, which
  21991. ;; doesn't contain the item. Therefore, do nothing special,
  21992. ;; just stay here.
  21993. (when (looking-at org-list-full-item-re)
  21994. ;; Set special position at first white space character after
  21995. ;; bullet, and check-box, if any.
  21996. (let ((after-bullet
  21997. (let ((box (match-end 3)))
  21998. (if (not box) (match-end 1)
  21999. (let ((after (char-after box)))
  22000. (if (and after (= after ? )) (1+ box) box))))))
  22001. ;; Special case: Move point to special position when
  22002. ;; currently after it or at beginning of line.
  22003. (if (eq special t)
  22004. (when (or (> pos after-bullet) (= (point) pos))
  22005. (goto-char after-bullet))
  22006. ;; Reversed case: Move point to special position when
  22007. ;; point was already at beginning of line and command is
  22008. ;; repeated.
  22009. (when (and (= (point) pos) (eq last-command this-command))
  22010. (goto-char after-bullet))))))))
  22011. (org-no-warnings
  22012. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22013. (setq disable-point-adjustment
  22014. (or (not (invisible-p (point)))
  22015. (not (invisible-p (max (point-min) (1- (point))))))))
  22016. (defun org-end-of-line (&optional arg)
  22017. "Go to the end of the line.
  22018. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  22019. tags on the first attempt, and only move to after the tags when
  22020. the cursor is already beyond the end of the headline."
  22021. (interactive "P")
  22022. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  22023. org-special-ctrl-a/e))
  22024. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  22025. 'end-of-visual-line)
  22026. ((fboundp 'move-end-of-line) 'move-end-of-line)
  22027. (t 'end-of-line)))
  22028. deactivate-mark)
  22029. (if (or (not special) arg) (call-interactively move-fun)
  22030. (let* ((element (save-excursion (beginning-of-line)
  22031. (org-element-at-point)))
  22032. (type (org-element-type element)))
  22033. (cond
  22034. ((memq type '(headline inlinetask))
  22035. (let ((pos (point)))
  22036. (beginning-of-line 1)
  22037. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  22038. (if (eq special t)
  22039. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  22040. (goto-char (match-beginning 1))
  22041. (goto-char (match-end 0)))
  22042. (if (or (< pos (match-end 0))
  22043. (not (eq this-command last-command)))
  22044. (goto-char (match-end 0))
  22045. (goto-char (match-beginning 1))))
  22046. (call-interactively move-fun))))
  22047. ((outline-invisible-p (line-end-position))
  22048. ;; If element is hidden, `move-end-of-line' would put point
  22049. ;; after it. Use `end-of-line' to stay on current line.
  22050. (call-interactively 'end-of-line))
  22051. (t (call-interactively move-fun)))))
  22052. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22053. (setq disable-point-adjustment
  22054. (or (not (invisible-p (point)))
  22055. (not (invisible-p (max (point-min) (1- (point))))))))
  22056. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22057. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22058. (defun org-backward-sentence (&optional _arg)
  22059. "Go to beginning of sentence, or beginning of table field.
  22060. This will call `backward-sentence' or `org-table-beginning-of-field',
  22061. depending on context."
  22062. (interactive)
  22063. (let* ((element (org-element-at-point))
  22064. (contents-begin (org-element-property :contents-begin element))
  22065. (table (org-element-lineage element '(table) t)))
  22066. (if (and table
  22067. (> (point) contents-begin)
  22068. (<= (point) (org-element-property :contents-end table)))
  22069. (call-interactively #'org-table-beginning-of-field)
  22070. (save-restriction
  22071. (when (and contents-begin
  22072. (< (point-min) contents-begin)
  22073. (> (point) contents-begin))
  22074. (narrow-to-region contents-begin
  22075. (org-element-property :contents-end element)))
  22076. (call-interactively #'backward-sentence)))))
  22077. (defun org-forward-sentence (&optional _arg)
  22078. "Go to end of sentence, or end of table field.
  22079. This will call `forward-sentence' or `org-table-end-of-field',
  22080. depending on context."
  22081. (interactive)
  22082. (let* ((element (org-element-at-point))
  22083. (contents-end (org-element-property :contents-end element))
  22084. (table (org-element-lineage element '(table) t)))
  22085. (if (and table
  22086. (>= (point) (org-element-property :contents-begin table))
  22087. (< (point) contents-end))
  22088. (call-interactively #'org-table-end-of-field)
  22089. (save-restriction
  22090. (when (and contents-end
  22091. (> (point-max) contents-end)
  22092. ;; Skip blank lines between elements.
  22093. (< (org-element-property :end element)
  22094. (save-excursion (goto-char contents-end)
  22095. (skip-chars-forward " \r\t\n"))))
  22096. (narrow-to-region (org-element-property :contents-begin element)
  22097. contents-end))
  22098. (call-interactively #'forward-sentence)))))
  22099. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22100. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22101. (defun org-kill-line (&optional _arg)
  22102. "Kill line, to tags or end of line."
  22103. (interactive)
  22104. (cond
  22105. ((or (not org-special-ctrl-k)
  22106. (bolp)
  22107. (not (org-at-heading-p)))
  22108. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22109. org-ctrl-k-protect-subtree)
  22110. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22111. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22112. (user-error "C-k aborted as it would kill a hidden subtree")))
  22113. (call-interactively
  22114. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22115. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22116. (kill-region (point) (match-beginning 1))
  22117. (org-set-tags nil t))
  22118. (t (kill-region (point) (point-at-eol)))))
  22119. (define-key org-mode-map "\C-k" 'org-kill-line)
  22120. (defun org-yank (&optional arg)
  22121. "Yank. If the kill is a subtree, treat it specially.
  22122. This command will look at the current kill and check if is a single
  22123. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22124. cursor is at the beginning of a line or after the stars of a currently
  22125. empty headline, then the yank is handled specially. How exactly depends
  22126. on the value of the following variables, both set by default.
  22127. `org-yank-folded-subtrees'
  22128. When set, the subtree(s) will be folded after insertion, but only
  22129. if doing so would now swallow text after the yanked text.
  22130. `org-yank-adjusted-subtrees'
  22131. When set, the subtree will be promoted or demoted in order to
  22132. fit into the local outline tree structure, which means that the
  22133. level will be adjusted so that it becomes the smaller one of the
  22134. two *visible* surrounding headings.
  22135. Any prefix to this command will cause `yank' to be called directly with
  22136. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22137. will just
  22138. plainly yank the text as it is.
  22139. \[1] The test checks if the first non-white line is a heading
  22140. and if there are no other headings with fewer stars."
  22141. (interactive "P")
  22142. (org-yank-generic 'yank arg))
  22143. (defun org-yank-generic (command arg)
  22144. "Perform some yank-like command.
  22145. This function implements the behavior described in the `org-yank'
  22146. documentation. However, it has been generalized to work for any
  22147. interactive command with similar behavior."
  22148. ;; pretend to be command COMMAND
  22149. (setq this-command command)
  22150. (if arg
  22151. (call-interactively command)
  22152. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22153. (and (org-kill-is-subtree-p)
  22154. (or (bolp)
  22155. (and (looking-at "[ \t]*$")
  22156. (string-match
  22157. "\\`\\*+\\'"
  22158. (buffer-substring (point-at-bol) (point)))))))
  22159. swallowp)
  22160. (cond
  22161. ((and subtreep org-yank-folded-subtrees)
  22162. (let ((beg (point))
  22163. end)
  22164. (if (and subtreep org-yank-adjusted-subtrees)
  22165. (org-paste-subtree nil nil 'for-yank)
  22166. (call-interactively command))
  22167. (setq end (point))
  22168. (goto-char beg)
  22169. (when (and (bolp) subtreep
  22170. (not (setq swallowp
  22171. (org-yank-folding-would-swallow-text beg end))))
  22172. (org-with-limited-levels
  22173. (or (looking-at org-outline-regexp)
  22174. (re-search-forward org-outline-regexp-bol end t))
  22175. (while (and (< (point) end) (looking-at org-outline-regexp))
  22176. (outline-hide-subtree)
  22177. (org-cycle-show-empty-lines 'folded)
  22178. (condition-case nil
  22179. (outline-forward-same-level 1)
  22180. (error (goto-char end))))))
  22181. (when swallowp
  22182. (message
  22183. "Inserted text not folded because that would swallow text"))
  22184. (goto-char end)
  22185. (skip-chars-forward " \t\n\r")
  22186. (beginning-of-line 1)
  22187. (push-mark beg 'nomsg)))
  22188. ((and subtreep org-yank-adjusted-subtrees)
  22189. (let ((beg (point-at-bol)))
  22190. (org-paste-subtree nil nil 'for-yank)
  22191. (push-mark beg 'nomsg)))
  22192. (t
  22193. (call-interactively command))))))
  22194. (defun org-yank-folding-would-swallow-text (beg end)
  22195. "Would hide-subtree at BEG swallow any text after END?"
  22196. (let (level)
  22197. (org-with-limited-levels
  22198. (save-excursion
  22199. (goto-char beg)
  22200. (when (or (looking-at org-outline-regexp)
  22201. (re-search-forward org-outline-regexp-bol end t))
  22202. (setq level (org-outline-level)))
  22203. (goto-char end)
  22204. (skip-chars-forward " \t\r\n\v\f")
  22205. (if (or (eobp)
  22206. (and (bolp) (looking-at org-outline-regexp)
  22207. (<= (org-outline-level) level)))
  22208. nil ; Nothing would be swallowed
  22209. t))))) ; something would swallow
  22210. (define-key org-mode-map "\C-y" 'org-yank)
  22211. (defun org-truely-invisible-p ()
  22212. "Check if point is at a character currently not visible.
  22213. This version does not only check the character property, but also
  22214. `visible-mode'."
  22215. ;; Early versions of noutline don't have `outline-invisible-p'.
  22216. (if (org-bound-and-true-p visible-mode)
  22217. nil
  22218. (outline-invisible-p)))
  22219. (defun org-invisible-p2 ()
  22220. "Check if point is at a character currently not visible."
  22221. (save-excursion
  22222. (if (and (eolp) (not (bobp))) (backward-char 1))
  22223. ;; Early versions of noutline don't have `outline-invisible-p'.
  22224. (outline-invisible-p)))
  22225. (defun org-back-to-heading (&optional invisible-ok)
  22226. "Call `outline-back-to-heading', but provide a better error message."
  22227. (condition-case nil
  22228. (outline-back-to-heading invisible-ok)
  22229. (error (error "Before first headline at position %d in buffer %s"
  22230. (point) (current-buffer)))))
  22231. (defun org-before-first-heading-p ()
  22232. "Before first heading?"
  22233. (save-excursion
  22234. (end-of-line)
  22235. (null (re-search-backward org-outline-regexp-bol nil t))))
  22236. (defun org-at-heading-p (&optional ignored)
  22237. (outline-on-heading-p t))
  22238. ;; Compatibility alias with Org versions < 7.8.03
  22239. (defalias 'org-on-heading-p 'org-at-heading-p)
  22240. (defun org-in-commented-heading-p (&optional no-inheritance)
  22241. "Non-nil if point is under a commented heading.
  22242. This function also checks ancestors of the current headline,
  22243. unless optional argument NO-INHERITANCE is non-nil."
  22244. (cond
  22245. ((org-before-first-heading-p) nil)
  22246. ((let ((headline (nth 4 (org-heading-components))))
  22247. (and headline
  22248. (let ((case-fold-search nil))
  22249. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22250. headline)))))
  22251. (no-inheritance nil)
  22252. (t
  22253. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22254. (defun org-at-comment-p nil
  22255. "Is cursor in a commented line?"
  22256. (save-excursion
  22257. (save-match-data
  22258. (beginning-of-line)
  22259. (looking-at "^[ \t]*# "))))
  22260. (defun org-at-drawer-p nil
  22261. "Is cursor at a drawer keyword?"
  22262. (save-excursion
  22263. (move-beginning-of-line 1)
  22264. (looking-at org-drawer-regexp)))
  22265. (defun org-at-block-p nil
  22266. "Is cursor at a block keyword?"
  22267. (save-excursion
  22268. (move-beginning-of-line 1)
  22269. (looking-at org-block-regexp)))
  22270. (defun org-point-at-end-of-empty-headline ()
  22271. "If point is at the end of an empty headline, return t, else nil.
  22272. If the heading only contains a TODO keyword, it is still still considered
  22273. empty."
  22274. (and (looking-at "[ \t]*$")
  22275. (when org-todo-line-regexp
  22276. (save-excursion
  22277. (beginning-of-line 1)
  22278. (let ((case-fold-search nil))
  22279. (looking-at org-todo-line-regexp)
  22280. (string= (match-string 3) ""))))))
  22281. (defun org-at-heading-or-item-p ()
  22282. (or (org-at-heading-p) (org-at-item-p)))
  22283. (defun org-at-target-p ()
  22284. (or (org-in-regexp org-radio-target-regexp)
  22285. (org-in-regexp org-target-regexp)))
  22286. ;; Compatibility alias with Org versions < 7.8.03
  22287. (defalias 'org-on-target-p 'org-at-target-p)
  22288. (defun org-up-heading-all (arg)
  22289. "Move to the heading line of which the present line is a subheading.
  22290. This function considers both visible and invisible heading lines.
  22291. With argument, move up ARG levels."
  22292. (if (fboundp 'outline-up-heading-all)
  22293. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22294. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22295. (defun org-up-heading-safe ()
  22296. "Move to the heading line of which the present line is a subheading.
  22297. This version will not throw an error. It will return the level of the
  22298. headline found, or nil if no higher level is found.
  22299. Also, this function will be a lot faster than `outline-up-heading',
  22300. because it relies on stars being the outline starters. This can really
  22301. make a significant difference in outlines with very many siblings."
  22302. (when (ignore-errors (org-back-to-heading t))
  22303. (let ((level-up (1- (funcall outline-level))))
  22304. (and (> level-up 0)
  22305. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22306. (funcall outline-level)))))
  22307. (defun org-first-sibling-p ()
  22308. "Is this heading the first child of its parents?"
  22309. (interactive)
  22310. (let ((re org-outline-regexp-bol)
  22311. level l)
  22312. (unless (org-at-heading-p t)
  22313. (user-error "Not at a heading"))
  22314. (setq level (funcall outline-level))
  22315. (save-excursion
  22316. (if (not (re-search-backward re nil t))
  22317. t
  22318. (setq l (funcall outline-level))
  22319. (< l level)))))
  22320. (defun org-goto-sibling (&optional previous)
  22321. "Goto the next sibling, even if it is invisible.
  22322. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22323. when a sibling was found. When none is found, return nil and don't
  22324. move point."
  22325. (let ((fun (if previous 're-search-backward 're-search-forward))
  22326. (pos (point))
  22327. (re org-outline-regexp-bol)
  22328. level l)
  22329. (when (ignore-errors (org-back-to-heading t))
  22330. (setq level (funcall outline-level))
  22331. (catch 'exit
  22332. (or previous (forward-char 1))
  22333. (while (funcall fun re nil t)
  22334. (setq l (funcall outline-level))
  22335. (when (< l level) (goto-char pos) (throw 'exit nil))
  22336. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22337. (goto-char pos)
  22338. nil))))
  22339. (defun org-show-siblings ()
  22340. "Show all siblings of the current headline."
  22341. (save-excursion
  22342. (while (org-goto-sibling) (org-flag-heading nil)))
  22343. (save-excursion
  22344. (while (org-goto-sibling 'previous)
  22345. (org-flag-heading nil))))
  22346. (defun org-goto-first-child ()
  22347. "Goto the first child, even if it is invisible.
  22348. Return t when a child was found. Otherwise don't move point and
  22349. return nil."
  22350. (let (level (pos (point)) (re org-outline-regexp-bol))
  22351. (when (ignore-errors (org-back-to-heading t))
  22352. (setq level (outline-level))
  22353. (forward-char 1)
  22354. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22355. (progn (goto-char (match-beginning 0)) t)
  22356. (goto-char pos) nil))))
  22357. (defun org-show-hidden-entry ()
  22358. "Show an entry where even the heading is hidden."
  22359. (save-excursion
  22360. (org-show-entry)))
  22361. (defun org-flag-heading (flag &optional entry)
  22362. "Flag the current heading. FLAG non-nil means make invisible.
  22363. When ENTRY is non-nil, show the entire entry."
  22364. (save-excursion
  22365. (org-back-to-heading t)
  22366. ;; Check if we should show the entire entry
  22367. (if entry
  22368. (progn
  22369. (org-show-entry)
  22370. (save-excursion
  22371. (and (outline-next-heading)
  22372. (org-flag-heading nil))))
  22373. (outline-flag-region (max (point-min) (1- (point)))
  22374. (save-excursion (outline-end-of-heading) (point))
  22375. flag))))
  22376. (defun org-get-next-sibling ()
  22377. "Move to next heading of the same level, and return point.
  22378. If there is no such heading, return nil.
  22379. This is like outline-next-sibling, but invisible headings are ok."
  22380. (let ((level (funcall outline-level)))
  22381. (outline-next-heading)
  22382. (while (and (not (eobp)) (> (funcall outline-level) level))
  22383. (outline-next-heading))
  22384. (if (or (eobp) (< (funcall outline-level) level))
  22385. nil
  22386. (point))))
  22387. (defun org-get-last-sibling ()
  22388. "Move to previous heading of the same level, and return point.
  22389. If there is no such heading, return nil."
  22390. (let ((opoint (point))
  22391. (level (funcall outline-level)))
  22392. (outline-previous-heading)
  22393. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22394. (while (and (> (funcall outline-level) level)
  22395. (not (bobp)))
  22396. (outline-previous-heading))
  22397. (if (< (funcall outline-level) level)
  22398. nil
  22399. (point)))))
  22400. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22401. "Goto to the end of a subtree."
  22402. ;; This contains an exact copy of the original function, but it uses
  22403. ;; `org-back-to-heading', to make it work also in invisible
  22404. ;; trees. And is uses an invisible-ok argument.
  22405. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22406. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22407. ;; with many children and grandchildren etc, this can be much faster
  22408. ;; than the outline version.
  22409. (org-back-to-heading invisible-ok)
  22410. (let ((first t)
  22411. (level (funcall outline-level)))
  22412. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22413. ;; A true heading (not a plain list item), in Org-mode
  22414. ;; This means we can easily find the end by looking
  22415. ;; only for the right number of stars. Using a regexp to do
  22416. ;; this is so much faster than using a Lisp loop.
  22417. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22418. (forward-char 1)
  22419. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22420. ;; something else, do it the slow way
  22421. (while (and (not (eobp))
  22422. (or first (> (funcall outline-level) level)))
  22423. (setq first nil)
  22424. (outline-next-heading)))
  22425. (unless to-heading
  22426. (if (memq (preceding-char) '(?\n ?\^M))
  22427. (progn
  22428. ;; Go to end of line before heading
  22429. (forward-char -1)
  22430. (if (memq (preceding-char) '(?\n ?\^M))
  22431. ;; leave blank line before heading
  22432. (forward-char -1))))))
  22433. (point))
  22434. (defun org-end-of-meta-data (&optional full)
  22435. "Skip planning line and properties drawer in current entry.
  22436. When optional argument FULL is non-nil, also skip empty lines,
  22437. clocking lines and regular drawers at the beginning of the
  22438. entry."
  22439. (org-back-to-heading t)
  22440. (forward-line)
  22441. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22442. (when (looking-at org-property-drawer-re)
  22443. (goto-char (match-end 0))
  22444. (forward-line))
  22445. (when (and full (not (org-at-heading-p)))
  22446. (catch 'exit
  22447. (let ((end (save-excursion (outline-next-heading) (point)))
  22448. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22449. (while (not (eobp))
  22450. (cond ((org-looking-at-p org-drawer-regexp)
  22451. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22452. (forward-line)
  22453. (throw 'exit t)))
  22454. ((org-looking-at-p re) (forward-line))
  22455. (t (throw 'exit t))))))))
  22456. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22457. "Move forward to the ARG'th subheading at same level as this one.
  22458. Stop at the first and last subheadings of a superior heading.
  22459. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22460. non-nil it will also look at invisible ones."
  22461. (interactive "p")
  22462. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22463. (if (and arg (< arg 0))
  22464. (goto-char (point-min))
  22465. (outline-next-heading))
  22466. (org-at-heading-p)
  22467. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22468. (f (if (and arg (< arg 0))
  22469. 're-search-backward
  22470. 're-search-forward))
  22471. (count (if arg (abs arg) 1))
  22472. (result (point)))
  22473. (while (and (prog1 (> count 0)
  22474. (forward-char (if (and arg (< arg 0)) -1 1)))
  22475. (funcall f org-outline-regexp-bol nil 'move))
  22476. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22477. (cond ((< l level) (setq count 0))
  22478. ((and (= l level)
  22479. (or invisible-ok
  22480. (progn
  22481. (goto-char (line-beginning-position))
  22482. (not (outline-invisible-p)))))
  22483. (setq count (1- count))
  22484. (when (eq l level)
  22485. (setq result (point)))))))
  22486. (goto-char result))
  22487. (beginning-of-line 1)))
  22488. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22489. "Move backward to the ARG'th subheading at same level as this one.
  22490. Stop at the first and last subheadings of a superior heading."
  22491. (interactive "p")
  22492. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22493. (defun org-next-visible-heading (arg)
  22494. "Move to the next visible heading.
  22495. This function wraps `outline-next-visible-heading' with
  22496. `org-with-limited-levels' in order to skip over inline tasks and
  22497. respect customization of `org-odd-levels-only'."
  22498. (interactive "p")
  22499. (org-with-limited-levels
  22500. (outline-next-visible-heading arg)))
  22501. (defun org-previous-visible-heading (arg)
  22502. "Move to the previous visible heading.
  22503. This function wraps `outline-previous-visible-heading' with
  22504. `org-with-limited-levels' in order to skip over inline tasks and
  22505. respect customization of `org-odd-levels-only'."
  22506. (interactive "p")
  22507. (org-with-limited-levels
  22508. (outline-previous-visible-heading arg)))
  22509. (defun org-next-block (arg &optional backward block-regexp)
  22510. "Jump to the next block.
  22511. With a prefix argument ARG, jump forward ARG many blocks.
  22512. When BACKWARD is non-nil, jump to the previous block.
  22513. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22514. Match data is set according to this regexp when the function
  22515. returns.
  22516. Return point at beginning of the opening line of found block.
  22517. Throw an error if no block is found."
  22518. (interactive "p")
  22519. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22520. (case-fold-search t)
  22521. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22522. (count (or arg 1))
  22523. (origin (point))
  22524. last-element)
  22525. (if backward (beginning-of-line) (end-of-line))
  22526. (while (and (> count 0) (funcall search-fn re nil t))
  22527. (let ((element (save-excursion
  22528. (goto-char (match-beginning 0))
  22529. (save-match-data (org-element-at-point)))))
  22530. (when (and (memq (org-element-type element)
  22531. '(center-block comment-block dynamic-block
  22532. example-block export-block quote-block
  22533. special-block src-block verse-block))
  22534. (<= (match-beginning 0)
  22535. (org-element-property :post-affiliated element)))
  22536. (setq last-element element)
  22537. (decf count))))
  22538. (if (= count 0)
  22539. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22540. (save-match-data (org-show-context)))
  22541. (goto-char origin)
  22542. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22543. (defun org-previous-block (arg &optional block-regexp)
  22544. "Jump to the previous block.
  22545. With a prefix argument ARG, jump backward ARG many source blocks.
  22546. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22547. (interactive "p")
  22548. (org-next-block arg t block-regexp))
  22549. (defun org-forward-paragraph ()
  22550. "Move forward to beginning of next paragraph or equivalent.
  22551. The function moves point to the beginning of the next visible
  22552. structural element, which can be a paragraph, a table, a list
  22553. item, etc. It also provides some special moves for convenience:
  22554. - On an affiliated keyword, jump to the beginning of the
  22555. relative element.
  22556. - On an item or a footnote definition, move to the second
  22557. element inside, if any.
  22558. - On a table or a property drawer, jump after it.
  22559. - On a verse or source block, stop after blank lines."
  22560. (interactive)
  22561. (when (eobp) (user-error "Cannot move further down"))
  22562. (let* ((deactivate-mark nil)
  22563. (element (org-element-at-point))
  22564. (type (org-element-type element))
  22565. (post-affiliated (org-element-property :post-affiliated element))
  22566. (contents-begin (org-element-property :contents-begin element))
  22567. (contents-end (org-element-property :contents-end element))
  22568. (end (let ((end (org-element-property :end element)) (parent element))
  22569. (while (and (setq parent (org-element-property :parent parent))
  22570. (= (org-element-property :contents-end parent) end))
  22571. (setq end (org-element-property :end parent)))
  22572. end)))
  22573. (cond ((not element)
  22574. (skip-chars-forward " \r\t\n")
  22575. (or (eobp) (beginning-of-line)))
  22576. ;; On affiliated keywords, move to element's beginning.
  22577. ((< (point) post-affiliated)
  22578. (goto-char post-affiliated))
  22579. ;; At a table row, move to the end of the table. Similarly,
  22580. ;; at a node property, move to the end of the property
  22581. ;; drawer.
  22582. ((memq type '(node-property table-row))
  22583. (goto-char (org-element-property
  22584. :end (org-element-property :parent element))))
  22585. ((memq type '(property-drawer table)) (goto-char end))
  22586. ;; Consider blank lines as separators in verse and source
  22587. ;; blocks to ease editing.
  22588. ((memq type '(src-block verse-block))
  22589. (when (eq type 'src-block)
  22590. (setq contents-end
  22591. (save-excursion (goto-char end)
  22592. (skip-chars-backward " \r\t\n")
  22593. (line-beginning-position))))
  22594. (beginning-of-line)
  22595. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22596. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22597. (goto-char end)
  22598. (skip-chars-forward " \r\t\n")
  22599. (if (= (point) contents-end) (goto-char end)
  22600. (beginning-of-line))))
  22601. ;; With no contents, just skip element.
  22602. ((not contents-begin) (goto-char end))
  22603. ;; If contents are invisible, skip the element altogether.
  22604. ((outline-invisible-p (line-end-position))
  22605. (case type
  22606. (headline
  22607. (org-with-limited-levels (outline-next-visible-heading 1)))
  22608. ;; At a plain list, make sure we move to the next item
  22609. ;; instead of skipping the whole list.
  22610. (plain-list (forward-char)
  22611. (org-forward-paragraph))
  22612. (otherwise (goto-char end))))
  22613. ((>= (point) contents-end) (goto-char end))
  22614. ((>= (point) contents-begin)
  22615. ;; This can only happen on paragraphs and plain lists.
  22616. (case type
  22617. (paragraph (goto-char end))
  22618. ;; At a plain list, try to move to second element in
  22619. ;; first item, if possible.
  22620. (plain-list (end-of-line)
  22621. (org-forward-paragraph))))
  22622. ;; When contents start on the middle of a line (e.g. in
  22623. ;; items and footnote definitions), try to reach first
  22624. ;; element starting after current line.
  22625. ((> (line-end-position) contents-begin)
  22626. (end-of-line)
  22627. (org-forward-paragraph))
  22628. (t (goto-char contents-begin)))))
  22629. (defun org-backward-paragraph ()
  22630. "Move backward to start of previous paragraph or equivalent.
  22631. The function moves point to the beginning of the current
  22632. structural element, which can be a paragraph, a table, a list
  22633. item, etc., or to the beginning of the previous visible one if
  22634. point is already there. It also provides some special moves for
  22635. convenience:
  22636. - On an affiliated keyword, jump to the first one.
  22637. - On a table or a property drawer, move to its beginning.
  22638. - On a verse or source block, stop before blank lines."
  22639. (interactive)
  22640. (when (bobp) (user-error "Cannot move further up"))
  22641. (let* ((deactivate-mark nil)
  22642. (element (org-element-at-point))
  22643. (type (org-element-type element))
  22644. (contents-begin (org-element-property :contents-begin element))
  22645. (contents-end (org-element-property :contents-end element))
  22646. (post-affiliated (org-element-property :post-affiliated element))
  22647. (begin (org-element-property :begin element)))
  22648. (cond
  22649. ((not element) (goto-char (point-min)))
  22650. ((= (point) begin)
  22651. (backward-char)
  22652. (org-backward-paragraph))
  22653. ((<= (point) post-affiliated) (goto-char begin))
  22654. ((memq type '(node-property table-row))
  22655. (goto-char (org-element-property
  22656. :post-affiliated (org-element-property :parent element))))
  22657. ((memq type '(property-drawer table)) (goto-char begin))
  22658. ((memq type '(src-block verse-block))
  22659. (when (eq type 'src-block)
  22660. (setq contents-begin
  22661. (save-excursion (goto-char begin) (forward-line) (point))))
  22662. (if (= (point) contents-begin) (goto-char post-affiliated)
  22663. ;; Inside a verse block, see blank lines as paragraph
  22664. ;; separators.
  22665. (let ((origin (point)))
  22666. (skip-chars-backward " \r\t\n" contents-begin)
  22667. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22668. (skip-chars-forward " \r\t\n" origin)
  22669. (if (= (point) origin) (goto-char contents-begin)
  22670. (beginning-of-line))))))
  22671. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22672. ((eq type 'paragraph)
  22673. (goto-char contents-begin)
  22674. ;; When at first paragraph in an item or a footnote definition,
  22675. ;; move directly to beginning of line.
  22676. (let ((parent-contents
  22677. (org-element-property
  22678. :contents-begin (org-element-property :parent element))))
  22679. (when (and parent-contents (= parent-contents contents-begin))
  22680. (beginning-of-line))))
  22681. ;; At the end of a greater element, move to the beginning of the
  22682. ;; last element within.
  22683. ((>= (point) contents-end)
  22684. (goto-char (1- contents-end))
  22685. (org-backward-paragraph))
  22686. (t (goto-char (or post-affiliated begin))))
  22687. ;; Ensure we never leave point invisible.
  22688. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22689. (defun org-forward-element ()
  22690. "Move forward by one element.
  22691. Move to the next element at the same level, when possible."
  22692. (interactive)
  22693. (cond ((eobp) (user-error "Cannot move further down"))
  22694. ((org-with-limited-levels (org-at-heading-p))
  22695. (let ((origin (point)))
  22696. (goto-char (org-end-of-subtree nil t))
  22697. (unless (org-with-limited-levels (org-at-heading-p))
  22698. (goto-char origin)
  22699. (user-error "Cannot move further down"))))
  22700. (t
  22701. (let* ((elem (org-element-at-point))
  22702. (end (org-element-property :end elem))
  22703. (parent (org-element-property :parent elem)))
  22704. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22705. (goto-char (org-element-property :end parent)))
  22706. ((integer-or-marker-p end) (goto-char end))
  22707. (t (message "No element at point")))))))
  22708. (defun org-backward-element ()
  22709. "Move backward by one element.
  22710. Move to the previous element at the same level, when possible."
  22711. (interactive)
  22712. (cond ((bobp) (user-error "Cannot move further up"))
  22713. ((org-with-limited-levels (org-at-heading-p))
  22714. ;; At a headline, move to the previous one, if any, or stay
  22715. ;; here.
  22716. (let ((origin (point)))
  22717. (org-with-limited-levels (org-backward-heading-same-level 1))
  22718. ;; When current headline has no sibling above, move to its
  22719. ;; parent.
  22720. (when (= (point) origin)
  22721. (or (org-with-limited-levels (org-up-heading-safe))
  22722. (progn (goto-char origin)
  22723. (user-error "Cannot move further up"))))))
  22724. (t
  22725. (let* ((elem (org-element-at-point))
  22726. (beg (org-element-property :begin elem)))
  22727. (cond
  22728. ;; Move to beginning of current element if point isn't
  22729. ;; there already.
  22730. ((null beg) (message "No element at point"))
  22731. ((/= (point) beg) (goto-char beg))
  22732. (t (goto-char beg)
  22733. (skip-chars-backward " \r\t\n")
  22734. (unless (bobp)
  22735. (let ((prev (org-element-at-point)))
  22736. (goto-char (org-element-property :begin prev))
  22737. (while (and (setq prev (org-element-property :parent prev))
  22738. (<= (org-element-property :end prev) beg))
  22739. (goto-char (org-element-property :begin prev)))))))))))
  22740. (defun org-up-element ()
  22741. "Move to upper element."
  22742. (interactive)
  22743. (if (org-with-limited-levels (org-at-heading-p))
  22744. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22745. (let* ((elem (org-element-at-point))
  22746. (parent (org-element-property :parent elem)))
  22747. (if parent (goto-char (org-element-property :begin parent))
  22748. (if (org-with-limited-levels (org-before-first-heading-p))
  22749. (user-error "No surrounding element")
  22750. (org-with-limited-levels (org-back-to-heading)))))))
  22751. (defvar org-element-greater-elements)
  22752. (defun org-down-element ()
  22753. "Move to inner element."
  22754. (interactive)
  22755. (let ((element (org-element-at-point)))
  22756. (cond
  22757. ((memq (org-element-type element) '(plain-list table))
  22758. (goto-char (org-element-property :contents-begin element))
  22759. (forward-char))
  22760. ((memq (org-element-type element) org-element-greater-elements)
  22761. ;; If contents are hidden, first disclose them.
  22762. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22763. (goto-char (or (org-element-property :contents-begin element)
  22764. (user-error "No content for this element"))))
  22765. (t (user-error "No inner element")))))
  22766. (defun org-drag-element-backward ()
  22767. "Move backward element at point."
  22768. (interactive)
  22769. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22770. (let* ((elem (org-element-at-point))
  22771. (prev-elem
  22772. (save-excursion
  22773. (goto-char (org-element-property :begin elem))
  22774. (skip-chars-backward " \r\t\n")
  22775. (unless (bobp)
  22776. (let* ((beg (org-element-property :begin elem))
  22777. (prev (org-element-at-point))
  22778. (up prev))
  22779. (while (and (setq up (org-element-property :parent up))
  22780. (<= (org-element-property :end up) beg))
  22781. (setq prev up))
  22782. prev)))))
  22783. ;; Error out if no previous element or previous element is
  22784. ;; a parent of the current one.
  22785. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22786. (user-error "Cannot drag element backward")
  22787. (let ((pos (point)))
  22788. (org-element-swap-A-B prev-elem elem)
  22789. (goto-char (+ (org-element-property :begin prev-elem)
  22790. (- pos (org-element-property :begin elem)))))))))
  22791. (defun org-drag-element-forward ()
  22792. "Move forward element at point."
  22793. (interactive)
  22794. (let* ((pos (point))
  22795. (elem (org-element-at-point)))
  22796. (when (= (point-max) (org-element-property :end elem))
  22797. (user-error "Cannot drag element forward"))
  22798. (goto-char (org-element-property :end elem))
  22799. (let ((next-elem (org-element-at-point)))
  22800. (when (or (org-element-nested-p elem next-elem)
  22801. (and (eq (org-element-type next-elem) 'headline)
  22802. (not (eq (org-element-type elem) 'headline))))
  22803. (goto-char pos)
  22804. (user-error "Cannot drag element forward"))
  22805. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22806. ;; body's length (without final blanks) and by the length of
  22807. ;; blanks between ELEM and NEXT-ELEM.
  22808. (let ((size-next (- (save-excursion
  22809. (goto-char (org-element-property :end next-elem))
  22810. (skip-chars-backward " \r\t\n")
  22811. (forward-line)
  22812. ;; Small correction if buffer doesn't end
  22813. ;; with a newline character.
  22814. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22815. (org-element-property :begin next-elem)))
  22816. (size-blank (- (org-element-property :end elem)
  22817. (save-excursion
  22818. (goto-char (org-element-property :end elem))
  22819. (skip-chars-backward " \r\t\n")
  22820. (forward-line)
  22821. (point)))))
  22822. (org-element-swap-A-B elem next-elem)
  22823. (goto-char (+ pos size-next size-blank))))))
  22824. (defun org-drag-line-forward (arg)
  22825. "Drag the line at point ARG lines forward."
  22826. (interactive "p")
  22827. (dotimes (n (abs arg))
  22828. (let ((c (current-column)))
  22829. (if (< 0 arg)
  22830. (progn
  22831. (beginning-of-line 2)
  22832. (transpose-lines 1)
  22833. (beginning-of-line 0))
  22834. (transpose-lines 1)
  22835. (beginning-of-line -1))
  22836. (org-move-to-column c))))
  22837. (defun org-drag-line-backward (arg)
  22838. "Drag the line at point ARG lines backward."
  22839. (interactive "p")
  22840. (org-drag-line-forward (- arg)))
  22841. (defun org-mark-element ()
  22842. "Put point at beginning of this element, mark at end.
  22843. Interactively, if this command is repeated or (in Transient Mark
  22844. mode) if the mark is active, it marks the next element after the
  22845. ones already marked."
  22846. (interactive)
  22847. (let (deactivate-mark)
  22848. (if (and (org-called-interactively-p 'any)
  22849. (or (and (eq last-command this-command) (mark t))
  22850. (and transient-mark-mode mark-active)))
  22851. (set-mark
  22852. (save-excursion
  22853. (goto-char (mark))
  22854. (goto-char (org-element-property :end (org-element-at-point)))))
  22855. (let ((element (org-element-at-point)))
  22856. (end-of-line)
  22857. (push-mark (org-element-property :end element) t t)
  22858. (goto-char (org-element-property :begin element))))))
  22859. (defun org-narrow-to-element ()
  22860. "Narrow buffer to current element."
  22861. (interactive)
  22862. (let ((elem (org-element-at-point)))
  22863. (cond
  22864. ((eq (car elem) 'headline)
  22865. (narrow-to-region
  22866. (org-element-property :begin elem)
  22867. (org-element-property :end elem)))
  22868. ((memq (car elem) org-element-greater-elements)
  22869. (narrow-to-region
  22870. (org-element-property :contents-begin elem)
  22871. (org-element-property :contents-end elem)))
  22872. (t
  22873. (narrow-to-region
  22874. (org-element-property :begin elem)
  22875. (org-element-property :end elem))))))
  22876. (defun org-transpose-element ()
  22877. "Transpose current and previous elements, keeping blank lines between.
  22878. Point is moved after both elements."
  22879. (interactive)
  22880. (org-skip-whitespace)
  22881. (let ((end (org-element-property :end (org-element-at-point))))
  22882. (org-drag-element-backward)
  22883. (goto-char end)))
  22884. (defun org-unindent-buffer ()
  22885. "Un-indent the visible part of the buffer.
  22886. Relative indentation (between items, inside blocks, etc.) isn't
  22887. modified."
  22888. (interactive)
  22889. (unless (eq major-mode 'org-mode)
  22890. (user-error "Cannot un-indent a buffer not in Org mode"))
  22891. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22892. unindent-tree ; For byte-compiler.
  22893. (unindent-tree
  22894. (function
  22895. (lambda (contents)
  22896. (mapc
  22897. (lambda (element)
  22898. (if (memq (org-element-type element) '(headline section))
  22899. (funcall unindent-tree (org-element-contents element))
  22900. (save-excursion
  22901. (save-restriction
  22902. (narrow-to-region
  22903. (org-element-property :begin element)
  22904. (org-element-property :end element))
  22905. (org-do-remove-indentation)))))
  22906. (reverse contents))))))
  22907. (funcall unindent-tree (org-element-contents parse-tree))))
  22908. (defun org-show-subtree ()
  22909. "Show everything after this heading at deeper levels."
  22910. (interactive)
  22911. (outline-flag-region
  22912. (point)
  22913. (save-excursion
  22914. (org-end-of-subtree t t))
  22915. nil))
  22916. (defun org-show-entry ()
  22917. "Show the body directly following this heading.
  22918. Show the heading too, if it is currently invisible."
  22919. (interactive)
  22920. (save-excursion
  22921. (ignore-errors
  22922. (org-back-to-heading t)
  22923. (outline-flag-region
  22924. (max (point-min) (1- (point)))
  22925. (save-excursion
  22926. (if (re-search-forward
  22927. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22928. (match-beginning 1)
  22929. (point-max)))
  22930. nil)
  22931. (org-cycle-hide-drawers 'children))))
  22932. (defun org-make-options-regexp (kwds &optional extra)
  22933. "Make a regular expression for keyword lines.
  22934. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22935. when non-nil, is a regexp matching keywords names."
  22936. (concat "^[ \t]*#\\+\\("
  22937. (regexp-opt kwds)
  22938. (and extra (concat (and kwds "\\|") extra))
  22939. "\\):[ \t]*\\(.*\\)"))
  22940. ;; Make isearch reveal the necessary context
  22941. (defun org-isearch-end ()
  22942. "Reveal context after isearch exits."
  22943. (when isearch-success ; only if search was successful
  22944. (if (featurep 'xemacs)
  22945. ;; Under XEmacs, the hook is run in the correct place,
  22946. ;; we directly show the context.
  22947. (org-show-context 'isearch)
  22948. ;; In Emacs the hook runs *before* restoring the overlays.
  22949. ;; So we have to use a one-time post-command-hook to do this.
  22950. ;; (Emacs 22 has a special variable, see function `org-mode')
  22951. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22952. isearch-mode-end-hook-quit)
  22953. ;; Only when the isearch was not quitted.
  22954. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22955. 'append 'local)))))
  22956. (defun org-isearch-post-command ()
  22957. "Remove self from hook, and show context."
  22958. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22959. (org-show-context 'isearch))
  22960. ;;;; Integration with and fixes for other packages
  22961. ;;; Imenu support
  22962. (defvar org-imenu-markers nil
  22963. "All markers currently used by Imenu.")
  22964. (make-variable-buffer-local 'org-imenu-markers)
  22965. (defun org-imenu-new-marker (&optional pos)
  22966. "Return a new marker for use by Imenu, and remember the marker."
  22967. (let ((m (make-marker)))
  22968. (move-marker m (or pos (point)))
  22969. (push m org-imenu-markers)
  22970. m))
  22971. (defun org-imenu-get-tree ()
  22972. "Produce the index for Imenu."
  22973. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22974. (setq org-imenu-markers nil)
  22975. (let* ((n org-imenu-depth)
  22976. (re (concat "^" (org-get-limited-outline-regexp)))
  22977. (subs (make-vector (1+ n) nil))
  22978. (last-level 0)
  22979. m level head0 head)
  22980. (save-excursion
  22981. (save-restriction
  22982. (widen)
  22983. (goto-char (point-max))
  22984. (while (re-search-backward re nil t)
  22985. (setq level (org-reduced-level (funcall outline-level)))
  22986. (when (and (<= level n)
  22987. (looking-at org-complex-heading-regexp)
  22988. (setq head0 (org-match-string-no-properties 4)))
  22989. (setq head (org-link-display-format head0)
  22990. m (org-imenu-new-marker))
  22991. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22992. (if (>= level last-level)
  22993. (push (cons head m) (aref subs level))
  22994. (push (cons head (aref subs (1+ level))) (aref subs level))
  22995. (loop for i from (1+ level) to n do (aset subs i nil)))
  22996. (setq last-level level)))))
  22997. (aref subs 1)))
  22998. (eval-after-load "imenu"
  22999. '(progn
  23000. (add-hook 'imenu-after-jump-hook
  23001. (lambda ()
  23002. (if (derived-mode-p 'org-mode)
  23003. (org-show-context 'org-goto))))))
  23004. (defun org-link-display-format (s)
  23005. "Replace links in string S with their description.
  23006. If there is no description, use the link target."
  23007. (save-match-data
  23008. (replace-regexp-in-string
  23009. org-bracket-link-analytic-regexp
  23010. (lambda (m)
  23011. (if (match-end 5) (match-string 5 m)
  23012. (concat (match-string 1 m) (match-string 3 m))))
  23013. s nil t)))
  23014. (defun org-toggle-link-display ()
  23015. "Toggle the literal or descriptive display of links."
  23016. (interactive)
  23017. (if org-descriptive-links
  23018. (progn (org-remove-from-invisibility-spec '(org-link))
  23019. (org-restart-font-lock)
  23020. (setq org-descriptive-links nil))
  23021. (progn (add-to-invisibility-spec '(org-link))
  23022. (org-restart-font-lock)
  23023. (setq org-descriptive-links t))))
  23024. ;; Speedbar support
  23025. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23026. "Overlay marking the agenda restriction line in speedbar.")
  23027. (overlay-put org-speedbar-restriction-lock-overlay
  23028. 'face 'org-agenda-restriction-lock)
  23029. (overlay-put org-speedbar-restriction-lock-overlay
  23030. 'help-echo "Agendas are currently limited to this item.")
  23031. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  23032. (defun org-speedbar-set-agenda-restriction ()
  23033. "Restrict future agenda commands to the location at point in speedbar.
  23034. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23035. (interactive)
  23036. (require 'org-agenda)
  23037. (let (p m tp np dir txt)
  23038. (cond
  23039. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23040. 'org-imenu t))
  23041. (setq m (get-text-property p 'org-imenu-marker))
  23042. (with-current-buffer (marker-buffer m)
  23043. (goto-char m)
  23044. (org-agenda-set-restriction-lock 'subtree)))
  23045. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23046. 'speedbar-function 'speedbar-find-file))
  23047. (setq tp (previous-single-property-change
  23048. (1+ p) 'speedbar-function)
  23049. np (next-single-property-change
  23050. tp 'speedbar-function)
  23051. dir (speedbar-line-directory)
  23052. txt (buffer-substring-no-properties (or tp (point-min))
  23053. (or np (point-max))))
  23054. (with-current-buffer (find-file-noselect
  23055. (let ((default-directory dir))
  23056. (expand-file-name txt)))
  23057. (unless (derived-mode-p 'org-mode)
  23058. (user-error "Cannot restrict to non-Org-mode file"))
  23059. (org-agenda-set-restriction-lock 'file)))
  23060. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23061. (move-overlay org-speedbar-restriction-lock-overlay
  23062. (point-at-bol) (point-at-eol))
  23063. (setq current-prefix-arg nil)
  23064. (org-agenda-maybe-redo)))
  23065. (defvar speedbar-file-key-map)
  23066. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23067. (eval-after-load "speedbar"
  23068. '(progn
  23069. (speedbar-add-supported-extension ".org")
  23070. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23071. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23072. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23073. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23074. (add-hook 'speedbar-visiting-tag-hook
  23075. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23076. ;;; Fixes and Hacks for problems with other packages
  23077. (defun org--flyspell-object-check-p (element)
  23078. "Non-nil when Flyspell can check object at point.
  23079. ELEMENT is the element at point."
  23080. (let ((object (save-excursion
  23081. (when (org-looking-at-p "\\>") (backward-char))
  23082. (org-element-context element))))
  23083. (case (org-element-type object)
  23084. ;; Prevent checks in links due to keybinding conflict with
  23085. ;; Flyspell.
  23086. ((code entity export-snippet inline-babel-call
  23087. inline-src-block line-break latex-fragment link macro
  23088. statistics-cookie target timestamp verbatim)
  23089. nil)
  23090. (footnote-reference
  23091. ;; Only in inline footnotes, within the definition.
  23092. (and (eq (org-element-property :type object) 'inline)
  23093. (< (save-excursion
  23094. (goto-char (org-element-property :begin object))
  23095. (search-forward ":" nil t 2))
  23096. (point))))
  23097. (otherwise t))))
  23098. (defun org-mode-flyspell-verify ()
  23099. "Function used for `flyspell-generic-check-word-predicate'."
  23100. (if (org-at-heading-p)
  23101. ;; At a headline or an inlinetask, check title only. This is
  23102. ;; faster than relying on `org-element-at-point'.
  23103. (and (save-excursion (beginning-of-line)
  23104. (and (let ((case-fold-search t))
  23105. (not (looking-at "\\*+ END[ \t]*$")))
  23106. (looking-at org-complex-heading-regexp)))
  23107. (match-beginning 4)
  23108. (>= (point) (match-beginning 4))
  23109. (or (not (match-beginning 5))
  23110. (< (point) (match-beginning 5))))
  23111. (let* ((element (org-element-at-point))
  23112. (post-affiliated (org-element-property :post-affiliated element)))
  23113. (cond
  23114. ;; Ignore checks in all affiliated keywords but captions.
  23115. ((< (point) post-affiliated)
  23116. (and (save-excursion
  23117. (beginning-of-line)
  23118. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23119. (> (point) (match-end 0))
  23120. (org--flyspell-object-check-p element)))
  23121. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23122. ((let ((log (org-log-into-drawer)))
  23123. (and log
  23124. (let ((drawer (org-element-lineage element '(drawer))))
  23125. (and drawer
  23126. (eq (compare-strings
  23127. log nil nil
  23128. (org-element-property :drawer-name drawer) nil nil t)
  23129. t)))))
  23130. nil)
  23131. (t
  23132. (case (org-element-type element)
  23133. ((comment quote-section) t)
  23134. (comment-block
  23135. ;; Allow checks between block markers, not on them.
  23136. (and (> (line-beginning-position) post-affiliated)
  23137. (save-excursion
  23138. (end-of-line)
  23139. (skip-chars-forward " \r\t\n")
  23140. (< (point) (org-element-property :end element)))))
  23141. ;; Arbitrary list of keywords where checks are meaningful.
  23142. ;; Make sure point is on the value part of the element.
  23143. (keyword
  23144. (and (member (org-element-property :key element)
  23145. '("DESCRIPTION" "TITLE"))
  23146. (save-excursion
  23147. (search-backward ":" (line-beginning-position) t))))
  23148. ;; Check is globally allowed in paragraphs verse blocks and
  23149. ;; table rows (after affiliated keywords) but some objects
  23150. ;; must not be affected.
  23151. ((paragraph table-row verse-block)
  23152. (let ((cbeg (org-element-property :contents-begin element))
  23153. (cend (org-element-property :contents-end element)))
  23154. (and cbeg (>= (point) cbeg) (< (point) cend)
  23155. (org--flyspell-object-check-p element))))))))))
  23156. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23157. (defun org-remove-flyspell-overlays-in (beg end)
  23158. "Remove flyspell overlays in region."
  23159. (and (org-bound-and-true-p flyspell-mode)
  23160. (fboundp 'flyspell-delete-region-overlays)
  23161. (flyspell-delete-region-overlays beg end)))
  23162. (defvar flyspell-delayed-commands)
  23163. (eval-after-load "flyspell"
  23164. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23165. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23166. (eval-after-load "bookmark"
  23167. '(if (boundp 'bookmark-after-jump-hook)
  23168. ;; We can use the hook
  23169. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23170. ;; Hook not available, use advice
  23171. (defadvice bookmark-jump (after org-make-visible activate)
  23172. "Make the position visible."
  23173. (org-bookmark-jump-unhide))))
  23174. ;; Make sure saveplace shows the location if it was hidden
  23175. (eval-after-load "saveplace"
  23176. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23177. "Make the position visible."
  23178. (org-bookmark-jump-unhide)))
  23179. ;; Make sure ecb shows the location if it was hidden
  23180. (eval-after-load "ecb"
  23181. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23182. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23183. (if (derived-mode-p 'org-mode)
  23184. (org-show-context))))
  23185. (defun org-bookmark-jump-unhide ()
  23186. "Unhide the current position, to show the bookmark location."
  23187. (and (derived-mode-p 'org-mode)
  23188. (or (outline-invisible-p)
  23189. (save-excursion (goto-char (max (point-min) (1- (point))))
  23190. (outline-invisible-p)))
  23191. (org-show-context 'bookmark-jump)))
  23192. (defun org-mark-jump-unhide ()
  23193. "Make the point visible with `org-show-context' after jumping to the mark."
  23194. (when (and (derived-mode-p 'org-mode)
  23195. (outline-invisible-p))
  23196. (org-show-context 'mark-goto)))
  23197. (eval-after-load "simple"
  23198. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23199. "Make the point visible with `org-show-context'."
  23200. (org-mark-jump-unhide)))
  23201. (eval-after-load "simple"
  23202. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23203. "Make the point visible with `org-show-context'."
  23204. (org-mark-jump-unhide)))
  23205. (eval-after-load "simple"
  23206. '(defadvice pop-global-mark (after org-make-visible activate)
  23207. "Make the point visible with `org-show-context'."
  23208. (org-mark-jump-unhide)))
  23209. ;; Make session.el ignore our circular variable
  23210. (defvar session-globals-exclude)
  23211. (eval-after-load "session"
  23212. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23213. ;;;; Finish up
  23214. (provide 'org)
  23215. (run-hooks 'org-load-hook)
  23216. ;;; org.el ends here