org.el 958 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 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 "Stan" stan)
  283. (const :tag "ebnf2ps" ebnf2ps))
  284. :value-type (boolean :tag "Activate" :value t)))
  285. ;;;; Customization variables
  286. (defcustom org-clone-delete-id nil
  287. "Remove ID property of clones of a subtree.
  288. When non-nil, clones of a subtree don't inherit the ID property.
  289. Otherwise they inherit the ID property with a new unique
  290. identifier."
  291. :type 'boolean
  292. :version "24.1"
  293. :group 'org-id)
  294. ;;; Version
  295. (org-check-version)
  296. ;;;###autoload
  297. (defun org-version (&optional here full message)
  298. "Show the org-mode version.
  299. Interactively, or when MESSAGE is non-nil, show it in echo area.
  300. With prefix argument, or when HERE is non-nil, insert it at point.
  301. In non-interactive uses, a reduced version string is output unless
  302. FULL is given."
  303. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  304. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  305. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  306. (load-suffixes (list ".el"))
  307. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  308. (org-trash (or
  309. (and (fboundp 'org-release) (fboundp 'org-git-version))
  310. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  311. (load-suffixes save-load-suffixes)
  312. (org-version (org-release))
  313. (git-version (org-git-version))
  314. (version (format "Org-mode version %s (%s @ %s)"
  315. org-version
  316. git-version
  317. (if org-install-dir
  318. (if (string= org-dir org-install-dir)
  319. org-install-dir
  320. (concat "mixed installation! " org-install-dir " and " org-dir))
  321. "org-loaddefs.el can not be found!")))
  322. (version1 (if full version org-version)))
  323. (when here (insert version1))
  324. (when message (message "%s" version1))
  325. version1))
  326. (defconst org-version (org-version))
  327. ;;; Syntax Constants
  328. ;;;; Block
  329. (defconst org-block-regexp
  330. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  331. "Regular expression for hiding blocks.")
  332. (defconst org-dblock-start-re
  333. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  334. "Matches the start line of a dynamic block, with parameters.")
  335. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  336. "Matches the end of a dynamic block.")
  337. ;;;; Clock and Planning
  338. (defconst org-clock-string "CLOCK:"
  339. "String used as prefix for timestamps clocking work hours on an item.")
  340. (defvar org-closed-string "CLOSED:"
  341. "String used as the prefix for timestamps logging closing a TODO entry.")
  342. (defvar org-deadline-string "DEADLINE:"
  343. "String to mark deadline entries.
  344. A deadline is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-deadline].")
  347. (defvar org-scheduled-string "SCHEDULED:"
  348. "String to mark scheduled TODO entries.
  349. A schedule is this string, followed by a time stamp. Should be a word,
  350. terminated by a colon. You can insert a schedule keyword and
  351. a timestamp with \\[org-schedule].")
  352. (defconst org-ds-keyword-length
  353. (+ 2
  354. (apply #'max
  355. (mapcar #'length
  356. (list org-deadline-string org-scheduled-string
  357. org-clock-string org-closed-string))))
  358. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  359. (defconst org-planning-line-re
  360. (concat "^[ \t]*"
  361. (regexp-opt
  362. (list org-closed-string org-deadline-string org-scheduled-string)
  363. t))
  364. "Matches a line with planning info.
  365. Matched keyword is in group 1.")
  366. (defconst org-clock-line-re
  367. (concat "^[ \t]*" org-clock-string)
  368. "Matches a line with clock info.")
  369. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  370. "Matches the DEADLINE keyword.")
  371. (defconst org-deadline-time-regexp
  372. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  373. "Matches the DEADLINE keyword together with a time stamp.")
  374. (defconst org-deadline-time-hour-regexp
  375. (concat "\\<" org-deadline-string
  376. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  377. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  378. (defconst org-deadline-line-regexp
  379. (concat "\\<\\(" org-deadline-string "\\).*")
  380. "Matches the DEADLINE keyword and the rest of the line.")
  381. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  382. "Matches the SCHEDULED keyword.")
  383. (defconst org-scheduled-time-regexp
  384. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  385. "Matches the SCHEDULED keyword together with a time stamp.")
  386. (defconst org-scheduled-time-hour-regexp
  387. (concat "\\<" org-scheduled-string
  388. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  389. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  390. (defconst org-closed-time-regexp
  391. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  392. "Matches the CLOSED keyword together with a time stamp.")
  393. (defconst org-keyword-time-regexp
  394. (concat "\\<"
  395. (regexp-opt
  396. (list org-scheduled-string org-deadline-string org-closed-string
  397. org-clock-string)
  398. t)
  399. " *[[<]\\([^]>]+\\)[]>]")
  400. "Matches any of the 4 keywords, together with the time stamp.")
  401. (defconst org-keyword-time-not-clock-regexp
  402. (concat
  403. "\\<"
  404. (regexp-opt
  405. (list org-scheduled-string org-deadline-string org-closed-string) t)
  406. " *[[<]\\([^]>]+\\)[]>]")
  407. "Matches any of the 3 keywords, together with the time stamp.")
  408. (defconst org-maybe-keyword-time-regexp
  409. (concat "\\(\\<"
  410. (regexp-opt
  411. (list org-scheduled-string org-deadline-string org-closed-string
  412. org-clock-string)
  413. t)
  414. "\\)?"
  415. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  416. "\\|"
  417. "<%%([^\r\n>]*>\\)")
  418. "Matches a timestamp, possibly preceded by a keyword.")
  419. (defconst org-all-time-keywords
  420. (mapcar (lambda (w) (substring w 0 -1))
  421. (list org-scheduled-string org-deadline-string
  422. org-clock-string org-closed-string))
  423. "List of time keywords.")
  424. ;;;; Drawer
  425. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  426. "Matches first or last line of a hidden block.
  427. Group 1 contains drawer's name or \"END\".")
  428. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  429. "Regular expression matching the first line of a property drawer.")
  430. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  431. "Regular expression matching the last line of a property drawer.")
  432. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  433. "Regular expression matching the first line of a clock drawer.")
  434. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  435. "Regular expression matching the last line of a clock drawer.")
  436. (defconst org-property-drawer-re
  437. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  438. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  439. "[ \t]*:END:[ \t]*$")
  440. "Matches an entire property drawer.")
  441. (defconst org-clock-drawer-re
  442. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  443. org-clock-drawer-end-re "\\)\n?")
  444. "Matches an entire clock drawer.")
  445. ;;;; Headline
  446. (defconst org-heading-keyword-regexp-format
  447. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  448. "Printf format for a regexp matching a headline with some keyword.
  449. This regexp will match the headline of any node which has the
  450. exact keyword that is put into the format. The keyword isn't in
  451. any group by default, but the stars and the body are.")
  452. (defconst org-heading-keyword-maybe-regexp-format
  453. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  454. "Printf format for a regexp matching a headline, possibly with some keyword.
  455. This regexp can match any headline with the specified keyword, or
  456. without a keyword. The keyword isn't in any group by default,
  457. but the stars and the body are.")
  458. (defconst org-archive-tag "ARCHIVE"
  459. "The tag that marks a subtree as archived.
  460. An archived subtree does not open during visibility cycling, and does
  461. not contribute to the agenda listings.")
  462. (defconst org-comment-string "COMMENT"
  463. "Entries starting with this keyword will never be exported.
  464. An entry can be toggled between COMMENT and normal with
  465. \\[org-toggle-comment].")
  466. ;;;; LaTeX Environments and Fragments
  467. (defconst org-latex-regexps
  468. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  469. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  470. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  471. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  472. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  473. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  474. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  475. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  476. "Regular expressions for matching embedded LaTeX.")
  477. ;;;; Node Property
  478. (defconst org-effort-property "Effort"
  479. "The property that is being used to keep track of effort estimates.
  480. Effort estimates given in this property need to have the format H:MM.")
  481. ;;;; Table
  482. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  483. "Detect an org-type or table-type table.")
  484. (defconst org-table-line-regexp "^[ \t]*|"
  485. "Detect an org-type table line.")
  486. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  487. "Detect an org-type table line.")
  488. (defconst org-table-hline-regexp "^[ \t]*|-"
  489. "Detect an org-type table hline.")
  490. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  491. "Detect a table-type table hline.")
  492. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  493. "Detect the first line outside a table when searching from within it.
  494. This works for both table types.")
  495. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  496. "Detect a #+TBLFM line.")
  497. ;;;; Timestamp
  498. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  499. "Regular expression for fast time stamp matching.")
  500. (defconst org-ts-regexp-inactive
  501. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  502. "Regular expression for fast inactive time stamp matching.")
  503. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  504. "Regular expression for fast time stamp matching.")
  505. (defconst org-ts-regexp0
  506. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  507. "Regular expression matching time strings for analysis.
  508. This one does not require the space after the date, so it can be used
  509. on a string that terminates immediately after the date.")
  510. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  511. "Regular expression matching time strings for analysis.")
  512. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  513. "Regular expression matching time stamps, with groups.")
  514. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  515. "Regular expression matching time stamps (also [..]), with groups.")
  516. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  517. "Regular expression matching a time stamp range.")
  518. (defconst org-tr-regexp-both
  519. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  520. "Regular expression matching a time stamp range.")
  521. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  522. org-ts-regexp "\\)?")
  523. "Regular expression matching a time stamp or time stamp range.")
  524. (defconst org-tsr-regexp-both
  525. (concat org-ts-regexp-both "\\(--?-?"
  526. org-ts-regexp-both "\\)?")
  527. "Regular expression matching a time stamp or time stamp range.
  528. The time stamps may be either active or inactive.")
  529. (defconst org-repeat-re
  530. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  531. "Regular expression for specifying repeated events.
  532. After a match, group 1 contains the repeat expression.")
  533. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  534. "Formats for `format-time-string' which are used for time stamps.")
  535. ;;; The custom variables
  536. (defgroup org nil
  537. "Outline-based notes management and organizer."
  538. :tag "Org"
  539. :group 'outlines
  540. :group 'calendar)
  541. (defcustom org-mode-hook nil
  542. "Mode hook for Org-mode, run after the mode was turned on."
  543. :group 'org
  544. :type 'hook)
  545. (defcustom org-load-hook nil
  546. "Hook that is run after org.el has been loaded."
  547. :group 'org
  548. :type 'hook)
  549. (defcustom org-log-buffer-setup-hook nil
  550. "Hook that is run after an Org log buffer is created."
  551. :group 'org
  552. :version "24.1"
  553. :type 'hook)
  554. (defvar org-modules) ; defined below
  555. (defvar org-modules-loaded nil
  556. "Have the modules been loaded already?")
  557. (defun org-load-modules-maybe (&optional force)
  558. "Load all extensions listed in `org-modules'."
  559. (when (or force (not org-modules-loaded))
  560. (mapc (lambda (ext)
  561. (condition-case nil (require ext)
  562. (error (message "Problems while trying to load feature `%s'" ext))))
  563. org-modules)
  564. (setq org-modules-loaded t)))
  565. (defun org-set-modules (var value)
  566. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  567. (set var value)
  568. (when (featurep 'org)
  569. (org-load-modules-maybe 'force)
  570. (org-element-cache-reset 'all)))
  571. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  572. "Modules that should always be loaded together with org.el.
  573. If a description starts with <C>, the file is not part of Emacs
  574. and loading it will require that you have downloaded and properly
  575. installed the Org mode distribution.
  576. You can also use this system to load external packages (i.e. neither Org
  577. core modules, nor modules from the CONTRIB directory). Just add symbols
  578. to the end of the list. If the package is called org-xyz.el, then you need
  579. to add the symbol `xyz', and the package must have a call to:
  580. (provide \\='org-xyz)
  581. For export specific modules, see also `org-export-backends'."
  582. :group 'org
  583. :set 'org-set-modules
  584. :version "24.4"
  585. :package-version '(Org . "8.0")
  586. :type
  587. '(set :greedy t
  588. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  589. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  590. (const :tag " crypt: Encryption of subtrees" org-crypt)
  591. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  592. (const :tag " docview: Links to doc-view buffers" org-docview)
  593. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  594. (const :tag " habit: Track your consistency with habits" org-habit)
  595. (const :tag " id: Global IDs for identifying entries" org-id)
  596. (const :tag " info: Links to Info nodes" org-info)
  597. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  598. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  599. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  600. (const :tag " mouse: Additional mouse support" org-mouse)
  601. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  602. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  603. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  604. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  605. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  606. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  607. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  608. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  609. (const :tag "C collector: Collect properties into tables" org-collector)
  610. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  611. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  612. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  613. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  614. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  615. (const :tag "C eval: Include command output as text" org-eval)
  616. (const :tag "C eww: Store link to url of eww" org-eww)
  617. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  618. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  619. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  620. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  621. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  622. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  623. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  624. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  625. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  626. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  627. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  628. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  629. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  630. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  631. (const :tag "C registry: A registry for Org-mode links" org-registry)
  632. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  633. (const :tag "C secretary: Team management with org-mode" org-secretary)
  634. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  635. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  636. (const :tag "C track: Keep up with Org-mode development" org-track)
  637. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  638. (const :tag "C vm: Links to VM folders/messages" org-vm)
  639. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  640. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  641. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  642. (defvar org-export-registered-backends) ; From ox.el.
  643. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  644. (declare-function org-export-backend-name "ox" (backend))
  645. (defcustom org-export-backends '(ascii html icalendar latex)
  646. "List of export back-ends that should be always available.
  647. If a description starts with <C>, the file is not part of Emacs
  648. and loading it will require that you have downloaded and properly
  649. installed the Org mode distribution.
  650. Unlike to `org-modules', libraries in this list will not be
  651. loaded along with Org, but only once the export framework is
  652. needed.
  653. This variable needs to be set before org.el is loaded. If you
  654. need to make a change while Emacs is running, use the customize
  655. interface or run the following code, where VAL stands for the new
  656. value of the variable, after updating it:
  657. (progn
  658. (setq org-export-registered-backends
  659. (org-remove-if-not
  660. (lambda (backend)
  661. (let ((name (org-export-backend-name backend)))
  662. (or (memq name val)
  663. (catch \\='parentp
  664. (dolist (b val)
  665. (and (org-export-derived-backend-p b name)
  666. (throw \\='parentp t)))))))
  667. org-export-registered-backends))
  668. (let ((new-list (mapcar #\\='org-export-backend-name
  669. org-export-registered-backends)))
  670. (dolist (backend val)
  671. (cond
  672. ((not (load (format \"ox-%s\" backend) t t))
  673. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  674. backend))
  675. ((not (memq backend new-list)) (push backend new-list))))
  676. (set-default \\='org-export-backends new-list)))
  677. Adding a back-end to this list will also pull the back-end it
  678. depends on, if any."
  679. :group 'org
  680. :group 'org-export
  681. :version "24.4"
  682. :package-version '(Org . "8.0")
  683. :initialize 'custom-initialize-set
  684. :set (lambda (var val)
  685. (if (not (featurep 'ox)) (set-default var val)
  686. ;; Any back-end not required anymore (not present in VAL and not
  687. ;; a parent of any back-end in the new value) is removed from the
  688. ;; list of registered back-ends.
  689. (setq org-export-registered-backends
  690. (org-remove-if-not
  691. (lambda (backend)
  692. (let ((name (org-export-backend-name backend)))
  693. (or (memq name val)
  694. (catch 'parentp
  695. (dolist (b val)
  696. (and (org-export-derived-backend-p b name)
  697. (throw 'parentp t)))))))
  698. org-export-registered-backends))
  699. ;; Now build NEW-LIST of both new back-ends and required
  700. ;; parents.
  701. (let ((new-list (mapcar #'org-export-backend-name
  702. org-export-registered-backends)))
  703. (dolist (backend val)
  704. (cond
  705. ((not (load (format "ox-%s" backend) t t))
  706. (message "Problems while trying to load export back-end `%s'"
  707. backend))
  708. ((not (memq backend new-list)) (push backend new-list))))
  709. ;; Set VAR to that list with fixed dependencies.
  710. (set-default var new-list))))
  711. :type '(set :greedy t
  712. (const :tag " ascii Export buffer to ASCII format" ascii)
  713. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  714. (const :tag " html Export buffer to HTML format" html)
  715. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  716. (const :tag " latex Export buffer to LaTeX format" latex)
  717. (const :tag " man Export buffer to MAN format" man)
  718. (const :tag " md Export buffer to Markdown format" md)
  719. (const :tag " odt Export buffer to ODT format" odt)
  720. (const :tag " org Export buffer to Org format" org)
  721. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  722. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  723. (const :tag "C deck Export buffer to deck.js presentations" deck)
  724. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  725. (const :tag "C groff Export buffer to Groff format" groff)
  726. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  727. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  728. (const :tag "C s5 Export buffer to s5 presentations" s5)
  729. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  730. (eval-after-load 'ox
  731. '(mapc
  732. (lambda (backend)
  733. (condition-case nil (require (intern (format "ox-%s" backend)))
  734. (error (message "Problems while trying to load export back-end `%s'"
  735. backend))))
  736. org-export-backends))
  737. (defcustom org-support-shift-select nil
  738. "Non-nil means make shift-cursor commands select text when possible.
  739. \\<org-mode-map>\
  740. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  741. start selecting a region, or enlarge regions started in this way.
  742. In Org-mode, in special contexts, these same keys are used for
  743. other purposes, important enough to compete with shift selection.
  744. Org tries to balance these needs by supporting `shift-select-mode'
  745. outside these special contexts, under control of this variable.
  746. The default of this variable is nil, to avoid confusing behavior. Shifted
  747. cursor keys will then execute Org commands in the following contexts:
  748. - on a headline, changing TODO state (left/right) and priority (up/down)
  749. - on a time stamp, changing the time
  750. - in a plain list item, changing the bullet type
  751. - in a property definition line, switching between allowed values
  752. - in the BEGIN line of a clock table (changing the time block).
  753. Outside these contexts, the commands will throw an error.
  754. When this variable is t and the cursor is not in a special
  755. context, Org-mode will support shift-selection for making and
  756. enlarging regions. To make this more effective, the bullet
  757. cycling will no longer happen anywhere in an item line, but only
  758. if the cursor is exactly on the bullet.
  759. If you set this variable to the symbol `always', then the keys
  760. will not be special in headlines, property lines, and item lines,
  761. to make shift selection work there as well. If this is what you
  762. want, you can use the following alternative commands:
  763. `\\[org-todo]' and `\\[org-priority]' \
  764. to change TODO state and priority,
  765. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  766. can be used to switch TODO sets,
  767. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  768. and properties can be edited by hand or in column view.
  769. However, when the cursor is on a timestamp, shift-cursor commands
  770. will still edit the time stamp - this is just too good to give up.
  771. XEmacs user should have this variable set to nil, because
  772. `shift-select-mode' is in Emacs 23 or later only."
  773. :group 'org
  774. :type '(choice
  775. (const :tag "Never" nil)
  776. (const :tag "When outside special context" t)
  777. (const :tag "Everywhere except timestamps" always)))
  778. (defcustom org-loop-over-headlines-in-active-region nil
  779. "Shall some commands act upon headlines in the active region?
  780. When set to t, some commands will be performed in all headlines
  781. within the active region.
  782. When set to `start-level', some commands will be performed in all
  783. headlines within the active region, provided that these headlines
  784. are of the same level than the first one.
  785. When set to a string, those commands will be performed on the
  786. matching headlines within the active region. Such string must be
  787. a tags/property/todo match as it is used in the agenda tags view.
  788. The list of commands is: `org-schedule', `org-deadline',
  789. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  790. `org-archive-to-archive-sibling'. The archiving commands skip
  791. already archived entries."
  792. :type '(choice (const :tag "Don't loop" nil)
  793. (const :tag "All headlines in active region" t)
  794. (const :tag "In active region, headlines at the same level than the first one" start-level)
  795. (string :tag "Tags/Property/Todo matcher"))
  796. :version "24.1"
  797. :group 'org-todo
  798. :group 'org-archive)
  799. (defgroup org-startup nil
  800. "Options concerning startup of Org-mode."
  801. :tag "Org Startup"
  802. :group 'org)
  803. (defcustom org-startup-folded t
  804. "Non-nil means entering Org-mode will switch to OVERVIEW.
  805. This can also be configured on a per-file basis by adding one of
  806. the following lines anywhere in the buffer:
  807. #+STARTUP: fold (or `overview', this is equivalent)
  808. #+STARTUP: nofold (or `showall', this is equivalent)
  809. #+STARTUP: content
  810. #+STARTUP: showeverything
  811. By default, this option is ignored when Org opens agenda files
  812. for the first time. If you want the agenda to honor the startup
  813. option, set `org-agenda-inhibit-startup' to nil."
  814. :group 'org-startup
  815. :type '(choice
  816. (const :tag "nofold: show all" nil)
  817. (const :tag "fold: overview" t)
  818. (const :tag "content: all headlines" content)
  819. (const :tag "show everything, even drawers" showeverything)))
  820. (defcustom org-startup-truncated t
  821. "Non-nil means entering Org-mode will set `truncate-lines'.
  822. This is useful since some lines containing links can be very long and
  823. uninteresting. Also tables look terrible when wrapped."
  824. :group 'org-startup
  825. :type 'boolean)
  826. (defcustom org-startup-indented nil
  827. "Non-nil means turn on `org-indent-mode' on startup.
  828. This can also be configured on a per-file basis by adding one of
  829. the following lines anywhere in the buffer:
  830. #+STARTUP: indent
  831. #+STARTUP: noindent"
  832. :group 'org-structure
  833. :type '(choice
  834. (const :tag "Not" nil)
  835. (const :tag "Globally (slow on startup in large files)" t)))
  836. (defcustom org-use-sub-superscripts t
  837. "Non-nil means interpret \"_\" and \"^\" for display.
  838. If you want to control how Org exports those characters, see
  839. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  840. used to be an alias for `org-export-with-sub-superscripts' in
  841. Org <8.0, it is not anymore.
  842. When this option is turned on, you can use TeX-like syntax for
  843. sub- and superscripts within the buffer. Several characters after
  844. \"_\" or \"^\" will be considered as a single item - so grouping
  845. with {} is normally not needed. For example, the following things
  846. will be parsed as single sub- or superscripts:
  847. 10^24 or 10^tau several digits will be considered 1 item.
  848. 10^-12 or 10^-tau a leading sign with digits or a word
  849. x^2-y^3 will be read as x^2 - y^3, because items are
  850. terminated by almost any nonword/nondigit char.
  851. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  852. Still, ambiguity is possible. So when in doubt, use {} to enclose
  853. the sub/superscript. If you set this variable to the symbol `{}',
  854. the braces are *required* in order to trigger interpretations as
  855. sub/superscript. This can be helpful in documents that need \"_\"
  856. frequently in plain text."
  857. :group 'org-startup
  858. :version "24.4"
  859. :package-version '(Org . "8.0")
  860. :type '(choice
  861. (const :tag "Always interpret" t)
  862. (const :tag "Only with braces" {})
  863. (const :tag "Never interpret" nil)))
  864. (defcustom org-startup-with-beamer-mode nil
  865. "Non-nil means turn on `org-beamer-mode' on startup.
  866. This can also be configured on a per-file basis by adding one of
  867. the following lines anywhere in the buffer:
  868. #+STARTUP: beamer"
  869. :group 'org-startup
  870. :version "24.1"
  871. :type 'boolean)
  872. (defcustom org-startup-align-all-tables nil
  873. "Non-nil means align all tables when visiting a file.
  874. This is useful when the column width in tables is forced with <N> cookies
  875. in table fields. Such tables will look correct only after the first re-align.
  876. This can also be configured on a per-file basis by adding one of
  877. the following lines anywhere in the buffer:
  878. #+STARTUP: align
  879. #+STARTUP: noalign"
  880. :group 'org-startup
  881. :type 'boolean)
  882. (defcustom org-startup-with-inline-images nil
  883. "Non-nil means show inline images when loading a new Org file.
  884. This can also be configured on a per-file basis by adding one of
  885. the following lines anywhere in the buffer:
  886. #+STARTUP: inlineimages
  887. #+STARTUP: noinlineimages"
  888. :group 'org-startup
  889. :version "24.1"
  890. :type 'boolean)
  891. (defcustom org-startup-with-latex-preview nil
  892. "Non-nil means preview LaTeX fragments when loading a new Org file.
  893. This can also be configured on a per-file basis by adding one of
  894. the following lines anywhere in the buffer:
  895. #+STARTUP: latexpreview
  896. #+STARTUP: nolatexpreview"
  897. :group 'org-startup
  898. :version "24.4"
  899. :package-version '(Org . "8.0")
  900. :type 'boolean)
  901. (defcustom org-insert-mode-line-in-empty-file nil
  902. "Non-nil means insert the first line setting Org-mode in empty files.
  903. When the function `org-mode' is called interactively in an empty file, this
  904. normally means that the file name does not automatically trigger Org-mode.
  905. To ensure that the file will always be in Org-mode in the future, a
  906. line enforcing Org-mode will be inserted into the buffer, if this option
  907. has been set."
  908. :group 'org-startup
  909. :type 'boolean)
  910. (defcustom org-replace-disputed-keys nil
  911. "Non-nil means use alternative key bindings for some keys.
  912. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  913. These keys are also used by other packages like shift-selection-mode'
  914. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  915. If you want to use Org-mode together with one of these other modes,
  916. or more generally if you would like to move some Org-mode commands to
  917. other keys, set this variable and configure the keys with the variable
  918. `org-disputed-keys'.
  919. This option is only relevant at load-time of Org-mode, and must be set
  920. *before* org.el is loaded. Changing it requires a restart of Emacs to
  921. become effective."
  922. :group 'org-startup
  923. :type 'boolean)
  924. (defcustom org-use-extra-keys nil
  925. "Non-nil means use extra key sequence definitions for certain commands.
  926. This happens automatically if you run XEmacs or if `window-system'
  927. is nil. This variable lets you do the same manually. You must
  928. set it before loading org.
  929. Example: on Carbon Emacs 22 running graphically, with an external
  930. keyboard on a Powerbook, the default way of setting M-left might
  931. not work for either Alt or ESC. Setting this variable will make
  932. it work for ESC."
  933. :group 'org-startup
  934. :type 'boolean)
  935. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  936. (defcustom org-disputed-keys
  937. '(([(shift up)] . [(meta p)])
  938. ([(shift down)] . [(meta n)])
  939. ([(shift left)] . [(meta -)])
  940. ([(shift right)] . [(meta +)])
  941. ([(control shift right)] . [(meta shift +)])
  942. ([(control shift left)] . [(meta shift -)]))
  943. "Keys for which Org-mode and other modes compete.
  944. This is an alist, cars are the default keys, second element specifies
  945. the alternative to use when `org-replace-disputed-keys' is t.
  946. Keys can be specified in any syntax supported by `define-key'.
  947. The value of this option takes effect only at Org-mode's startup,
  948. therefore you'll have to restart Emacs to apply it after changing."
  949. :group 'org-startup
  950. :type 'alist)
  951. (defun org-key (key)
  952. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  953. Or return the original if not disputed.
  954. Also apply the translations defined in `org-xemacs-key-equivalents'."
  955. (when org-replace-disputed-keys
  956. (let* ((nkey (key-description key))
  957. (x (org-find-if (lambda (x)
  958. (equal (key-description (car x)) nkey))
  959. org-disputed-keys)))
  960. (setq key (if x (cdr x) key))))
  961. (when (featurep 'xemacs)
  962. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  963. key)
  964. (defun org-find-if (predicate seq)
  965. (catch 'exit
  966. (while seq
  967. (if (funcall predicate (car seq))
  968. (throw 'exit (car seq))
  969. (pop seq)))))
  970. (defun org-defkey (keymap key def)
  971. "Define a key, possibly translated, as returned by `org-key'."
  972. (define-key keymap (org-key key) def))
  973. (defcustom org-ellipsis nil
  974. "The ellipsis to use in the Org-mode outline.
  975. When nil, just use the standard three dots.
  976. When a string, use that string instead.
  977. When a face, use the standard 3 dots, but with the specified face.
  978. The change affects only Org-mode (which will then use its own display table).
  979. Changing this requires executing \\[org-mode] in a buffer to become
  980. effective."
  981. :group 'org-startup
  982. :type '(choice (const :tag "Default" nil)
  983. (face :tag "Face" :value org-warning)
  984. (string :tag "String" :value "...#")))
  985. (defvar org-display-table nil
  986. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  987. (defgroup org-keywords nil
  988. "Keywords in Org-mode."
  989. :tag "Org Keywords"
  990. :group 'org)
  991. (defcustom org-closed-keep-when-no-todo nil
  992. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  993. :group 'org-todo
  994. :group 'org-keywords
  995. :version "24.4"
  996. :package-version '(Org . "8.0")
  997. :type 'boolean)
  998. (defgroup org-structure nil
  999. "Options concerning the general structure of Org-mode files."
  1000. :tag "Org Structure"
  1001. :group 'org)
  1002. (defgroup org-reveal-location nil
  1003. "Options about how to make context of a location visible."
  1004. :tag "Org Reveal Location"
  1005. :group 'org-structure)
  1006. (defcustom org-show-context-detail '((isearch . lineage)
  1007. (bookmark-jump . lineage)
  1008. (default . ancestors))
  1009. "Alist between context and visibility span when revealing a location.
  1010. \\<org-mode-map>Some actions may move point into invisible
  1011. locations. As a consequence, Org always expose a neighborhood
  1012. around point. How much is shown depends on the initial action,
  1013. or context. Valid contexts are
  1014. agenda when exposing an entry from the agenda
  1015. org-goto when using the command `org-goto' (\\[org-goto])
  1016. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1017. tags-tree when constructing a sparse tree based on tags matches
  1018. link-search when exposing search matches associated with a link
  1019. mark-goto when exposing the jump goal of a mark
  1020. bookmark-jump when exposing a bookmark location
  1021. isearch when exiting from an incremental search
  1022. default default for all contexts not set explicitly
  1023. Allowed visibility spans are
  1024. minimal show current headline; if point is not on headline,
  1025. also show entry
  1026. local show current headline, entry and next headline
  1027. ancestors show current headline and its direct ancestors; if
  1028. point is not on headline, also show entry
  1029. lineage show current headline, its direct ancestors and all
  1030. their children; if point is not on headline, also show
  1031. entry and first child
  1032. tree show current headline, its direct ancestors and all
  1033. their children; if point is not on headline, also show
  1034. entry and all children
  1035. canonical show current headline, its direct ancestors along with
  1036. their entries and children; if point is not located on
  1037. the headline, also show current entry and all children
  1038. As special cases, a nil or t value means show all contexts in
  1039. `minimal' or `canonical' view, respectively.
  1040. Some views can make displayed information very compact, but also
  1041. make it harder to edit the location of the match. In such
  1042. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1043. more context."
  1044. :group 'org-reveal-location
  1045. :version "25.1"
  1046. :package-version '(Org . "8.3")
  1047. :type '(choice
  1048. (const :tag "Canonical" t)
  1049. (const :tag "Minimal" nil)
  1050. (repeat :greedy t :tag "Individual contexts"
  1051. (cons
  1052. (choice :tag "Context"
  1053. (const agenda)
  1054. (const org-goto)
  1055. (const occur-tree)
  1056. (const tags-tree)
  1057. (const link-search)
  1058. (const mark-goto)
  1059. (const bookmark-jump)
  1060. (const isearch)
  1061. (const default))
  1062. (choice :tag "Detail level"
  1063. (const minimal)
  1064. (const local)
  1065. (const ancestors)
  1066. (const lineage)
  1067. (const tree)
  1068. (const canonical))))))
  1069. (defcustom org-indirect-buffer-display 'other-window
  1070. "How should indirect tree buffers be displayed?
  1071. This applies to indirect buffers created with the commands
  1072. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1073. Valid values are:
  1074. current-window Display in the current window
  1075. other-window Just display in another window.
  1076. dedicated-frame Create one new frame, and re-use it each time.
  1077. new-frame Make a new frame each time. Note that in this case
  1078. previously-made indirect buffers are kept, and you need to
  1079. kill these buffers yourself."
  1080. :group 'org-structure
  1081. :group 'org-agenda-windows
  1082. :type '(choice
  1083. (const :tag "In current window" current-window)
  1084. (const :tag "In current frame, other window" other-window)
  1085. (const :tag "Each time a new frame" new-frame)
  1086. (const :tag "One dedicated frame" dedicated-frame)))
  1087. (defcustom org-use-speed-commands nil
  1088. "Non-nil means activate single letter commands at beginning of a headline.
  1089. This may also be a function to test for appropriate locations where speed
  1090. commands should be active.
  1091. For example, to activate speed commands when the point is on any
  1092. star at the beginning of the headline, you can do this:
  1093. (setq org-use-speed-commands
  1094. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1095. :group 'org-structure
  1096. :type '(choice
  1097. (const :tag "Never" nil)
  1098. (const :tag "At beginning of headline stars" t)
  1099. (function)))
  1100. (defcustom org-speed-commands-user nil
  1101. "Alist of additional speed commands.
  1102. This list will be checked before `org-speed-commands-default'
  1103. when the variable `org-use-speed-commands' is non-nil
  1104. and when the cursor is at the beginning of a headline.
  1105. The car if each entry is a string with a single letter, which must
  1106. be assigned to `self-insert-command' in the global map.
  1107. The cdr is either a command to be called interactively, a function
  1108. to be called, or a form to be evaluated.
  1109. An entry that is just a list with a single string will be interpreted
  1110. as a descriptive headline that will be added when listing the speed
  1111. commands in the Help buffer using the `?' speed command."
  1112. :group 'org-structure
  1113. :type '(repeat :value ("k" . ignore)
  1114. (choice :value ("k" . ignore)
  1115. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1116. (cons :tag "Letter and Command"
  1117. (string :tag "Command letter")
  1118. (choice
  1119. (function)
  1120. (sexp))))))
  1121. (defcustom org-bookmark-names-plist
  1122. '(:last-capture "org-capture-last-stored"
  1123. :last-refile "org-refile-last-stored"
  1124. :last-capture-marker "org-capture-last-stored-marker")
  1125. "Names for bookmarks automatically set by some Org commands.
  1126. This can provide strings as names for a number of bookmarks Org sets
  1127. automatically. The following keys are currently implemented:
  1128. :last-capture
  1129. :last-capture-marker
  1130. :last-refile
  1131. When a key does not show up in the property list, the corresponding bookmark
  1132. is not set."
  1133. :group 'org-structure
  1134. :type 'plist)
  1135. (defgroup org-cycle nil
  1136. "Options concerning visibility cycling in Org-mode."
  1137. :tag "Org Cycle"
  1138. :group 'org-structure)
  1139. (defcustom org-cycle-skip-children-state-if-no-children t
  1140. "Non-nil means skip CHILDREN state in entries that don't have any."
  1141. :group 'org-cycle
  1142. :type 'boolean)
  1143. (defcustom org-cycle-max-level nil
  1144. "Maximum level which should still be subject to visibility cycling.
  1145. Levels higher than this will, for cycling, be treated as text, not a headline.
  1146. When `org-odd-levels-only' is set, a value of N in this variable actually
  1147. means 2N-1 stars as the limiting headline.
  1148. When nil, cycle all levels.
  1149. Note that the limiting level of cycling is also influenced by
  1150. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1151. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1152. than its value."
  1153. :group 'org-cycle
  1154. :type '(choice
  1155. (const :tag "No limit" nil)
  1156. (integer :tag "Maximum level")))
  1157. (defcustom org-hide-block-startup nil
  1158. "Non-nil means entering Org-mode will fold all blocks.
  1159. This can also be set in on a per-file basis with
  1160. #+STARTUP: hideblocks
  1161. #+STARTUP: showblocks"
  1162. :group 'org-startup
  1163. :group 'org-cycle
  1164. :type 'boolean)
  1165. (defcustom org-cycle-global-at-bob nil
  1166. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1167. This makes it possible to do global cycling without having to use S-TAB or
  1168. \\[universal-argument] TAB. For this special case to work, the first line
  1169. of the buffer must not be a headline -- it may be empty or some other text.
  1170. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1171. sure the cursor stays at the beginning of the buffer. When this option is
  1172. nil, don't do anything special at the beginning of the buffer."
  1173. :group 'org-cycle
  1174. :type 'boolean)
  1175. (defcustom org-cycle-level-after-item/entry-creation t
  1176. "Non-nil means cycle entry level or item indentation in new empty entries.
  1177. When the cursor is at the end of an empty headline, i.e., with only stars
  1178. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1179. and then all possible ancestor states, before returning to the original state.
  1180. This makes data entry extremely fast: M-RET to create a new headline,
  1181. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1182. When the cursor is at the end of an empty plain list item, one TAB will
  1183. make it a subitem, two or more tabs will back up to make this an item
  1184. higher up in the item hierarchy."
  1185. :group 'org-cycle
  1186. :type 'boolean)
  1187. (defcustom org-cycle-emulate-tab t
  1188. "Where should `org-cycle' emulate TAB.
  1189. nil Never
  1190. white Only in completely white lines
  1191. whitestart Only at the beginning of lines, before the first non-white char
  1192. t Everywhere except in headlines
  1193. exc-hl-bol Everywhere except at the start of a headline
  1194. If TAB is used in a place where it does not emulate TAB, the current subtree
  1195. visibility is cycled."
  1196. :group 'org-cycle
  1197. :type '(choice (const :tag "Never" nil)
  1198. (const :tag "Only in completely white lines" white)
  1199. (const :tag "Before first char in a line" whitestart)
  1200. (const :tag "Everywhere except in headlines" t)
  1201. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1202. (defcustom org-cycle-separator-lines 2
  1203. "Number of empty lines needed to keep an empty line between collapsed trees.
  1204. If you leave an empty line between the end of a subtree and the following
  1205. headline, this empty line is hidden when the subtree is folded.
  1206. Org-mode will leave (exactly) one empty line visible if the number of
  1207. empty lines is equal or larger to the number given in this variable.
  1208. So the default 2 means at least 2 empty lines after the end of a subtree
  1209. are needed to produce free space between a collapsed subtree and the
  1210. following headline.
  1211. If the number is negative, and the number of empty lines is at least -N,
  1212. all empty lines are shown.
  1213. Special case: when 0, never leave empty lines in collapsed view."
  1214. :group 'org-cycle
  1215. :type 'integer)
  1216. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1217. (defcustom org-pre-cycle-hook nil
  1218. "Hook that is run before visibility cycling is happening.
  1219. The function(s) in this hook must accept a single argument which indicates
  1220. the new state that will be set right after running this hook. The
  1221. argument is a symbol. Before a global state change, it can have the values
  1222. `overview', `content', or `all'. Before a local state change, it can have
  1223. the values `folded', `children', or `subtree'."
  1224. :group 'org-cycle
  1225. :type 'hook)
  1226. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1227. org-cycle-hide-drawers
  1228. org-cycle-show-empty-lines
  1229. org-optimize-window-after-visibility-change)
  1230. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1231. The function(s) in this hook must accept a single argument which indicates
  1232. the new state that was set by the most recent `org-cycle' command. The
  1233. argument is a symbol. After a global state change, it can have the values
  1234. `overview', `contents', or `all'. After a local state change, it can have
  1235. the values `folded', `children', or `subtree'."
  1236. :group 'org-cycle
  1237. :type 'hook
  1238. :version "25.1"
  1239. :package-version '(Org . "8.3"))
  1240. (defgroup org-edit-structure nil
  1241. "Options concerning structure editing in Org-mode."
  1242. :tag "Org Edit Structure"
  1243. :group 'org-structure)
  1244. (defcustom org-odd-levels-only nil
  1245. "Non-nil means skip even levels and only use odd levels for the outline.
  1246. This has the effect that two stars are being added/taken away in
  1247. promotion/demotion commands. It also influences how levels are
  1248. handled by the exporters.
  1249. Changing it requires restart of `font-lock-mode' to become effective
  1250. for fontification also in regions already fontified.
  1251. You may also set this on a per-file basis by adding one of the following
  1252. lines to the buffer:
  1253. #+STARTUP: odd
  1254. #+STARTUP: oddeven"
  1255. :group 'org-edit-structure
  1256. :group 'org-appearance
  1257. :type 'boolean)
  1258. (defcustom org-adapt-indentation t
  1259. "Non-nil means adapt indentation to outline node level.
  1260. When this variable is set, Org assumes that you write outlines by
  1261. indenting text in each node to align with the headline (after the
  1262. stars). The following issues are influenced by this variable:
  1263. - The indentation is increased by one space in a demotion
  1264. command, and decreased by one in a promotion command. However,
  1265. in the latter case, if shifting some line in the entry body
  1266. would alter document structure (e.g., insert a new headline),
  1267. indentation is not changed at all.
  1268. - Property drawers and planning information is inserted indented
  1269. when this variable is set. When nil, they will not be indented.
  1270. - TAB indents a line relative to current level. The lines below
  1271. a headline will be indented when this variable is set.
  1272. Note that this is all about true indentation, by adding and
  1273. removing space characters. See also `org-indent.el' which does
  1274. level-dependent indentation in a virtual way, i.e. at display
  1275. time in Emacs."
  1276. :group 'org-edit-structure
  1277. :type 'boolean)
  1278. (defcustom org-special-ctrl-a/e nil
  1279. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1280. When t, `C-a' will bring back the cursor to the beginning of the
  1281. headline text, i.e. after the stars and after a possible TODO
  1282. keyword. In an item, this will be the position after bullet and
  1283. check-box, if any. When the cursor is already at that position,
  1284. another `C-a' will bring it to the beginning of the line.
  1285. `C-e' will jump to the end of the headline, ignoring the presence
  1286. of tags in the headline. A second `C-e' will then jump to the
  1287. true end of the line, after any tags. This also means that, when
  1288. this variable is non-nil, `C-e' also will never jump beyond the
  1289. end of the heading of a folded section, i.e. not after the
  1290. ellipses.
  1291. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1292. normally, going to the true line boundary first. Only a directly
  1293. following, identical keypress will bring the cursor to the
  1294. special positions.
  1295. This may also be a cons cell where the behavior for `C-a' and
  1296. `C-e' is set separately."
  1297. :group 'org-edit-structure
  1298. :type '(choice
  1299. (const :tag "off" nil)
  1300. (const :tag "on: after stars/bullet and before tags first" t)
  1301. (const :tag "reversed: true line boundary first" reversed)
  1302. (cons :tag "Set C-a and C-e separately"
  1303. (choice :tag "Special C-a"
  1304. (const :tag "off" nil)
  1305. (const :tag "on: after stars/bullet first" t)
  1306. (const :tag "reversed: before stars/bullet first" reversed))
  1307. (choice :tag "Special C-e"
  1308. (const :tag "off" nil)
  1309. (const :tag "on: before tags first" t)
  1310. (const :tag "reversed: after tags first" reversed)))))
  1311. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1312. (defcustom org-special-ctrl-k nil
  1313. "Non-nil means `C-k' will behave specially in headlines.
  1314. When nil, `C-k' will call the default `kill-line' command.
  1315. When t, the following will happen while the cursor is in the headline:
  1316. - When the cursor is at the beginning of a headline, kill the entire
  1317. line and possible the folded subtree below the line.
  1318. - When in the middle of the headline text, kill the headline up to the tags.
  1319. - When after the headline text, kill the tags."
  1320. :group 'org-edit-structure
  1321. :type 'boolean)
  1322. (defcustom org-ctrl-k-protect-subtree nil
  1323. "Non-nil means, do not delete a hidden subtree with C-k.
  1324. When set to the symbol `error', simply throw an error when C-k is
  1325. used to kill (part-of) a headline that has hidden text behind it.
  1326. Any other non-nil value will result in a query to the user, if it is
  1327. OK to kill that hidden subtree. When nil, kill without remorse."
  1328. :group 'org-edit-structure
  1329. :version "24.1"
  1330. :type '(choice
  1331. (const :tag "Do not protect hidden subtrees" nil)
  1332. (const :tag "Protect hidden subtrees with a security query" t)
  1333. (const :tag "Never kill a hidden subtree with C-k" error)))
  1334. (defcustom org-special-ctrl-o t
  1335. "Non-nil means, make `C-o' insert a row in tables."
  1336. :group 'org-edit-structure
  1337. :type 'boolean)
  1338. (defcustom org-catch-invisible-edits nil
  1339. "Check if in invisible region before inserting or deleting a character.
  1340. Valid values are:
  1341. nil Do not check, so just do invisible edits.
  1342. error Throw an error and do nothing.
  1343. show Make point visible, and do the requested edit.
  1344. show-and-error Make point visible, then throw an error and abort the edit.
  1345. smart Make point visible, and do insertion/deletion if it is
  1346. adjacent to visible text and the change feels predictable.
  1347. Never delete a previously invisible character or add in the
  1348. middle or right after an invisible region. Basically, this
  1349. allows insertion and backward-delete right before ellipses.
  1350. FIXME: maybe in this case we should not even show?"
  1351. :group 'org-edit-structure
  1352. :version "24.1"
  1353. :type '(choice
  1354. (const :tag "Do not check" nil)
  1355. (const :tag "Throw error when trying to edit" error)
  1356. (const :tag "Unhide, but do not do the edit" show-and-error)
  1357. (const :tag "Show invisible part and do the edit" show)
  1358. (const :tag "Be smart and do the right thing" smart)))
  1359. (defcustom org-yank-folded-subtrees t
  1360. "Non-nil means when yanking subtrees, fold them.
  1361. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1362. it starts with a heading and all other headings in it are either children
  1363. or siblings, then fold all the subtrees. However, do this only if no
  1364. text after the yank would be swallowed into a folded tree by this action."
  1365. :group 'org-edit-structure
  1366. :type 'boolean)
  1367. (defcustom org-yank-adjusted-subtrees nil
  1368. "Non-nil means when yanking subtrees, adjust the level.
  1369. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1370. this function for details."
  1371. :group 'org-edit-structure
  1372. :type 'boolean)
  1373. (defcustom org-M-RET-may-split-line '((default . t))
  1374. "Non-nil means M-RET will split the line at the cursor position.
  1375. When nil, it will go to the end of the line before making a
  1376. new line.
  1377. You may also set this option in a different way for different
  1378. contexts. Valid contexts are:
  1379. headline when creating a new headline
  1380. item when creating a new item
  1381. table in a table field
  1382. default the value to be used for all contexts not explicitly
  1383. customized"
  1384. :group 'org-structure
  1385. :group 'org-table
  1386. :type '(choice
  1387. (const :tag "Always" t)
  1388. (const :tag "Never" nil)
  1389. (repeat :greedy t :tag "Individual contexts"
  1390. (cons
  1391. (choice :tag "Context"
  1392. (const headline)
  1393. (const item)
  1394. (const table)
  1395. (const default))
  1396. (boolean)))))
  1397. (defcustom org-insert-heading-respect-content nil
  1398. "Non-nil means insert new headings after the current subtree.
  1399. When nil, the new heading is created directly after the current line.
  1400. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1401. this variable on for the duration of the command."
  1402. :group 'org-structure
  1403. :type 'boolean)
  1404. (defcustom org-blank-before-new-entry '((heading . auto)
  1405. (plain-list-item . auto))
  1406. "Should `org-insert-heading' leave a blank line before new heading/item?
  1407. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1408. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1409. which case Org will look at the surrounding headings/items and try to
  1410. make an intelligent decision whether to insert a blank line or not."
  1411. :group 'org-edit-structure
  1412. :type '(list
  1413. (cons (const heading)
  1414. (choice (const :tag "Never" nil)
  1415. (const :tag "Always" t)
  1416. (const :tag "Auto" auto)))
  1417. (cons (const plain-list-item)
  1418. (choice (const :tag "Never" nil)
  1419. (const :tag "Always" t)
  1420. (const :tag "Auto" auto)))))
  1421. (defcustom org-insert-heading-hook nil
  1422. "Hook being run after inserting a new heading."
  1423. :group 'org-edit-structure
  1424. :type 'hook)
  1425. (defcustom org-enable-fixed-width-editor t
  1426. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1427. This currently only means they are never auto-wrapped.
  1428. When nil, such lines will be treated like ordinary lines."
  1429. :group 'org-edit-structure
  1430. :type 'boolean)
  1431. (defcustom org-goto-auto-isearch t
  1432. "Non-nil means typing characters in `org-goto' starts incremental search.
  1433. When nil, you can use these keybindings to navigate the buffer:
  1434. q Quit the org-goto interface
  1435. n Go to the next visible heading
  1436. p Go to the previous visible heading
  1437. f Go one heading forward on same level
  1438. b Go one heading backward on same level
  1439. u Go one heading up"
  1440. :group 'org-edit-structure
  1441. :type 'boolean)
  1442. (defgroup org-sparse-trees nil
  1443. "Options concerning sparse trees in Org-mode."
  1444. :tag "Org Sparse Trees"
  1445. :group 'org-structure)
  1446. (defcustom org-highlight-sparse-tree-matches t
  1447. "Non-nil means highlight all matches that define a sparse tree.
  1448. The highlights will automatically disappear the next time the buffer is
  1449. changed by an edit command."
  1450. :group 'org-sparse-trees
  1451. :type 'boolean)
  1452. (defcustom org-remove-highlights-with-change t
  1453. "Non-nil means any change to the buffer will remove temporary highlights.
  1454. \\<org-mode-map>\
  1455. Such highlights are created by `org-occur' and `org-clock-display'.
  1456. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1457. to get rid of the highlights.
  1458. The highlights created by `org-toggle-latex-fragment' always need
  1459. `\\[org-toggle-latex-fragment]' to be removed."
  1460. :group 'org-sparse-trees
  1461. :group 'org-time
  1462. :type 'boolean)
  1463. (defcustom org-occur-hook '(org-first-headline-recenter)
  1464. "Hook that is run after `org-occur' has constructed a sparse tree.
  1465. This can be used to recenter the window to show as much of the structure
  1466. as possible."
  1467. :group 'org-sparse-trees
  1468. :type 'hook)
  1469. (defgroup org-imenu-and-speedbar nil
  1470. "Options concerning imenu and speedbar in Org-mode."
  1471. :tag "Org Imenu and Speedbar"
  1472. :group 'org-structure)
  1473. (defcustom org-imenu-depth 2
  1474. "The maximum level for Imenu access to Org-mode headlines.
  1475. This also applied for speedbar access."
  1476. :group 'org-imenu-and-speedbar
  1477. :type 'integer)
  1478. (defgroup org-table nil
  1479. "Options concerning tables in Org-mode."
  1480. :tag "Org Table"
  1481. :group 'org)
  1482. (defcustom org-enable-table-editor 'optimized
  1483. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1484. When nil, such lines will be treated like ordinary lines.
  1485. When equal to the symbol `optimized', the table editor will be optimized to
  1486. do the following:
  1487. - Automatic overwrite mode in front of whitespace in table fields.
  1488. This makes the structure of the table stay in tact as long as the edited
  1489. field does not exceed the column width.
  1490. - Minimize the number of realigns. Normally, the table is aligned each time
  1491. TAB or RET are pressed to move to another field. With optimization this
  1492. happens only if changes to a field might have changed the column width.
  1493. Optimization requires replacing the functions `self-insert-command',
  1494. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1495. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1496. very good at guessing when a re-align will be necessary, but you can always
  1497. force one with \\[org-ctrl-c-ctrl-c].
  1498. If you would like to use the optimized version in Org-mode, but the
  1499. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1500. This variable can be used to turn on and off the table editor during a session,
  1501. but in order to toggle optimization, a restart is required.
  1502. See also the variable `org-table-auto-blank-field'."
  1503. :group 'org-table
  1504. :type '(choice
  1505. (const :tag "off" nil)
  1506. (const :tag "on" t)
  1507. (const :tag "on, optimized" optimized)))
  1508. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1509. (version<= emacs-version "24.1"))
  1510. "Non-nil means cluster self-insert commands for undo when possible.
  1511. If this is set, then, like in the Emacs command loop, 20 consecutive
  1512. characters will be undone together.
  1513. This is configurable, because there is some impact on typing performance."
  1514. :group 'org-table
  1515. :type 'boolean)
  1516. (defcustom org-table-tab-recognizes-table.el t
  1517. "Non-nil means TAB will automatically notice a table.el table.
  1518. When it sees such a table, it moves point into it and - if necessary -
  1519. calls `table-recognize-table'."
  1520. :group 'org-table-editing
  1521. :type 'boolean)
  1522. (defgroup org-link nil
  1523. "Options concerning links in Org-mode."
  1524. :tag "Org Link"
  1525. :group 'org)
  1526. (defvar org-link-abbrev-alist-local nil
  1527. "Buffer-local version of `org-link-abbrev-alist', which see.
  1528. The value of this is taken from the #+LINK lines.")
  1529. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1530. (defcustom org-link-abbrev-alist nil
  1531. "Alist of link abbreviations.
  1532. The car of each element is a string, to be replaced at the start of a link.
  1533. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1534. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1535. [[linkkey:tag][description]]
  1536. The `linkkey' must be a word word, starting with a letter, followed
  1537. by letters, numbers, `-' or `_'.
  1538. If REPLACE is a string, the tag will simply be appended to create the link.
  1539. If the string contains \"%s\", the tag will be inserted there. If the string
  1540. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1541. at that point (see the function `url-hexify-string'). If the string contains
  1542. the specifier \"%(my-function)\", then the custom function `my-function' will
  1543. be invoked: this function takes the tag as its only argument and must return
  1544. a string.
  1545. REPLACE may also be a function that will be called with the tag as the
  1546. only argument to create the link, which should be returned as a string.
  1547. See the manual for examples."
  1548. :group 'org-link
  1549. :type '(repeat
  1550. (cons
  1551. (string :tag "Protocol")
  1552. (choice
  1553. (string :tag "Format")
  1554. (function)))))
  1555. (defcustom org-descriptive-links t
  1556. "Non-nil means Org will display descriptive links.
  1557. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1558. \"Org Website\", hiding the link itself and just displaying its
  1559. description. When set to nil, Org will display the full links
  1560. literally.
  1561. You can interactively set the value of this variable by calling
  1562. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1563. :group 'org-link
  1564. :type 'boolean)
  1565. (defcustom org-link-file-path-type 'adaptive
  1566. "How the path name in file links should be stored.
  1567. Valid values are:
  1568. relative Relative to the current directory, i.e. the directory of the file
  1569. into which the link is being inserted.
  1570. absolute Absolute path, if possible with ~ for home directory.
  1571. noabbrev Absolute path, no abbreviation of home directory.
  1572. adaptive Use relative path for files in the current directory and sub-
  1573. directories of it. For other files, use an absolute path."
  1574. :group 'org-link
  1575. :type '(choice
  1576. (const relative)
  1577. (const absolute)
  1578. (const noabbrev)
  1579. (const adaptive)))
  1580. (defvaralias 'org-activate-links 'org-highlight-links)
  1581. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1582. "Types of links that should be highlighted in Org-mode files.
  1583. This is a list of symbols, each one of them leading to the
  1584. highlighting of a certain link type.
  1585. You can still open links that are not highlighted.
  1586. In principle, it does not hurt to turn on highlighting for all
  1587. link types. There may be a small gain when turning off unused
  1588. link types. The types are:
  1589. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1590. angle Links in angular brackets that may contain whitespace like
  1591. <bbdb:Carsten Dominik>.
  1592. plain Plain links in normal text, no whitespace, like http://google.com.
  1593. radio Text that is matched by a radio target, see manual for details.
  1594. tag Tag settings in a headline (link to tag search).
  1595. date Time stamps (link to calendar).
  1596. footnote Footnote labels.
  1597. If you set this variable during an Emacs session, use `org-mode-restart'
  1598. in the Org buffer so that the change takes effect."
  1599. :group 'org-link
  1600. :group 'org-appearance
  1601. :type '(set :greedy t
  1602. (const :tag "Double bracket links" bracket)
  1603. (const :tag "Angular bracket links" angle)
  1604. (const :tag "Plain text links" plain)
  1605. (const :tag "Radio target matches" radio)
  1606. (const :tag "Tags" tag)
  1607. (const :tag "Timestamps" date)
  1608. (const :tag "Footnotes" footnote)))
  1609. (defcustom org-make-link-description-function nil
  1610. "Function to use for generating link descriptions from links.
  1611. When nil, the link location will be used. This function must take
  1612. two parameters: the first one is the link, the second one is the
  1613. description generated by `org-insert-link'. The function should
  1614. return the description to use."
  1615. :group 'org-link
  1616. :type '(choice (const nil) (function)))
  1617. (defgroup org-link-store nil
  1618. "Options concerning storing links in Org-mode."
  1619. :tag "Org Store Link"
  1620. :group 'org-link)
  1621. (defcustom org-url-hexify-p t
  1622. "When non-nil, hexify URL when creating a link."
  1623. :type 'boolean
  1624. :version "24.3"
  1625. :group 'org-link-store)
  1626. (defcustom org-email-link-description-format "Email %c: %.30s"
  1627. "Format of the description part of a link to an email or usenet message.
  1628. The following %-escapes will be replaced by corresponding information:
  1629. %F full \"From\" field
  1630. %f name, taken from \"From\" field, address if no name
  1631. %T full \"To\" field
  1632. %t first name in \"To\" field, address if no name
  1633. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1634. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1635. %s subject
  1636. %d date
  1637. %m message-id.
  1638. You may use normal field width specification between the % and the letter.
  1639. This is for example useful to limit the length of the subject.
  1640. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1641. :group 'org-link-store
  1642. :type 'string)
  1643. (defcustom org-from-is-user-regexp
  1644. (let (r1 r2)
  1645. (when (and user-mail-address (not (string= user-mail-address "")))
  1646. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1647. (when (and user-full-name (not (string= user-full-name "")))
  1648. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1649. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1650. "Regexp matched against the \"From:\" header of an email or usenet message.
  1651. It should match if the message is from the user him/herself."
  1652. :group 'org-link-store
  1653. :type 'regexp)
  1654. (defcustom org-context-in-file-links t
  1655. "Non-nil means file links from `org-store-link' contain context.
  1656. A search string will be added to the file name with :: as separator and
  1657. used to find the context when the link is activated by the command
  1658. `org-open-at-point'. When this option is t, the entire active region
  1659. will be placed in the search string of the file link. If set to a
  1660. positive integer, only the first n lines of context will be stored.
  1661. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1662. negates this setting for the duration of the command."
  1663. :group 'org-link-store
  1664. :type '(choice boolean integer))
  1665. (defcustom org-keep-stored-link-after-insertion nil
  1666. "Non-nil means keep link in list for entire session.
  1667. The command `org-store-link' adds a link pointing to the current
  1668. location to an internal list. These links accumulate during a session.
  1669. The command `org-insert-link' can be used to insert links into any
  1670. Org-mode file (offering completion for all stored links). When this
  1671. option is nil, every link which has been inserted once using \\[org-insert-link]
  1672. will be removed from the list, to make completing the unused links
  1673. more efficient."
  1674. :group 'org-link-store
  1675. :type 'boolean)
  1676. (defgroup org-link-follow nil
  1677. "Options concerning following links in Org-mode."
  1678. :tag "Org Follow Link"
  1679. :group 'org-link)
  1680. (defcustom org-link-translation-function nil
  1681. "Function to translate links with different syntax to Org syntax.
  1682. This can be used to translate links created for example by the Planner
  1683. or emacs-wiki packages to Org syntax.
  1684. The function must accept two parameters, a TYPE containing the link
  1685. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1686. which is everything after the link protocol. It should return a cons
  1687. with possibly modified values of type and path.
  1688. Org contains a function for this, so if you set this variable to
  1689. `org-translate-link-from-planner', you should be able follow many
  1690. links created by planner."
  1691. :group 'org-link-follow
  1692. :type '(choice (const nil) (function)))
  1693. (defcustom org-follow-link-hook nil
  1694. "Hook that is run after a link has been followed."
  1695. :group 'org-link-follow
  1696. :type 'hook)
  1697. (defcustom org-tab-follows-link nil
  1698. "Non-nil means on links TAB will follow the link.
  1699. Needs to be set before org.el is loaded.
  1700. This really should not be used, it does not make sense, and the
  1701. implementation is bad."
  1702. :group 'org-link-follow
  1703. :type 'boolean)
  1704. (defcustom org-return-follows-link nil
  1705. "Non-nil means on links RET will follow the link.
  1706. In tables, the special behavior of RET has precedence."
  1707. :group 'org-link-follow
  1708. :type 'boolean)
  1709. (defcustom org-mouse-1-follows-link
  1710. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1711. "Non-nil means mouse-1 on a link will follow the link.
  1712. A longer mouse click will still set point. Does not work on XEmacs.
  1713. Needs to be set before org.el is loaded."
  1714. :group 'org-link-follow
  1715. :version "24.4"
  1716. :package-version '(Org . "8.3")
  1717. :type '(choice
  1718. (const :tag "A double click follows the link" double)
  1719. (const :tag "Unconditionally follow the link with mouse-1" t)
  1720. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1721. (defcustom org-mark-ring-length 4
  1722. "Number of different positions to be recorded in the ring.
  1723. Changing this requires a restart of Emacs to work correctly."
  1724. :group 'org-link-follow
  1725. :type 'integer)
  1726. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1727. "Non-nil means internal links in Org files must exactly match a headline.
  1728. When nil, the link search tries to match a phrase with all words
  1729. in the search text."
  1730. :group 'org-link-follow
  1731. :version "24.1"
  1732. :type '(choice
  1733. (const :tag "Use fuzzy text search" nil)
  1734. (const :tag "Match only exact headline" t)
  1735. (const :tag "Match exact headline or query to create it"
  1736. query-to-create)))
  1737. (defcustom org-link-frame-setup
  1738. '((vm . vm-visit-folder-other-frame)
  1739. (vm-imap . vm-visit-imap-folder-other-frame)
  1740. (gnus . org-gnus-no-new-news)
  1741. (file . find-file-other-window)
  1742. (wl . wl-other-frame))
  1743. "Setup the frame configuration for following links.
  1744. When following a link with Emacs, it may often be useful to display
  1745. this link in another window or frame. This variable can be used to
  1746. set this up for the different types of links.
  1747. For VM, use any of
  1748. `vm-visit-folder'
  1749. `vm-visit-folder-other-window'
  1750. `vm-visit-folder-other-frame'
  1751. For Gnus, use any of
  1752. `gnus'
  1753. `gnus-other-frame'
  1754. `org-gnus-no-new-news'
  1755. For FILE, use any of
  1756. `find-file'
  1757. `find-file-other-window'
  1758. `find-file-other-frame'
  1759. For Wanderlust use any of
  1760. `wl'
  1761. `wl-other-frame'
  1762. For the calendar, use the variable `calendar-setup'.
  1763. For BBDB, it is currently only possible to display the matches in
  1764. another window."
  1765. :group 'org-link-follow
  1766. :type '(list
  1767. (cons (const vm)
  1768. (choice
  1769. (const vm-visit-folder)
  1770. (const vm-visit-folder-other-window)
  1771. (const vm-visit-folder-other-frame)))
  1772. (cons (const vm-imap)
  1773. (choice
  1774. (const vm-visit-imap-folder)
  1775. (const vm-visit-imap-folder-other-window)
  1776. (const vm-visit-imap-folder-other-frame)))
  1777. (cons (const gnus)
  1778. (choice
  1779. (const gnus)
  1780. (const gnus-other-frame)
  1781. (const org-gnus-no-new-news)))
  1782. (cons (const file)
  1783. (choice
  1784. (const find-file)
  1785. (const find-file-other-window)
  1786. (const find-file-other-frame)))
  1787. (cons (const wl)
  1788. (choice
  1789. (const wl)
  1790. (const wl-other-frame)))))
  1791. (defcustom org-display-internal-link-with-indirect-buffer nil
  1792. "Non-nil means use indirect buffer to display infile links.
  1793. Activating internal links (from one location in a file to another location
  1794. in the same file) normally just jumps to the location. When the link is
  1795. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1796. is displayed in
  1797. another window. When this option is set, the other window actually displays
  1798. an indirect buffer clone of the current buffer, to avoid any visibility
  1799. changes to the current buffer."
  1800. :group 'org-link-follow
  1801. :type 'boolean)
  1802. (defcustom org-open-non-existing-files nil
  1803. "Non-nil means `org-open-file' will open non-existing files.
  1804. When nil, an error will be generated.
  1805. This variable applies only to external applications because they
  1806. might choke on non-existing files. If the link is to a file that
  1807. will be opened in Emacs, the variable is ignored."
  1808. :group 'org-link-follow
  1809. :type 'boolean)
  1810. (defcustom org-open-directory-means-index-dot-org nil
  1811. "Non-nil means a link to a directory really means to index.org.
  1812. When nil, following a directory link will run dired or open a finder/explorer
  1813. window on that directory."
  1814. :group 'org-link-follow
  1815. :type 'boolean)
  1816. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1817. "Non-nil means ask for confirmation before executing shell links.
  1818. Shell links can be dangerous: just think about a link
  1819. [[shell:rm -rf ~/*][Google Search]]
  1820. This link would show up in your Org-mode document as \"Google Search\",
  1821. but really it would remove your entire home directory.
  1822. Therefore we advise against setting this variable to nil.
  1823. Just change it to `y-or-n-p' if you want to confirm with a
  1824. single keystroke rather than having to type \"yes\"."
  1825. :group 'org-link-follow
  1826. :type '(choice
  1827. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1828. (const :tag "with y-or-n (faster)" y-or-n-p)
  1829. (const :tag "no confirmation (dangerous)" nil)))
  1830. (put 'org-confirm-shell-link-function
  1831. 'safe-local-variable
  1832. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1833. (defcustom org-confirm-shell-link-not-regexp ""
  1834. "A regexp to skip confirmation for shell links."
  1835. :group 'org-link-follow
  1836. :version "24.1"
  1837. :type 'regexp)
  1838. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1839. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1840. Elisp links can be dangerous: just think about a link
  1841. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1842. This link would show up in your Org-mode document as \"Google Search\",
  1843. but really it would remove your entire home directory.
  1844. Therefore we advise against setting this variable to nil.
  1845. Just change it to `y-or-n-p' if you want to confirm with a
  1846. single keystroke rather than having to type \"yes\"."
  1847. :group 'org-link-follow
  1848. :type '(choice
  1849. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1850. (const :tag "with y-or-n (faster)" y-or-n-p)
  1851. (const :tag "no confirmation (dangerous)" nil)))
  1852. (put 'org-confirm-shell-link-function
  1853. 'safe-local-variable
  1854. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1855. (defcustom org-confirm-elisp-link-not-regexp ""
  1856. "A regexp to skip confirmation for Elisp links."
  1857. :group 'org-link-follow
  1858. :version "24.1"
  1859. :type 'regexp)
  1860. (defconst org-file-apps-defaults-gnu
  1861. '((remote . emacs)
  1862. (system . mailcap)
  1863. (t . mailcap))
  1864. "Default file applications on a UNIX or GNU/Linux system.
  1865. See `org-file-apps'.")
  1866. (defconst org-file-apps-defaults-macosx
  1867. '((remote . emacs)
  1868. (t . "open %s")
  1869. (system . "open %s")
  1870. ("ps.gz" . "gv %s")
  1871. ("eps.gz" . "gv %s")
  1872. ("dvi" . "xdvi %s")
  1873. ("fig" . "xfig %s"))
  1874. "Default file applications on a MacOS X system.
  1875. The system \"open\" is known as a default, but we use X11 applications
  1876. for some files for which the OS does not have a good default.
  1877. See `org-file-apps'.")
  1878. (defconst org-file-apps-defaults-windowsnt
  1879. (list
  1880. '(remote . emacs)
  1881. (cons t
  1882. (list (if (featurep 'xemacs)
  1883. 'mswindows-shell-execute
  1884. 'w32-shell-execute)
  1885. "open" 'file))
  1886. (cons 'system
  1887. (list (if (featurep 'xemacs)
  1888. 'mswindows-shell-execute
  1889. 'w32-shell-execute)
  1890. "open" 'file)))
  1891. "Default file applications on a Windows NT system.
  1892. The system \"open\" is used for most files.
  1893. See `org-file-apps'.")
  1894. (defcustom org-file-apps
  1895. '((auto-mode . emacs)
  1896. ("\\.mm\\'" . default)
  1897. ("\\.x?html?\\'" . default)
  1898. ("\\.pdf\\'" . default))
  1899. "External applications for opening `file:path' items in a document.
  1900. Org-mode uses system defaults for different file types, but
  1901. you can use this variable to set the application for a given file
  1902. extension. The entries in this list are cons cells where the car identifies
  1903. files and the cdr the corresponding command. Possible values for the
  1904. file identifier are
  1905. \"string\" A string as a file identifier can be interpreted in different
  1906. ways, depending on its contents:
  1907. - Alphanumeric characters only:
  1908. Match links with this file extension.
  1909. Example: (\"pdf\" . \"evince %s\")
  1910. to open PDFs with evince.
  1911. - Regular expression: Match links where the
  1912. filename matches the regexp. If you want to
  1913. use groups here, use shy groups.
  1914. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1915. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1916. to open *.html and *.xhtml with firefox.
  1917. - Regular expression which contains (non-shy) groups:
  1918. Match links where the whole link, including \"::\", and
  1919. anything after that, matches the regexp.
  1920. In a custom command string, %1, %2, etc. are replaced with
  1921. the parts of the link that were matched by the groups.
  1922. For backwards compatibility, if a command string is given
  1923. that does not use any of the group matches, this case is
  1924. handled identically to the second one (i.e. match against
  1925. file name only).
  1926. In a custom lisp form, you can access the group matches with
  1927. (match-string n link).
  1928. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1929. to open [[file:document.pdf::5]] with evince at page 5.
  1930. `directory' Matches a directory
  1931. `remote' Matches a remote file, accessible through tramp or efs.
  1932. Remote files most likely should be visited through Emacs
  1933. because external applications cannot handle such paths.
  1934. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1935. so all files Emacs knows how to handle. Using this with
  1936. command `emacs' will open most files in Emacs. Beware that this
  1937. will also open html files inside Emacs, unless you add
  1938. (\"html\" . default) to the list as well.
  1939. t Default for files not matched by any of the other options.
  1940. `system' The system command to open files, like `open' on Windows
  1941. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1942. that will be selected if you call `C-c C-o' with a double
  1943. \\[universal-argument] \\[universal-argument] prefix.
  1944. Possible values for the command are:
  1945. `emacs' The file will be visited by the current Emacs process.
  1946. `default' Use the default application for this file type, which is the
  1947. association for t in the list, most likely in the system-specific
  1948. part.
  1949. This can be used to overrule an unwanted setting in the
  1950. system-specific variable.
  1951. `system' Use the system command for opening files, like \"open\".
  1952. This command is specified by the entry whose car is `system'.
  1953. Most likely, the system-specific version of this variable
  1954. does define this command, but you can overrule/replace it
  1955. here.
  1956. string A command to be executed by a shell; %s will be replaced
  1957. by the path to the file.
  1958. sexp A Lisp form which will be evaluated. The file path will
  1959. be available in the Lisp variable `file'.
  1960. For more examples, see the system specific constants
  1961. `org-file-apps-defaults-macosx'
  1962. `org-file-apps-defaults-windowsnt'
  1963. `org-file-apps-defaults-gnu'."
  1964. :group 'org-link-follow
  1965. :type '(repeat
  1966. (cons (choice :value ""
  1967. (string :tag "Extension")
  1968. (const :tag "System command to open files" system)
  1969. (const :tag "Default for unrecognized files" t)
  1970. (const :tag "Remote file" remote)
  1971. (const :tag "Links to a directory" directory)
  1972. (const :tag "Any files that have Emacs modes"
  1973. auto-mode))
  1974. (choice :value ""
  1975. (const :tag "Visit with Emacs" emacs)
  1976. (const :tag "Use default" default)
  1977. (const :tag "Use the system command" system)
  1978. (string :tag "Command")
  1979. (sexp :tag "Lisp form")))))
  1980. (defcustom org-doi-server-url "http://dx.doi.org/"
  1981. "The URL of the DOI server."
  1982. :type 'string
  1983. :version "24.3"
  1984. :group 'org-link-follow)
  1985. (defgroup org-refile nil
  1986. "Options concerning refiling entries in Org-mode."
  1987. :tag "Org Refile"
  1988. :group 'org)
  1989. (defcustom org-directory "~/org"
  1990. "Directory with org files.
  1991. This is just a default location to look for Org files. There is no need
  1992. at all to put your files into this directory. It is only used in the
  1993. following situations:
  1994. 1. When a capture template specifies a target file that is not an
  1995. absolute path. The path will then be interpreted relative to
  1996. `org-directory'
  1997. 2. When a capture note is filed away in an interactive way (when exiting the
  1998. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1999. with `org-directory' as the default path."
  2000. :group 'org-refile
  2001. :group 'org-capture
  2002. :type 'directory)
  2003. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2004. "Default target for storing notes.
  2005. Used as a fall back file for org-capture.el, for templates that
  2006. do not specify a target file."
  2007. :group 'org-refile
  2008. :group 'org-capture
  2009. :type 'file)
  2010. (defcustom org-goto-interface 'outline
  2011. "The default interface to be used for `org-goto'.
  2012. Allowed values are:
  2013. outline The interface shows an outline of the relevant file
  2014. and the correct heading is found by moving through
  2015. the outline or by searching with incremental search.
  2016. outline-path-completion Headlines in the current buffer are offered via
  2017. completion. This is the interface also used by
  2018. the refile command."
  2019. :group 'org-refile
  2020. :type '(choice
  2021. (const :tag "Outline" outline)
  2022. (const :tag "Outline-path-completion" outline-path-completion)))
  2023. (defcustom org-goto-max-level 5
  2024. "Maximum target level when running `org-goto' with refile interface."
  2025. :group 'org-refile
  2026. :type 'integer)
  2027. (defcustom org-reverse-note-order nil
  2028. "Non-nil means store new notes at the beginning of a file or entry.
  2029. When nil, new notes will be filed to the end of a file or entry.
  2030. This can also be a list with cons cells of regular expressions that
  2031. are matched against file names, and values."
  2032. :group 'org-capture
  2033. :group 'org-refile
  2034. :type '(choice
  2035. (const :tag "Reverse always" t)
  2036. (const :tag "Reverse never" nil)
  2037. (repeat :tag "By file name regexp"
  2038. (cons regexp boolean))))
  2039. (defcustom org-log-refile nil
  2040. "Information to record when a task is refiled.
  2041. Possible values are:
  2042. nil Don't add anything
  2043. time Add a time stamp to the task
  2044. note Prompt for a note and add it with template `org-log-note-headings'
  2045. This option can also be set with on a per-file-basis with
  2046. #+STARTUP: nologrefile
  2047. #+STARTUP: logrefile
  2048. #+STARTUP: lognoterefile
  2049. You can have local logging settings for a subtree by setting the LOGGING
  2050. property to one or more of these keywords.
  2051. When bulk-refiling from the agenda, the value `note' is forbidden and
  2052. will temporarily be changed to `time'."
  2053. :group 'org-refile
  2054. :group 'org-progress
  2055. :version "24.1"
  2056. :type '(choice
  2057. (const :tag "No logging" nil)
  2058. (const :tag "Record timestamp" time)
  2059. (const :tag "Record timestamp with note." note)))
  2060. (defcustom org-refile-targets nil
  2061. "Targets for refiling entries with \\[org-refile].
  2062. This is a list of cons cells. Each cell contains:
  2063. - a specification of the files to be considered, either a list of files,
  2064. or a symbol whose function or variable value will be used to retrieve
  2065. a file name or a list of file names. If you use `org-agenda-files' for
  2066. that, all agenda files will be scanned for targets. Nil means consider
  2067. headings in the current buffer.
  2068. - A specification of how to find candidate refile targets. This may be
  2069. any of:
  2070. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2071. This tag has to be present in all target headlines, inheritance will
  2072. not be considered.
  2073. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2074. todo keyword.
  2075. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2076. headlines that are refiling targets.
  2077. - a cons cell (:level . N). Any headline of level N is considered a target.
  2078. Note that, when `org-odd-levels-only' is set, level corresponds to
  2079. order in hierarchy, not to the number of stars.
  2080. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2081. Note that, when `org-odd-levels-only' is set, level corresponds to
  2082. order in hierarchy, not to the number of stars.
  2083. Each element of this list generates a set of possible targets.
  2084. The union of these sets is presented (with completion) to
  2085. the user by `org-refile'.
  2086. You can set the variable `org-refile-target-verify-function' to a function
  2087. to verify each headline found by the simple criteria above.
  2088. When this variable is nil, all top-level headlines in the current buffer
  2089. are used, equivalent to the value `((nil . (:level . 1))'."
  2090. :group 'org-refile
  2091. :type '(repeat
  2092. (cons
  2093. (choice :value org-agenda-files
  2094. (const :tag "All agenda files" org-agenda-files)
  2095. (const :tag "Current buffer" nil)
  2096. (function) (variable) (file))
  2097. (choice :tag "Identify target headline by"
  2098. (cons :tag "Specific tag" (const :value :tag) (string))
  2099. (cons :tag "TODO keyword" (const :value :todo) (string))
  2100. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2101. (cons :tag "Level number" (const :value :level) (integer))
  2102. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2103. (defcustom org-refile-target-verify-function nil
  2104. "Function to verify if the headline at point should be a refile target.
  2105. The function will be called without arguments, with point at the
  2106. beginning of the headline. It should return t and leave point
  2107. where it is if the headline is a valid target for refiling.
  2108. If the target should not be selected, the function must return nil.
  2109. In addition to this, it may move point to a place from where the search
  2110. should be continued. For example, the function may decide that the entire
  2111. subtree of the current entry should be excluded and move point to the end
  2112. of the subtree."
  2113. :group 'org-refile
  2114. :type '(choice
  2115. (const nil)
  2116. (function)))
  2117. (defcustom org-refile-use-cache nil
  2118. "Non-nil means cache refile targets to speed up the process.
  2119. \\<org-mode-map>\
  2120. The cache for a particular file will be updated automatically when
  2121. the buffer has been killed, or when any of the marker used for flagging
  2122. refile targets no longer points at a live buffer.
  2123. If you have added new entries to a buffer that might themselves be targets,
  2124. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2125. if you find that easier, \
  2126. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2127. \\[org-refile]'."
  2128. :group 'org-refile
  2129. :version "24.1"
  2130. :type 'boolean)
  2131. (defcustom org-refile-use-outline-path nil
  2132. "Non-nil means provide refile targets as paths.
  2133. So a level 3 headline will be available as level1/level2/level3.
  2134. When the value is `file', also include the file name (without directory)
  2135. into the path. In this case, you can also stop the completion after
  2136. the file name, to get entries inserted as top level in the file.
  2137. When `full-file-path', include the full file path."
  2138. :group 'org-refile
  2139. :type '(choice
  2140. (const :tag "Not" nil)
  2141. (const :tag "Yes" t)
  2142. (const :tag "Start with file name" file)
  2143. (const :tag "Start with full file path" full-file-path)))
  2144. (defcustom org-outline-path-complete-in-steps t
  2145. "Non-nil means complete the outline path in hierarchical steps.
  2146. When Org-mode uses the refile interface to select an outline path
  2147. \(see variable `org-refile-use-outline-path'), the completion of
  2148. the path can be done in a single go, or it can be done in steps down
  2149. the headline hierarchy. Going in steps is probably the best if you
  2150. do not use a special completion package like `ido' or `icicles'.
  2151. However, when using these packages, going in one step can be very
  2152. fast, while still showing the whole path to the entry."
  2153. :group 'org-refile
  2154. :type 'boolean)
  2155. (defcustom org-refile-allow-creating-parent-nodes nil
  2156. "Non-nil means allow to create new nodes as refile targets.
  2157. New nodes are then created by adding \"/new node name\" to the completion
  2158. of an existing node. When the value of this variable is `confirm',
  2159. new node creation must be confirmed by the user (recommended).
  2160. When nil, the completion must match an existing entry.
  2161. Note that, if the new heading is not seen by the criteria
  2162. listed in `org-refile-targets', multiple instances of the same
  2163. heading would be created by trying again to file under the new
  2164. heading."
  2165. :group 'org-refile
  2166. :type '(choice
  2167. (const :tag "Never" nil)
  2168. (const :tag "Always" t)
  2169. (const :tag "Prompt for confirmation" confirm)))
  2170. (defcustom org-refile-active-region-within-subtree nil
  2171. "Non-nil means also refile active region within a subtree.
  2172. By default `org-refile' doesn't allow refiling regions if they
  2173. don't contain a set of subtrees, but it might be convenient to
  2174. do so sometimes: in that case, the first line of the region is
  2175. converted to a headline before refiling."
  2176. :group 'org-refile
  2177. :version "24.1"
  2178. :type 'boolean)
  2179. (defgroup org-todo nil
  2180. "Options concerning TODO items in Org-mode."
  2181. :tag "Org TODO"
  2182. :group 'org)
  2183. (defgroup org-progress nil
  2184. "Options concerning Progress logging in Org-mode."
  2185. :tag "Org Progress"
  2186. :group 'org-time)
  2187. (defvar org-todo-interpretation-widgets
  2188. '((:tag "Sequence (cycling hits every state)" sequence)
  2189. (:tag "Type (cycling directly to DONE)" type))
  2190. "The available interpretation symbols for customizing `org-todo-keywords'.
  2191. Interested libraries should add to this list.")
  2192. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2193. "List of TODO entry keyword sequences and their interpretation.
  2194. \\<org-mode-map>This is a list of sequences.
  2195. Each sequence starts with a symbol, either `sequence' or `type',
  2196. indicating if the keywords should be interpreted as a sequence of
  2197. action steps, or as different types of TODO items. The first
  2198. keywords are states requiring action - these states will select a headline
  2199. for inclusion into the global TODO list Org-mode produces. If one of
  2200. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2201. signify that no further action is necessary. If \"|\" is not found,
  2202. the last keyword is treated as the only DONE state of the sequence.
  2203. The command \\[org-todo] cycles an entry through these states, and one
  2204. additional state where no keyword is present. For details about this
  2205. cycling, see the manual.
  2206. TODO keywords and interpretation can also be set on a per-file basis with
  2207. the special #+SEQ_TODO and #+TYP_TODO lines.
  2208. Each keyword can optionally specify a character for fast state selection
  2209. \(in combination with the variable `org-use-fast-todo-selection')
  2210. and specifiers for state change logging, using the same syntax that
  2211. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2212. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2213. indicates to record a time stamp each time this state is selected.
  2214. Each keyword may also specify if a timestamp or a note should be
  2215. recorded when entering or leaving the state, by adding additional
  2216. characters in the parenthesis after the keyword. This looks like this:
  2217. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2218. record only the time of the state change. With X and Y being either
  2219. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2220. Y when leaving the state if and only if the *target* state does not
  2221. define X. You may omit any of the fast-selection key or X or /Y,
  2222. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2223. For backward compatibility, this variable may also be just a list
  2224. of keywords. In this case the interpretation (sequence or type) will be
  2225. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2226. :group 'org-todo
  2227. :group 'org-keywords
  2228. :type '(choice
  2229. (repeat :tag "Old syntax, just keywords"
  2230. (string :tag "Keyword"))
  2231. (repeat :tag "New syntax"
  2232. (cons
  2233. (choice
  2234. :tag "Interpretation"
  2235. ;;Quick and dirty way to see
  2236. ;;`org-todo-interpretations'. This takes the
  2237. ;;place of item arguments
  2238. :convert-widget
  2239. (lambda (widget)
  2240. (widget-put widget
  2241. :args (mapcar
  2242. (lambda (x)
  2243. (widget-convert
  2244. (cons 'const x)))
  2245. org-todo-interpretation-widgets))
  2246. widget))
  2247. (repeat
  2248. (string :tag "Keyword"))))))
  2249. (defvar org-todo-keywords-1 nil
  2250. "All TODO and DONE keywords active in a buffer.")
  2251. (make-variable-buffer-local 'org-todo-keywords-1)
  2252. (defvar org-todo-keywords-for-agenda nil)
  2253. (defvar org-done-keywords-for-agenda nil)
  2254. (defvar org-todo-keyword-alist-for-agenda nil)
  2255. (defvar org-tag-alist-for-agenda nil
  2256. "Alist of all tags from all agenda files.")
  2257. (defvar org-tag-groups-alist-for-agenda nil
  2258. "Alist of all groups tags from all current agenda files.")
  2259. (defvar org-tag-groups-alist nil)
  2260. (make-variable-buffer-local 'org-tag-groups-alist)
  2261. (defvar org-agenda-contributing-files nil)
  2262. (defvar org-not-done-keywords nil)
  2263. (make-variable-buffer-local 'org-not-done-keywords)
  2264. (defvar org-done-keywords nil)
  2265. (make-variable-buffer-local 'org-done-keywords)
  2266. (defvar org-todo-heads nil)
  2267. (make-variable-buffer-local 'org-todo-heads)
  2268. (defvar org-todo-sets nil)
  2269. (make-variable-buffer-local 'org-todo-sets)
  2270. (defvar org-todo-log-states nil)
  2271. (make-variable-buffer-local 'org-todo-log-states)
  2272. (defvar org-todo-kwd-alist nil)
  2273. (make-variable-buffer-local 'org-todo-kwd-alist)
  2274. (defvar org-todo-key-alist nil)
  2275. (make-variable-buffer-local 'org-todo-key-alist)
  2276. (defvar org-todo-key-trigger nil)
  2277. (make-variable-buffer-local 'org-todo-key-trigger)
  2278. (defcustom org-todo-interpretation 'sequence
  2279. "Controls how TODO keywords are interpreted.
  2280. This variable is in principle obsolete and is only used for
  2281. backward compatibility, if the interpretation of todo keywords is
  2282. not given already in `org-todo-keywords'. See that variable for
  2283. more information."
  2284. :group 'org-todo
  2285. :group 'org-keywords
  2286. :type '(choice (const sequence)
  2287. (const type)))
  2288. (defcustom org-use-fast-todo-selection t
  2289. "\\<org-mode-map>\
  2290. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2291. This variable describes if and under what circumstances the cycling
  2292. mechanism for TODO keywords will be replaced by a single-key, direct
  2293. selection scheme.
  2294. When nil, fast selection is never used.
  2295. When the symbol `prefix', it will be used when `org-todo' is called
  2296. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2297. in an Org-mode buffer, and
  2298. `\\[universal-argument] t' in an agenda buffer.
  2299. When t, fast selection is used by default. In this case, the prefix
  2300. argument forces cycling instead.
  2301. In all cases, the special interface is only used if access keys have
  2302. actually been assigned by the user, i.e. if keywords in the configuration
  2303. are followed by a letter in parenthesis, like TODO(t)."
  2304. :group 'org-todo
  2305. :type '(choice
  2306. (const :tag "Never" nil)
  2307. (const :tag "By default" t)
  2308. (const :tag "Only with C-u C-c C-t" prefix)))
  2309. (defcustom org-provide-todo-statistics t
  2310. "Non-nil means update todo statistics after insert and toggle.
  2311. ALL-HEADLINES means update todo statistics by including headlines
  2312. with no TODO keyword as well, counting them as not done.
  2313. A list of TODO keywords means the same, but skip keywords that are
  2314. not in this list.
  2315. When set to a list of two lists, the first list contains keywords
  2316. to consider as TODO keywords, the second list contains keywords
  2317. to consider as DONE keywords.
  2318. When this is set, todo statistics is updated in the parent of the
  2319. current entry each time a todo state is changed."
  2320. :group 'org-todo
  2321. :type '(choice
  2322. (const :tag "Yes, only for TODO entries" t)
  2323. (const :tag "Yes, including all entries" all-headlines)
  2324. (repeat :tag "Yes, for TODOs in this list"
  2325. (string :tag "TODO keyword"))
  2326. (list :tag "Yes, for TODOs and DONEs in these lists"
  2327. (repeat (string :tag "TODO keyword"))
  2328. (repeat (string :tag "DONE keyword")))
  2329. (other :tag "No TODO statistics" nil)))
  2330. (defcustom org-hierarchical-todo-statistics t
  2331. "Non-nil means TODO statistics covers just direct children.
  2332. When nil, all entries in the subtree are considered.
  2333. This has only an effect if `org-provide-todo-statistics' is set.
  2334. To set this to nil for only a single subtree, use a COOKIE_DATA
  2335. property and include the word \"recursive\" into the value."
  2336. :group 'org-todo
  2337. :type 'boolean)
  2338. (defcustom org-after-todo-state-change-hook nil
  2339. "Hook which is run after the state of a TODO item was changed.
  2340. The new state (a string with a TODO keyword, or nil) is available in the
  2341. Lisp variable `org-state'."
  2342. :group 'org-todo
  2343. :type 'hook)
  2344. (defvar org-blocker-hook nil
  2345. "Hook for functions that are allowed to block a state change.
  2346. Functions in this hook should not modify the buffer.
  2347. Each function gets as its single argument a property list,
  2348. see `org-trigger-hook' for more information about this list.
  2349. If any of the functions in this hook returns nil, the state change
  2350. is blocked.")
  2351. (defvar org-trigger-hook nil
  2352. "Hook for functions that are triggered by a state change.
  2353. Each function gets as its single argument a property list with at
  2354. least the following elements:
  2355. (:type type-of-change :position pos-at-entry-start
  2356. :from old-state :to new-state)
  2357. Depending on the type, more properties may be present.
  2358. This mechanism is currently implemented for:
  2359. TODO state changes
  2360. ------------------
  2361. :type todo-state-change
  2362. :from previous state (keyword as a string), or nil, or a symbol
  2363. `todo' or `done', to indicate the general type of state.
  2364. :to new state, like in :from")
  2365. (defcustom org-enforce-todo-dependencies nil
  2366. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2367. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2368. be blocked if any prior sibling is not yet done.
  2369. Finally, if the parent is blocked because of ordered siblings of its own,
  2370. the child will also be blocked."
  2371. :set (lambda (var val)
  2372. (set var val)
  2373. (if val
  2374. (add-hook 'org-blocker-hook
  2375. 'org-block-todo-from-children-or-siblings-or-parent)
  2376. (remove-hook 'org-blocker-hook
  2377. 'org-block-todo-from-children-or-siblings-or-parent)))
  2378. :group 'org-todo
  2379. :type 'boolean)
  2380. (defcustom org-enforce-todo-checkbox-dependencies nil
  2381. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2382. When this is nil, checkboxes have no influence on switching TODO states.
  2383. When non-nil, you first need to check off all check boxes before the TODO
  2384. entry can be switched to DONE.
  2385. This variable needs to be set before org.el is loaded, and you need to
  2386. restart Emacs after a change to make the change effective. The only way
  2387. to change is while Emacs is running is through the customize interface."
  2388. :set (lambda (var val)
  2389. (set var val)
  2390. (if val
  2391. (add-hook 'org-blocker-hook
  2392. 'org-block-todo-from-checkboxes)
  2393. (remove-hook 'org-blocker-hook
  2394. 'org-block-todo-from-checkboxes)))
  2395. :group 'org-todo
  2396. :type 'boolean)
  2397. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2398. "Non-nil means inserting a TODO heading is treated as state change.
  2399. So when the command \\[org-insert-todo-heading] is used, state change
  2400. logging will apply if appropriate. When nil, the new TODO item will
  2401. be inserted directly, and no logging will take place."
  2402. :group 'org-todo
  2403. :type 'boolean)
  2404. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2405. "Non-nil means switching TODO states with S-cursor counts as state change.
  2406. This is the default behavior. However, setting this to nil allows a
  2407. convenient way to select a TODO state and bypass any logging associated
  2408. with that."
  2409. :group 'org-todo
  2410. :type 'boolean)
  2411. (defcustom org-todo-state-tags-triggers nil
  2412. "Tag changes that should be triggered by TODO state changes.
  2413. This is a list. Each entry is
  2414. (state-change (tag . flag) .......)
  2415. State-change can be a string with a state, and empty string to indicate the
  2416. state that has no TODO keyword, or it can be one of the symbols `todo'
  2417. or `done', meaning any not-done or done state, respectively."
  2418. :group 'org-todo
  2419. :group 'org-tags
  2420. :type '(repeat
  2421. (cons (choice :tag "When changing to"
  2422. (const :tag "Not-done state" todo)
  2423. (const :tag "Done state" done)
  2424. (string :tag "State"))
  2425. (repeat
  2426. (cons :tag "Tag action"
  2427. (string :tag "Tag")
  2428. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2429. (defcustom org-log-done nil
  2430. "Information to record when a task moves to the DONE state.
  2431. Possible values are:
  2432. nil Don't add anything, just change the keyword
  2433. time Add a time stamp to the task
  2434. note Prompt for a note and add it with template `org-log-note-headings'
  2435. This option can also be set with on a per-file-basis with
  2436. #+STARTUP: nologdone
  2437. #+STARTUP: logdone
  2438. #+STARTUP: lognotedone
  2439. You can have local logging settings for a subtree by setting the LOGGING
  2440. property to one or more of these keywords."
  2441. :group 'org-todo
  2442. :group 'org-progress
  2443. :type '(choice
  2444. (const :tag "No logging" nil)
  2445. (const :tag "Record CLOSED timestamp" time)
  2446. (const :tag "Record CLOSED timestamp with note." note)))
  2447. ;; Normalize old uses of org-log-done.
  2448. (cond
  2449. ((eq org-log-done t) (setq org-log-done 'time))
  2450. ((and (listp org-log-done) (memq 'done org-log-done))
  2451. (setq org-log-done 'note)))
  2452. (defcustom org-log-reschedule nil
  2453. "Information to record when the scheduling date of a tasks is modified.
  2454. Possible values are:
  2455. nil Don't add anything, just change the date
  2456. time Add a time stamp to the task
  2457. note Prompt for a note and add it with template `org-log-note-headings'
  2458. This option can also be set with on a per-file-basis with
  2459. #+STARTUP: nologreschedule
  2460. #+STARTUP: logreschedule
  2461. #+STARTUP: lognotereschedule"
  2462. :group 'org-todo
  2463. :group 'org-progress
  2464. :type '(choice
  2465. (const :tag "No logging" nil)
  2466. (const :tag "Record timestamp" time)
  2467. (const :tag "Record timestamp with note." note)))
  2468. (defcustom org-log-redeadline nil
  2469. "Information to record when the deadline date of a tasks is modified.
  2470. Possible values are:
  2471. nil Don't add anything, just change the date
  2472. time Add a time stamp to the task
  2473. note Prompt for a note and add it with template `org-log-note-headings'
  2474. This option can also be set with on a per-file-basis with
  2475. #+STARTUP: nologredeadline
  2476. #+STARTUP: logredeadline
  2477. #+STARTUP: lognoteredeadline
  2478. You can have local logging settings for a subtree by setting the LOGGING
  2479. property to one or more of these keywords."
  2480. :group 'org-todo
  2481. :group 'org-progress
  2482. :type '(choice
  2483. (const :tag "No logging" nil)
  2484. (const :tag "Record timestamp" time)
  2485. (const :tag "Record timestamp with note." note)))
  2486. (defcustom org-log-note-clock-out nil
  2487. "Non-nil means record a note when clocking out of an item.
  2488. This can also be configured on a per-file basis by adding one of
  2489. the following lines anywhere in the buffer:
  2490. #+STARTUP: lognoteclock-out
  2491. #+STARTUP: nolognoteclock-out"
  2492. :group 'org-todo
  2493. :group 'org-progress
  2494. :type 'boolean)
  2495. (defcustom org-log-done-with-time t
  2496. "Non-nil means the CLOSED time stamp will contain date and time.
  2497. When nil, only the date will be recorded."
  2498. :group 'org-progress
  2499. :type 'boolean)
  2500. (defcustom org-log-note-headings
  2501. '((done . "CLOSING NOTE %t")
  2502. (state . "State %-12s from %-12S %t")
  2503. (note . "Note taken on %t")
  2504. (reschedule . "Rescheduled from %S on %t")
  2505. (delschedule . "Not scheduled, was %S on %t")
  2506. (redeadline . "New deadline from %S on %t")
  2507. (deldeadline . "Removed deadline, was %S on %t")
  2508. (refile . "Refiled on %t")
  2509. (clock-out . ""))
  2510. "Headings for notes added to entries.
  2511. The value is an alist, with the car being a symbol indicating the
  2512. note context, and the cdr is the heading to be used. The heading
  2513. may also be the empty string. The following placeholders can be
  2514. used:
  2515. %t a time stamp.
  2516. %T an active time stamp instead the default inactive one
  2517. %d a short-format time stamp.
  2518. %D an active short-format time stamp.
  2519. %s the new TODO state or time stamp (inactive), in double quotes.
  2520. %S the old TODO state or time stamp (inactive), in double quotes.
  2521. %u the user name.
  2522. %U full user name.
  2523. In fact, it is not a good idea to change the `state' entry,
  2524. because Agenda Log mode depends on the format of these entries."
  2525. :group 'org-todo
  2526. :group 'org-progress
  2527. :type '(list :greedy t
  2528. (cons (const :tag "Heading when closing an item" done) string)
  2529. (cons (const :tag
  2530. "Heading when changing todo state (todo sequence only)"
  2531. state) string)
  2532. (cons (const :tag "Heading when just taking a note" note) string)
  2533. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2534. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2535. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2536. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2537. (cons (const :tag "Heading when refiling" refile) string)
  2538. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2539. (unless (assq 'note org-log-note-headings)
  2540. (push '(note . "%t") org-log-note-headings))
  2541. (defcustom org-log-into-drawer nil
  2542. "Non-nil means insert state change notes and time stamps into a drawer.
  2543. When nil, state changes notes will be inserted after the headline and
  2544. any scheduling and clock lines, but not inside a drawer.
  2545. The value of this variable should be the name of the drawer to use.
  2546. LOGBOOK is proposed as the default drawer for this purpose, you can
  2547. also set this to a string to define the drawer of your choice.
  2548. A value of t is also allowed, representing \"LOGBOOK\".
  2549. A value of t or nil can also be set with on a per-file-basis with
  2550. #+STARTUP: logdrawer
  2551. #+STARTUP: nologdrawer
  2552. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2553. will be ignored.
  2554. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2555. a subtree.
  2556. Do not check directly this variable in a Lisp program. Call
  2557. function `org-log-into-drawer' instead."
  2558. :group 'org-todo
  2559. :group 'org-progress
  2560. :type '(choice
  2561. (const :tag "Not into a drawer" nil)
  2562. (const :tag "LOGBOOK" t)
  2563. (string :tag "Other")))
  2564. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2565. (defun org-log-into-drawer ()
  2566. "Name of the log drawer, as a string, or nil.
  2567. This is the value of `org-log-into-drawer'. However, if the
  2568. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2569. be used instead of the default value."
  2570. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2571. (cond ((equal p "nil") nil)
  2572. ((equal p "t") "LOGBOOK")
  2573. ((stringp p) p)
  2574. (p "LOGBOOK")
  2575. ((stringp org-log-into-drawer) org-log-into-drawer)
  2576. (org-log-into-drawer "LOGBOOK"))))
  2577. (defcustom org-log-state-notes-insert-after-drawers nil
  2578. "Non-nil means insert state change notes after any drawers in entry.
  2579. Only the drawers that *immediately* follow the headline and the
  2580. deadline/scheduled line are skipped.
  2581. When nil, insert notes right after the heading and perhaps the line
  2582. with deadline/scheduling if present.
  2583. This variable will have no effect if `org-log-into-drawer' is
  2584. set."
  2585. :group 'org-todo
  2586. :group 'org-progress
  2587. :type 'boolean)
  2588. (defcustom org-log-states-order-reversed t
  2589. "Non-nil means the latest state note will be directly after heading.
  2590. When nil, the state change notes will be ordered according to time.
  2591. This option can also be set with on a per-file-basis with
  2592. #+STARTUP: logstatesreversed
  2593. #+STARTUP: nologstatesreversed"
  2594. :group 'org-todo
  2595. :group 'org-progress
  2596. :type 'boolean)
  2597. (defcustom org-todo-repeat-to-state nil
  2598. "The TODO state to which a repeater should return the repeating task.
  2599. By default this is the first task in a TODO sequence, or the previous state
  2600. in a TODO_TYP set. But you can specify another task here.
  2601. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2602. :group 'org-todo
  2603. :version "24.1"
  2604. :type '(choice (const :tag "Head of sequence" nil)
  2605. (string :tag "Specific state")))
  2606. (defcustom org-log-repeat 'time
  2607. "Non-nil means record moving through the DONE state when triggering repeat.
  2608. An auto-repeating task is immediately switched back to TODO when
  2609. marked DONE. If you are not logging state changes (by adding \"@\"
  2610. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2611. record a closing note, there will be no record of the task moving
  2612. through DONE. This variable forces taking a note anyway.
  2613. nil Don't force a record
  2614. time Record a time stamp
  2615. note Prompt for a note and add it with template `org-log-note-headings'
  2616. This option can also be set with on a per-file-basis with
  2617. #+STARTUP: nologrepeat
  2618. #+STARTUP: logrepeat
  2619. #+STARTUP: lognoterepeat
  2620. You can have local logging settings for a subtree by setting the LOGGING
  2621. property to one or more of these keywords."
  2622. :group 'org-todo
  2623. :group 'org-progress
  2624. :type '(choice
  2625. (const :tag "Don't force a record" nil)
  2626. (const :tag "Force recording the DONE state" time)
  2627. (const :tag "Force recording a note with the DONE state" note)))
  2628. (defgroup org-priorities nil
  2629. "Priorities in Org-mode."
  2630. :tag "Org Priorities"
  2631. :group 'org-todo)
  2632. (defcustom org-enable-priority-commands t
  2633. "Non-nil means priority commands are active.
  2634. When nil, these commands will be disabled, so that you never accidentally
  2635. set a priority."
  2636. :group 'org-priorities
  2637. :type 'boolean)
  2638. (defcustom org-highest-priority ?A
  2639. "The highest priority of TODO items. A character like ?A, ?B etc.
  2640. Must have a smaller ASCII number than `org-lowest-priority'."
  2641. :group 'org-priorities
  2642. :type 'character)
  2643. (defcustom org-lowest-priority ?C
  2644. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2645. Must have a larger ASCII number than `org-highest-priority'."
  2646. :group 'org-priorities
  2647. :type 'character)
  2648. (defcustom org-default-priority ?B
  2649. "The default priority of TODO items.
  2650. This is the priority an item gets if no explicit priority is given.
  2651. When starting to cycle on an empty priority the first step in the cycle
  2652. depends on `org-priority-start-cycle-with-default'. The resulting first
  2653. step priority must not exceed the range from `org-highest-priority' to
  2654. `org-lowest-priority' which means that `org-default-priority' has to be
  2655. in this range exclusive or inclusive the range boundaries. Else the
  2656. first step refuses to set the default and the second will fall back
  2657. to (depending on the command used) the highest or lowest priority."
  2658. :group 'org-priorities
  2659. :type 'character)
  2660. (defcustom org-priority-start-cycle-with-default t
  2661. "Non-nil means start with default priority when starting to cycle.
  2662. When this is nil, the first step in the cycle will be (depending on the
  2663. command used) one higher or lower than the default priority.
  2664. See also `org-default-priority'."
  2665. :group 'org-priorities
  2666. :type 'boolean)
  2667. (defcustom org-get-priority-function nil
  2668. "Function to extract the priority from a string.
  2669. The string is normally the headline. If this is nil Org computes the
  2670. priority from the priority cookie like [#A] in the headline. It returns
  2671. an integer, increasing by 1000 for each priority level.
  2672. The user can set a different function here, which should take a string
  2673. as an argument and return the numeric priority."
  2674. :group 'org-priorities
  2675. :version "24.1"
  2676. :type '(choice
  2677. (const nil)
  2678. (function)))
  2679. (defgroup org-time nil
  2680. "Options concerning time stamps and deadlines in Org-mode."
  2681. :tag "Org Time"
  2682. :group 'org)
  2683. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2684. "Number of minutes to round time stamps to.
  2685. \\<org-mode-map>\
  2686. These are two values, the first applies when first creating a time stamp.
  2687. The second applies when changing it with the commands `S-up' and `S-down'.
  2688. When changing the time stamp, this means that it will change in steps
  2689. of N minutes, as given by the second value.
  2690. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2691. numbers should be factors of 60, so for example 5, 10, 15.
  2692. When this is larger than 1, you can still force an exact time stamp by using
  2693. a double prefix argument to a time stamp command like \
  2694. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2695. and by using a prefix arg to `S-up/down' to specify the exact number
  2696. of minutes to shift."
  2697. :group 'org-time
  2698. :get (lambda (var) ; Make sure both elements are there
  2699. (if (integerp (default-value var))
  2700. (list (default-value var) 5)
  2701. (default-value var)))
  2702. :type '(list
  2703. (integer :tag "when inserting times")
  2704. (integer :tag "when modifying times")))
  2705. ;; Normalize old customizations of this variable.
  2706. (when (integerp org-time-stamp-rounding-minutes)
  2707. (setq org-time-stamp-rounding-minutes
  2708. (list org-time-stamp-rounding-minutes
  2709. org-time-stamp-rounding-minutes)))
  2710. (defcustom org-display-custom-times nil
  2711. "Non-nil means overlay custom formats over all time stamps.
  2712. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2713. To turn this on on a per-file basis, insert anywhere in the file:
  2714. #+STARTUP: customtime"
  2715. :group 'org-time
  2716. :set 'set-default
  2717. :type 'sexp)
  2718. (make-variable-buffer-local 'org-display-custom-times)
  2719. (defcustom org-time-stamp-custom-formats
  2720. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2721. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2722. These are overlaid over the default ISO format if the variable
  2723. `org-display-custom-times' is set. Time like %H:%M should be at the
  2724. end of the second format. The custom formats are also honored by export
  2725. commands, if custom time display is turned on at the time of export."
  2726. :group 'org-time
  2727. :type 'sexp)
  2728. (defun org-time-stamp-format (&optional long inactive)
  2729. "Get the right format for a time string."
  2730. (let ((f (if long (cdr org-time-stamp-formats)
  2731. (car org-time-stamp-formats))))
  2732. (if inactive
  2733. (concat "[" (substring f 1 -1) "]")
  2734. f)))
  2735. (defcustom org-time-clocksum-format
  2736. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2737. "The format string used when creating CLOCKSUM lines.
  2738. This is also used when Org mode generates a time duration.
  2739. The value can be a single format string containing two
  2740. %-sequences, which will be filled with the number of hours and
  2741. minutes in that order.
  2742. Alternatively, the value can be a plist associating any of the
  2743. keys :years, :months, :weeks, :days, :hours or :minutes with
  2744. format strings. The time duration is formatted using only the
  2745. time components that are needed and concatenating the results.
  2746. If a time unit in absent, it falls back to the next smallest
  2747. unit.
  2748. The keys :require-years, :require-months, :require-days,
  2749. :require-weeks, :require-hours, :require-minutes are also
  2750. meaningful. A non-nil value for these keys indicates that the
  2751. corresponding time component should always be included, even if
  2752. its value is 0.
  2753. For example,
  2754. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2755. :require-minutes t)
  2756. means durations longer than a day will be expressed in days,
  2757. hours and minutes, and durations less than a day will always be
  2758. expressed in hours and minutes (even for durations less than an
  2759. hour).
  2760. The value
  2761. (:days \"%dd\" :minutes \"%dm\")
  2762. means durations longer than a day will be expressed in days and
  2763. minutes, and durations less than a day will be expressed entirely
  2764. in minutes (even for durations longer than an hour)."
  2765. :group 'org-time
  2766. :group 'org-clock
  2767. :version "24.4"
  2768. :package-version '(Org . "8.0")
  2769. :type '(choice (string :tag "Format string")
  2770. (set :tag "Plist"
  2771. (group :inline t (const :tag "Years" :years)
  2772. (string :tag "Format string"))
  2773. (group :inline t
  2774. (const :tag "Always show years" :require-years)
  2775. (const t))
  2776. (group :inline t (const :tag "Months" :months)
  2777. (string :tag "Format string"))
  2778. (group :inline t
  2779. (const :tag "Always show months" :require-months)
  2780. (const t))
  2781. (group :inline t (const :tag "Weeks" :weeks)
  2782. (string :tag "Format string"))
  2783. (group :inline t
  2784. (const :tag "Always show weeks" :require-weeks)
  2785. (const t))
  2786. (group :inline t (const :tag "Days" :days)
  2787. (string :tag "Format string"))
  2788. (group :inline t
  2789. (const :tag "Always show days" :require-days)
  2790. (const t))
  2791. (group :inline t (const :tag "Hours" :hours)
  2792. (string :tag "Format string"))
  2793. (group :inline t
  2794. (const :tag "Always show hours" :require-hours)
  2795. (const t))
  2796. (group :inline t (const :tag "Minutes" :minutes)
  2797. (string :tag "Format string"))
  2798. (group :inline t
  2799. (const :tag "Always show minutes" :require-minutes)
  2800. (const t)))))
  2801. (defcustom org-time-clocksum-use-fractional nil
  2802. "When non-nil, \\[org-clock-display] uses fractional times.
  2803. See `org-time-clocksum-format' for more on time clock formats."
  2804. :group 'org-time
  2805. :group 'org-clock
  2806. :version "24.3"
  2807. :type 'boolean)
  2808. (defcustom org-time-clocksum-use-effort-durations nil
  2809. "When non-nil, \\[org-clock-display] uses effort durations.
  2810. E.g. by default, one day is considered to be a 8 hours effort,
  2811. so a task that has been clocked for 16 hours will be displayed
  2812. as during 2 days in the clock display or in the clocktable.
  2813. See `org-effort-durations' on how to set effort durations
  2814. and `org-time-clocksum-format' for more on time clock formats."
  2815. :group 'org-time
  2816. :group 'org-clock
  2817. :version "24.4"
  2818. :package-version '(Org . "8.0")
  2819. :type 'boolean)
  2820. (defcustom org-time-clocksum-fractional-format "%.2f"
  2821. "The format string used when creating CLOCKSUM lines,
  2822. or when Org mode generates a time duration, if
  2823. `org-time-clocksum-use-fractional' is enabled.
  2824. The value can be a single format string containing one
  2825. %-sequence, which will be filled with the number of hours as
  2826. a float.
  2827. Alternatively, the value can be a plist associating any of the
  2828. keys :years, :months, :weeks, :days, :hours or :minutes with
  2829. a format string. The time duration is formatted using the
  2830. largest time unit which gives a non-zero integer part. If all
  2831. specified formats have zero integer part, the smallest time unit
  2832. is used."
  2833. :group 'org-time
  2834. :type '(choice (string :tag "Format string")
  2835. (set (group :inline t (const :tag "Years" :years)
  2836. (string :tag "Format string"))
  2837. (group :inline t (const :tag "Months" :months)
  2838. (string :tag "Format string"))
  2839. (group :inline t (const :tag "Weeks" :weeks)
  2840. (string :tag "Format string"))
  2841. (group :inline t (const :tag "Days" :days)
  2842. (string :tag "Format string"))
  2843. (group :inline t (const :tag "Hours" :hours)
  2844. (string :tag "Format string"))
  2845. (group :inline t (const :tag "Minutes" :minutes)
  2846. (string :tag "Format string")))))
  2847. (defcustom org-deadline-warning-days 14
  2848. "Number of days before expiration during which a deadline becomes active.
  2849. This variable governs the display in sparse trees and in the agenda.
  2850. When 0 or negative, it means use this number (the absolute value of it)
  2851. even if a deadline has a different individual lead time specified.
  2852. Custom commands can set this variable in the options section."
  2853. :group 'org-time
  2854. :group 'org-agenda-daily/weekly
  2855. :type 'integer)
  2856. (defcustom org-scheduled-delay-days 0
  2857. "Number of days before a scheduled item becomes active.
  2858. This variable governs the display in sparse trees and in the agenda.
  2859. The default value (i.e. 0) means: don't delay scheduled item.
  2860. When negative, it means use this number (the absolute value of it)
  2861. even if a scheduled item has a different individual delay time
  2862. specified.
  2863. Custom commands can set this variable in the options section."
  2864. :group 'org-time
  2865. :group 'org-agenda-daily/weekly
  2866. :version "24.4"
  2867. :package-version '(Org . "8.0")
  2868. :type 'integer)
  2869. (defcustom org-read-date-prefer-future t
  2870. "Non-nil means assume future for incomplete date input from user.
  2871. This affects the following situations:
  2872. 1. The user gives a month but not a year.
  2873. For example, if it is April and you enter \"feb 2\", this will be read
  2874. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2875. 2. The user gives a day, but no month.
  2876. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2877. read this as the third of *next* month. However, if you enter \"17\",
  2878. it will be considered as *this* month.
  2879. If you set this variable to the symbol `time', then also the following
  2880. will work:
  2881. 3. If the user gives a time.
  2882. If the time is before now, it will be interpreted as tomorrow.
  2883. Currently none of this works for ISO week specifications.
  2884. When this option is nil, the current day, month and year will always be
  2885. used as defaults.
  2886. See also `org-agenda-jump-prefer-future'."
  2887. :group 'org-time
  2888. :type '(choice
  2889. (const :tag "Never" nil)
  2890. (const :tag "Check month and day" t)
  2891. (const :tag "Check month, day, and time" time)))
  2892. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2893. "Should the agenda jump command prefer the future for incomplete dates?
  2894. The default is to do the same as configured in `org-read-date-prefer-future'.
  2895. But you can also set a deviating value here.
  2896. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2897. :group 'org-agenda
  2898. :group 'org-time
  2899. :version "24.1"
  2900. :type '(choice
  2901. (const :tag "Use org-read-date-prefer-future"
  2902. org-read-date-prefer-future)
  2903. (const :tag "Never" nil)
  2904. (const :tag "Always" t)))
  2905. (defcustom org-read-date-force-compatible-dates t
  2906. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2907. Depending on the system Emacs is running on, certain dates cannot
  2908. be represented with the type used internally to represent time.
  2909. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2910. correctly. Some systems allow for earlier dates, some for later,
  2911. some for both. One way to find out it to insert any date into an
  2912. Org buffer, putting the cursor on the year and hitting S-up and
  2913. S-down to test the range.
  2914. When this variable is set to t, the date/time prompt will not let
  2915. you specify dates outside the 1970-2037 range, so it is certain that
  2916. these dates will work in whatever version of Emacs you are
  2917. running, and also that you can move a file from one Emacs implementation
  2918. to another. WHenever Org is forcing the year for you, it will display
  2919. a message and beep.
  2920. When this variable is nil, Org will check if the date is
  2921. representable in the specific Emacs implementation you are using.
  2922. If not, it will force a year, usually the current year, and beep
  2923. to remind you. Currently this setting is not recommended because
  2924. the likelihood that you will open your Org files in an Emacs that
  2925. has limited date range is not negligible.
  2926. A workaround for this problem is to use diary sexp dates for time
  2927. stamps outside of this range."
  2928. :group 'org-time
  2929. :version "24.1"
  2930. :type 'boolean)
  2931. (defcustom org-read-date-display-live t
  2932. "Non-nil means display current interpretation of date prompt live.
  2933. This display will be in an overlay, in the minibuffer."
  2934. :group 'org-time
  2935. :type 'boolean)
  2936. (defcustom org-read-date-popup-calendar t
  2937. "Non-nil means pop up a calendar when prompting for a date.
  2938. In the calendar, the date can be selected with mouse-1. However, the
  2939. minibuffer will also be active, and you can simply enter the date as well.
  2940. When nil, only the minibuffer will be available."
  2941. :group 'org-time
  2942. :type 'boolean)
  2943. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2944. 'org-read-date-popup-calendar)
  2945. (make-obsolete-variable
  2946. 'org-read-date-minibuffer-setup-hook
  2947. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2948. (defcustom org-read-date-minibuffer-setup-hook nil
  2949. "Hook to be used to set up keys for the date/time interface.
  2950. Add key definitions to `minibuffer-local-map', which will be a
  2951. temporary copy.
  2952. WARNING: This option is obsolete, you should use
  2953. `org-read-date-minibuffer-local-map' to set up keys."
  2954. :group 'org-time
  2955. :type 'hook)
  2956. (defcustom org-extend-today-until 0
  2957. "The hour when your day really ends. Must be an integer.
  2958. This has influence for the following applications:
  2959. - When switching the agenda to \"today\". It it is still earlier than
  2960. the time given here, the day recognized as TODAY is actually yesterday.
  2961. - When a date is read from the user and it is still before the time given
  2962. here, the current date and time will be assumed to be yesterday, 23:59.
  2963. Also, timestamps inserted in capture templates follow this rule.
  2964. IMPORTANT: This is a feature whose implementation is and likely will
  2965. remain incomplete. Really, it is only here because past midnight seems to
  2966. be the favorite working time of John Wiegley :-)"
  2967. :group 'org-time
  2968. :type 'integer)
  2969. (defcustom org-use-effective-time nil
  2970. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2971. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2972. \"effective time\" of any timestamps between midnight and 8am will be
  2973. 23:59 of the previous day."
  2974. :group 'org-time
  2975. :version "24.1"
  2976. :type 'boolean)
  2977. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2978. "When non-nil, use the last clock out time for `org-todo'.
  2979. Note that this option has precedence over the combined use of
  2980. `org-use-effective-time' and `org-extend-today-until'."
  2981. :group 'org-time
  2982. :version "24.4"
  2983. :package-version '(Org . "8.0")
  2984. :type 'boolean)
  2985. (defcustom org-edit-timestamp-down-means-later nil
  2986. "Non-nil means S-down will increase the time in a time stamp.
  2987. When nil, S-up will increase."
  2988. :group 'org-time
  2989. :type 'boolean)
  2990. (defcustom org-calendar-follow-timestamp-change t
  2991. "Non-nil means make the calendar window follow timestamp changes.
  2992. When a timestamp is modified and the calendar window is visible, it will be
  2993. moved to the new date."
  2994. :group 'org-time
  2995. :type 'boolean)
  2996. (defgroup org-tags nil
  2997. "Options concerning tags in Org-mode."
  2998. :tag "Org Tags"
  2999. :group 'org)
  3000. (defcustom org-tag-alist nil
  3001. "List of tags allowed in Org-mode files.
  3002. When this list is nil, Org-mode will base TAG input on what is already in the
  3003. buffer.
  3004. The value of this variable is an alist, the car of each entry must be a
  3005. keyword as a string, the cdr may be a character that is used to select
  3006. that tag through the fast-tag-selection interface.
  3007. See the manual for details."
  3008. :group 'org-tags
  3009. :type '(repeat
  3010. (choice
  3011. (cons (string :tag "Tag name")
  3012. (character :tag "Access char"))
  3013. (list :tag "Start radio group"
  3014. (const :startgroup)
  3015. (option (string :tag "Group description")))
  3016. (list :tag "Start tag group, non distinct"
  3017. (const :startgrouptag)
  3018. (option (string :tag "Group description")))
  3019. (list :tag "Group tags delimiter"
  3020. (const :grouptags))
  3021. (list :tag "End radio group"
  3022. (const :endgroup)
  3023. (option (string :tag "Group description")))
  3024. (list :tag "End tag group, non distinct"
  3025. (const :endgrouptag)
  3026. (option (string :tag "Group description")))
  3027. (const :tag "New line" (:newline)))))
  3028. (defcustom org-tag-persistent-alist nil
  3029. "List of tags that will always appear in all Org-mode files.
  3030. This is in addition to any in buffer settings or customizations
  3031. of `org-tag-alist'.
  3032. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3033. The value of this variable is an alist, the car of each entry must be a
  3034. keyword as a string, the cdr may be a character that is used to select
  3035. that tag through the fast-tag-selection interface.
  3036. See the manual for details.
  3037. To disable these tags on a per-file basis, insert anywhere in the file:
  3038. #+STARTUP: noptag"
  3039. :group 'org-tags
  3040. :type '(repeat
  3041. (choice
  3042. (cons (string :tag "Tag name")
  3043. (character :tag "Access char"))
  3044. (const :tag "Start radio group" (:startgroup))
  3045. (const :tag "Group tags delimiter" (:grouptags))
  3046. (const :tag "End radio group" (:endgroup))
  3047. (const :tag "New line" (:newline)))))
  3048. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3049. "If non-nil, always offer completion for all tags of all agenda files.
  3050. Instead of customizing this variable directly, you might want to
  3051. set it locally for capture buffers, because there no list of
  3052. tags in that file can be created dynamically (there are none).
  3053. (add-hook \\='org-capture-mode-hook
  3054. (lambda ()
  3055. (set (make-local-variable
  3056. \\='org-complete-tags-always-offer-all-agenda-tags)
  3057. t)))"
  3058. :group 'org-tags
  3059. :version "24.1"
  3060. :type 'boolean)
  3061. (defvar org-file-tags nil
  3062. "List of tags that can be inherited by all entries in the file.
  3063. The tags will be inherited if the variable `org-use-tag-inheritance'
  3064. says they should be.
  3065. This variable is populated from #+FILETAGS lines.")
  3066. (defcustom org-use-fast-tag-selection 'auto
  3067. "Non-nil means use fast tag selection scheme.
  3068. This is a special interface to select and deselect tags with single keys.
  3069. When nil, fast selection is never used.
  3070. When the symbol `auto', fast selection is used if and only if selection
  3071. characters for tags have been configured, either through the variable
  3072. `org-tag-alist' or through a #+TAGS line in the buffer.
  3073. When t, fast selection is always used and selection keys are assigned
  3074. automatically if necessary."
  3075. :group 'org-tags
  3076. :type '(choice
  3077. (const :tag "Always" t)
  3078. (const :tag "Never" nil)
  3079. (const :tag "When selection characters are configured" auto)))
  3080. (defcustom org-fast-tag-selection-single-key nil
  3081. "Non-nil means fast tag selection exits after first change.
  3082. When nil, you have to press RET to exit it.
  3083. During fast tag selection, you can toggle this flag with `C-c'.
  3084. This variable can also have the value `expert'. In this case, the window
  3085. displaying the tags menu is not even shown, until you press C-c again."
  3086. :group 'org-tags
  3087. :type '(choice
  3088. (const :tag "No" nil)
  3089. (const :tag "Yes" t)
  3090. (const :tag "Expert" expert)))
  3091. (defvar org-fast-tag-selection-include-todo nil
  3092. "Non-nil means fast tags selection interface will also offer TODO states.
  3093. This is an undocumented feature, you should not rely on it.")
  3094. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3095. "The column to which tags should be indented in a headline.
  3096. If this number is positive, it specifies the column. If it is negative,
  3097. it means that the tags should be flushright to that column. For example,
  3098. -80 works well for a normal 80 character screen.
  3099. When 0, place tags directly after headline text, with only one space in
  3100. between."
  3101. :group 'org-tags
  3102. :type 'integer)
  3103. (defcustom org-auto-align-tags t
  3104. "Non-nil keeps tags aligned when modifying headlines.
  3105. Some operations (i.e. demoting) change the length of a headline and
  3106. therefore shift the tags around. With this option turned on, after
  3107. each such operation the tags are again aligned to `org-tags-column'."
  3108. :group 'org-tags
  3109. :type 'boolean)
  3110. (defcustom org-use-tag-inheritance t
  3111. "Non-nil means tags in levels apply also for sublevels.
  3112. When nil, only the tags directly given in a specific line apply there.
  3113. This may also be a list of tags that should be inherited, or a regexp that
  3114. matches tags that should be inherited. Additional control is possible
  3115. with the variable `org-tags-exclude-from-inheritance' which gives an
  3116. explicit list of tags to be excluded from inheritance, even if the value of
  3117. `org-use-tag-inheritance' would select it for inheritance.
  3118. If this option is t, a match early-on in a tree can lead to a large
  3119. number of matches in the subtree when constructing the agenda or creating
  3120. a sparse tree. If you only want to see the first match in a tree during
  3121. a search, check out the variable `org-tags-match-list-sublevels'."
  3122. :group 'org-tags
  3123. :type '(choice
  3124. (const :tag "Not" nil)
  3125. (const :tag "Always" t)
  3126. (repeat :tag "Specific tags" (string :tag "Tag"))
  3127. (regexp :tag "Tags matched by regexp")))
  3128. (defcustom org-tags-exclude-from-inheritance nil
  3129. "List of tags that should never be inherited.
  3130. This is a way to exclude a few tags from inheritance. For way to do
  3131. the opposite, to actively allow inheritance for selected tags,
  3132. see the variable `org-use-tag-inheritance'."
  3133. :group 'org-tags
  3134. :type '(repeat (string :tag "Tag")))
  3135. (defun org-tag-inherit-p (tag)
  3136. "Check if TAG is one that should be inherited."
  3137. (cond
  3138. ((member tag org-tags-exclude-from-inheritance) nil)
  3139. ((eq org-use-tag-inheritance t) t)
  3140. ((not org-use-tag-inheritance) nil)
  3141. ((stringp org-use-tag-inheritance)
  3142. (string-match org-use-tag-inheritance tag))
  3143. ((listp org-use-tag-inheritance)
  3144. (member tag org-use-tag-inheritance))
  3145. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3146. (defcustom org-tags-match-list-sublevels t
  3147. "Non-nil means list also sublevels of headlines matching a search.
  3148. This variable applies to tags/property searches, and also to stuck
  3149. projects because this search is based on a tags match as well.
  3150. When set to the symbol `indented', sublevels are indented with
  3151. leading dots.
  3152. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3153. the sublevels of a headline matching a tag search often also match
  3154. the same search. Listing all of them can create very long lists.
  3155. Setting this variable to nil causes subtrees of a match to be skipped.
  3156. This variable is semi-obsolete and probably should always be true. It
  3157. is better to limit inheritance to certain tags using the variables
  3158. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3159. :group 'org-tags
  3160. :type '(choice
  3161. (const :tag "No, don't list them" nil)
  3162. (const :tag "Yes, do list them" t)
  3163. (const :tag "List them, indented with leading dots" indented)))
  3164. (defcustom org-tags-sort-function nil
  3165. "When set, tags are sorted using this function as a comparator."
  3166. :group 'org-tags
  3167. :type '(choice
  3168. (const :tag "No sorting" nil)
  3169. (const :tag "Alphabetical" string<)
  3170. (const :tag "Reverse alphabetical" string>)
  3171. (function :tag "Custom function" nil)))
  3172. (defvar org-tags-history nil
  3173. "History of minibuffer reads for tags.")
  3174. (defvar org-last-tags-completion-table nil
  3175. "The last used completion table for tags.")
  3176. (defvar org-after-tags-change-hook nil
  3177. "Hook that is run after the tags in a line have changed.")
  3178. (defgroup org-properties nil
  3179. "Options concerning properties in Org-mode."
  3180. :tag "Org Properties"
  3181. :group 'org)
  3182. (defcustom org-property-format "%-10s %s"
  3183. "How property key/value pairs should be formatted by `indent-line'.
  3184. When `indent-line' hits a property definition, it will format the line
  3185. according to this format, mainly to make sure that the values are
  3186. lined-up with respect to each other."
  3187. :group 'org-properties
  3188. :type 'string)
  3189. (defcustom org-properties-postprocess-alist nil
  3190. "Alist of properties and functions to adjust inserted values.
  3191. Elements of this alist must be of the form
  3192. ([string] [function])
  3193. where [string] must be a property name and [function] must be a
  3194. lambda expression: this lambda expression must take one argument,
  3195. the value to adjust, and return the new value as a string.
  3196. For example, this element will allow the property \"Remaining\"
  3197. to be updated wrt the relation between the \"Effort\" property
  3198. and the clock summary:
  3199. ((\"Remaining\" (lambda(value)
  3200. (let ((clocksum (org-clock-sum-current-item))
  3201. (effort (org-duration-string-to-minutes
  3202. (org-entry-get (point) \"Effort\"))))
  3203. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3204. :group 'org-properties
  3205. :version "24.1"
  3206. :type '(alist :key-type (string :tag "Property")
  3207. :value-type (function :tag "Function")))
  3208. (defcustom org-use-property-inheritance nil
  3209. "Non-nil means properties apply also for sublevels.
  3210. This setting is chiefly used during property searches. Turning it on can
  3211. cause significant overhead when doing a search, which is why it is not
  3212. on by default.
  3213. When nil, only the properties directly given in the current entry count.
  3214. When t, every property is inherited. The value may also be a list of
  3215. properties that should have inheritance, or a regular expression matching
  3216. properties that should be inherited.
  3217. However, note that some special properties use inheritance under special
  3218. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3219. and the properties ending in \"_ALL\" when they are used as descriptor
  3220. for valid values of a property.
  3221. Note for programmers:
  3222. When querying an entry with `org-entry-get', you can control if inheritance
  3223. should be used. By default, `org-entry-get' looks only at the local
  3224. properties. You can request inheritance by setting the inherit argument
  3225. to t (to force inheritance) or to `selective' (to respect the setting
  3226. in this variable)."
  3227. :group 'org-properties
  3228. :type '(choice
  3229. (const :tag "Not" nil)
  3230. (const :tag "Always" t)
  3231. (repeat :tag "Specific properties" (string :tag "Property"))
  3232. (regexp :tag "Properties matched by regexp")))
  3233. (defun org-property-inherit-p (property)
  3234. "Check if PROPERTY is one that should be inherited."
  3235. (cond
  3236. ((eq org-use-property-inheritance t) t)
  3237. ((not org-use-property-inheritance) nil)
  3238. ((stringp org-use-property-inheritance)
  3239. (string-match org-use-property-inheritance property))
  3240. ((listp org-use-property-inheritance)
  3241. (member property org-use-property-inheritance))
  3242. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3243. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3244. "The default column format, if no other format has been defined.
  3245. This variable can be set on the per-file basis by inserting a line
  3246. #+COLUMNS: %25ITEM ....."
  3247. :group 'org-properties
  3248. :type 'string)
  3249. (defcustom org-columns-ellipses ".."
  3250. "The ellipses to be used when a field in column view is truncated.
  3251. When this is the empty string, as many characters as possible are shown,
  3252. but then there will be no visual indication that the field has been truncated.
  3253. When this is a string of length N, the last N characters of a truncated
  3254. field are replaced by this string. If the column is narrower than the
  3255. ellipses string, only part of the ellipses string will be shown."
  3256. :group 'org-properties
  3257. :type 'string)
  3258. (defcustom org-columns-modify-value-for-display-function nil
  3259. "Function that modifies values for display in column view.
  3260. For example, it can be used to cut out a certain part from a time stamp.
  3261. The function must take 2 arguments:
  3262. column-title The title of the column (*not* the property name)
  3263. value The value that should be modified.
  3264. The function should return the value that should be displayed,
  3265. or nil if the normal value should be used."
  3266. :group 'org-properties
  3267. :type '(choice (const nil) (function)))
  3268. (defconst org-global-properties-fixed
  3269. '(("VISIBILITY_ALL" . "folded children content all")
  3270. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3271. "List of property/value pairs that can be inherited by any entry.
  3272. These are fixed values, for the preset properties. The user variable
  3273. that can be used to add to this list is `org-global-properties'.
  3274. The entries in this list are cons cells where the car is a property
  3275. name and cdr is a string with the value. If the value represents
  3276. multiple items like an \"_ALL\" property, separate the items by
  3277. spaces.")
  3278. (defcustom org-global-properties nil
  3279. "List of property/value pairs that can be inherited by any entry.
  3280. This list will be combined with the constant `org-global-properties-fixed'.
  3281. The entries in this list are cons cells where the car is a property
  3282. name and cdr is a string with the value.
  3283. You can set buffer-local values for the same purpose in the variable
  3284. `org-file-properties' this by adding lines like
  3285. #+PROPERTY: NAME VALUE"
  3286. :group 'org-properties
  3287. :type '(repeat
  3288. (cons (string :tag "Property")
  3289. (string :tag "Value"))))
  3290. (defvar org-file-properties nil
  3291. "List of property/value pairs that can be inherited by any entry.
  3292. Valid for the current buffer.
  3293. This variable is populated from #+PROPERTY lines.")
  3294. (make-variable-buffer-local 'org-file-properties)
  3295. (defgroup org-agenda nil
  3296. "Options concerning agenda views in Org-mode."
  3297. :tag "Org Agenda"
  3298. :group 'org)
  3299. (defvar org-category nil
  3300. "Variable used by org files to set a category for agenda display.
  3301. Such files should use a file variable to set it, for example
  3302. # -*- mode: org; org-category: \"ELisp\"
  3303. or contain a special line
  3304. #+CATEGORY: ELisp
  3305. If the file does not specify a category, then file's base name
  3306. is used instead.")
  3307. (make-variable-buffer-local 'org-category)
  3308. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3309. (defcustom org-agenda-files nil
  3310. "The files to be used for agenda display.
  3311. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3312. \\[org-remove-file]. You can also use customize to edit the list.
  3313. If an entry is a directory, all files in that directory that are matched by
  3314. `org-agenda-file-regexp' will be part of the file list.
  3315. If the value of the variable is not a list but a single file name, then
  3316. the list of agenda files is actually stored and maintained in that file, one
  3317. agenda file per line. In this file paths can be given relative to
  3318. `org-directory'. Tilde expansion and environment variable substitution
  3319. are also made."
  3320. :group 'org-agenda
  3321. :type '(choice
  3322. (repeat :tag "List of files and directories" file)
  3323. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3324. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3325. "Regular expression to match files for `org-agenda-files'.
  3326. If any element in the list in that variable contains a directory instead
  3327. of a normal file, all files in that directory that are matched by this
  3328. regular expression will be included."
  3329. :group 'org-agenda
  3330. :type 'regexp)
  3331. (defcustom org-agenda-text-search-extra-files nil
  3332. "List of extra files to be searched by text search commands.
  3333. These files will be searched in addition to the agenda files by the
  3334. commands `org-search-view' (`\\[org-agenda] s') \
  3335. and `org-occur-in-agenda-files'.
  3336. Note that these files will only be searched for text search commands,
  3337. not for the other agenda views like todo lists, tag searches or the weekly
  3338. agenda. This variable is intended to list notes and possibly archive files
  3339. that should also be searched by these two commands.
  3340. In fact, if the first element in the list is the symbol `agenda-archives',
  3341. then all archive files of all agenda files will be added to the search
  3342. scope."
  3343. :group 'org-agenda
  3344. :type '(set :greedy t
  3345. (const :tag "Agenda Archives" agenda-archives)
  3346. (repeat :inline t (file))))
  3347. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3348. 'org-agenda-text-search-extra-files)
  3349. (defcustom org-agenda-skip-unavailable-files nil
  3350. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3351. A nil value means to remove them, after a query, from the list."
  3352. :group 'org-agenda
  3353. :type 'boolean)
  3354. (defcustom org-calendar-to-agenda-key [?c]
  3355. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3356. The command `org-calendar-goto-agenda' will be bound to this key. The
  3357. default is the character `c' because then `c' can be used to switch back and
  3358. forth between agenda and calendar."
  3359. :group 'org-agenda
  3360. :type 'sexp)
  3361. (defcustom org-calendar-insert-diary-entry-key [?i]
  3362. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3363. This option is irrelevant until `org-agenda-diary-file' has been configured
  3364. to point to an Org-mode file. When that is the case, the command
  3365. `org-agenda-diary-entry' will be bound to the key given here, by default
  3366. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3367. if you want to continue doing this, you need to change this to a different
  3368. key."
  3369. :group 'org-agenda
  3370. :type 'sexp)
  3371. (defcustom org-agenda-diary-file 'diary-file
  3372. "File to which to add new entries with the `i' key in agenda and calendar.
  3373. When this is the symbol `diary-file', the functionality in the Emacs
  3374. calendar will be used to add entries to the `diary-file'. But when this
  3375. points to a file, `org-agenda-diary-entry' will be used instead."
  3376. :group 'org-agenda
  3377. :type '(choice
  3378. (const :tag "The standard Emacs diary file" diary-file)
  3379. (file :tag "Special Org file diary entries")))
  3380. (eval-after-load "calendar"
  3381. '(progn
  3382. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3383. 'org-calendar-goto-agenda)
  3384. (add-hook 'calendar-mode-hook
  3385. (lambda ()
  3386. (unless (eq org-agenda-diary-file 'diary-file)
  3387. (define-key calendar-mode-map
  3388. org-calendar-insert-diary-entry-key
  3389. 'org-agenda-diary-entry))))))
  3390. (defgroup org-latex nil
  3391. "Options for embedding LaTeX code into Org-mode."
  3392. :tag "Org LaTeX"
  3393. :group 'org)
  3394. (defcustom org-format-latex-options
  3395. '(:foreground default :background default :scale 1.0
  3396. :html-foreground "Black" :html-background "Transparent"
  3397. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3398. "Options for creating images from LaTeX fragments.
  3399. This is a property list with the following properties:
  3400. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3401. `default' means use the foreground of the default face.
  3402. `auto' means use the foreground from the text face.
  3403. :background the background color, or \"Transparent\".
  3404. `default' means use the background of the default face.
  3405. `auto' means use the background from the text face.
  3406. :scale a scaling factor for the size of the images, to get more pixels
  3407. :html-foreground, :html-background, :html-scale
  3408. the same numbers for HTML export.
  3409. :matchers a list indicating which matchers should be used to
  3410. find LaTeX fragments. Valid members of this list are:
  3411. \"begin\" find environments
  3412. \"$1\" find single characters surrounded by $.$
  3413. \"$\" find math expressions surrounded by $...$
  3414. \"$$\" find math expressions surrounded by $$....$$
  3415. \"\\(\" find math expressions surrounded by \\(...\\)
  3416. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3417. :group 'org-latex
  3418. :type 'plist)
  3419. (defcustom org-format-latex-signal-error t
  3420. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3421. When nil, just push out a message."
  3422. :group 'org-latex
  3423. :version "24.1"
  3424. :type 'boolean)
  3425. (defcustom org-latex-to-mathml-jar-file nil
  3426. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3427. Use this to specify additional executable file say a jar file.
  3428. When using MathToWeb as the converter, specify the full-path to
  3429. your mathtoweb.jar file."
  3430. :group 'org-latex
  3431. :version "24.1"
  3432. :type '(choice
  3433. (const :tag "None" nil)
  3434. (file :tag "JAR file" :must-match t)))
  3435. (defcustom org-latex-to-mathml-convert-command nil
  3436. "Command to convert LaTeX fragments to MathML.
  3437. Replace format-specifiers in the command as noted below and use
  3438. `shell-command' to convert LaTeX to MathML.
  3439. %j: Executable file in fully expanded form as specified by
  3440. `org-latex-to-mathml-jar-file'.
  3441. %I: Input LaTeX file in fully expanded form.
  3442. %i: The latex fragment to be converted.
  3443. %o: Output MathML file.
  3444. This command is used by `org-create-math-formula'.
  3445. When using MathToWeb as the converter, set this option to
  3446. \"java -jar %j -unicode -force -df %o %I\".
  3447. When using LaTeXML set this option to
  3448. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3449. :group 'org-latex
  3450. :version "24.1"
  3451. :type '(choice
  3452. (const :tag "None" nil)
  3453. (string :tag "\nShell command")))
  3454. (defcustom org-latex-create-formula-image-program 'dvipng
  3455. "Program to convert LaTeX fragments with.
  3456. dvipng Process the LaTeX fragments to dvi file, then convert
  3457. dvi files to png files using dvipng.
  3458. This will also include processing of non-math environments.
  3459. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3460. to convert pdf files to png files"
  3461. :group 'org-latex
  3462. :version "24.1"
  3463. :type '(choice
  3464. (const :tag "dvipng" dvipng)
  3465. (const :tag "imagemagick" imagemagick)))
  3466. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3467. "Path to store latex preview images.
  3468. A relative path here creates many directories relative to the
  3469. processed org files paths. An absolute path puts all preview
  3470. images at the same place."
  3471. :group 'org-latex
  3472. :version "24.3"
  3473. :type 'string)
  3474. (defun org-format-latex-mathml-available-p ()
  3475. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3476. (save-match-data
  3477. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3478. org-latex-to-mathml-convert-command)
  3479. (let ((executable (car (split-string
  3480. org-latex-to-mathml-convert-command))))
  3481. (when (executable-find executable)
  3482. (if (string-match
  3483. "%j" org-latex-to-mathml-convert-command)
  3484. (file-readable-p org-latex-to-mathml-jar-file)
  3485. t))))))
  3486. (defcustom org-format-latex-header "\\documentclass{article}
  3487. \\usepackage[usenames]{color}
  3488. \[PACKAGES]
  3489. \[DEFAULT-PACKAGES]
  3490. \\pagestyle{empty} % do not remove
  3491. % The settings below are copied from fullpage.sty
  3492. \\setlength{\\textwidth}{\\paperwidth}
  3493. \\addtolength{\\textwidth}{-3cm}
  3494. \\setlength{\\oddsidemargin}{1.5cm}
  3495. \\addtolength{\\oddsidemargin}{-2.54cm}
  3496. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3497. \\setlength{\\textheight}{\\paperheight}
  3498. \\addtolength{\\textheight}{-\\headheight}
  3499. \\addtolength{\\textheight}{-\\headsep}
  3500. \\addtolength{\\textheight}{-\\footskip}
  3501. \\addtolength{\\textheight}{-3cm}
  3502. \\setlength{\\topmargin}{1.5cm}
  3503. \\addtolength{\\topmargin}{-2.54cm}"
  3504. "The document header used for processing LaTeX fragments.
  3505. It is imperative that this header make sure that no page number
  3506. appears on the page. The package defined in the variables
  3507. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3508. will either replace the placeholder \"[PACKAGES]\" in this
  3509. header, or they will be appended."
  3510. :group 'org-latex
  3511. :type 'string)
  3512. (defun org-set-packages-alist (var val)
  3513. "Set the packages alist and make sure it has 3 elements per entry."
  3514. (set var (mapcar (lambda (x)
  3515. (if (and (consp x) (= (length x) 2))
  3516. (list (car x) (nth 1 x) t)
  3517. x))
  3518. val)))
  3519. (defun org-get-packages-alist (var)
  3520. "Get the packages alist and make sure it has 3 elements per entry."
  3521. (mapcar (lambda (x)
  3522. (if (and (consp x) (= (length x) 2))
  3523. (list (car x) (nth 1 x) t)
  3524. x))
  3525. (default-value var)))
  3526. (defcustom org-latex-default-packages-alist
  3527. '(("AUTO" "inputenc" t)
  3528. ("T1" "fontenc" t)
  3529. ("" "graphicx" t)
  3530. ("" "grffile" t)
  3531. ("" "longtable" nil)
  3532. ("" "wrapfig" nil)
  3533. ("" "rotating" nil)
  3534. ("normalem" "ulem" t)
  3535. ("" "amsmath" t)
  3536. ("" "textcomp" t)
  3537. ("" "amssymb" t)
  3538. ("" "capt-of" nil)
  3539. ("" "hyperref" nil))
  3540. "Alist of default packages to be inserted in the header.
  3541. Change this only if one of the packages here causes an
  3542. incompatibility with another package you are using.
  3543. The packages in this list are needed by one part or another of
  3544. Org mode to function properly:
  3545. - inputenc, fontenc: for basic font and character selection
  3546. - graphicx: for including images
  3547. - grffile: allow periods and spaces in graphics file names
  3548. - longtable: For multipage tables
  3549. - wrapfig: for figure placement
  3550. - rotating: for sideways figures and tables
  3551. - ulem: for underline and strike-through
  3552. - amsmath: for subscript and superscript and math environments
  3553. - textcomp, amssymb: for various symbols used
  3554. for interpreting the entities in `org-entities'. You can skip
  3555. some of these packages if you don't use any of their symbols.
  3556. - capt-of: for captions outside of floats
  3557. - hyperref: for cross references
  3558. Therefore you should not modify this variable unless you know
  3559. what you are doing. The one reason to change it anyway is that
  3560. you might be loading some other package that conflicts with one
  3561. of the default packages. Each element is either a cell or
  3562. a string.
  3563. A cell is of the format
  3564. (\"options\" \"package\" SNIPPET-FLAG)
  3565. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3566. when compiling LaTeX snippets into images for inclusion into
  3567. non-LaTeX output.
  3568. A string will be inserted as-is in the header of the document."
  3569. :group 'org-latex
  3570. :group 'org-export-latex
  3571. :set 'org-set-packages-alist
  3572. :get 'org-get-packages-alist
  3573. :version "25.1"
  3574. :package-version '(Org . "8.3")
  3575. :type '(repeat
  3576. (choice
  3577. (list :tag "options/package pair"
  3578. (string :tag "options")
  3579. (string :tag "package")
  3580. (boolean :tag "Snippet"))
  3581. (string :tag "A line of LaTeX"))))
  3582. (defcustom org-latex-packages-alist nil
  3583. "Alist of packages to be inserted in every LaTeX header.
  3584. These will be inserted after `org-latex-default-packages-alist'.
  3585. Each element is either a cell or a string.
  3586. A cell is of the format:
  3587. (\"options\" \"package\" SNIPPET-FLAG)
  3588. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3589. needed when turning LaTeX snippets into images for inclusion into
  3590. non-LaTeX output.
  3591. A string will be inserted as-is in the header of the document.
  3592. Make sure that you only list packages here which:
  3593. - you want in every file;
  3594. - do not conflict with the setup in `org-format-latex-header';
  3595. - do not conflict with the default packages in
  3596. `org-latex-default-packages-alist'."
  3597. :group 'org-latex
  3598. :group 'org-export-latex
  3599. :set 'org-set-packages-alist
  3600. :get 'org-get-packages-alist
  3601. :type '(repeat
  3602. (choice
  3603. (list :tag "options/package pair"
  3604. (string :tag "options")
  3605. (string :tag "package")
  3606. (boolean :tag "Snippet"))
  3607. (string :tag "A line of LaTeX"))))
  3608. (defgroup org-appearance nil
  3609. "Settings for Org-mode appearance."
  3610. :tag "Org Appearance"
  3611. :group 'org)
  3612. (defcustom org-level-color-stars-only nil
  3613. "Non-nil means fontify only the stars in each headline.
  3614. When nil, the entire headline is fontified.
  3615. Changing it requires restart of `font-lock-mode' to become effective
  3616. also in regions already fontified."
  3617. :group 'org-appearance
  3618. :type 'boolean)
  3619. (defcustom org-hide-leading-stars nil
  3620. "Non-nil means hide the first N-1 stars in a headline.
  3621. This works by using the face `org-hide' for these stars. This
  3622. face is white for a light background, and black for a dark
  3623. background. You may have to customize the face `org-hide' to
  3624. make this work.
  3625. Changing it requires restart of `font-lock-mode' to become effective
  3626. also in regions already fontified.
  3627. You may also set this on a per-file basis by adding one of the following
  3628. lines to the buffer:
  3629. #+STARTUP: hidestars
  3630. #+STARTUP: showstars"
  3631. :group 'org-appearance
  3632. :type 'boolean)
  3633. (defcustom org-hidden-keywords nil
  3634. "List of symbols corresponding to keywords to be hidden the org buffer.
  3635. For example, a value \\='(title) for this list will make the document's title
  3636. appear in the buffer without the initial #+TITLE: keyword."
  3637. :group 'org-appearance
  3638. :version "24.1"
  3639. :type '(set (const :tag "#+AUTHOR" author)
  3640. (const :tag "#+DATE" date)
  3641. (const :tag "#+EMAIL" email)
  3642. (const :tag "#+TITLE" title)))
  3643. (defcustom org-custom-properties nil
  3644. "List of properties (as strings) with a special meaning.
  3645. The default use of these custom properties is to let the user
  3646. hide them with `org-toggle-custom-properties-visibility'."
  3647. :group 'org-properties
  3648. :group 'org-appearance
  3649. :version "24.3"
  3650. :type '(repeat (string :tag "Property Name")))
  3651. (defcustom org-fontify-done-headline nil
  3652. "Non-nil means change the face of a headline if it is marked DONE.
  3653. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3654. When this is non-nil, the headline after the keyword is set to the
  3655. `org-headline-done' as an additional indication."
  3656. :group 'org-appearance
  3657. :type 'boolean)
  3658. (defcustom org-fontify-emphasized-text t
  3659. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3660. Changing this variable requires a restart of Emacs to take effect."
  3661. :group 'org-appearance
  3662. :type 'boolean)
  3663. (defcustom org-fontify-whole-heading-line nil
  3664. "Non-nil means fontify the whole line for headings.
  3665. This is useful when setting a background color for the
  3666. org-level-* faces."
  3667. :group 'org-appearance
  3668. :type 'boolean)
  3669. (defcustom org-highlight-latex-and-related nil
  3670. "Non-nil means highlight LaTeX related syntax in the buffer.
  3671. When non nil, the value should be a list containing any of the
  3672. following symbols:
  3673. `latex' Highlight LaTeX snippets and environments.
  3674. `script' Highlight subscript and superscript.
  3675. `entities' Highlight entities."
  3676. :group 'org-appearance
  3677. :version "24.4"
  3678. :package-version '(Org . "8.0")
  3679. :type '(choice
  3680. (const :tag "No highlighting" nil)
  3681. (set :greedy t :tag "Highlight"
  3682. (const :tag "LaTeX snippets and environments" latex)
  3683. (const :tag "Subscript and superscript" script)
  3684. (const :tag "Entities" entities))))
  3685. (defcustom org-hide-emphasis-markers nil
  3686. "Non-nil mean font-lock should hide the emphasis marker characters."
  3687. :group 'org-appearance
  3688. :type 'boolean)
  3689. (defcustom org-hide-macro-markers nil
  3690. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3691. :group 'org-appearance
  3692. :type 'boolean)
  3693. (defcustom org-pretty-entities nil
  3694. "Non-nil means show entities as UTF8 characters.
  3695. When nil, the \\name form remains in the buffer."
  3696. :group 'org-appearance
  3697. :version "24.1"
  3698. :type 'boolean)
  3699. (defcustom org-pretty-entities-include-sub-superscripts t
  3700. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3701. :group 'org-appearance
  3702. :version "24.1"
  3703. :type 'boolean)
  3704. (defvar org-emph-re nil
  3705. "Regular expression for matching emphasis.
  3706. After a match, the match groups contain these elements:
  3707. 0 The match of the full regular expression, including the characters
  3708. before and after the proper match
  3709. 1 The character before the proper match, or empty at beginning of line
  3710. 2 The proper match, including the leading and trailing markers
  3711. 3 The leading marker like * or /, indicating the type of highlighting
  3712. 4 The text between the emphasis markers, not including the markers
  3713. 5 The character after the match, empty at the end of a line")
  3714. (defvar org-verbatim-re nil
  3715. "Regular expression for matching verbatim text.")
  3716. (defvar org-emphasis-regexp-components) ; defined just below
  3717. (defvar org-emphasis-alist) ; defined just below
  3718. (defun org-set-emph-re (var val)
  3719. "Set variable and compute the emphasis regular expression."
  3720. (set var val)
  3721. (when (and (boundp 'org-emphasis-alist)
  3722. (boundp 'org-emphasis-regexp-components)
  3723. org-emphasis-alist org-emphasis-regexp-components)
  3724. (let* ((e org-emphasis-regexp-components)
  3725. (pre (car e))
  3726. (post (nth 1 e))
  3727. (border (nth 2 e))
  3728. (body (nth 3 e))
  3729. (nl (nth 4 e))
  3730. (body1 (concat body "*?"))
  3731. (markers (mapconcat 'car org-emphasis-alist ""))
  3732. (vmarkers (mapconcat
  3733. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3734. org-emphasis-alist "")))
  3735. ;; make sure special characters appear at the right position in the class
  3736. (if (string-match "\\^" markers)
  3737. (setq markers (concat (replace-match "" t t markers) "^")))
  3738. (if (string-match "-" markers)
  3739. (setq markers (concat (replace-match "" t t markers) "-")))
  3740. (if (string-match "\\^" vmarkers)
  3741. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3742. (if (string-match "-" vmarkers)
  3743. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3744. (if (> nl 0)
  3745. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3746. (int-to-string nl) "\\}")))
  3747. ;; Make the regexp
  3748. (setq org-emph-re
  3749. (concat "\\([" pre "]\\|^\\)"
  3750. "\\("
  3751. "\\([" markers "]\\)"
  3752. "\\("
  3753. "[^" border "]\\|"
  3754. "[^" border "]"
  3755. body1
  3756. "[^" border "]"
  3757. "\\)"
  3758. "\\3\\)"
  3759. "\\([" post "]\\|$\\)"))
  3760. (setq org-verbatim-re
  3761. (concat "\\([" pre "]\\|^\\)"
  3762. "\\("
  3763. "\\([" vmarkers "]\\)"
  3764. "\\("
  3765. "[^" border "]\\|"
  3766. "[^" border "]"
  3767. body1
  3768. "[^" border "]"
  3769. "\\)"
  3770. "\\3\\)"
  3771. "\\([" post "]\\|$\\)")))))
  3772. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3773. ;; set this option proved cumbersome. See this message/thread:
  3774. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3775. (defvar org-emphasis-regexp-components
  3776. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3777. "Components used to build the regular expression for emphasis.
  3778. This is a list with five entries. Terminology: In an emphasis string
  3779. like \" *strong word* \", we call the initial space PREMATCH, the final
  3780. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3781. and \"trong wor\" is the body. The different components in this variable
  3782. specify what is allowed/forbidden in each part:
  3783. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3784. post Chars allowed as postmatch. End of line will be allowed too.
  3785. border The chars *forbidden* as border characters.
  3786. body-regexp A regexp like \".\" to match a body character. Don't use
  3787. non-shy groups here, and don't allow newline here.
  3788. newline The maximum number of newlines allowed in an emphasis exp.
  3789. You need to reload Org or to restart Emacs after customizing this.")
  3790. (defcustom org-emphasis-alist
  3791. `(("*" bold)
  3792. ("/" italic)
  3793. ("_" underline)
  3794. ("=" org-verbatim verbatim)
  3795. ("~" org-code verbatim)
  3796. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3797. "Alist of characters and faces to emphasize text.
  3798. Text starting and ending with a special character will be emphasized,
  3799. for example *bold*, _underlined_ and /italic/. This variable sets the
  3800. marker characters and the face to be used by font-lock for highlighting
  3801. in Org-mode Emacs buffers.
  3802. You need to reload Org or to restart Emacs after customizing this."
  3803. :group 'org-appearance
  3804. :set 'org-set-emph-re
  3805. :version "24.4"
  3806. :package-version '(Org . "8.0")
  3807. :type '(repeat
  3808. (list
  3809. (string :tag "Marker character")
  3810. (choice
  3811. (face :tag "Font-lock-face")
  3812. (plist :tag "Face property list"))
  3813. (option (const verbatim)))))
  3814. (defvar org-protecting-blocks
  3815. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3816. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3817. This is needed for font-lock setup.")
  3818. ;;; Miscellaneous options
  3819. (defgroup org-completion nil
  3820. "Completion in Org-mode."
  3821. :tag "Org Completion"
  3822. :group 'org)
  3823. (defcustom org-completion-use-ido nil
  3824. "Non-nil means use ido completion wherever possible.
  3825. Note that `ido-mode' must be active for this variable to be relevant.
  3826. If you decide to turn this variable on, you might well want to turn off
  3827. `org-outline-path-complete-in-steps'.
  3828. See also `org-completion-use-iswitchb'."
  3829. :group 'org-completion
  3830. :type 'boolean)
  3831. (defcustom org-completion-use-iswitchb nil
  3832. "Non-nil means use iswitchb completion wherever possible.
  3833. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3834. If you decide to turn this variable on, you might well want to turn off
  3835. `org-outline-path-complete-in-steps'.
  3836. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3837. :group 'org-completion
  3838. :type 'boolean)
  3839. (defcustom org-completion-fallback-command 'hippie-expand
  3840. "The expansion command called by \\[pcomplete] in normal context.
  3841. Normal means, no org-mode-specific context."
  3842. :group 'org-completion
  3843. :type 'function)
  3844. ;;; Functions and variables from their packages
  3845. ;; Declared here to avoid compiler warnings
  3846. ;; XEmacs only
  3847. (defvar outline-mode-menu-heading)
  3848. (defvar outline-mode-menu-show)
  3849. (defvar outline-mode-menu-hide)
  3850. (defvar zmacs-regions) ; XEmacs regions
  3851. ;; Emacs only
  3852. (defvar mark-active)
  3853. ;; Various packages
  3854. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3855. (declare-function calendar-forward-day "cal-move" (arg))
  3856. (declare-function calendar-goto-date "cal-move" (date))
  3857. (declare-function calendar-goto-today "cal-move" ())
  3858. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3859. (defvar calc-embedded-close-formula)
  3860. (defvar calc-embedded-open-formula)
  3861. (declare-function cdlatex-tab "ext:cdlatex" ())
  3862. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3863. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3864. (defvar font-lock-unfontify-region-function)
  3865. (declare-function iswitchb-read-buffer "iswitchb"
  3866. (prompt &optional default require-match start matches-set))
  3867. (defvar iswitchb-temp-buflist)
  3868. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3869. (defvar org-agenda-tags-todo-honor-ignore-options)
  3870. (declare-function org-agenda-skip "org-agenda" ())
  3871. (declare-function
  3872. org-agenda-format-item "org-agenda"
  3873. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3874. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3875. (declare-function org-agenda-change-all-lines "org-agenda"
  3876. (newhead hdmarker &optional fixface just-this))
  3877. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3878. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3879. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3880. (beg end))
  3881. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3882. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3883. "org-agenda" (&optional end))
  3884. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3885. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3886. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3887. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3888. (declare-function org-indent-mode "org-indent" (&optional arg))
  3889. (declare-function parse-time-string "parse-time" (string))
  3890. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3891. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3892. (defvar remember-data-file)
  3893. (defvar texmathp-why)
  3894. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3895. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3896. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3897. ;;;###autoload
  3898. (defun turn-on-orgtbl ()
  3899. "Unconditionally turn on `orgtbl-mode'."
  3900. (require 'org-table)
  3901. (orgtbl-mode 1))
  3902. (defun org-at-table-p (&optional table-type)
  3903. "Non-nil if the cursor is inside an Org table.
  3904. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3905. If `org-enable-table-editor' is nil, return nil unconditionally."
  3906. (and
  3907. org-enable-table-editor
  3908. (save-excursion
  3909. (beginning-of-line)
  3910. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3911. (or (not (derived-mode-p 'org-mode))
  3912. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3913. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3914. (defsubst org-table-p () (org-at-table-p))
  3915. (defun org-at-table.el-p ()
  3916. "Non-nil when point is at a table.el table."
  3917. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3918. (let ((element (org-element-at-point)))
  3919. (and (eq (org-element-type element) 'table)
  3920. (eq (org-element-property :type element) 'table.el)))))
  3921. (defun org-table-recognize-table.el ()
  3922. "If there is a table.el table nearby, recognize it and move into it."
  3923. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3924. (beginning-of-line)
  3925. (unless (or (looking-at org-table-dataline-regexp)
  3926. (not (looking-at org-table1-hline-regexp)))
  3927. (forward-line)
  3928. (when (looking-at org-table-any-border-regexp)
  3929. (forward-line -2)))
  3930. (if (re-search-forward "|" (org-table-end t) t)
  3931. (progn
  3932. (require 'table)
  3933. (if (table--at-cell-p (point)) t
  3934. (message "recognizing table.el table...")
  3935. (table-recognize-table)
  3936. (message "recognizing table.el table...done")))
  3937. (error "This should not happen"))))
  3938. (defun org-at-table-hline-p ()
  3939. "Non-nil when point is inside a hline in a table.
  3940. Assume point is already in a table. If `org-enable-table-editor'
  3941. is nil, return nil unconditionally."
  3942. (and org-enable-table-editor
  3943. (save-excursion
  3944. (beginning-of-line)
  3945. (looking-at org-table-hline-regexp))))
  3946. (defun org-table-map-tables (function &optional quietly)
  3947. "Apply FUNCTION to the start of all tables in the buffer."
  3948. (save-excursion
  3949. (save-restriction
  3950. (widen)
  3951. (goto-char (point-min))
  3952. (while (re-search-forward org-table-any-line-regexp nil t)
  3953. (unless quietly
  3954. (message "Mapping tables: %d%%"
  3955. (floor (* 100.0 (point)) (buffer-size))))
  3956. (beginning-of-line 1)
  3957. (when (and (looking-at org-table-line-regexp)
  3958. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3959. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3960. (save-excursion (funcall function))
  3961. (or (looking-at org-table-line-regexp)
  3962. (forward-char 1)))
  3963. (re-search-forward org-table-any-border-regexp nil 1))))
  3964. (unless quietly (message "Mapping tables: done")))
  3965. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3966. (declare-function org-clock-update-mode-line "org-clock" ())
  3967. (declare-function org-resolve-clocks "org-clock"
  3968. (&optional also-non-dangling-p prompt last-valid))
  3969. (defun org-at-TBLFM-p (&optional pos)
  3970. "Non-nil when point (or POS) is in #+TBLFM line."
  3971. (save-excursion
  3972. (goto-char (or pos (point)))
  3973. (beginning-of-line)
  3974. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3975. (eq (org-element-type (org-element-at-point)) 'table))))
  3976. (defvar org-clock-start-time)
  3977. (defvar org-clock-marker (make-marker)
  3978. "Marker recording the last clock-in.")
  3979. (defvar org-clock-hd-marker (make-marker)
  3980. "Marker recording the last clock-in, but the headline position.")
  3981. (defvar org-clock-heading ""
  3982. "The heading of the current clock entry.")
  3983. (defun org-clock-is-active ()
  3984. "Return the buffer where the clock is currently running.
  3985. Return nil if no clock is running."
  3986. (marker-buffer org-clock-marker))
  3987. (defun org-check-running-clock ()
  3988. "Check if the current buffer contains the running clock.
  3989. If yes, offer to stop it and to save the buffer with the changes."
  3990. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3991. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3992. (buffer-name))))
  3993. (org-clock-out)
  3994. (when (y-or-n-p "Save changed buffer?")
  3995. (save-buffer))))
  3996. (defun org-clocktable-try-shift (dir n)
  3997. "Check if this line starts a clock table, if yes, shift the time block."
  3998. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3999. (org-clocktable-shift dir n)))
  4000. ;;;###autoload
  4001. (defun org-clock-persistence-insinuate ()
  4002. "Set up hooks for clock persistence."
  4003. (require 'org-clock)
  4004. (add-hook 'org-mode-hook 'org-clock-load)
  4005. (add-hook 'kill-emacs-hook 'org-clock-save))
  4006. (defgroup org-archive nil
  4007. "Options concerning archiving in Org-mode."
  4008. :tag "Org Archive"
  4009. :group 'org-structure)
  4010. (defcustom org-archive-location "%s_archive::"
  4011. "The location where subtrees should be archived.
  4012. The value of this variable is a string, consisting of two parts,
  4013. separated by a double-colon. The first part is a filename and
  4014. the second part is a headline.
  4015. When the filename is omitted, archiving happens in the same file.
  4016. %s in the filename will be replaced by the current file
  4017. name (without the directory part). Archiving to a different file
  4018. is useful to keep archived entries from contributing to the
  4019. Org-mode Agenda.
  4020. The archived entries will be filed as subtrees of the specified
  4021. headline. When the headline is omitted, the subtrees are simply
  4022. filed away at the end of the file, as top-level entries. Also in
  4023. the heading you can use %s to represent the file name, this can be
  4024. useful when using the same archive for a number of different files.
  4025. Here are a few examples:
  4026. \"%s_archive::\"
  4027. If the current file is Projects.org, archive in file
  4028. Projects.org_archive, as top-level trees. This is the default.
  4029. \"::* Archived Tasks\"
  4030. Archive in the current file, under the top-level headline
  4031. \"* Archived Tasks\".
  4032. \"~/org/archive.org::\"
  4033. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4034. \"~/org/archive.org::* From %s\"
  4035. Archive in file ~/org/archive.org (absolute path), under headlines
  4036. \"From FILENAME\" where file name is the current file name.
  4037. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4038. The \"datetree/\" string is special, signifying to archive
  4039. items to the datetree. Items are placed in either the CLOSED
  4040. date of the item, or the current date if there is no CLOSED date.
  4041. The heading will be a subentry to the current date. There doesn't
  4042. need to be a heading, but there always needs to be a slash after
  4043. datetree. For example, to store archived items directly in the
  4044. datetree, use \"~/org/datetree.org::datetree/\".
  4045. \"basement::** Finished Tasks\"
  4046. Archive in file ./basement (relative path), as level 3 trees
  4047. below the level 2 heading \"** Finished Tasks\".
  4048. You may set this option on a per-file basis by adding to the buffer a
  4049. line like
  4050. #+ARCHIVE: basement::** Finished Tasks
  4051. You may also define it locally for a subtree by setting an ARCHIVE property
  4052. in the entry. If such a property is found in an entry, or anywhere up
  4053. the hierarchy, it will be used."
  4054. :group 'org-archive
  4055. :type 'string)
  4056. (defcustom org-agenda-skip-archived-trees t
  4057. "Non-nil means the agenda will skip any items located in archived trees.
  4058. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4059. variable is no longer recommended, you should leave it at the value t.
  4060. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4061. :group 'org-archive
  4062. :group 'org-agenda-skip
  4063. :type 'boolean)
  4064. (defcustom org-columns-skip-archived-trees t
  4065. "Non-nil means ignore archived trees when creating column view."
  4066. :group 'org-archive
  4067. :group 'org-properties
  4068. :type 'boolean)
  4069. (defcustom org-cycle-open-archived-trees nil
  4070. "Non-nil means `org-cycle' will open archived trees.
  4071. An archived tree is a tree marked with the tag ARCHIVE.
  4072. When nil, archived trees will stay folded. You can still open them with
  4073. normal outline commands like `show-all', but not with the cycling commands."
  4074. :group 'org-archive
  4075. :group 'org-cycle
  4076. :type 'boolean)
  4077. (defcustom org-sparse-tree-open-archived-trees nil
  4078. "Non-nil means sparse tree construction shows matches in archived trees.
  4079. When nil, matches in these trees are highlighted, but the trees are kept in
  4080. collapsed state."
  4081. :group 'org-archive
  4082. :group 'org-sparse-trees
  4083. :type 'boolean)
  4084. (defcustom org-sparse-tree-default-date-type nil
  4085. "The default date type when building a sparse tree.
  4086. When this is nil, a date is a scheduled or a deadline timestamp.
  4087. Otherwise, these types are allowed:
  4088. all: all timestamps
  4089. active: only active timestamps (<...>)
  4090. inactive: only inactive timestamps ([...])
  4091. scheduled: only scheduled timestamps
  4092. deadline: only deadline timestamps"
  4093. :type '(choice (const :tag "Scheduled or deadline" nil)
  4094. (const :tag "All timestamps" all)
  4095. (const :tag "Only active timestamps" active)
  4096. (const :tag "Only inactive timestamps" inactive)
  4097. (const :tag "Only scheduled timestamps" scheduled)
  4098. (const :tag "Only deadline timestamps" deadline)
  4099. (const :tag "Only closed timestamps" closed))
  4100. :version "25.1"
  4101. :package-version '(Org . "8.3")
  4102. :group 'org-sparse-trees)
  4103. (defun org-cycle-hide-archived-subtrees (state)
  4104. "Re-hide all archived subtrees after a visibility state change."
  4105. (when (and (not org-cycle-open-archived-trees)
  4106. (not (memq state '(overview folded))))
  4107. (save-excursion
  4108. (let* ((globalp (memq state '(contents all)))
  4109. (beg (if globalp (point-min) (point)))
  4110. (end (if globalp (point-max) (org-end-of-subtree t))))
  4111. (org-hide-archived-subtrees beg end)
  4112. (goto-char beg)
  4113. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4114. (message "%s" (substitute-command-keys
  4115. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4116. (defun org-force-cycle-archived ()
  4117. "Cycle subtree even if it is archived."
  4118. (interactive)
  4119. (setq this-command 'org-cycle)
  4120. (let ((org-cycle-open-archived-trees t))
  4121. (call-interactively 'org-cycle)))
  4122. (defun org-hide-archived-subtrees (beg end)
  4123. "Re-hide all archived subtrees after a visibility state change."
  4124. (org-with-wide-buffer
  4125. (let ((case-fold-search nil)
  4126. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4127. (goto-char beg)
  4128. ;; Include headline point is currently on.
  4129. (beginning-of-line)
  4130. (while (and (< (point) end) (re-search-forward re end t))
  4131. (when (member org-archive-tag (org-get-tags))
  4132. (org-flag-subtree t)
  4133. (org-end-of-subtree t))))))
  4134. (declare-function outline-end-of-heading "outline" ())
  4135. (declare-function outline-flag-region "outline" (from to flag))
  4136. (defun org-flag-subtree (flag)
  4137. (save-excursion
  4138. (org-back-to-heading t)
  4139. (outline-end-of-heading)
  4140. (outline-flag-region (point)
  4141. (progn (org-end-of-subtree t) (point))
  4142. flag)))
  4143. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4144. ;; Declare Column View Code
  4145. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4146. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4147. (declare-function org-columns-compute "org-colview" (property))
  4148. ;; Declare ID code
  4149. (declare-function org-id-store-link "org-id")
  4150. (declare-function org-id-locations-load "org-id")
  4151. (declare-function org-id-locations-save "org-id")
  4152. (defvar org-id-track-globally)
  4153. ;;; Variables for pre-computed regular expressions, all buffer local
  4154. (defvar org-todo-regexp nil
  4155. "Matches any of the TODO state keywords.")
  4156. (make-variable-buffer-local 'org-todo-regexp)
  4157. (defvar org-not-done-regexp nil
  4158. "Matches any of the TODO state keywords except the last one.")
  4159. (make-variable-buffer-local 'org-not-done-regexp)
  4160. (defvar org-not-done-heading-regexp nil
  4161. "Matches a TODO headline that is not done.")
  4162. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4163. (defvar org-todo-line-regexp nil
  4164. "Matches a headline and puts TODO state into group 2 if present.")
  4165. (make-variable-buffer-local 'org-todo-line-regexp)
  4166. (defvar org-complex-heading-regexp nil
  4167. "Matches a headline and puts everything into groups:
  4168. group 1: the stars
  4169. group 2: The todo keyword, maybe
  4170. group 3: Priority cookie
  4171. group 4: True headline
  4172. group 5: Tags")
  4173. (make-variable-buffer-local 'org-complex-heading-regexp)
  4174. (defvar org-complex-heading-regexp-format nil
  4175. "Printf format to make regexp to match an exact headline.
  4176. This regexp will match the headline of any node which has the
  4177. exact headline text that is put into the format, but may have any
  4178. TODO state, priority and tags.")
  4179. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4180. (defvar org-todo-line-tags-regexp nil
  4181. "Matches a headline and puts TODO state into group 2 if present.
  4182. Also put tags into group 4 if tags are present.")
  4183. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4184. (defconst org-plain-time-of-day-regexp
  4185. (concat
  4186. "\\(\\<[012]?[0-9]"
  4187. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4188. "\\(--?"
  4189. "\\(\\<[012]?[0-9]"
  4190. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4191. "\\)?")
  4192. "Regular expression to match a plain time or time range.
  4193. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4194. groups carry important information:
  4195. 0 the full match
  4196. 1 the first time, range or not
  4197. 8 the second time, if it is a range.")
  4198. (defconst org-plain-time-extension-regexp
  4199. (concat
  4200. "\\(\\<[012]?[0-9]"
  4201. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4202. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4203. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4204. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4205. groups carry important information:
  4206. 0 the full match
  4207. 7 hours of duration
  4208. 9 minutes of duration")
  4209. (defconst org-stamp-time-of-day-regexp
  4210. (concat
  4211. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4212. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4213. "\\(--?"
  4214. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4215. "Regular expression to match a timestamp time or time range.
  4216. After a match, the following groups carry important information:
  4217. 0 the full match
  4218. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4219. 2 the first time, range or not
  4220. 4 the second time, if it is a range.")
  4221. (defconst org-startup-options
  4222. '(("fold" org-startup-folded t)
  4223. ("overview" org-startup-folded t)
  4224. ("nofold" org-startup-folded nil)
  4225. ("showall" org-startup-folded nil)
  4226. ("showeverything" org-startup-folded showeverything)
  4227. ("content" org-startup-folded content)
  4228. ("indent" org-startup-indented t)
  4229. ("noindent" org-startup-indented nil)
  4230. ("hidestars" org-hide-leading-stars t)
  4231. ("showstars" org-hide-leading-stars nil)
  4232. ("odd" org-odd-levels-only t)
  4233. ("oddeven" org-odd-levels-only nil)
  4234. ("align" org-startup-align-all-tables t)
  4235. ("noalign" org-startup-align-all-tables nil)
  4236. ("inlineimages" org-startup-with-inline-images t)
  4237. ("noinlineimages" org-startup-with-inline-images nil)
  4238. ("latexpreview" org-startup-with-latex-preview t)
  4239. ("nolatexpreview" org-startup-with-latex-preview nil)
  4240. ("customtime" org-display-custom-times t)
  4241. ("logdone" org-log-done time)
  4242. ("lognotedone" org-log-done note)
  4243. ("nologdone" org-log-done nil)
  4244. ("lognoteclock-out" org-log-note-clock-out t)
  4245. ("nolognoteclock-out" org-log-note-clock-out nil)
  4246. ("logrepeat" org-log-repeat state)
  4247. ("lognoterepeat" org-log-repeat note)
  4248. ("logdrawer" org-log-into-drawer t)
  4249. ("nologdrawer" org-log-into-drawer nil)
  4250. ("logstatesreversed" org-log-states-order-reversed t)
  4251. ("nologstatesreversed" org-log-states-order-reversed nil)
  4252. ("nologrepeat" org-log-repeat nil)
  4253. ("logreschedule" org-log-reschedule time)
  4254. ("lognotereschedule" org-log-reschedule note)
  4255. ("nologreschedule" org-log-reschedule nil)
  4256. ("logredeadline" org-log-redeadline time)
  4257. ("lognoteredeadline" org-log-redeadline note)
  4258. ("nologredeadline" org-log-redeadline nil)
  4259. ("logrefile" org-log-refile time)
  4260. ("lognoterefile" org-log-refile note)
  4261. ("nologrefile" org-log-refile nil)
  4262. ("fninline" org-footnote-define-inline t)
  4263. ("nofninline" org-footnote-define-inline nil)
  4264. ("fnlocal" org-footnote-section nil)
  4265. ("fnauto" org-footnote-auto-label t)
  4266. ("fnprompt" org-footnote-auto-label nil)
  4267. ("fnconfirm" org-footnote-auto-label confirm)
  4268. ("fnplain" org-footnote-auto-label plain)
  4269. ("fnadjust" org-footnote-auto-adjust t)
  4270. ("nofnadjust" org-footnote-auto-adjust nil)
  4271. ("constcgs" constants-unit-system cgs)
  4272. ("constSI" constants-unit-system SI)
  4273. ("noptag" org-tag-persistent-alist nil)
  4274. ("hideblocks" org-hide-block-startup t)
  4275. ("nohideblocks" org-hide-block-startup nil)
  4276. ("beamer" org-startup-with-beamer-mode t)
  4277. ("entitiespretty" org-pretty-entities t)
  4278. ("entitiesplain" org-pretty-entities nil))
  4279. "Variable associated with STARTUP options for org-mode.
  4280. Each element is a list of three items: the startup options (as written
  4281. in the #+STARTUP line), the corresponding variable, and the value to set
  4282. this variable to if the option is found. An optional forth element PUSH
  4283. means to push this value onto the list in the variable.")
  4284. (defcustom org-group-tags t
  4285. "When non-nil (the default), use group tags.
  4286. This can be turned on/off through `org-toggle-tags-groups'."
  4287. :group 'org-tags
  4288. :group 'org-startup
  4289. :type 'boolean)
  4290. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4291. (defun org-toggle-tags-groups ()
  4292. "Toggle support for group tags.
  4293. Support for group tags is controlled by the option
  4294. `org-group-tags', which is non-nil by default."
  4295. (interactive)
  4296. (setq org-group-tags (not org-group-tags))
  4297. (cond ((and (derived-mode-p 'org-agenda-mode)
  4298. org-group-tags)
  4299. (org-agenda-redo))
  4300. ((derived-mode-p 'org-mode)
  4301. (let ((org-inhibit-startup t)) (org-mode))))
  4302. (message "Groups tags support has been turned %s"
  4303. (if org-group-tags "on" "off")))
  4304. (defun org-set-regexps-and-options (&optional tags-only)
  4305. "Precompute regular expressions used in the current buffer.
  4306. When optional argument TAGS-ONLY is non-nil, only compute tags
  4307. related expressions."
  4308. (when (derived-mode-p 'org-mode)
  4309. (let ((alist (org--setup-collect-keywords
  4310. (org-make-options-regexp
  4311. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4312. (and (not tags-only)
  4313. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4314. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4315. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4316. (org--setup-process-tags
  4317. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4318. (unless tags-only
  4319. ;; File properties.
  4320. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4321. ;; Archive location.
  4322. (let ((archive (cdr (assq 'archive alist))))
  4323. (when archive (org-set-local 'org-archive-location archive)))
  4324. ;; Category.
  4325. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4326. (when cat
  4327. (org-set-local 'org-category (intern cat))
  4328. (org-set-local 'org-file-properties
  4329. (org--update-property-plist
  4330. "CATEGORY" cat org-file-properties))))
  4331. ;; Columns.
  4332. (let ((column (cdr (assq 'columns alist))))
  4333. (when column (org-set-local 'org-columns-default-format column)))
  4334. ;; Constants.
  4335. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4336. ;; Link abbreviations.
  4337. (let ((links (cdr (assq 'link alist))))
  4338. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4339. ;; Priorities.
  4340. (let ((priorities (cdr (assq 'priorities alist))))
  4341. (when priorities
  4342. (org-set-local 'org-highest-priority (nth 0 priorities))
  4343. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4344. (org-set-local 'org-default-priority (nth 2 priorities))))
  4345. ;; Scripts.
  4346. (let ((scripts (assq 'scripts alist)))
  4347. (when scripts
  4348. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4349. ;; Startup options.
  4350. (let ((startup (cdr (assq 'startup alist))))
  4351. (dolist (option startup)
  4352. (let ((entry (assoc-string option org-startup-options t)))
  4353. (when entry
  4354. (let ((var (nth 1 entry))
  4355. (val (nth 2 entry)))
  4356. (if (not (nth 3 entry)) (org-set-local var val)
  4357. (unless (listp (symbol-value var))
  4358. (org-set-local var nil))
  4359. (add-to-list var val)))))))
  4360. ;; TODO keywords.
  4361. (org-set-local 'org-todo-kwd-alist nil)
  4362. (org-set-local 'org-todo-key-alist nil)
  4363. (org-set-local 'org-todo-key-trigger nil)
  4364. (org-set-local 'org-todo-keywords-1 nil)
  4365. (org-set-local 'org-done-keywords nil)
  4366. (org-set-local 'org-todo-heads nil)
  4367. (org-set-local 'org-todo-sets nil)
  4368. (org-set-local 'org-todo-log-states nil)
  4369. (let ((todo-sequences
  4370. (or (nreverse (cdr (assq 'todo alist)))
  4371. (let ((d (default-value 'org-todo-keywords)))
  4372. (if (not (stringp (car d))) d
  4373. ;; XXX: Backward compatibility code.
  4374. (list (cons org-todo-interpretation d)))))))
  4375. (dolist (sequence todo-sequences)
  4376. (let* ((sequence (or (run-hook-with-args-until-success
  4377. 'org-todo-setup-filter-hook sequence)
  4378. sequence))
  4379. (sequence-type (car sequence))
  4380. (keywords (cdr sequence))
  4381. (sep (member "|" keywords))
  4382. names alist)
  4383. (dolist (k (remove "|" keywords))
  4384. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4385. k)
  4386. (error "Invalid TODO keyword %s" k))
  4387. (let ((name (match-string 1 k))
  4388. (key (match-string 2 k))
  4389. (log (org-extract-log-state-settings k)))
  4390. (push name names)
  4391. (push (cons name (and key (string-to-char key))) alist)
  4392. (when log (push log org-todo-log-states))))
  4393. (let* ((names (nreverse names))
  4394. (done (if sep (org-remove-keyword-keys (cdr sep))
  4395. (last names)))
  4396. (head (car names))
  4397. (tail (list sequence-type head (car done) (org-last done))))
  4398. (add-to-list 'org-todo-heads head 'append)
  4399. (push names org-todo-sets)
  4400. (setq org-done-keywords (append org-done-keywords done nil))
  4401. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4402. (setq org-todo-key-alist
  4403. (append org-todo-key-alist
  4404. (and alist
  4405. (append '((:startgroup))
  4406. (nreverse alist)
  4407. '((:endgroup))))))
  4408. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4409. (setq org-todo-sets (nreverse org-todo-sets)
  4410. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4411. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4412. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4413. ;; Compute the regular expressions and other local variables.
  4414. ;; Using `org-outline-regexp-bol' would complicate them much,
  4415. ;; because of the fixed white space at the end of that string.
  4416. (if (not org-done-keywords)
  4417. (setq org-done-keywords
  4418. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4419. (setq org-not-done-keywords
  4420. (org-delete-all org-done-keywords
  4421. (copy-sequence org-todo-keywords-1))
  4422. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4423. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4424. org-not-done-heading-regexp
  4425. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4426. org-todo-line-regexp
  4427. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4428. org-complex-heading-regexp
  4429. (concat "^\\(\\*+\\)"
  4430. "\\(?: +" org-todo-regexp "\\)?"
  4431. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4432. "\\(?: +\\(.*?\\)\\)??"
  4433. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4434. "[ \t]*$")
  4435. org-complex-heading-regexp-format
  4436. (concat "^\\(\\*+\\)"
  4437. "\\(?: +" org-todo-regexp "\\)?"
  4438. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4439. "\\(?: +"
  4440. ;; Stats cookies can be stuck to body.
  4441. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4442. "\\(%s\\)"
  4443. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4444. "\\)"
  4445. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4446. "[ \t]*$")
  4447. org-todo-line-tags-regexp
  4448. (concat "^\\(\\*+\\)"
  4449. "\\(?: +" org-todo-regexp "\\)?"
  4450. "\\(?: +\\(.*?\\)\\)??"
  4451. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4452. "[ \t]*$"))
  4453. (org-compute-latex-and-related-regexp)))))
  4454. (defun org--setup-collect-keywords (regexp &optional files alist)
  4455. "Return setup keywords values as an alist.
  4456. REGEXP matches a subset of setup keywords. FILES is a list of
  4457. file names already visited. It is used to avoid circular setup
  4458. files. ALIST, when non-nil, is the alist computed so far.
  4459. Return value contains the following keys: `archive', `category',
  4460. `columns', `constants', `filetags', `link', `priorities',
  4461. `property', `scripts', `startup', `tags' and `todo'."
  4462. (org-with-wide-buffer
  4463. (goto-char (point-min))
  4464. (let ((case-fold-search t))
  4465. (while (re-search-forward regexp nil t)
  4466. (let ((element (org-element-at-point)))
  4467. (when (eq (org-element-type element) 'keyword)
  4468. (let ((key (org-element-property :key element))
  4469. (value (org-element-property :value element)))
  4470. (cond
  4471. ((equal key "ARCHIVE")
  4472. (when (org-string-nw-p value)
  4473. (push (cons 'archive value) alist)))
  4474. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4475. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4476. ((equal key "CONSTANTS")
  4477. (let* ((constants (assq 'constants alist))
  4478. (store (cdr constants)))
  4479. (dolist (pair (org-split-string value))
  4480. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4481. pair)
  4482. (let* ((name (match-string 1 pair))
  4483. (value (match-string 2 pair))
  4484. (old (assoc name store)))
  4485. (if old (setcdr old value)
  4486. (push (cons name value) store)))))
  4487. (if constants (setcdr constants store)
  4488. (push (cons 'constants store) alist))))
  4489. ((equal key "FILETAGS")
  4490. (when (org-string-nw-p value)
  4491. (let ((old (assq 'filetags alist))
  4492. (new (apply #'nconc
  4493. (mapcar (lambda (x) (org-split-string x ":"))
  4494. (org-split-string value)))))
  4495. (if old (setcdr old (append new (cdr old)))
  4496. (push (cons 'filetags new) alist)))))
  4497. ((equal key "LINK")
  4498. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4499. (let ((links (assq 'link alist))
  4500. (pair (cons (org-match-string-no-properties 1 value)
  4501. (org-match-string-no-properties 2 value))))
  4502. (if links (push pair (cdr links))
  4503. (push (list 'link pair) alist)))))
  4504. ((equal key "OPTIONS")
  4505. (when (and (org-string-nw-p value)
  4506. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4507. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4508. ((equal key "PRIORITIES")
  4509. (push (cons 'priorities
  4510. (let ((prio (org-split-string value)))
  4511. (if (< (length prio) 3) '(?A ?C ?B)
  4512. (mapcar #'string-to-char prio))))
  4513. alist))
  4514. ((equal key "PROPERTY")
  4515. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4516. (let* ((property (assq 'property alist))
  4517. (value (org--update-property-plist
  4518. (org-match-string-no-properties 1 value)
  4519. (org-match-string-no-properties 2 value)
  4520. (cdr property))))
  4521. (if property (setcdr property value)
  4522. (push (cons 'property value) alist)))))
  4523. ((equal key "STARTUP")
  4524. (let ((startup (assq 'startup alist)))
  4525. (if startup
  4526. (setcdr startup
  4527. (append (cdr startup) (org-split-string value)))
  4528. (push (cons 'startup (org-split-string value)) alist))))
  4529. ((equal key "TAGS")
  4530. (let ((tag-cell (assq 'tags alist)))
  4531. (if tag-cell
  4532. (setcdr tag-cell
  4533. (append (cdr tag-cell)
  4534. '("\\n")
  4535. (org-split-string value)))
  4536. (push (cons 'tags (org-split-string value)) alist))))
  4537. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4538. (let ((todo (assq 'todo alist))
  4539. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4540. (org-split-string value))))
  4541. (if todo (push value (cdr todo))
  4542. (push (list 'todo value) alist))))
  4543. ((equal key "SETUPFILE")
  4544. (unless buffer-read-only ; Do not check in Gnus messages.
  4545. (let ((f (and (org-string-nw-p value)
  4546. (expand-file-name
  4547. (org-remove-double-quotes value)))))
  4548. (when (and f (file-readable-p f) (not (member f files)))
  4549. (with-temp-buffer
  4550. (setq default-directory (file-name-directory f))
  4551. (insert-file-contents f)
  4552. (setq alist
  4553. ;; Fake Org mode to benefit from cache
  4554. ;; without recurring needlessly.
  4555. (let ((major-mode 'org-mode))
  4556. (org--setup-collect-keywords
  4557. regexp (cons f files) alist)))))))))))))))
  4558. alist)
  4559. (defun org--setup-process-tags (tags filetags)
  4560. "Precompute variables used for tags.
  4561. TAGS is a list of tags and tag group symbols, as strings.
  4562. FILETAGS is a list of tags, as strings."
  4563. ;; Process the file tags.
  4564. (org-set-local 'org-file-tags
  4565. (mapcar #'org-add-prop-inherited filetags))
  4566. ;; Provide default tags if no local tags are found.
  4567. (when (and (not tags) org-tag-alist)
  4568. (setq tags
  4569. (mapcar (lambda (tag)
  4570. (case (car tag)
  4571. (:startgroup "{")
  4572. (:endgroup "}")
  4573. (:startgrouptag "[")
  4574. (:endgrouptag "]")
  4575. (:grouptags ":")
  4576. (:newline "\\n")
  4577. (otherwise (concat (car tag)
  4578. (and (characterp (cdr tag))
  4579. (format "(%c)" (cdr tag)))))))
  4580. org-tag-alist)))
  4581. ;; Process the tags.
  4582. (org-set-local 'org-tag-groups-alist nil)
  4583. (org-set-local 'org-tag-alist nil)
  4584. (let (group-flag)
  4585. (while tags
  4586. (let ((e (car tags)))
  4587. (setq tags (cdr tags))
  4588. (cond
  4589. ((equal e "{")
  4590. (push '(:startgroup) org-tag-alist)
  4591. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4592. ((equal e "}")
  4593. (push '(:endgroup) org-tag-alist)
  4594. (setq group-flag nil))
  4595. ((equal e "[")
  4596. (push '(:startgrouptag) org-tag-alist)
  4597. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4598. ((equal e "]")
  4599. (push '(:endgrouptag) org-tag-alist)
  4600. (setq group-flag nil))
  4601. ((equal e ":")
  4602. (push '(:grouptags) org-tag-alist)
  4603. (setq group-flag 'append))
  4604. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4605. ((string-match
  4606. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4607. "\\|{.+?}\\)" ; regular expression
  4608. "\\(?:(\\(.\\))\\)?\\'")) e)
  4609. (let ((tag (match-string 1 e))
  4610. (key (and (match-beginning 2)
  4611. (string-to-char (match-string 2 e)))))
  4612. (cond ((eq group-flag 'append)
  4613. (setcar org-tag-groups-alist
  4614. (append (car org-tag-groups-alist) (list tag))))
  4615. (group-flag (push (list tag) org-tag-groups-alist)))
  4616. ;; Push all tags in groups, no matter if they already exist.
  4617. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4618. (push (cons tag key) org-tag-alist))))))))
  4619. (setq org-tag-alist (nreverse org-tag-alist)))
  4620. (defun org-file-contents (file &optional noerror)
  4621. "Return the contents of FILE, as a string."
  4622. (if (and file (file-readable-p file))
  4623. (with-temp-buffer
  4624. (insert-file-contents file)
  4625. (buffer-string))
  4626. (funcall (if noerror 'message 'error)
  4627. "Cannot read file \"%s\"%s"
  4628. file
  4629. (let ((from (buffer-file-name (buffer-base-buffer))))
  4630. (if from (concat " (referenced in file \"" from "\")") "")))))
  4631. (defun org-extract-log-state-settings (x)
  4632. "Extract the log state setting from a TODO keyword string.
  4633. This will extract info from a string like \"WAIT(w@/!)\"."
  4634. (let (kw key log1 log2)
  4635. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4636. (setq kw (match-string 1 x)
  4637. key (and (match-end 2) (match-string 2 x))
  4638. log1 (and (match-end 3) (match-string 3 x))
  4639. log2 (and (match-end 4) (match-string 4 x)))
  4640. (and (or log1 log2)
  4641. (list kw
  4642. (and log1 (if (equal log1 "!") 'time 'note))
  4643. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4644. (defun org-remove-keyword-keys (list)
  4645. "Remove a pair of parenthesis at the end of each string in LIST."
  4646. (mapcar (lambda (x)
  4647. (if (string-match "(.*)$" x)
  4648. (substring x 0 (match-beginning 0))
  4649. x))
  4650. list))
  4651. (defun org-assign-fast-keys (alist)
  4652. "Assign fast keys to a keyword-key alist.
  4653. Respect keys that are already there."
  4654. (let (new e (alt ?0))
  4655. (while (setq e (pop alist))
  4656. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4657. (cdr e)) ;; Key already assigned.
  4658. (push e new)
  4659. (let ((clist (string-to-list (downcase (car e))))
  4660. (used (append new alist)))
  4661. (when (= (car clist) ?@)
  4662. (pop clist))
  4663. (while (and clist (rassoc (car clist) used))
  4664. (pop clist))
  4665. (unless clist
  4666. (while (rassoc alt used)
  4667. (incf alt)))
  4668. (push (cons (car e) (or (car clist) alt)) new))))
  4669. (nreverse new)))
  4670. ;;; Some variables used in various places
  4671. (defvar org-window-configuration nil
  4672. "Used in various places to store a window configuration.")
  4673. (defvar org-selected-window nil
  4674. "Used in various places to store a window configuration.")
  4675. (defvar org-finish-function nil
  4676. "Function to be called when `C-c C-c' is used.
  4677. This is for getting out of special buffers like capture.")
  4678. ;; FIXME: Occasionally check by commenting these, to make sure
  4679. ;; no other functions uses these, forgetting to let-bind them.
  4680. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4681. (defvar org-last-state)
  4682. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4683. ;; Defined somewhere in this file, but used before definition.
  4684. (defvar org-entities) ;; defined in org-entities.el
  4685. (defvar org-struct-menu)
  4686. (defvar org-org-menu)
  4687. (defvar org-tbl-menu)
  4688. ;;;; Define the Org-mode
  4689. ;; We use a before-change function to check if a table might need
  4690. ;; an update.
  4691. (defvar org-table-may-need-update t
  4692. "Indicates that a table might need an update.
  4693. This variable is set by `org-before-change-function'.
  4694. `org-table-align' sets it back to nil.")
  4695. (defun org-before-change-function (beg end)
  4696. "Every change indicates that a table might need an update."
  4697. (setq org-table-may-need-update t))
  4698. (defvar org-mode-map)
  4699. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4700. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4701. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4702. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4703. (defvar org-table-buffer-is-an nil)
  4704. (defvar bidi-paragraph-direction)
  4705. (defvar buffer-face-mode-face)
  4706. (require 'outline)
  4707. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4708. (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"))
  4709. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4710. ;; Other stuff we need.
  4711. (require 'time-date)
  4712. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4713. (require 'easymenu)
  4714. (autoload 'easy-menu-add "easymenu")
  4715. (require 'overlay)
  4716. ;; (require 'org-macs) moved higher up in the file before it is first used
  4717. (require 'org-entities)
  4718. ;; (require 'org-compat) moved higher up in the file before it is first used
  4719. (require 'org-faces)
  4720. (require 'org-list)
  4721. (require 'org-pcomplete)
  4722. (require 'org-src)
  4723. (require 'org-footnote)
  4724. (require 'org-macro)
  4725. ;; babel
  4726. (require 'ob)
  4727. ;;;###autoload
  4728. (define-derived-mode org-mode outline-mode "Org"
  4729. "Outline-based notes management and organizer, alias
  4730. \"Carsten's outline-mode for keeping track of everything.\"
  4731. Org-mode develops organizational tasks around a NOTES file which
  4732. contains information about projects as plain text. Org-mode is
  4733. implemented on top of outline-mode, which is ideal to keep the content
  4734. of large files well structured. It supports ToDo items, deadlines and
  4735. time stamps, which magically appear in the diary listing of the Emacs
  4736. calendar. Tables are easily created with a built-in table editor.
  4737. Plain text URL-like links connect to websites, emails (VM), Usenet
  4738. messages (Gnus), BBDB entries, and any files related to the project.
  4739. For printing and sharing of notes, an Org-mode file (or a part of it)
  4740. can be exported as a structured ASCII or HTML file.
  4741. The following commands are available:
  4742. \\{org-mode-map}"
  4743. ;; Get rid of Outline menus, they are not needed
  4744. ;; Need to do this here because define-derived-mode sets up
  4745. ;; the keymap so late. Still, it is a waste to call this each time
  4746. ;; we switch another buffer into org-mode.
  4747. (if (featurep 'xemacs)
  4748. (when (boundp 'outline-mode-menu-heading)
  4749. ;; Assume this is Greg's port, it uses easymenu
  4750. (easy-menu-remove outline-mode-menu-heading)
  4751. (easy-menu-remove outline-mode-menu-show)
  4752. (easy-menu-remove outline-mode-menu-hide))
  4753. (define-key org-mode-map [menu-bar headings] 'undefined)
  4754. (define-key org-mode-map [menu-bar hide] 'undefined)
  4755. (define-key org-mode-map [menu-bar show] 'undefined))
  4756. (org-load-modules-maybe)
  4757. (when (featurep 'xemacs)
  4758. (easy-menu-add org-org-menu)
  4759. (easy-menu-add org-tbl-menu))
  4760. (org-install-agenda-files-menu)
  4761. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4762. (add-to-invisibility-spec '(org-cwidth))
  4763. (add-to-invisibility-spec '(org-hide-block . t))
  4764. (when (featurep 'xemacs)
  4765. (org-set-local 'line-move-ignore-invisible t))
  4766. (org-set-local 'outline-regexp org-outline-regexp)
  4767. (org-set-local 'outline-level 'org-outline-level)
  4768. (setq bidi-paragraph-direction 'left-to-right)
  4769. (when (and org-ellipsis
  4770. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4771. (fboundp 'make-glyph-code))
  4772. (unless org-display-table
  4773. (setq org-display-table (make-display-table)))
  4774. (set-display-table-slot
  4775. org-display-table 4
  4776. (vconcat (mapcar
  4777. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4778. org-ellipsis)))
  4779. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4780. (setq buffer-display-table org-display-table))
  4781. (org-set-regexps-and-options)
  4782. (org-set-font-lock-defaults)
  4783. (when (and org-tag-faces (not org-tags-special-faces-re))
  4784. ;; tag faces set outside customize.... force initialization.
  4785. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4786. ;; Calc embedded
  4787. (org-set-local 'calc-embedded-open-mode "# ")
  4788. ;; Modify a few syntax entries
  4789. (modify-syntax-entry ?@ "w")
  4790. (modify-syntax-entry ?\" "\"")
  4791. (modify-syntax-entry ?\\ "_")
  4792. (modify-syntax-entry ?~ "_")
  4793. (if org-startup-truncated (setq truncate-lines t))
  4794. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4795. (org-set-local 'font-lock-unfontify-region-function
  4796. 'org-unfontify-region)
  4797. ;; Activate before-change-function
  4798. (org-set-local 'org-table-may-need-update t)
  4799. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4800. 'local)
  4801. ;; Check for running clock before killing a buffer
  4802. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4803. ;; Initialize macros templates.
  4804. (org-macro-initialize-templates)
  4805. ;; Initialize radio targets.
  4806. (org-update-radio-target-regexp)
  4807. ;; Indentation.
  4808. (org-set-local 'indent-line-function 'org-indent-line)
  4809. (org-set-local 'indent-region-function 'org-indent-region)
  4810. ;; Filling and auto-filling.
  4811. (org-setup-filling)
  4812. ;; Comments.
  4813. (org-setup-comments-handling)
  4814. ;; Initialize cache.
  4815. (org-element-cache-reset)
  4816. ;; Beginning/end of defun
  4817. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4818. (org-set-local 'end-of-defun-function
  4819. (lambda ()
  4820. (if (not (org-at-heading-p))
  4821. (org-forward-element)
  4822. (org-forward-element)
  4823. (forward-char -1))))
  4824. ;; Next error for sparse trees
  4825. (org-set-local 'next-error-function 'org-occur-next-match)
  4826. ;; Make sure dependence stuff works reliably, even for users who set it
  4827. ;; too late :-(
  4828. (if org-enforce-todo-dependencies
  4829. (add-hook 'org-blocker-hook
  4830. 'org-block-todo-from-children-or-siblings-or-parent)
  4831. (remove-hook 'org-blocker-hook
  4832. 'org-block-todo-from-children-or-siblings-or-parent))
  4833. (if org-enforce-todo-checkbox-dependencies
  4834. (add-hook 'org-blocker-hook
  4835. 'org-block-todo-from-checkboxes)
  4836. (remove-hook 'org-blocker-hook
  4837. 'org-block-todo-from-checkboxes))
  4838. ;; Align options lines
  4839. (org-set-local
  4840. 'align-mode-rules-list
  4841. '((org-in-buffer-settings
  4842. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4843. (modes . '(org-mode)))))
  4844. ;; Imenu
  4845. (org-set-local 'imenu-create-index-function
  4846. 'org-imenu-get-tree)
  4847. ;; Make isearch reveal context
  4848. (if (or (featurep 'xemacs)
  4849. (not (boundp 'outline-isearch-open-invisible-function)))
  4850. ;; Emacs 21 and XEmacs make use of the hook
  4851. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4852. ;; Emacs 22 deals with this through a special variable
  4853. (org-set-local 'outline-isearch-open-invisible-function
  4854. (lambda (&rest ignore) (org-show-context 'isearch))))
  4855. ;; Setup the pcomplete hooks
  4856. (set (make-local-variable 'pcomplete-command-completion-function)
  4857. 'org-pcomplete-initial)
  4858. (set (make-local-variable 'pcomplete-command-name-function)
  4859. 'org-command-at-point)
  4860. (set (make-local-variable 'pcomplete-default-completion-function)
  4861. 'ignore)
  4862. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4863. 'org-parse-arguments)
  4864. (set (make-local-variable 'pcomplete-termination-string) "")
  4865. (when (>= emacs-major-version 23)
  4866. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4867. ;; If empty file that did not turn on org-mode automatically, make it to.
  4868. (if (and org-insert-mode-line-in-empty-file
  4869. (org-called-interactively-p 'any)
  4870. (= (point-min) (point-max)))
  4871. (insert "# -*- mode: org -*-\n\n"))
  4872. (unless org-inhibit-startup
  4873. (org-unmodified
  4874. (and org-startup-with-beamer-mode (org-beamer-mode))
  4875. (when org-startup-align-all-tables
  4876. (org-table-map-tables 'org-table-align 'quietly))
  4877. (when org-startup-with-inline-images
  4878. (org-display-inline-images))
  4879. (when org-startup-with-latex-preview
  4880. (org-toggle-latex-fragment))
  4881. (unless org-inhibit-startup-visibility-stuff
  4882. (org-set-startup-visibility))
  4883. (org-refresh-effort-properties)))
  4884. ;; Try to set org-hide correctly
  4885. (let ((foreground (org-find-invisible-foreground)))
  4886. (if foreground
  4887. (set-face-foreground 'org-hide foreground))))
  4888. ;; Update `customize-package-emacs-version-alist'
  4889. (add-to-list 'customize-package-emacs-version-alist
  4890. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4891. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4892. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4893. (defvar org-mode-transpose-word-syntax-table
  4894. (let ((st (make-syntax-table text-mode-syntax-table)))
  4895. (mapc (lambda(c) (modify-syntax-entry
  4896. (string-to-char (car c)) "w p" st))
  4897. org-emphasis-alist)
  4898. st))
  4899. (when (fboundp 'abbrev-table-put)
  4900. (abbrev-table-put org-mode-abbrev-table
  4901. :parents (list text-mode-abbrev-table)))
  4902. (defun org-find-invisible-foreground ()
  4903. (let ((candidates (remove
  4904. "unspecified-bg"
  4905. (nconc
  4906. (list (face-background 'default)
  4907. (face-background 'org-default))
  4908. (mapcar
  4909. (lambda (alist)
  4910. (when (boundp alist)
  4911. (cdr (assoc 'background-color (symbol-value alist)))))
  4912. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4913. (list (face-foreground 'org-hide))))))
  4914. (car (remove nil candidates))))
  4915. (defun org-current-time (&optional rounding-minutes past)
  4916. "Current time, possibly rounded to ROUNDING-MINUTES.
  4917. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4918. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4919. the rounding returns a past time."
  4920. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4921. (car org-time-stamp-rounding-minutes)))
  4922. (time (decode-time)) res)
  4923. (if (< r 1)
  4924. (current-time)
  4925. (setq res
  4926. (apply 'encode-time
  4927. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4928. (nthcdr 2 time))))
  4929. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4930. (seconds-to-time (- (org-float-time res) (* r 60)))
  4931. res))))
  4932. (defun org-today ()
  4933. "Return today date, considering `org-extend-today-until'."
  4934. (time-to-days
  4935. (time-subtract (current-time)
  4936. (list 0 (* 3600 org-extend-today-until) 0))))
  4937. ;;;; Font-Lock stuff, including the activators
  4938. (defvar org-mouse-map (make-sparse-keymap))
  4939. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4940. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4941. (when org-mouse-1-follows-link
  4942. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4943. (when org-tab-follows-link
  4944. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4945. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4946. (require 'font-lock)
  4947. (defconst org-non-link-chars "]\t\n\r<>")
  4948. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4949. "file+sys" "news" "shell" "elisp" "doi" "message"
  4950. "help"))
  4951. (defvar org-link-types-re nil
  4952. "Matches a link that has a url-like prefix like \"http:\"")
  4953. (defvar org-link-re-with-space nil
  4954. "Matches a link with spaces, optional angular brackets around it.")
  4955. (defvar org-link-re-with-space2 nil
  4956. "Matches a link with spaces, optional angular brackets around it.")
  4957. (defvar org-link-re-with-space3 nil
  4958. "Matches a link with spaces, only for internal part in bracket links.")
  4959. (defvar org-angle-link-re nil
  4960. "Matches link with angular brackets, spaces are allowed.")
  4961. (defvar org-plain-link-re nil
  4962. "Matches plain link, without spaces.")
  4963. (defvar org-bracket-link-regexp nil
  4964. "Matches a link in double brackets.")
  4965. (defvar org-bracket-link-analytic-regexp nil
  4966. "Regular expression used to analyze links.
  4967. Here is what the match groups contain after a match:
  4968. 1: http:
  4969. 2: http
  4970. 3: path
  4971. 4: [desc]
  4972. 5: desc")
  4973. (defvar org-bracket-link-analytic-regexp++ nil
  4974. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4975. (defvar org-any-link-re nil
  4976. "Regular expression matching any link.")
  4977. (defconst org-match-sexp-depth 3
  4978. "Number of stacked braces for sub/superscript matching.")
  4979. (defun org-create-multibrace-regexp (left right n)
  4980. "Create a regular expression which will match a balanced sexp.
  4981. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4982. as single character strings.
  4983. The regexp returned will match the entire expression including the
  4984. delimiters. It will also define a single group which contains the
  4985. match except for the outermost delimiters. The maximum depth of
  4986. stacked delimiters is N. Escaping delimiters is not possible."
  4987. (let* ((nothing (concat "[^" left right "]*?"))
  4988. (or "\\|")
  4989. (re nothing)
  4990. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4991. (while (> n 1)
  4992. (setq n (1- n)
  4993. re (concat re or next)
  4994. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4995. (concat left "\\(" re "\\)" right)))
  4996. (defconst org-match-substring-regexp
  4997. (concat
  4998. "\\(\\S-\\)\\([_^]\\)\\("
  4999. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  5000. "\\|"
  5001. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  5002. "\\|"
  5003. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  5004. "The regular expression matching a sub- or superscript.")
  5005. (defconst org-match-substring-with-braces-regexp
  5006. (concat
  5007. "\\(\\S-\\)\\([_^]\\)"
  5008. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5009. "The regular expression matching a sub- or superscript, forcing braces.")
  5010. (defun org-make-link-regexps ()
  5011. "Update the link regular expressions.
  5012. This should be called after the variable `org-link-types' has changed."
  5013. (let ((types-re (regexp-opt org-link-types t)))
  5014. (setq org-link-types-re
  5015. (concat "\\`" types-re ":")
  5016. org-link-re-with-space
  5017. (concat "<?" types-re ":"
  5018. "\\([^" org-non-link-chars " ]"
  5019. "[^" org-non-link-chars "]*"
  5020. "[^" org-non-link-chars " ]\\)>?")
  5021. org-link-re-with-space2
  5022. (concat "<?" types-re ":"
  5023. "\\([^" org-non-link-chars " ]"
  5024. "[^\t\n\r]*"
  5025. "[^" org-non-link-chars " ]\\)>?")
  5026. org-link-re-with-space3
  5027. (concat "<?" types-re ":"
  5028. "\\([^" org-non-link-chars " ]"
  5029. "[^\t\n\r]*\\)")
  5030. org-angle-link-re
  5031. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5032. types-re)
  5033. org-plain-link-re
  5034. (concat
  5035. "\\<" types-re ":"
  5036. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5037. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5038. org-bracket-link-regexp
  5039. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5040. org-bracket-link-analytic-regexp
  5041. (concat
  5042. "\\[\\["
  5043. "\\(" types-re ":\\)?"
  5044. "\\([^]]+\\)"
  5045. "\\]"
  5046. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5047. "\\]")
  5048. org-bracket-link-analytic-regexp++
  5049. (concat
  5050. "\\[\\["
  5051. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5052. "\\([^]]+\\)"
  5053. "\\]"
  5054. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5055. "\\]")
  5056. org-any-link-re
  5057. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5058. org-angle-link-re "\\)\\|\\("
  5059. org-plain-link-re "\\)"))))
  5060. (org-make-link-regexps)
  5061. (defvar org-emph-face nil)
  5062. (defun org-do-emphasis-faces (limit)
  5063. "Run through the buffer and emphasize strings."
  5064. (let (rtn a)
  5065. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5066. (let* ((border (char-after (match-beginning 3)))
  5067. (bre (regexp-quote (char-to-string border))))
  5068. (if (and (not (= border (char-after (match-beginning 4))))
  5069. (not (save-match-data
  5070. (string-match (concat bre ".*" bre)
  5071. (replace-regexp-in-string
  5072. "\n" " "
  5073. (substring (match-string 2) 1 -1))))))
  5074. (progn
  5075. (setq rtn t)
  5076. (setq a (assoc (match-string 3) org-emphasis-alist))
  5077. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5078. 'face
  5079. (nth 1 a))
  5080. (and (nth 2 a)
  5081. (org-remove-flyspell-overlays-in
  5082. (match-beginning 0) (match-end 0)))
  5083. (add-text-properties (match-beginning 2) (match-end 2)
  5084. '(font-lock-multiline t org-emphasis t))
  5085. (when org-hide-emphasis-markers
  5086. (add-text-properties (match-end 4) (match-beginning 5)
  5087. '(invisible org-link))
  5088. (add-text-properties (match-beginning 3) (match-end 3)
  5089. '(invisible org-link))))))
  5090. (goto-char (1+ (match-beginning 0))))
  5091. rtn))
  5092. (defun org-emphasize (&optional char)
  5093. "Insert or change an emphasis, i.e. a font like bold or italic.
  5094. If there is an active region, change that region to a new emphasis.
  5095. If there is no region, just insert the marker characters and position
  5096. the cursor between them.
  5097. CHAR should be the marker character. If it is a space, it means to
  5098. remove the emphasis of the selected region.
  5099. If CHAR is not given (for example in an interactive call) it will be
  5100. prompted for."
  5101. (interactive)
  5102. (let ((erc org-emphasis-regexp-components)
  5103. (prompt "")
  5104. (string "") beg end move c s)
  5105. (if (org-region-active-p)
  5106. (setq beg (region-beginning) end (region-end)
  5107. string (buffer-substring beg end))
  5108. (setq move t))
  5109. (unless char
  5110. (message "Emphasis marker or tag: [%s]"
  5111. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5112. (setq char (read-char-exclusive)))
  5113. (if (equal char ?\ )
  5114. (setq s "" move nil)
  5115. (unless (assoc (char-to-string char) org-emphasis-alist)
  5116. (user-error "No such emphasis marker: \"%c\"" char))
  5117. (setq s (char-to-string char)))
  5118. (while (and (> (length string) 1)
  5119. (equal (substring string 0 1) (substring string -1))
  5120. (assoc (substring string 0 1) org-emphasis-alist))
  5121. (setq string (substring string 1 -1)))
  5122. (setq string (concat s string s))
  5123. (if beg (delete-region beg end))
  5124. (unless (or (bolp)
  5125. (string-match (concat "[" (nth 0 erc) "\n]")
  5126. (char-to-string (char-before (point)))))
  5127. (insert " "))
  5128. (unless (or (eobp)
  5129. (string-match (concat "[" (nth 1 erc) "\n]")
  5130. (char-to-string (char-after (point)))))
  5131. (insert " ") (backward-char 1))
  5132. (insert string)
  5133. (and move (backward-char 1))))
  5134. (defconst org-nonsticky-props
  5135. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5136. (defsubst org-rear-nonsticky-at (pos)
  5137. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5138. (defun org-activate-plain-links (limit)
  5139. "Add link properties for plain links."
  5140. (let (f hl)
  5141. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5142. (not (member 'org-tag
  5143. (get-text-property (1- (match-beginning 0)) 'face)))
  5144. (not (org-in-src-block-p)))
  5145. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5146. (setq f (get-text-property (match-beginning 0) 'face))
  5147. (setq hl (org-match-string-no-properties 0))
  5148. (if (or (eq f 'org-tag)
  5149. (and (listp f) (memq 'org-tag f)))
  5150. nil
  5151. (add-text-properties (match-beginning 0) (match-end 0)
  5152. (list 'mouse-face 'highlight
  5153. 'face 'org-link
  5154. 'htmlize-link `(:uri ,hl)
  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. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5509. (progn
  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. (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. (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. (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 (org-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. (show-all)
  6182. (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. (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. (hide-subtree)
  6217. (org-reveal)
  6218. (cond
  6219. ((equal state "folded")
  6220. (hide-subtree))
  6221. ((equal state "children")
  6222. (org-show-hidden-entry)
  6223. (org-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. (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 (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. (org-show-children (1- arg))
  6271. (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. (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 (outline-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. (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. (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 (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. (hide-subtree)
  7561. (org-show-entry)
  7562. (org-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. (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. (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. (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-children
  8262. org-show-subtree
  8263. org-sort
  8264. org-up-element
  8265. outline-demote
  8266. outline-next-visible-heading
  8267. outline-previous-visible-heading
  8268. outline-promote
  8269. outline-up-heading))
  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. (defun org-link-try-special-completion (type)
  9383. "If there is completion support for link type TYPE, offer it."
  9384. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9385. (if (functionp fun)
  9386. (funcall fun)
  9387. (read-string "Link (no completion support): " (concat type ":")))))
  9388. (defun org-file-complete-link (&optional arg)
  9389. "Create a file link using completion."
  9390. (let ((file (org-iread-file-name "File: "))
  9391. (pwd (file-name-as-directory (expand-file-name ".")))
  9392. (pwd1 (file-name-as-directory (abbreviate-file-name
  9393. (expand-file-name ".")))))
  9394. (cond ((equal arg '(16))
  9395. (concat "file:"
  9396. (abbreviate-file-name (expand-file-name file))))
  9397. ((string-match
  9398. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9399. (concat "file:" (match-string 1 file)))
  9400. ((string-match
  9401. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9402. (expand-file-name file))
  9403. (concat "file:"
  9404. (match-string 1 (expand-file-name file))))
  9405. (t (concat "file:" file)))))
  9406. (defun org-iread-file-name (&rest args)
  9407. "Read-file-name using `ido-mode' speedup if available.
  9408. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9409. See `read-file-name' for a description of parameters."
  9410. (org-without-partial-completion
  9411. (if (and org-completion-use-ido
  9412. (fboundp 'ido-read-file-name)
  9413. (org-bound-and-true-p ido-mode)
  9414. (listp (nth 1 args)))
  9415. (let ((ido-enter-matching-directory nil))
  9416. (apply #'ido-read-file-name args))
  9417. (apply #'read-file-name args))))
  9418. (defun org-completing-read (&rest args)
  9419. "Completing-read with SPACE being a normal character."
  9420. (let ((enable-recursive-minibuffers t)
  9421. (minibuffer-local-completion-map
  9422. (copy-keymap minibuffer-local-completion-map)))
  9423. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9424. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9425. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9426. (apply 'org-icompleting-read args)))
  9427. (defun org-completing-read-no-i (&rest args)
  9428. (let (org-completion-use-ido org-completion-use-iswitchb)
  9429. (apply 'org-completing-read args)))
  9430. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9431. "Use iswitch as a completing-read replacement to choose from choices.
  9432. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9433. from."
  9434. (let* ((iswitchb-use-virtual-buffers nil)
  9435. (iswitchb-make-buflist-hook
  9436. (lambda ()
  9437. (setq iswitchb-temp-buflist choices))))
  9438. (iswitchb-read-buffer prompt)))
  9439. (defun org-icompleting-read (&rest args)
  9440. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9441. Should be called like `completing-read'."
  9442. (org-without-partial-completion
  9443. (if (not (listp (nth 1 args)))
  9444. ;; Ido only supports lists as the COLLECTION argument. Use
  9445. ;; default completion function when second argument is not
  9446. ;; a list.
  9447. (apply #'completing-read args)
  9448. (let ((ido-enter-matching-directory nil))
  9449. (apply (cond ((and org-completion-use-ido
  9450. (fboundp 'ido-completing-read)
  9451. (org-bound-and-true-p ido-mode))
  9452. #'ido-completing-read)
  9453. ((and org-completion-use-iswitchb
  9454. (org-bound-and-true-p iswitchb-mode))
  9455. #'org-iswitchb-completing-read)
  9456. (t #'completing-read))
  9457. args)))))
  9458. ;;; Opening/following a link
  9459. (defvar org-link-search-failed nil)
  9460. (defvar org-open-link-functions nil
  9461. "Hook for functions finding a plain text link.
  9462. These functions must take a single argument, the link content.
  9463. They will be called for links that look like [[link text][description]]
  9464. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9465. like a filename (e.g. \"./blue.png\").
  9466. These functions will be called *before* Org attempts to resolve the
  9467. link by doing text searches in the current buffer - so if you want a
  9468. link \"[[target]]\" to still find \"<<target>>\", your function should
  9469. handle this as a special case.
  9470. When the function does handle the link, it must return a non-nil value.
  9471. If it decides that it is not responsible for this link, it must return
  9472. nil to indicate that that Org-mode can continue with other options
  9473. like exact and fuzzy text search.")
  9474. (defun org-next-link (&optional search-backward)
  9475. "Move forward to the next link.
  9476. If the link is in hidden text, expose it."
  9477. (interactive "P")
  9478. (when (and org-link-search-failed (eq this-command last-command))
  9479. (goto-char (point-min))
  9480. (message "Link search wrapped back to beginning of buffer"))
  9481. (setq org-link-search-failed nil)
  9482. (let* ((pos (point))
  9483. (ct (org-context))
  9484. (a (assoc :link ct))
  9485. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9486. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9487. ((looking-at org-any-link-re)
  9488. ;; Don't stay stuck at link without an org-link face
  9489. (forward-char (if search-backward -1 1))))
  9490. (if (funcall srch-fun org-any-link-re nil t)
  9491. (progn
  9492. (goto-char (match-beginning 0))
  9493. (if (outline-invisible-p) (org-show-context)))
  9494. (goto-char pos)
  9495. (setq org-link-search-failed t)
  9496. (message "No further link found"))))
  9497. (defun org-previous-link ()
  9498. "Move backward to the previous link.
  9499. If the link is in hidden text, expose it."
  9500. (interactive)
  9501. (funcall 'org-next-link t))
  9502. (defun org-translate-link (s)
  9503. "Translate a link string if a translation function has been defined."
  9504. (with-temp-buffer
  9505. (insert (org-trim s))
  9506. (org-trim (org-element-interpret-data (org-element-context)))))
  9507. (defun org-translate-link-from-planner (type path)
  9508. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9509. This is still an experimental function, your mileage may vary."
  9510. (cond
  9511. ((member type '("http" "https" "news" "ftp"))
  9512. ;; standard Internet links are the same.
  9513. nil)
  9514. ((and (equal type "irc") (string-match "^//" path))
  9515. ;; Planner has two / at the beginning of an irc link, we have 1.
  9516. ;; We should have zero, actually....
  9517. (setq path (substring path 1)))
  9518. ((and (equal type "lisp") (string-match "^/" path))
  9519. ;; Planner has a slash, we do not.
  9520. (setq type "elisp" path (substring path 1)))
  9521. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9522. ;; A typical message link. Planner has the id after the final slash,
  9523. ;; we separate it with a hash mark
  9524. (setq path (concat (match-string 1 path) "#"
  9525. (org-remove-angle-brackets (match-string 2 path))))))
  9526. (cons type path))
  9527. (defun org-find-file-at-mouse (ev)
  9528. "Open file link or URL at mouse."
  9529. (interactive "e")
  9530. (mouse-set-point ev)
  9531. (org-open-at-point 'in-emacs))
  9532. (defun org-open-at-mouse (ev)
  9533. "Open file link or URL at mouse.
  9534. See the docstring of `org-open-file' for details."
  9535. (interactive "e")
  9536. (mouse-set-point ev)
  9537. (if (eq major-mode 'org-agenda-mode)
  9538. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9539. (org-open-at-point))
  9540. (defvar org-window-config-before-follow-link nil
  9541. "The window configuration before following a link.
  9542. This is saved in case the need arises to restore it.")
  9543. ;;;###autoload
  9544. (defun org-open-at-point-global ()
  9545. "Follow a link like Org-mode does.
  9546. This command can be called in any mode to follow a link that has
  9547. Org-mode syntax."
  9548. (interactive)
  9549. (org-run-like-in-org-mode 'org-open-at-point))
  9550. ;;;###autoload
  9551. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9552. "Open a link in the string S, as if it was in Org-mode."
  9553. (interactive "sLink: \nP")
  9554. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9555. (with-temp-buffer
  9556. (let ((org-inhibit-startup (not reference-buffer)))
  9557. (org-mode)
  9558. (insert s)
  9559. (goto-char (point-min))
  9560. (when reference-buffer
  9561. (setq org-link-abbrev-alist-local
  9562. (with-current-buffer reference-buffer
  9563. org-link-abbrev-alist-local)))
  9564. (org-open-at-point arg reference-buffer)))))
  9565. (defvar org-open-at-point-functions nil
  9566. "Hook that is run when following a link at point.
  9567. Functions in this hook must return t if they identify and follow
  9568. a link at point. If they don't find anything interesting at point,
  9569. they must return nil.")
  9570. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9571. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9572. (defun org-open-at-point (&optional arg reference-buffer)
  9573. "Open link, timestamp, footnote or tags at point.
  9574. When point is on a link, follow it. Normally, files will be
  9575. opened by an appropriate application. If the optional prefix
  9576. argument ARG is non-nil, Emacs will visit the file. With
  9577. a double prefix argument, try to open outside of Emacs, in the
  9578. application the system uses for this file type.
  9579. When point is on a timestamp, open the agenda at the day
  9580. specified.
  9581. When point is a footnote definition, move to the first reference
  9582. found. If it is on a reference, move to the associated
  9583. definition.
  9584. When point is on a headline, display a list of every link in the
  9585. entry, so it is possible to pick one, or all, of them. If point
  9586. is on a tag, call `org-tags-view' instead.
  9587. When optional argument REFERENCE-BUFFER is non-nil, it should
  9588. specify a buffer from where the link search should happen. This
  9589. is used internally by `org-open-link-from-string'.
  9590. On top of syntactically correct links, this function will open
  9591. the link at point in comments or comment blocks and the first
  9592. link in a property drawer line."
  9593. (interactive "P")
  9594. ;; On a code block, open block's results.
  9595. (unless (call-interactively 'org-babel-open-src-block-result)
  9596. (org-load-modules-maybe)
  9597. (setq org-window-config-before-follow-link (current-window-configuration))
  9598. (org-remove-occur-highlights nil nil t)
  9599. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9600. (let* ((context
  9601. ;; Only consider supported types, even if they are not
  9602. ;; the closest one.
  9603. (org-element-lineage
  9604. (org-element-context)
  9605. '(clock comment comment-block footnote-definition
  9606. footnote-reference headline inlinetask keyword link
  9607. node-property timestamp)
  9608. t))
  9609. (type (org-element-type context))
  9610. (value (org-element-property :value context)))
  9611. (cond
  9612. ((not context) (user-error "No link found"))
  9613. ;; Exception: open timestamps and links in properties
  9614. ;; drawers, keywords and comments.
  9615. ((memq type '(comment comment-block keyword node-property))
  9616. (cond ((org-in-regexp org-any-link-re)
  9617. (org-open-link-from-string (match-string-no-properties 0)))
  9618. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9619. (org-follow-timestamp-link))
  9620. (t (user-error "No link found"))))
  9621. ;; On a headline or an inlinetask, but not on a timestamp,
  9622. ;; a link, a footnote reference or on tags.
  9623. ((and (memq type '(headline inlinetask))
  9624. ;; Not on tags.
  9625. (progn (save-excursion (beginning-of-line)
  9626. (looking-at org-complex-heading-regexp))
  9627. (or (not (match-beginning 5))
  9628. (< (point) (match-beginning 5)))))
  9629. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9630. (links (car data))
  9631. (links-end (cdr data)))
  9632. (if links
  9633. (dolist (link (if (stringp links) (list links) links))
  9634. (search-forward link nil links-end)
  9635. (goto-char (match-beginning 0))
  9636. (org-open-at-point))
  9637. (require 'org-attach)
  9638. (org-attach-reveal 'if-exists))))
  9639. ;; On a clock line, make sure point is on the timestamp
  9640. ;; before opening it.
  9641. ((and (eq type 'clock)
  9642. value
  9643. (>= (point) (org-element-property :begin value))
  9644. (<= (point) (org-element-property :end value)))
  9645. (org-follow-timestamp-link))
  9646. ;; Do nothing on white spaces after an object, unless point
  9647. ;; is right after it.
  9648. ((> (point)
  9649. (save-excursion
  9650. (goto-char (org-element-property :end context))
  9651. (skip-chars-backward " \t")
  9652. (point)))
  9653. (user-error "No link found"))
  9654. ((eq type 'timestamp) (org-follow-timestamp-link))
  9655. ;; On tags within a headline or an inlinetask.
  9656. ((and (memq type '(headline inlinetask))
  9657. (progn (save-excursion (beginning-of-line)
  9658. (looking-at org-complex-heading-regexp))
  9659. (and (match-beginning 5)
  9660. (>= (point) (match-beginning 5)))))
  9661. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9662. ((eq type 'link)
  9663. ;; When link is located within the description of another
  9664. ;; link (e.g., an inline image), always open the parent
  9665. ;; link.
  9666. (let*((link (let ((up (org-element-property :parent context)))
  9667. (if (eq (org-element-type up) 'link) up context)))
  9668. (type (org-element-property :type link))
  9669. (path (org-link-unescape (org-element-property :path link))))
  9670. ;; Switch back to REFERENCE-BUFFER needed when called in
  9671. ;; a temporary buffer through `org-open-link-from-string'.
  9672. (with-current-buffer (or reference-buffer (current-buffer))
  9673. (cond
  9674. ((equal type "file")
  9675. (if (string-match "[*?{]" (file-name-nondirectory path))
  9676. (dired path)
  9677. ;; Look into `org-link-protocols' in order to find
  9678. ;; a DEDICATED-FUNCTION to open file. The function
  9679. ;; will be applied on raw link instead of parsed
  9680. ;; link due to the limitation in `org-add-link-type'
  9681. ;; ("open" function called with a single argument).
  9682. ;; If no such function is found, fallback to
  9683. ;; `org-open-file'.
  9684. ;;
  9685. ;; Note : "file+emacs" and "file+sys" types are
  9686. ;; hard-coded in order to escape the previous
  9687. ;; limitation.
  9688. (let* ((option (org-element-property :search-option link))
  9689. (app (org-element-property :application link))
  9690. (dedicated-function
  9691. (nth 1 (assoc app org-link-protocols))))
  9692. (if dedicated-function
  9693. (funcall dedicated-function
  9694. (concat path
  9695. (and option (concat "::" option))))
  9696. (apply #'org-open-file
  9697. path
  9698. (cond (arg)
  9699. ((equal app "emacs") 'emacs)
  9700. ((equal app "sys") 'system))
  9701. (cond ((not option) nil)
  9702. ((org-string-match-p "\\`[0-9]+\\'" option)
  9703. (list (string-to-number option)))
  9704. (t (list nil
  9705. (org-link-unescape option)))))))))
  9706. ((assoc type org-link-protocols)
  9707. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9708. ((equal type "help")
  9709. (let ((f-or-v (intern path)))
  9710. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9711. ((boundp f-or-v) (describe-variable f-or-v))
  9712. (t (error "Not a known function or variable")))))
  9713. ((member type '("http" "https" "ftp" "mailto" "news"))
  9714. (browse-url (org-link-escape-browser (concat type ":" path))))
  9715. ((equal type "doi")
  9716. (browse-url
  9717. (org-link-escape-browser (concat org-doi-server-url path))))
  9718. ((equal type "message") (browse-url (concat type ":" path)))
  9719. ((equal type "shell")
  9720. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9721. (cmd path))
  9722. (if (or (and (org-string-nw-p
  9723. org-confirm-shell-link-not-regexp)
  9724. (string-match
  9725. org-confirm-shell-link-not-regexp cmd))
  9726. (not org-confirm-shell-link-function)
  9727. (funcall org-confirm-shell-link-function
  9728. (format "Execute \"%s\" in shell? "
  9729. (org-add-props cmd nil
  9730. 'face 'org-warning))))
  9731. (progn
  9732. (message "Executing %s" cmd)
  9733. (shell-command cmd buf)
  9734. (when (featurep 'midnight)
  9735. (setq clean-buffer-list-kill-buffer-names
  9736. (cons (buffer-name buf)
  9737. clean-buffer-list-kill-buffer-names))))
  9738. (user-error "Abort"))))
  9739. ((equal type "elisp")
  9740. (let ((cmd path))
  9741. (if (or (and (org-string-nw-p
  9742. org-confirm-elisp-link-not-regexp)
  9743. (org-string-match-p
  9744. org-confirm-elisp-link-not-regexp cmd))
  9745. (not org-confirm-elisp-link-function)
  9746. (funcall org-confirm-elisp-link-function
  9747. (format "Execute \"%s\" as elisp? "
  9748. (org-add-props cmd nil
  9749. 'face 'org-warning))))
  9750. (message "%s => %s" cmd
  9751. (if (eq (string-to-char cmd) ?\()
  9752. (eval (read cmd))
  9753. (call-interactively (read cmd))))
  9754. (user-error "Abort"))))
  9755. ((equal type "id")
  9756. (require 'org-id)
  9757. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9758. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9759. (unless (run-hook-with-args-until-success
  9760. 'org-open-link-functions path)
  9761. (if (not arg) (org-mark-ring-push)
  9762. (switch-to-buffer-other-window
  9763. (org-get-buffer-for-internal-link (current-buffer))))
  9764. (let ((destination
  9765. (org-with-wide-buffer
  9766. (if (equal type "radio")
  9767. (org-search-radio-target
  9768. (org-element-property :path link))
  9769. (org-link-search
  9770. (if (member type '("custom-id" "coderef"))
  9771. (org-element-property :raw-link link)
  9772. path)
  9773. ;; Prevent fuzzy links from matching
  9774. ;; themselves.
  9775. (and (equal type "fuzzy")
  9776. (+ 2 (org-element-property :begin link)))))
  9777. (point))))
  9778. (unless (and (<= (point-min) destination)
  9779. (>= (point-max) destination))
  9780. (widen))
  9781. (goto-char destination))))
  9782. (t (browse-url-at-point))))))
  9783. ;; On a footnote reference or at a footnote definition's label.
  9784. ((or (eq type 'footnote-reference)
  9785. (and (eq type 'footnote-definition)
  9786. (save-excursion
  9787. ;; Do not validate action when point is on the
  9788. ;; spaces right after the footnote label, in
  9789. ;; order to be on par with behaviour on links.
  9790. (skip-chars-forward " \t")
  9791. (let ((begin
  9792. (org-element-property :contents-begin context)))
  9793. (if begin (< (point) begin)
  9794. (= (org-element-property :post-affiliated context)
  9795. (line-beginning-position)))))))
  9796. (org-footnote-action))
  9797. (t (user-error "No link found")))))
  9798. (run-hook-with-args 'org-follow-link-hook)))
  9799. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9800. "Offer links in the current entry and return the selected link.
  9801. If there is only one link, return it.
  9802. If NTH is an integer, return the NTH link found.
  9803. If ZERO is a string, check also this string for a link, and if
  9804. there is one, return it."
  9805. (with-current-buffer buffer
  9806. (save-excursion
  9807. (save-restriction
  9808. (widen)
  9809. (goto-char marker)
  9810. (let ((cnt ?0)
  9811. (in-emacs (if (integerp nth) nil nth))
  9812. have-zero end links link c)
  9813. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9814. (push (match-string 0 zero) links)
  9815. (setq cnt (1- cnt) have-zero t))
  9816. (save-excursion
  9817. (org-back-to-heading t)
  9818. (setq end (save-excursion (outline-next-heading) (point)))
  9819. (while (re-search-forward org-any-link-re end t)
  9820. (push (match-string 0) links))
  9821. (setq links (org-uniquify (reverse links))))
  9822. (cond
  9823. ((null links)
  9824. (message "No links"))
  9825. ((equal (length links) 1)
  9826. (setq link (car links)))
  9827. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9828. (setq link (nth (if have-zero nth (1- nth)) links)))
  9829. (t ; we have to select a link
  9830. (save-excursion
  9831. (save-window-excursion
  9832. (delete-other-windows)
  9833. (with-output-to-temp-buffer "*Select Link*"
  9834. (mapc (lambda (l)
  9835. (if (not (string-match org-bracket-link-regexp l))
  9836. (princ (format "[%c] %s\n" (incf cnt)
  9837. (org-remove-angle-brackets l)))
  9838. (if (match-end 3)
  9839. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9840. (match-string 3 l) (match-string 1 l)))
  9841. (princ (format "[%c] %s\n" (incf cnt)
  9842. (match-string 1 l))))))
  9843. links))
  9844. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9845. (message "Select link to open, RET to open all:")
  9846. (setq c (read-char-exclusive))
  9847. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9848. (when (equal c ?q) (user-error "Abort"))
  9849. (if (equal c ?\C-m)
  9850. (setq link links)
  9851. (setq nth (- c ?0))
  9852. (if have-zero (setq nth (1+ nth)))
  9853. (unless (and (integerp nth) (>= (length links) nth))
  9854. (user-error "Invalid link selection"))
  9855. (setq link (nth (1- nth) links)))))
  9856. (cons link end))))))
  9857. ;; TODO: These functions are deprecated since `org-open-at-point'
  9858. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9859. (defun org-open-file-with-system (path)
  9860. "Open file at PATH using the system way of opening it."
  9861. (org-open-file path 'system))
  9862. (defun org-open-file-with-emacs (path)
  9863. "Open file at PATH in Emacs."
  9864. (org-open-file path 'emacs))
  9865. ;;; File search
  9866. (defvar org-create-file-search-functions nil
  9867. "List of functions to construct the right search string for a file link.
  9868. These functions are called in turn with point at the location to
  9869. which the link should point.
  9870. A function in the hook should first test if it would like to
  9871. handle this file type, for example by checking the `major-mode'
  9872. or the file extension. If it decides not to handle this file, it
  9873. should just return nil to give other functions a chance. If it
  9874. does handle the file, it must return the search string to be used
  9875. when following the link. The search string will be part of the
  9876. file link, given after a double colon, and `org-open-at-point'
  9877. will automatically search for it. If special measures must be
  9878. taken to make the search successful, another function should be
  9879. added to the companion hook `org-execute-file-search-functions',
  9880. which see.
  9881. A function in this hook may also use `setq' to set the variable
  9882. `description' to provide a suggestion for the descriptive text to
  9883. be used for this link when it gets inserted into an Org-mode
  9884. buffer with \\[org-insert-link].")
  9885. (defvar org-execute-file-search-functions nil
  9886. "List of functions to execute a file search triggered by a link.
  9887. Functions added to this hook must accept a single argument, the
  9888. search string that was part of the file link, the part after the
  9889. double colon. The function must first check if it would like to
  9890. handle this search, for example by checking the `major-mode' or
  9891. the file extension. If it decides not to handle this search, it
  9892. should just return nil to give other functions a chance. If it
  9893. does handle the search, it must return a non-nil value to keep
  9894. other functions from trying.
  9895. Each function can access the current prefix argument through the
  9896. variable `current-prefix-arg'. Note that a single prefix is used
  9897. to force opening a link in Emacs, so it may be good to only use a
  9898. numeric or double prefix to guide the search function.
  9899. In case this is needed, a function in this hook can also restore
  9900. the window configuration before `org-open-at-point' was called using:
  9901. (set-window-configuration org-window-config-before-follow-link)")
  9902. (defun org-search-radio-target (target)
  9903. "Search a radio target matching TARGET in current buffer.
  9904. White spaces are not significant."
  9905. (let ((re (format "<<<%s>>>"
  9906. (mapconcat #'regexp-quote
  9907. (org-split-string target "[ \t\n]+")
  9908. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9909. (origin (point)))
  9910. (goto-char (point-min))
  9911. (catch :radio-match
  9912. (while (re-search-forward re nil t)
  9913. (backward-char)
  9914. (let ((object (org-element-context)))
  9915. (when (eq (org-element-type object) 'radio-target)
  9916. (goto-char (org-element-property :begin object))
  9917. (org-show-context 'link-search)
  9918. (throw :radio-match nil))))
  9919. (goto-char origin)
  9920. (user-error "No match for radio target: %s" target))))
  9921. (defun org-link-search (s &optional avoid-pos stealth)
  9922. "Search for a search string S.
  9923. If S starts with \"#\", it triggers a custom ID search.
  9924. If S is enclosed within parenthesis, it initiates a coderef
  9925. search.
  9926. If S is surrounded by forward slashes, it is interpreted as
  9927. a regular expression. In Org mode files, this will create an
  9928. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9929. to list matches. If the current buffer is in `dired-mode', grep
  9930. will be used to search in all files.
  9931. When AVOID-POS is given, ignore matches near that position.
  9932. When optional argument STEALTH is non-nil, do not modify
  9933. visibility around point, thus ignoring `org-show-context-detail'
  9934. variable.
  9935. Search is case-insensitive and ignores white spaces. Return type
  9936. of matched result, with is either `dedicated' or `fuzzy'."
  9937. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9938. (let* ((case-fold-search t)
  9939. (origin (point))
  9940. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9941. (words (org-split-string s "[ \t\n]+"))
  9942. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9943. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9944. type)
  9945. (cond
  9946. ;; Check if there are any special search functions.
  9947. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9948. ((eq (string-to-char s) ?#)
  9949. ;; Look for a custom ID S if S starts with "#".
  9950. (let* ((id (substring normalized 1))
  9951. (match (org-find-property "CUSTOM_ID" id)))
  9952. (if match (progn (goto-char match) (setf type 'dedicated))
  9953. (error "No match for custom ID: %s" id))))
  9954. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9955. ;; Look for coderef targets if S is enclosed within parenthesis.
  9956. (let ((coderef (match-string-no-properties 1 normalized))
  9957. (re (substring s-single-re 1 -1)))
  9958. (goto-char (point-min))
  9959. (catch :coderef-match
  9960. (while (re-search-forward re nil t)
  9961. (let ((element (org-element-at-point)))
  9962. (when (and (memq (org-element-type element)
  9963. '(example-block src-block))
  9964. ;; Build proper regexp according to current
  9965. ;; block's label format.
  9966. (let ((label-fmt
  9967. (regexp-quote
  9968. (or (org-element-property :label-fmt element)
  9969. org-coderef-label-format))))
  9970. (save-excursion
  9971. (beginning-of-line)
  9972. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9973. (format label-fmt coderef))))))
  9974. (setq type 'dedicated)
  9975. (goto-char (match-beginning 1))
  9976. (throw :coderef-match nil))))
  9977. (goto-char origin)
  9978. (error "No match for coderef: %s" coderef))))
  9979. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9980. ;; Look for a regular expression.
  9981. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9982. (match-string 1 s)))
  9983. ;; Fuzzy links.
  9984. (t
  9985. (let* ((starred (eq (string-to-char normalized) ?*))
  9986. (headline-search (and (derived-mode-p 'org-mode)
  9987. (or org-link-search-must-match-exact-headline
  9988. starred))))
  9989. (cond
  9990. ;; Look for targets, only if not in a headline search.
  9991. ((and (not starred)
  9992. (let ((target (format "<<%s>>" s-multi-re)))
  9993. (catch :target-match
  9994. (goto-char (point-min))
  9995. (while (re-search-forward target nil t)
  9996. (backward-char)
  9997. (let ((context (org-element-context)))
  9998. (when (eq (org-element-type context) 'target)
  9999. (setq type 'dedicated)
  10000. (goto-char (org-element-property :begin context))
  10001. (throw :target-match t))))
  10002. nil))))
  10003. ;; Look for elements named after S, only if not in a headline
  10004. ;; search.
  10005. ((and (not starred)
  10006. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  10007. (catch :name-match
  10008. (goto-char (point-min))
  10009. (while (re-search-forward name nil t)
  10010. (let ((element (org-element-at-point)))
  10011. (when (equal (org-split-string
  10012. (org-element-property :name element)
  10013. "[ \t]+")
  10014. words)
  10015. (setq type 'dedicated)
  10016. (beginning-of-line)
  10017. (throw :name-match t))))
  10018. nil))))
  10019. ;; Regular text search. Prefer headlines in Org mode
  10020. ;; buffers.
  10021. ((and (derived-mode-p 'org-mode)
  10022. (let* ((wspace "[ \t]")
  10023. (wspaceopt (concat wspace "*"))
  10024. (cookie (concat "\\(?:"
  10025. wspaceopt
  10026. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10027. wspaceopt
  10028. "\\)"))
  10029. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10030. (re (concat
  10031. org-outline-regexp-bol
  10032. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10033. "\\(?:\\[#.\\][ \t]+\\)?"
  10034. "\\(?:" org-comment-string "[ \t]+\\)?"
  10035. sep "?"
  10036. (let ((title (mapconcat #'regexp-quote
  10037. words
  10038. sep)))
  10039. (if starred (substring title 1) title))
  10040. sep "?"
  10041. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10042. "[ \t]*$")))
  10043. (goto-char (point-min))
  10044. (re-search-forward re nil t)))
  10045. (goto-char (match-beginning 0))
  10046. (setq type 'dedicated))
  10047. ;; Offer to create non-existent headline depending on
  10048. ;; `org-link-search-must-match-exact-headline'.
  10049. ((and (derived-mode-p 'org-mode)
  10050. (not org-link-search-inhibit-query)
  10051. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10052. (yes-or-no-p "No match - create this as a new heading? "))
  10053. (goto-char (point-max))
  10054. (unless (bolp) (newline))
  10055. (org-insert-heading nil t t)
  10056. (insert s "\n")
  10057. (beginning-of-line 0))
  10058. ;; Only headlines are looked after. No need to process
  10059. ;; further: throw an error.
  10060. ((and (derived-mode-p 'org-mode)
  10061. (or starred org-link-search-must-match-exact-headline))
  10062. (goto-char origin)
  10063. (error "No match for fuzzy expression: %s" normalized))
  10064. ;; Regular text search.
  10065. ((catch :fuzzy-match
  10066. (goto-char (point-min))
  10067. (while (re-search-forward s-multi-re nil t)
  10068. ;; Skip match if it contains AVOID-POS or it is included
  10069. ;; in a link with a description but outside the
  10070. ;; description.
  10071. (unless (or (and avoid-pos
  10072. (<= (match-beginning 0) avoid-pos)
  10073. (> (match-end 0) avoid-pos))
  10074. (and (save-match-data
  10075. (org-in-regexp org-bracket-link-regexp))
  10076. (match-beginning 3)
  10077. (or (> (match-beginning 3) (point))
  10078. (<= (match-end 3) (point)))
  10079. (org-element-lineage
  10080. (save-match-data (org-element-context))
  10081. '(link) t)))
  10082. (goto-char (match-beginning 0))
  10083. (setq type 'fuzzy)
  10084. (throw :fuzzy-match t)))
  10085. nil))
  10086. ;; All failed. Throw an error.
  10087. (t (goto-char origin)
  10088. (error "No match for fuzzy expression: %s" normalized))))))
  10089. ;; Disclose surroundings of match, if appropriate.
  10090. (when (and (derived-mode-p 'org-mode) (not stealth))
  10091. (org-show-context 'link-search))
  10092. type))
  10093. (defun org-get-buffer-for-internal-link (buffer)
  10094. "Return a buffer to be used for displaying the link target of internal links."
  10095. (cond
  10096. ((not org-display-internal-link-with-indirect-buffer)
  10097. buffer)
  10098. ((string-match "(Clone)$" (buffer-name buffer))
  10099. (message "Buffer is already a clone, not making another one")
  10100. ;; we also do not modify visibility in this case
  10101. buffer)
  10102. (t ; make a new indirect buffer for displaying the link
  10103. (let* ((bn (buffer-name buffer))
  10104. (ibn (concat bn "(Clone)"))
  10105. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10106. (with-current-buffer ib (org-overview))
  10107. ib))))
  10108. (defun org-do-occur (regexp &optional cleanup)
  10109. "Call the Emacs command `occur'.
  10110. If CLEANUP is non-nil, remove the printout of the regular expression
  10111. in the *Occur* buffer. This is useful if the regex is long and not useful
  10112. to read."
  10113. (occur regexp)
  10114. (when cleanup
  10115. (let ((cwin (selected-window)) win beg end)
  10116. (when (setq win (get-buffer-window "*Occur*"))
  10117. (select-window win))
  10118. (goto-char (point-min))
  10119. (when (re-search-forward "match[a-z]+" nil t)
  10120. (setq beg (match-end 0))
  10121. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10122. (setq end (1- (match-beginning 0)))))
  10123. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10124. (goto-char (point-min))
  10125. (select-window cwin))))
  10126. ;;; The mark ring for links jumps
  10127. (defvar org-mark-ring nil
  10128. "Mark ring for positions before jumps in Org-mode.")
  10129. (defvar org-mark-ring-last-goto nil
  10130. "Last position in the mark ring used to go back.")
  10131. ;; Fill and close the ring
  10132. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10133. (loop for i from 1 to org-mark-ring-length do
  10134. (push (make-marker) org-mark-ring))
  10135. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10136. org-mark-ring)
  10137. (defun org-mark-ring-push (&optional pos buffer)
  10138. "Put the current position or POS into the mark ring and rotate it."
  10139. (interactive)
  10140. (setq pos (or pos (point)))
  10141. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10142. (move-marker (car org-mark-ring)
  10143. (or pos (point))
  10144. (or buffer (current-buffer)))
  10145. (message "%s"
  10146. (substitute-command-keys
  10147. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10148. (defun org-mark-ring-goto (&optional n)
  10149. "Jump to the previous position in the mark ring.
  10150. With prefix arg N, jump back that many stored positions. When
  10151. called several times in succession, walk through the entire ring.
  10152. Org-mode commands jumping to a different position in the current file,
  10153. or to another Org-mode file, automatically push the old position
  10154. onto the ring."
  10155. (interactive "p")
  10156. (let (p m)
  10157. (if (eq last-command this-command)
  10158. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10159. (setq p org-mark-ring))
  10160. (setq org-mark-ring-last-goto p)
  10161. (setq m (car p))
  10162. (org-pop-to-buffer-same-window (marker-buffer m))
  10163. (goto-char m)
  10164. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10165. (defun org-remove-angle-brackets (s)
  10166. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10167. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10168. s)
  10169. (defun org-add-angle-brackets (s)
  10170. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10171. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10172. s)
  10173. (defun org-remove-double-quotes (s)
  10174. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10175. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10176. s)
  10177. ;;; Following specific links
  10178. (defun org-follow-timestamp-link ()
  10179. "Open an agenda view for the time-stamp date/range at point."
  10180. (cond
  10181. ((org-at-date-range-p t)
  10182. (let ((org-agenda-start-on-weekday)
  10183. (t1 (match-string 1))
  10184. (t2 (match-string 2)) tt1 tt2)
  10185. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10186. tt2 (time-to-days (org-time-string-to-time t2)))
  10187. (let ((org-agenda-buffer-tmp-name
  10188. (format "*Org Agenda(a:%s)"
  10189. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10190. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10191. ((org-at-timestamp-p t)
  10192. (let ((org-agenda-buffer-tmp-name
  10193. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10194. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10195. (substring (match-string 1) 0 10)))
  10196. 1)))
  10197. (t (error "This should not happen"))))
  10198. ;;; Following file links
  10199. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10200. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10201. (declare-function mailcap-mime-info
  10202. "mailcap" (string &optional request no-decode))
  10203. (defvar org-wait nil)
  10204. (defun org-open-file (path &optional in-emacs line search)
  10205. "Open the file at PATH.
  10206. First, this expands any special file name abbreviations. Then the
  10207. configuration variable `org-file-apps' is checked if it contains an
  10208. entry for this file type, and if yes, the corresponding command is launched.
  10209. If no application is found, Emacs simply visits the file.
  10210. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10211. With a double \\[universal-argument] \\[universal-argument] \
  10212. prefix arg, Org tries to avoid opening in Emacs
  10213. and to use an external application to visit the file.
  10214. Optional LINE specifies a line to go to, optional SEARCH a string
  10215. to search for. If LINE or SEARCH is given, the file will be
  10216. opened in Emacs, unless an entry from org-file-apps that makes
  10217. use of groups in a regexp matches.
  10218. If you want to change the way frames are used when following a
  10219. link, please customize `org-link-frame-setup'.
  10220. If the file does not exist, an error is thrown."
  10221. (let* ((file (if (equal path "")
  10222. buffer-file-name
  10223. (substitute-in-file-name (expand-file-name path))))
  10224. (file-apps (append org-file-apps (org-default-apps)))
  10225. (apps (org-remove-if
  10226. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10227. (apps-dlink (org-remove-if-not
  10228. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10229. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10230. (dirp (if remp nil (file-directory-p file)))
  10231. (file (if (and dirp org-open-directory-means-index-dot-org)
  10232. (concat (file-name-as-directory file) "index.org")
  10233. file))
  10234. (a-m-a-p (assq 'auto-mode apps))
  10235. (dfile (downcase file))
  10236. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10237. (link (cond ((and (eq line nil)
  10238. (eq search nil))
  10239. file)
  10240. (line
  10241. (concat file "::" (number-to-string line)))
  10242. (search
  10243. (concat file "::" search))))
  10244. (dlink (downcase link))
  10245. (old-buffer (current-buffer))
  10246. (old-pos (point))
  10247. (old-mode major-mode)
  10248. ext cmd link-match-data)
  10249. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10250. (setq ext (match-string 1 dfile))
  10251. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10252. (setq ext (match-string 1 dfile))))
  10253. (cond
  10254. ((member in-emacs '((16) system))
  10255. (setq cmd (cdr (assoc 'system apps))))
  10256. (in-emacs (setq cmd 'emacs))
  10257. (t
  10258. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10259. (and dirp (cdr (assoc 'directory apps)))
  10260. ; first, try matching against apps-dlink
  10261. ; if we get a match here, store the match data for later
  10262. (let ((match (assoc-default dlink apps-dlink
  10263. 'string-match)))
  10264. (if match
  10265. (progn (setq link-match-data (match-data))
  10266. match)
  10267. (progn (setq in-emacs (or in-emacs line search))
  10268. nil))) ; if we have no match in apps-dlink,
  10269. ; always open the file in emacs if line or search
  10270. ; is given (for backwards compatibility)
  10271. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10272. 'string-match)
  10273. (cdr (assoc ext apps))
  10274. (cdr (assoc t apps))))))
  10275. (when (eq cmd 'system)
  10276. (setq cmd (cdr (assoc 'system apps))))
  10277. (when (eq cmd 'default)
  10278. (setq cmd (cdr (assoc t apps))))
  10279. (when (eq cmd 'mailcap)
  10280. (require 'mailcap)
  10281. (mailcap-parse-mailcaps)
  10282. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10283. (command (mailcap-mime-info mime-type)))
  10284. (if (stringp command)
  10285. (setq cmd command)
  10286. (setq cmd 'emacs))))
  10287. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10288. (not (file-exists-p file))
  10289. (not org-open-non-existing-files))
  10290. (user-error "No such file: %s" file))
  10291. (cond
  10292. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10293. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10294. (while (string-match "['\"]%s['\"]" cmd)
  10295. (setq cmd (replace-match "%s" t t cmd)))
  10296. (while (string-match "%s" cmd)
  10297. (setq cmd (replace-match
  10298. (save-match-data
  10299. (shell-quote-argument
  10300. (convert-standard-filename file)))
  10301. t t cmd)))
  10302. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10303. (save-match-data
  10304. (let ((match-index 1)
  10305. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10306. (set-match-data link-match-data)
  10307. (while (<= match-index number-of-groups)
  10308. (let ((regex (concat "%" (number-to-string match-index)))
  10309. (replace-with (match-string match-index dlink)))
  10310. (while (string-match regex cmd)
  10311. (setq cmd (replace-match replace-with t t cmd))))
  10312. (setq match-index (+ match-index 1)))))
  10313. (save-window-excursion
  10314. (message "Running %s...done" cmd)
  10315. (start-process-shell-command cmd nil cmd)
  10316. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10317. ((or (stringp cmd)
  10318. (eq cmd 'emacs))
  10319. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10320. (widen)
  10321. (if line (progn (org-goto-line line)
  10322. (if (derived-mode-p 'org-mode)
  10323. (org-reveal)))
  10324. (if search (org-link-search search))))
  10325. ((consp cmd)
  10326. (let ((file (convert-standard-filename file)))
  10327. (save-match-data
  10328. (set-match-data link-match-data)
  10329. (eval cmd))))
  10330. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10331. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10332. (or (not (equal old-buffer (current-buffer)))
  10333. (not (equal old-pos (point))))
  10334. (org-mark-ring-push old-pos old-buffer))))
  10335. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10336. "This function returns non-nil if `entry' uses a regular
  10337. expression which should be matched against the whole link by
  10338. org-open-file.
  10339. It assumes that is the case when the entry uses a regular
  10340. expression which has at least one grouping construct and the
  10341. action is either a lisp form or a command string containing
  10342. `%1', i.e. using at least one subexpression match as a
  10343. parameter."
  10344. (let ((selector (car entry))
  10345. (action (cdr entry)))
  10346. (if (stringp selector)
  10347. (and (> (regexp-opt-depth selector) 0)
  10348. (or (and (stringp action)
  10349. (string-match "%[0-9]" action))
  10350. (consp action)))
  10351. nil)))
  10352. (defun org-default-apps ()
  10353. "Return the default applications for this operating system."
  10354. (cond
  10355. ((eq system-type 'darwin)
  10356. org-file-apps-defaults-macosx)
  10357. ((eq system-type 'windows-nt)
  10358. org-file-apps-defaults-windowsnt)
  10359. (t org-file-apps-defaults-gnu)))
  10360. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10361. "Convert extensions to regular expressions in the cars of LIST.
  10362. Also, weed out any non-string entries, because the return value is used
  10363. only for regexp matching.
  10364. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10365. point to the symbol `emacs', indicating that the file should
  10366. be opened in Emacs."
  10367. (append
  10368. (delq nil
  10369. (mapcar (lambda (x)
  10370. (if (not (stringp (car x)))
  10371. nil
  10372. (if (string-match "\\W" (car x))
  10373. x
  10374. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10375. list))
  10376. (if add-auto-mode
  10377. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10378. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10379. (defun org-file-remote-p (file)
  10380. "Test whether FILE specifies a location on a remote system.
  10381. Return non-nil if the location is indeed remote.
  10382. For example, the filename \"/user@host:/foo\" specifies a location
  10383. on the system \"/user@host:\"."
  10384. (cond ((fboundp 'file-remote-p)
  10385. (file-remote-p file))
  10386. ((fboundp 'tramp-handle-file-remote-p)
  10387. (tramp-handle-file-remote-p file))
  10388. ((and (boundp 'ange-ftp-name-format)
  10389. (string-match (car ange-ftp-name-format) file))
  10390. t)))
  10391. ;;;; Refiling
  10392. (defun org-get-org-file ()
  10393. "Read a filename, with default directory `org-directory'."
  10394. (let ((default (or org-default-notes-file remember-data-file)))
  10395. (read-file-name (format "File name [%s]: " default)
  10396. (file-name-as-directory org-directory)
  10397. default)))
  10398. (defun org-notes-order-reversed-p ()
  10399. "Check if the current file should receive notes in reversed order."
  10400. (cond
  10401. ((not org-reverse-note-order) nil)
  10402. ((eq t org-reverse-note-order) t)
  10403. ((not (listp org-reverse-note-order)) nil)
  10404. (t (catch 'exit
  10405. (dolist (entry org-reverse-note-order)
  10406. (if (string-match (car entry) buffer-file-name)
  10407. (throw 'exit (cdr entry))))))))
  10408. (defvar org-refile-target-table nil
  10409. "The list of refile targets, created by `org-refile'.")
  10410. (defvar org-agenda-new-buffers nil
  10411. "Buffers created to visit agenda files.")
  10412. (defvar org-refile-cache nil
  10413. "Cache for refile targets.")
  10414. (defvar org-refile-markers nil
  10415. "All the markers used for caching refile locations.")
  10416. (defun org-refile-marker (pos)
  10417. "Get a new refile marker, but only if caching is in use."
  10418. (if (not org-refile-use-cache)
  10419. pos
  10420. (let ((m (make-marker)))
  10421. (move-marker m pos)
  10422. (push m org-refile-markers)
  10423. m)))
  10424. (defun org-refile-cache-clear ()
  10425. "Clear the refile cache and disable all the markers."
  10426. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10427. (setq org-refile-markers nil)
  10428. (setq org-refile-cache nil)
  10429. (message "Refile cache has been cleared"))
  10430. (defun org-refile-cache-check-set (set)
  10431. "Check if all the markers in the cache still have live buffers."
  10432. (let (marker)
  10433. (catch 'exit
  10434. (while (and set (setq marker (nth 3 (pop set))))
  10435. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10436. (when (and marker (null (marker-buffer marker)))
  10437. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10438. (sit-for 3)
  10439. (throw 'exit nil)))
  10440. t)))
  10441. (defun org-refile-cache-put (set &rest identifiers)
  10442. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10443. (let* ((key (sha1 (prin1-to-string identifiers)))
  10444. (entry (assoc key org-refile-cache)))
  10445. (if entry
  10446. (setcdr entry set)
  10447. (push (cons key set) org-refile-cache))))
  10448. (defun org-refile-cache-get (&rest identifiers)
  10449. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10450. (cond
  10451. ((not org-refile-cache) nil)
  10452. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10453. (t
  10454. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10455. org-refile-cache))))
  10456. (and set (org-refile-cache-check-set set) set)))))
  10457. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10458. "Produce a table with refile targets."
  10459. (let ((case-fold-search nil)
  10460. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10461. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10462. targets tgs txt re files desc descre fast-path-p level pos0)
  10463. (message "Getting targets...")
  10464. (with-current-buffer (or default-buffer (current-buffer))
  10465. (dolist (entry entries)
  10466. (setq files (car entry) desc (cdr entry))
  10467. (setq fast-path-p nil)
  10468. (cond
  10469. ((null files) (setq files (list (current-buffer))))
  10470. ((eq files 'org-agenda-files)
  10471. (setq files (org-agenda-files 'unrestricted)))
  10472. ((and (symbolp files) (fboundp files))
  10473. (setq files (funcall files)))
  10474. ((and (symbolp files) (boundp files))
  10475. (setq files (symbol-value files))))
  10476. (if (stringp files) (setq files (list files)))
  10477. (cond
  10478. ((eq (car desc) :tag)
  10479. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10480. ((eq (car desc) :todo)
  10481. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10482. ((eq (car desc) :regexp)
  10483. (setq descre (cdr desc)))
  10484. ((eq (car desc) :level)
  10485. (setq descre (concat "^\\*\\{" (number-to-string
  10486. (if org-odd-levels-only
  10487. (1- (* 2 (cdr desc)))
  10488. (cdr desc)))
  10489. "\\}[ \t]")))
  10490. ((eq (car desc) :maxlevel)
  10491. (setq fast-path-p t)
  10492. (setq descre (concat "^\\*\\{1," (number-to-string
  10493. (if org-odd-levels-only
  10494. (1- (* 2 (cdr desc)))
  10495. (cdr desc)))
  10496. "\\}[ \t]")))
  10497. (t (error "Bad refiling target description %s" desc)))
  10498. (dolist (f files)
  10499. (with-current-buffer
  10500. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10501. (or
  10502. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10503. (progn
  10504. (if (bufferp f) (setq f (buffer-file-name
  10505. (buffer-base-buffer f))))
  10506. (setq f (and f (expand-file-name f)))
  10507. (if (eq org-refile-use-outline-path 'file)
  10508. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10509. (save-excursion
  10510. (save-restriction
  10511. (widen)
  10512. (goto-char (point-min))
  10513. (while (re-search-forward descre nil t)
  10514. (goto-char (setq pos0 (point-at-bol)))
  10515. (catch 'next
  10516. (when org-refile-target-verify-function
  10517. (save-match-data
  10518. (or (funcall org-refile-target-verify-function)
  10519. (throw 'next t))))
  10520. (when (and (looking-at org-complex-heading-regexp)
  10521. (not (member (match-string 4) excluded-entries))
  10522. (match-string 4))
  10523. (setq level (org-reduced-level
  10524. (- (match-end 1) (match-beginning 1)))
  10525. txt (org-link-display-format (match-string 4))
  10526. txt (replace-regexp-in-string "\\( *[[0-9]+/?[0-9]*%?]\\)+$" "" txt)
  10527. re (format org-complex-heading-regexp-format
  10528. (regexp-quote (match-string 4))))
  10529. (when org-refile-use-outline-path
  10530. (setq txt (mapconcat
  10531. 'org-protect-slash
  10532. (append
  10533. (if (eq org-refile-use-outline-path
  10534. 'file)
  10535. (list (file-name-nondirectory
  10536. (buffer-file-name
  10537. (buffer-base-buffer))))
  10538. (if (eq org-refile-use-outline-path
  10539. 'full-file-path)
  10540. (list (buffer-file-name
  10541. (buffer-base-buffer)))))
  10542. (org-get-outline-path fast-path-p
  10543. level txt)
  10544. (list txt))
  10545. "/")))
  10546. (push (list txt f re (org-refile-marker (point)))
  10547. tgs)))
  10548. (when (= (point) pos0)
  10549. ;; verification function has not moved point
  10550. (goto-char (point-at-eol))))))))
  10551. (when org-refile-use-cache
  10552. (org-refile-cache-put tgs (buffer-file-name) descre))
  10553. (setq targets (append tgs targets))))))
  10554. (message "Getting targets...done")
  10555. (nreverse targets)))
  10556. (defun org-protect-slash (s)
  10557. (while (string-match "/" s)
  10558. (setq s (replace-match "\\" t t s)))
  10559. s)
  10560. (defvar org-olpa (make-vector 20 nil))
  10561. (defun org-get-outline-path (&optional fastp level heading)
  10562. "Return the outline path to the current entry, as a list.
  10563. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10564. routine which makes outline path derivations for an entire file,
  10565. avoiding backtracing. Refile target collection makes use of that."
  10566. (if fastp
  10567. (progn
  10568. (if (> level 19)
  10569. (error "Outline path failure, more than 19 levels"))
  10570. (loop for i from level upto 19 do
  10571. (aset org-olpa i nil))
  10572. (prog1
  10573. (delq nil (append org-olpa nil))
  10574. (aset org-olpa level heading)))
  10575. (let (rtn case-fold-search)
  10576. (save-excursion
  10577. (save-restriction
  10578. (widen)
  10579. (while (org-up-heading-safe)
  10580. (when (looking-at org-complex-heading-regexp)
  10581. (push (org-trim
  10582. (replace-regexp-in-string
  10583. ;; Remove statistical/checkboxes cookies
  10584. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10585. (org-match-string-no-properties 4)))
  10586. rtn)))
  10587. rtn)))))
  10588. (defun org-format-outline-path (path &optional width prefix separator)
  10589. "Format the outline path PATH for display.
  10590. WIDTH is the maximum number of characters that is available.
  10591. PREFIX is a prefix to be included in the returned string,
  10592. such as the file name.
  10593. SEPARATOR is inserted between the different parts of the path,
  10594. the default is \"/\"."
  10595. (setq width (or width 79))
  10596. (if prefix (setq width (- width (length prefix))))
  10597. (if (not path)
  10598. (or prefix "")
  10599. (let* ((nsteps (length path))
  10600. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10601. (maxwidth (if (<= total-width width)
  10602. 10000 ;; everything fits
  10603. ;; we need to shorten the level headings
  10604. (/ (- width nsteps) nsteps)))
  10605. (org-odd-levels-only nil)
  10606. (n 0)
  10607. (total (1+ (length prefix))))
  10608. (setq maxwidth (max maxwidth 10))
  10609. (concat prefix
  10610. (if prefix (or separator "/"))
  10611. (mapconcat
  10612. (lambda (h)
  10613. (setq n (1+ n))
  10614. (if (and (= n nsteps) (< maxwidth 10000))
  10615. (setq maxwidth (- total-width total)))
  10616. (if (< (length h) maxwidth)
  10617. (progn (setq total (+ total (length h) 1)) h)
  10618. (setq h (substring h 0 (- maxwidth 2))
  10619. total (+ total maxwidth 1))
  10620. (if (string-match "[ \t]+\\'" h)
  10621. (setq h (substring h 0 (match-beginning 0))))
  10622. (setq h (concat h "..")))
  10623. (org-add-props h nil 'face
  10624. (nth (% (1- n) org-n-level-faces)
  10625. org-level-faces))
  10626. h)
  10627. path (or separator "/"))))))
  10628. (defun org-display-outline-path (&optional file current separator just-return-string)
  10629. "Display the current outline path in the echo area.
  10630. If FILE is non-nil, prepend the output with the file name.
  10631. If CURRENT is non-nil, append the current heading to the output.
  10632. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10633. the different parts of the path and defaults to \"/\".
  10634. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10635. (interactive "P")
  10636. (let* (case-fold-search
  10637. (bfn (buffer-file-name (buffer-base-buffer)))
  10638. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10639. res)
  10640. (if current (setq path (append path
  10641. (save-excursion
  10642. (org-back-to-heading t)
  10643. (if (looking-at org-complex-heading-regexp)
  10644. (list (match-string 4)))))))
  10645. (setq res
  10646. (org-format-outline-path
  10647. path
  10648. (1- (frame-width))
  10649. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10650. separator))
  10651. (if just-return-string
  10652. (org-no-properties res)
  10653. (org-unlogged-message "%s" res))))
  10654. (defvar org-refile-history nil
  10655. "History for refiling operations.")
  10656. (defvar org-after-refile-insert-hook nil
  10657. "Hook run after `org-refile' has inserted its stuff at the new location.
  10658. Note that this is still *before* the stuff will be removed from
  10659. the *old* location.")
  10660. (defvar org-capture-last-stored-marker)
  10661. (defvar org-refile-keep nil
  10662. "Non-nil means `org-refile' will copy instead of refile.")
  10663. (defun org-copy ()
  10664. "Like `org-refile', but copy."
  10665. (interactive)
  10666. (let ((org-refile-keep t))
  10667. (funcall 'org-refile nil nil nil "Copy")))
  10668. (defun org-refile (&optional arg default-buffer rfloc msg)
  10669. "Move the entry or entries at point to another heading.
  10670. The list of target headings is compiled using the information in
  10671. `org-refile-targets', which see.
  10672. At the target location, the entry is filed as a subitem of the
  10673. target heading. Depending on `org-reverse-note-order', the new
  10674. subitem will either be the first or the last subitem.
  10675. If there is an active region, all entries in that region will be
  10676. refiled. However, the region must fulfill the requirement that
  10677. the first heading sets the top-level of the moved text.
  10678. With prefix arg ARG, the command will only visit the target
  10679. location and not actually move anything.
  10680. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10681. refiling operation has put the subtree.
  10682. With a numeric prefix argument of `2', refile to the running clock.
  10683. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10684. being set to t and copy to the target location, don't move it.
  10685. Beware that keeping refiled entries may result in duplicated ID
  10686. properties.
  10687. RFLOC can be a refile location obtained in a different way.
  10688. MSG is a string to replace \"Refile\" in the default prompt with
  10689. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10690. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10691. If you are using target caching (see `org-refile-use-cache'), you
  10692. have to clear the target cache in order to find new targets.
  10693. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10694. prefix argument (`C-u C-u C-u C-c C-w')."
  10695. (interactive "P")
  10696. (if (member arg '(0 (64)))
  10697. (org-refile-cache-clear)
  10698. (let* ((actionmsg (cond (msg msg)
  10699. ((equal arg 3) "Refile (and keep)")
  10700. (t "Refile")))
  10701. (cbuf (current-buffer))
  10702. (regionp (org-region-active-p))
  10703. (region-start (and regionp (region-beginning)))
  10704. (region-end (and regionp (region-end)))
  10705. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10706. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10707. pos it nbuf file re level reversed)
  10708. (setq last-command nil)
  10709. (when regionp
  10710. (goto-char region-start)
  10711. (or (bolp) (goto-char (point-at-bol)))
  10712. (setq region-start (point))
  10713. (unless (or (org-kill-is-subtree-p
  10714. (buffer-substring region-start region-end))
  10715. (prog1 org-refile-active-region-within-subtree
  10716. (let ((s (point-at-eol)))
  10717. (org-toggle-heading)
  10718. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10719. (user-error "The region is not a (sequence of) subtree(s)")))
  10720. (if (equal arg '(16))
  10721. (org-refile-goto-last-stored)
  10722. (when (or
  10723. (and (equal arg 2)
  10724. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10725. (prog1
  10726. (setq it (list (or org-clock-heading "running clock")
  10727. (buffer-file-name
  10728. (marker-buffer org-clock-hd-marker))
  10729. ""
  10730. (marker-position org-clock-hd-marker)))
  10731. (setq arg nil)))
  10732. (setq it (or rfloc
  10733. (let (heading-text)
  10734. (save-excursion
  10735. (unless (and arg (listp arg))
  10736. (org-back-to-heading t)
  10737. (setq heading-text
  10738. (replace-regexp-in-string
  10739. org-bracket-link-regexp
  10740. "\\3"
  10741. (nth 4 (org-heading-components)))))
  10742. (org-refile-get-location
  10743. (cond ((and arg (listp arg)) "Goto")
  10744. (regionp (concat actionmsg " region to"))
  10745. (t (concat actionmsg " subtree \""
  10746. heading-text "\" to")))
  10747. default-buffer
  10748. (and (not (equal '(4) arg))
  10749. org-refile-allow-creating-parent-nodes)
  10750. arg))))))
  10751. (setq file (nth 1 it)
  10752. re (nth 2 it)
  10753. pos (nth 3 it))
  10754. (if (and (not arg)
  10755. pos
  10756. (equal (buffer-file-name) file)
  10757. (if regionp
  10758. (and (>= pos region-start)
  10759. (<= pos region-end))
  10760. (and (>= pos (point))
  10761. (< pos (save-excursion
  10762. (org-end-of-subtree t t))))))
  10763. (error "Cannot refile to position inside the tree or region"))
  10764. (setq nbuf (or (find-buffer-visiting file)
  10765. (find-file-noselect file)))
  10766. (if (and arg (not (equal arg 3)))
  10767. (progn
  10768. (org-pop-to-buffer-same-window nbuf)
  10769. (goto-char pos)
  10770. (org-show-context 'org-goto))
  10771. (if regionp
  10772. (progn
  10773. (org-kill-new (buffer-substring region-start region-end))
  10774. (org-save-markers-in-region region-start region-end))
  10775. (org-copy-subtree 1 nil t))
  10776. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10777. (find-file-noselect file)))
  10778. (setq reversed (org-notes-order-reversed-p))
  10779. (save-excursion
  10780. (save-restriction
  10781. (widen)
  10782. (if pos
  10783. (progn
  10784. (goto-char pos)
  10785. (looking-at org-outline-regexp)
  10786. (setq level (org-get-valid-level (funcall outline-level) 1))
  10787. (goto-char
  10788. (if reversed
  10789. (or (outline-next-heading) (point-max))
  10790. (or (save-excursion (org-get-next-sibling))
  10791. (org-end-of-subtree t t)
  10792. (point-max)))))
  10793. (setq level 1)
  10794. (if (not reversed)
  10795. (goto-char (point-max))
  10796. (goto-char (point-min))
  10797. (or (outline-next-heading) (goto-char (point-max)))))
  10798. (if (not (bolp)) (newline))
  10799. (org-paste-subtree level nil nil t)
  10800. (when org-log-refile
  10801. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10802. (unless (eq org-log-refile 'note)
  10803. (save-excursion (org-add-log-note))))
  10804. (and org-auto-align-tags
  10805. (let ((org-loop-over-headlines-in-active-region nil))
  10806. (org-set-tags nil t)))
  10807. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10808. :last-refile)))
  10809. (when bookmark-name
  10810. (with-demoted-errors
  10811. (bookmark-set bookmark-name))))
  10812. ;; If we are refiling for capture, make sure that the
  10813. ;; last-capture pointers point here
  10814. (when (org-bound-and-true-p org-refile-for-capture)
  10815. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10816. :last-capture-marker)))
  10817. (when bookmark-name
  10818. (with-demoted-errors
  10819. (bookmark-set bookmark-name))))
  10820. (move-marker org-capture-last-stored-marker (point)))
  10821. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10822. (run-hooks 'org-after-refile-insert-hook))))
  10823. (unless org-refile-keep
  10824. (if regionp
  10825. (delete-region (point) (+ (point) (- region-end region-start)))
  10826. (delete-region
  10827. (and (org-back-to-heading t) (point))
  10828. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10829. (when (featurep 'org-inlinetask)
  10830. (org-inlinetask-remove-END-maybe))
  10831. (setq org-markers-to-move nil)
  10832. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10833. (defun org-refile-goto-last-stored ()
  10834. "Go to the location where the last refile was stored."
  10835. (interactive)
  10836. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10837. (message "This is the location of the last refile"))
  10838. (defun org-refile--get-location (refloc tbl)
  10839. "When user refile to REFLOC, find the associated target in TBL.
  10840. Also check `org-refile-target-table'."
  10841. (car (delq
  10842. nil
  10843. (mapcar
  10844. (lambda (r) (or (assoc r tbl)
  10845. (assoc r org-refile-target-table)))
  10846. (list (replace-regexp-in-string "/$" "" refloc)
  10847. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10848. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10849. no-exclude)
  10850. "Prompt the user for a refile location, using PROMPT.
  10851. PROMPT should not be suffixed with a colon and a space, because
  10852. this function appends the default value from
  10853. `org-refile-history' automatically, if that is not empty.
  10854. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10855. this is used for the GOTO interface."
  10856. (let ((org-refile-targets org-refile-targets)
  10857. (org-refile-use-outline-path org-refile-use-outline-path)
  10858. excluded-entries)
  10859. (when (and (derived-mode-p 'org-mode)
  10860. (not org-refile-use-cache)
  10861. (not no-exclude))
  10862. (org-map-tree
  10863. (lambda()
  10864. (setq excluded-entries
  10865. (append excluded-entries (list (org-get-heading t t)))))))
  10866. (setq org-refile-target-table
  10867. (org-refile-get-targets default-buffer excluded-entries)))
  10868. (unless org-refile-target-table
  10869. (user-error "No refile targets"))
  10870. (let* ((cbuf (current-buffer))
  10871. (partial-completion-mode nil)
  10872. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10873. (cfunc (if (and org-refile-use-outline-path
  10874. org-outline-path-complete-in-steps)
  10875. 'org-olpath-completing-read
  10876. 'org-icompleting-read))
  10877. (extra (if org-refile-use-outline-path "/" ""))
  10878. (cbnex (concat (buffer-name) extra))
  10879. (filename (and cfn (expand-file-name cfn)))
  10880. (tbl (mapcar
  10881. (lambda (x)
  10882. (if (and (not (member org-refile-use-outline-path
  10883. '(file full-file-path)))
  10884. (not (equal filename (nth 1 x))))
  10885. (cons (concat (car x) extra " ("
  10886. (file-name-nondirectory (nth 1 x)) ")")
  10887. (cdr x))
  10888. (cons (concat (car x) extra) (cdr x))))
  10889. org-refile-target-table))
  10890. (completion-ignore-case t)
  10891. cdef
  10892. (prompt (concat prompt
  10893. (or (and (car org-refile-history)
  10894. (concat " (default " (car org-refile-history) ")"))
  10895. (and (assoc cbnex tbl) (setq cdef cbnex)
  10896. (concat " (default " cbnex ")"))) ": "))
  10897. pa answ parent-target child parent old-hist)
  10898. (setq old-hist org-refile-history)
  10899. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10900. nil 'org-refile-history (or cdef (car org-refile-history))))
  10901. (if (setq pa (org-refile--get-location answ tbl))
  10902. (progn
  10903. (org-refile-check-position pa)
  10904. (when (or (not org-refile-history)
  10905. (not (eq old-hist org-refile-history))
  10906. (not (equal (car pa) (car org-refile-history))))
  10907. (setq org-refile-history
  10908. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10909. org-refile-history
  10910. (cdr org-refile-history))))
  10911. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10912. (pop org-refile-history)))
  10913. pa)
  10914. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10915. (progn
  10916. (setq parent (match-string 1 answ)
  10917. child (match-string 2 answ))
  10918. (setq parent-target (org-refile--get-location parent tbl))
  10919. (when (and parent-target
  10920. (or (eq new-nodes t)
  10921. (and (eq new-nodes 'confirm)
  10922. (y-or-n-p (format "Create new node \"%s\"? "
  10923. child)))))
  10924. (org-refile-new-child parent-target child)))
  10925. (user-error "Invalid target location")))))
  10926. (declare-function org-string-nw-p "org-macs" (s))
  10927. (defun org-refile-check-position (refile-pointer)
  10928. "Check if the refile pointer matches the headline to which it points."
  10929. (let* ((file (nth 1 refile-pointer))
  10930. (re (nth 2 refile-pointer))
  10931. (pos (nth 3 refile-pointer))
  10932. buffer)
  10933. (if (and (not (markerp pos)) (not file))
  10934. (user-error "Please indicate a target file in the refile path")
  10935. (when (org-string-nw-p re)
  10936. (setq buffer (if (markerp pos)
  10937. (marker-buffer pos)
  10938. (or (find-buffer-visiting file)
  10939. (find-file-noselect file))))
  10940. (with-current-buffer buffer
  10941. (save-excursion
  10942. (save-restriction
  10943. (widen)
  10944. (goto-char pos)
  10945. (beginning-of-line 1)
  10946. (unless (org-looking-at-p re)
  10947. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10948. (defun org-refile-new-child (parent-target child)
  10949. "Use refile target PARENT-TARGET to add new CHILD below it."
  10950. (unless parent-target
  10951. (error "Cannot find parent for new node"))
  10952. (let ((file (nth 1 parent-target))
  10953. (pos (nth 3 parent-target))
  10954. level)
  10955. (with-current-buffer (or (find-buffer-visiting file)
  10956. (find-file-noselect file))
  10957. (save-excursion
  10958. (save-restriction
  10959. (widen)
  10960. (if pos
  10961. (goto-char pos)
  10962. (goto-char (point-max))
  10963. (if (not (bolp)) (newline)))
  10964. (when (looking-at org-outline-regexp)
  10965. (setq level (funcall outline-level))
  10966. (org-end-of-subtree t t))
  10967. (org-back-over-empty-lines)
  10968. (insert "\n" (make-string
  10969. (if pos (org-get-valid-level level 1) 1) ?*)
  10970. " " child "\n")
  10971. (beginning-of-line 0)
  10972. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10973. (defun org-olpath-completing-read (prompt collection &rest args)
  10974. "Read an outline path like a file name."
  10975. (let ((thetable collection)
  10976. (org-completion-use-ido nil) ; does not work with ido.
  10977. (org-completion-use-iswitchb nil)) ; or iswitchb
  10978. (apply #'org-icompleting-read
  10979. prompt
  10980. (lambda (string predicate &optional flag)
  10981. (cond
  10982. ((eq flag nil) (try-completion string thetable))
  10983. ((eq flag t)
  10984. (let ((l (length string)))
  10985. (mapcar (lambda (x)
  10986. (let ((r (substring x l))
  10987. (f (if (string-match " ([^)]*)$" x)
  10988. (match-string 0 x)
  10989. "")))
  10990. (if (string-match "/" r)
  10991. (concat string (substring r 0 (match-end 0)) f)
  10992. x)))
  10993. (all-completions string thetable predicate))))
  10994. ;; Exact match?
  10995. ((eq flag 'lambda) (assoc string thetable))))
  10996. args)))
  10997. ;;;; Dynamic blocks
  10998. (defun org-find-dblock (name)
  10999. "Find the first dynamic block with name NAME in the buffer.
  11000. If not found, stay at current position and return nil."
  11001. (let ((case-fold-search t) pos)
  11002. (save-excursion
  11003. (goto-char (point-min))
  11004. (setq pos (and (re-search-forward
  11005. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  11006. (match-beginning 0))))
  11007. (if pos (goto-char pos))
  11008. pos))
  11009. (defun org-create-dblock (plist)
  11010. "Create a dynamic block section, with parameters taken from PLIST.
  11011. PLIST must contain a :name entry which is used as the name of the block."
  11012. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11013. (end-of-line 1)
  11014. (newline))
  11015. (let ((col (current-column))
  11016. (name (plist-get plist :name)))
  11017. (insert "#+BEGIN: " name)
  11018. (while plist
  11019. (if (eq (car plist) :name)
  11020. (setq plist (cddr plist))
  11021. (insert " " (prin1-to-string (pop plist)))))
  11022. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11023. (beginning-of-line -2)))
  11024. (defun org-prepare-dblock ()
  11025. "Prepare dynamic block for refresh.
  11026. This empties the block, puts the cursor at the insert position and returns
  11027. the property list including an extra property :name with the block name."
  11028. (unless (looking-at org-dblock-start-re)
  11029. (user-error "Not at a dynamic block"))
  11030. (let* ((begdel (1+ (match-end 0)))
  11031. (name (org-no-properties (match-string 1)))
  11032. (params (append (list :name name)
  11033. (read (concat "(" (match-string 3) ")")))))
  11034. (save-excursion
  11035. (beginning-of-line 1)
  11036. (skip-chars-forward " \t")
  11037. (setq params (plist-put params :indentation-column (current-column))))
  11038. (unless (re-search-forward org-dblock-end-re nil t)
  11039. (error "Dynamic block not terminated"))
  11040. (setq params
  11041. (append params
  11042. (list :content (buffer-substring
  11043. begdel (match-beginning 0)))))
  11044. (delete-region begdel (match-beginning 0))
  11045. (goto-char begdel)
  11046. (open-line 1)
  11047. params))
  11048. (defun org-map-dblocks (&optional command)
  11049. "Apply COMMAND to all dynamic blocks in the current buffer.
  11050. If COMMAND is not given, use `org-update-dblock'."
  11051. (let ((cmd (or command 'org-update-dblock)))
  11052. (save-excursion
  11053. (goto-char (point-min))
  11054. (while (re-search-forward org-dblock-start-re nil t)
  11055. (goto-char (match-beginning 0))
  11056. (save-excursion
  11057. (condition-case nil
  11058. (funcall cmd)
  11059. (error (message "Error during update of dynamic block"))))
  11060. (unless (re-search-forward org-dblock-end-re nil t)
  11061. (error "Dynamic block not terminated"))))))
  11062. (defun org-dblock-update (&optional arg)
  11063. "User command for updating dynamic blocks.
  11064. Update the dynamic block at point. With prefix ARG, update all dynamic
  11065. blocks in the buffer."
  11066. (interactive "P")
  11067. (if arg
  11068. (org-update-all-dblocks)
  11069. (or (looking-at org-dblock-start-re)
  11070. (org-beginning-of-dblock))
  11071. (org-update-dblock)))
  11072. (defun org-update-dblock ()
  11073. "Update the dynamic block at point.
  11074. This means to empty the block, parse for parameters and then call
  11075. the correct writing function."
  11076. (interactive)
  11077. (save-excursion
  11078. (let* ((win (selected-window))
  11079. (pos (point))
  11080. (line (org-current-line))
  11081. (params (org-prepare-dblock))
  11082. (name (plist-get params :name))
  11083. (indent (plist-get params :indentation-column))
  11084. (cmd (intern (concat "org-dblock-write:" name))))
  11085. (message "Updating dynamic block `%s' at line %d..." name line)
  11086. (funcall cmd params)
  11087. (message "Updating dynamic block `%s' at line %d...done" name line)
  11088. (goto-char pos)
  11089. (when (and indent (> indent 0))
  11090. (setq indent (make-string indent ?\ ))
  11091. (save-excursion
  11092. (select-window win)
  11093. (org-beginning-of-dblock)
  11094. (forward-line 1)
  11095. (while (not (looking-at org-dblock-end-re))
  11096. (insert indent)
  11097. (beginning-of-line 2))
  11098. (when (looking-at org-dblock-end-re)
  11099. (and (looking-at "[ \t]+")
  11100. (replace-match ""))
  11101. (insert indent)))))))
  11102. (defun org-beginning-of-dblock ()
  11103. "Find the beginning of the dynamic block at point.
  11104. Error if there is no such block at point."
  11105. (let ((pos (point))
  11106. beg)
  11107. (end-of-line 1)
  11108. (if (and (re-search-backward org-dblock-start-re nil t)
  11109. (setq beg (match-beginning 0))
  11110. (re-search-forward org-dblock-end-re nil t)
  11111. (> (match-end 0) pos))
  11112. (goto-char beg)
  11113. (goto-char pos)
  11114. (error "Not in a dynamic block"))))
  11115. (defun org-update-all-dblocks ()
  11116. "Update all dynamic blocks in the buffer.
  11117. This function can be used in a hook."
  11118. (interactive)
  11119. (when (derived-mode-p 'org-mode)
  11120. (org-map-dblocks 'org-update-dblock)))
  11121. ;;;; Completion
  11122. (declare-function org-export-backend-name "org-export" (cl-x))
  11123. (declare-function org-export-backend-options "org-export" (cl-x))
  11124. (defun org-get-export-keywords ()
  11125. "Return a list of all currently understood export keywords.
  11126. Export keywords include options, block names, attributes and
  11127. keywords relative to each registered export back-end."
  11128. (let (keywords)
  11129. (dolist (backend
  11130. (org-bound-and-true-p org-export-registered-backends)
  11131. (delq nil keywords))
  11132. ;; Back-end name (for keywords, like #+LATEX:)
  11133. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11134. (dolist (option-entry (org-export-backend-options backend))
  11135. ;; Back-end options.
  11136. (push (nth 1 option-entry) keywords)))))
  11137. (defconst org-options-keywords
  11138. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11139. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11140. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11141. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11142. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11143. (defcustom org-structure-template-alist
  11144. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11145. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11146. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11147. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11148. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11149. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11150. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11151. ("L" "#+LaTeX: ")
  11152. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11153. ("H" "#+HTML: ")
  11154. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11155. ("A" "#+ASCII: ")
  11156. ("i" "#+INDEX: ?")
  11157. ("I" "#+INCLUDE: %file ?"))
  11158. "Structure completion elements.
  11159. This is a list of abbreviation keys and values. The value gets inserted
  11160. if you type `<' followed by the key and then press the completion key,
  11161. usually `TAB'. %file will be replaced by a file name after prompting
  11162. for the file using completion. The cursor will be placed at the position
  11163. of the `?' in the template.
  11164. There are two templates for each key, the first uses the original Org syntax,
  11165. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11166. the default when the /org-mtags.el/ module has been loaded. See also the
  11167. variable `org-mtags-prefer-muse-templates'."
  11168. :group 'org-completion
  11169. :type '(repeat
  11170. (list
  11171. (string :tag "Key")
  11172. (string :tag "Template")))
  11173. :version "25.1"
  11174. :package-version '(Org . "8.3"))
  11175. (defun org-try-structure-completion ()
  11176. "Try to complete a structure template before point.
  11177. This looks for strings like \"<e\" on an otherwise empty line and
  11178. expands them."
  11179. (let ((l (buffer-substring (point-at-bol) (point)))
  11180. a)
  11181. (when (and (looking-at "[ \t]*$")
  11182. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11183. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11184. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11185. (match-beginning 1)) a)
  11186. t)))
  11187. (defun org-complete-expand-structure-template (start cell)
  11188. "Expand a structure template."
  11189. (let ((rpl (nth 1 cell))
  11190. (ind ""))
  11191. (delete-region start (point))
  11192. (when (string-match "\\`[ \t]*#\\+" rpl)
  11193. (cond
  11194. ((bolp))
  11195. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11196. (setq ind (buffer-substring (point-at-bol) (point))))
  11197. (t (newline))))
  11198. (setq start (point))
  11199. (if (string-match "%file" rpl)
  11200. (setq rpl (replace-match
  11201. (concat
  11202. "\""
  11203. (save-match-data
  11204. (abbreviate-file-name (read-file-name "Include file: ")))
  11205. "\"")
  11206. t t rpl)))
  11207. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11208. (concat "\n" ind)))
  11209. (insert rpl)
  11210. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11211. ;;;; TODO, DEADLINE, Comments
  11212. (defun org-toggle-comment ()
  11213. "Change the COMMENT state of an entry."
  11214. (interactive)
  11215. (save-excursion
  11216. (org-back-to-heading)
  11217. (looking-at org-complex-heading-regexp)
  11218. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11219. (skip-chars-forward " \t")
  11220. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11221. (if (org-in-commented-heading-p t)
  11222. (delete-region (point)
  11223. (progn (search-forward " " (line-end-position) 'move)
  11224. (skip-chars-forward " \t")
  11225. (point)))
  11226. (insert org-comment-string)
  11227. (unless (eolp) (insert " ")))))
  11228. (defvar org-last-todo-state-is-todo nil
  11229. "This is non-nil when the last TODO state change led to a TODO state.
  11230. If the last change removed the TODO tag or switched to DONE, then
  11231. this is nil.")
  11232. (defvar org-setting-tags nil) ; dynamically skipped
  11233. (defvar org-todo-setup-filter-hook nil
  11234. "Hook for functions that pre-filter todo specs.
  11235. Each function takes a todo spec and returns either nil or the spec
  11236. transformed into canonical form." )
  11237. (defvar org-todo-get-default-hook nil
  11238. "Hook for functions that get a default item for todo.
  11239. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11240. nil or a string to be used for the todo mark." )
  11241. (defvar org-agenda-headline-snapshot-before-repeat)
  11242. (defun org-current-effective-time ()
  11243. "Return current time adjusted for `org-extend-today-until' variable."
  11244. (let* ((ct (org-current-time))
  11245. (dct (decode-time ct))
  11246. (ct1
  11247. (cond
  11248. (org-use-last-clock-out-time-as-effective-time
  11249. (or (org-clock-get-last-clock-out-time) ct))
  11250. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11251. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11252. (t ct))))
  11253. ct1))
  11254. (defun org-todo-yesterday (&optional arg)
  11255. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11256. (interactive "P")
  11257. (if (eq major-mode 'org-agenda-mode)
  11258. (apply 'org-agenda-todo-yesterday arg)
  11259. (let* ((org-use-effective-time t)
  11260. (hour (third (decode-time
  11261. (org-current-time))))
  11262. (org-extend-today-until (1+ hour)))
  11263. (org-todo arg))))
  11264. (defvar org-block-entry-blocking ""
  11265. "First entry preventing the TODO state change.")
  11266. (defun org-cancel-repeater ()
  11267. "Cancel a repeater by setting its numeric value to zero."
  11268. (interactive)
  11269. (save-excursion
  11270. (org-back-to-heading t)
  11271. (let ((bound1 (point))
  11272. (bound0 (save-excursion (outline-next-heading) (point))))
  11273. (when (re-search-forward
  11274. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11275. org-deadline-time-regexp "\\)\\|\\("
  11276. org-ts-regexp "\\)")
  11277. bound0 t)
  11278. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11279. (replace-match "0" t nil nil 1))))))
  11280. (defun org-todo (&optional arg)
  11281. "Change the TODO state of an item.
  11282. The state of an item is given by a keyword at the start of the heading,
  11283. like
  11284. *** TODO Write paper
  11285. *** DONE Call mom
  11286. The different keywords are specified in the variable `org-todo-keywords'.
  11287. By default the available states are \"TODO\" and \"DONE\".
  11288. So for this example: when the item starts with TODO, it is changed to DONE.
  11289. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11290. DONE are present, add TODO at the beginning of the heading.
  11291. With \\[universal-argument] prefix arg, use completion to determine the new \
  11292. state.
  11293. With numeric prefix arg, switch to that state.
  11294. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11295. keywords (nextset).
  11296. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11297. With a numeric prefix arg of 0, inhibit note taking for the change.
  11298. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11299. When called through ELisp, arg is also interpreted in the following way:
  11300. `none' -> empty state
  11301. \"\"(empty string) -> switch to empty state
  11302. `done' -> switch to DONE
  11303. `nextset' -> switch to the next set of keywords
  11304. `previousset' -> switch to the previous set of keywords
  11305. \"WAITING\" -> switch to the specified keyword, but only if it
  11306. really is a member of `org-todo-keywords'."
  11307. (interactive "P")
  11308. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11309. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11310. 'region-start-level 'region))
  11311. org-loop-over-headlines-in-active-region)
  11312. (org-map-entries
  11313. `(org-todo ,arg)
  11314. org-loop-over-headlines-in-active-region
  11315. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11316. (if (equal arg '(16)) (setq arg 'nextset))
  11317. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11318. (let ((org-blocker-hook org-blocker-hook)
  11319. commentp
  11320. case-fold-search)
  11321. (when (equal arg '(64))
  11322. (setq arg nil org-blocker-hook nil))
  11323. (when (and org-blocker-hook
  11324. (or org-inhibit-blocking
  11325. (org-entry-get nil "NOBLOCKING")))
  11326. (setq org-blocker-hook nil))
  11327. (save-excursion
  11328. (catch 'exit
  11329. (org-back-to-heading t)
  11330. (when (org-in-commented-heading-p t)
  11331. (org-toggle-comment)
  11332. (setq commentp t))
  11333. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11334. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11335. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11336. (let* ((match-data (match-data))
  11337. (startpos (point-at-bol))
  11338. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11339. (org-log-done org-log-done)
  11340. (org-log-repeat org-log-repeat)
  11341. (org-todo-log-states org-todo-log-states)
  11342. (org-inhibit-logging
  11343. (if (equal arg 0)
  11344. (progn (setq arg nil) 'note) org-inhibit-logging))
  11345. (this (match-string 1))
  11346. (hl-pos (match-beginning 0))
  11347. (head (org-get-todo-sequence-head this))
  11348. (ass (assoc head org-todo-kwd-alist))
  11349. (interpret (nth 1 ass))
  11350. (done-word (nth 3 ass))
  11351. (final-done-word (nth 4 ass))
  11352. (org-last-state (or this ""))
  11353. (completion-ignore-case t)
  11354. (member (member this org-todo-keywords-1))
  11355. (tail (cdr member))
  11356. (org-state (cond
  11357. ((and org-todo-key-trigger
  11358. (or (and (equal arg '(4))
  11359. (eq org-use-fast-todo-selection 'prefix))
  11360. (and (not arg) org-use-fast-todo-selection
  11361. (not (eq org-use-fast-todo-selection
  11362. 'prefix)))))
  11363. ;; Use fast selection
  11364. (org-fast-todo-selection))
  11365. ((and (equal arg '(4))
  11366. (or (not org-use-fast-todo-selection)
  11367. (not org-todo-key-trigger)))
  11368. ;; Read a state with completion
  11369. (org-icompleting-read
  11370. "State: " (mapcar 'list org-todo-keywords-1)
  11371. nil t))
  11372. ((eq arg 'right)
  11373. (if this
  11374. (if tail (car tail) nil)
  11375. (car org-todo-keywords-1)))
  11376. ((eq arg 'left)
  11377. (if (equal member org-todo-keywords-1)
  11378. nil
  11379. (if this
  11380. (nth (- (length org-todo-keywords-1)
  11381. (length tail) 2)
  11382. org-todo-keywords-1)
  11383. (org-last org-todo-keywords-1))))
  11384. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11385. (setq arg nil))) ; hack to fall back to cycling
  11386. (arg
  11387. ;; user or caller requests a specific state
  11388. (cond
  11389. ((equal arg "") nil)
  11390. ((eq arg 'none) nil)
  11391. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11392. ((eq arg 'nextset)
  11393. (or (car (cdr (member head org-todo-heads)))
  11394. (car org-todo-heads)))
  11395. ((eq arg 'previousset)
  11396. (let ((org-todo-heads (reverse org-todo-heads)))
  11397. (or (car (cdr (member head org-todo-heads)))
  11398. (car org-todo-heads))))
  11399. ((car (member arg org-todo-keywords-1)))
  11400. ((stringp arg)
  11401. (user-error "State `%s' not valid in this file" arg))
  11402. ((nth (1- (prefix-numeric-value arg))
  11403. org-todo-keywords-1))))
  11404. ((null member) (or head (car org-todo-keywords-1)))
  11405. ((equal this final-done-word) nil) ;; -> make empty
  11406. ((null tail) nil) ;; -> first entry
  11407. ((memq interpret '(type priority))
  11408. (if (eq this-command last-command)
  11409. (car tail)
  11410. (if (> (length tail) 0)
  11411. (or done-word (car org-done-keywords))
  11412. nil)))
  11413. (t
  11414. (car tail))))
  11415. (org-state (or
  11416. (run-hook-with-args-until-success
  11417. 'org-todo-get-default-hook org-state org-last-state)
  11418. org-state))
  11419. (next (if org-state (concat " " org-state " ") " "))
  11420. (change-plist (list :type 'todo-state-change :from this :to org-state
  11421. :position startpos))
  11422. dolog now-done-p)
  11423. (when org-blocker-hook
  11424. (setq org-last-todo-state-is-todo
  11425. (not (member this org-done-keywords)))
  11426. (unless (save-excursion
  11427. (save-match-data
  11428. (org-with-wide-buffer
  11429. (run-hook-with-args-until-failure
  11430. 'org-blocker-hook change-plist))))
  11431. (if (org-called-interactively-p 'interactive)
  11432. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11433. this org-state org-block-entry-blocking)
  11434. ;; fail silently
  11435. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11436. this org-state org-block-entry-blocking)
  11437. (throw 'exit nil))))
  11438. (store-match-data match-data)
  11439. (replace-match next t t)
  11440. (cond ((equal this org-state)
  11441. (message "TODO state was already %s" (org-trim next)))
  11442. ((pos-visible-in-window-p hl-pos)
  11443. (message "TODO state changed to %s" (org-trim next))))
  11444. (unless head
  11445. (setq head (org-get-todo-sequence-head org-state)
  11446. ass (assoc head org-todo-kwd-alist)
  11447. interpret (nth 1 ass)
  11448. done-word (nth 3 ass)
  11449. final-done-word (nth 4 ass)))
  11450. (when (memq arg '(nextset previousset))
  11451. (message "Keyword-Set %d/%d: %s"
  11452. (- (length org-todo-sets) -1
  11453. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11454. (length org-todo-sets)
  11455. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11456. (setq org-last-todo-state-is-todo
  11457. (not (member org-state org-done-keywords)))
  11458. (setq now-done-p (and (member org-state org-done-keywords)
  11459. (not (member this org-done-keywords))))
  11460. (and logging (org-local-logging logging))
  11461. (when (and (or org-todo-log-states org-log-done)
  11462. (not (eq org-inhibit-logging t))
  11463. (not (memq arg '(nextset previousset))))
  11464. ;; we need to look at recording a time and note
  11465. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11466. (nth 2 (assoc this org-todo-log-states))))
  11467. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11468. (setq dolog 'time))
  11469. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11470. (and org-state
  11471. (member org-state org-not-done-keywords)
  11472. (not (member this org-not-done-keywords))))
  11473. ;; This is now a todo state and was not one before
  11474. ;; If there was a CLOSED time stamp, get rid of it.
  11475. (org-add-planning-info nil nil 'closed))
  11476. (when (and now-done-p org-log-done)
  11477. ;; It is now done, and it was not done before
  11478. (org-add-planning-info 'closed (org-current-effective-time))
  11479. (if (and (not dolog) (eq 'note org-log-done))
  11480. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11481. (when (and org-state dolog)
  11482. ;; This is a non-nil state, and we need to log it
  11483. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11484. ;; Fixup tag positioning
  11485. (org-todo-trigger-tag-changes org-state)
  11486. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11487. (when org-provide-todo-statistics
  11488. (org-update-parent-todo-statistics))
  11489. (run-hooks 'org-after-todo-state-change-hook)
  11490. (if (and arg (not (member org-state org-done-keywords)))
  11491. (setq head (org-get-todo-sequence-head org-state)))
  11492. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11493. ;; Do we need to trigger a repeat?
  11494. (when now-done-p
  11495. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11496. ;; This is for the agenda, take a snapshot of the headline.
  11497. (save-match-data
  11498. (setq org-agenda-headline-snapshot-before-repeat
  11499. (org-get-heading))))
  11500. (org-auto-repeat-maybe org-state))
  11501. ;; Fixup cursor location if close to the keyword
  11502. (if (and (outline-on-heading-p)
  11503. (not (bolp))
  11504. (save-excursion (beginning-of-line 1)
  11505. (looking-at org-todo-line-regexp))
  11506. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11507. (progn
  11508. (goto-char (or (match-end 2) (match-end 1)))
  11509. (and (looking-at " ") (just-one-space))))
  11510. (when org-trigger-hook
  11511. (save-excursion
  11512. (run-hook-with-args 'org-trigger-hook change-plist)))
  11513. (when commentp (org-toggle-comment))))))))
  11514. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11515. "Block turning an entry into a TODO, using the hierarchy.
  11516. This checks whether the current task should be blocked from state
  11517. changes. Such blocking occurs when:
  11518. 1. The task has children which are not all in a completed state.
  11519. 2. A task has a parent with the property :ORDERED:, and there
  11520. are siblings prior to the current task with incomplete
  11521. status.
  11522. 3. The parent of the task is blocked because it has siblings that should
  11523. be done first, or is child of a block grandparent TODO entry."
  11524. (if (not org-enforce-todo-dependencies)
  11525. t ; if locally turned off don't block
  11526. (catch 'dont-block
  11527. ;; If this is not a todo state change, or if this entry is already DONE,
  11528. ;; do not block
  11529. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11530. (member (plist-get change-plist :from)
  11531. (cons 'done org-done-keywords))
  11532. (member (plist-get change-plist :to)
  11533. (cons 'todo org-not-done-keywords))
  11534. (not (plist-get change-plist :to)))
  11535. (throw 'dont-block t))
  11536. ;; If this task has children, and any are undone, it's blocked
  11537. (save-excursion
  11538. (org-back-to-heading t)
  11539. (let ((this-level (funcall outline-level)))
  11540. (outline-next-heading)
  11541. (let ((child-level (funcall outline-level)))
  11542. (while (and (not (eobp))
  11543. (> child-level this-level))
  11544. ;; this todo has children, check whether they are all
  11545. ;; completed
  11546. (if (and (not (org-entry-is-done-p))
  11547. (org-entry-is-todo-p))
  11548. (progn (setq org-block-entry-blocking (org-get-heading))
  11549. (throw 'dont-block nil)))
  11550. (outline-next-heading)
  11551. (setq child-level (funcall outline-level))))))
  11552. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11553. ;; any previous siblings are undone, it's blocked
  11554. (save-excursion
  11555. (org-back-to-heading t)
  11556. (let* ((pos (point))
  11557. (parent-pos (and (org-up-heading-safe) (point))))
  11558. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11559. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11560. (forward-line 1)
  11561. (re-search-forward org-not-done-heading-regexp pos t))
  11562. (setq org-block-entry-blocking (match-string 0))
  11563. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11564. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11565. (while t
  11566. (goto-char parent-pos)
  11567. (if (not (looking-at org-not-done-heading-regexp))
  11568. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11569. (setq pos (point))
  11570. (setq parent-pos (and (org-up-heading-safe) (point)))
  11571. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11572. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11573. (forward-line 1)
  11574. (re-search-forward org-not-done-heading-regexp pos t)
  11575. (setq org-block-entry-blocking (org-get-heading)))
  11576. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11577. (defcustom org-track-ordered-property-with-tag nil
  11578. "Should the ORDERED property also be shown as a tag?
  11579. The ORDERED property decides if an entry should require subtasks to be
  11580. completed in sequence. Since a property is not very visible, setting
  11581. this option means that toggling the ORDERED property with the command
  11582. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11583. not relevant for the behavior, but it makes things more visible.
  11584. Note that toggling the tag with tags commands will not change the property
  11585. and therefore not influence behavior!
  11586. This can be t, meaning the tag ORDERED should be used, It can also be a
  11587. string to select a different tag for this task."
  11588. :group 'org-todo
  11589. :type '(choice
  11590. (const :tag "No tracking" nil)
  11591. (const :tag "Track with ORDERED tag" t)
  11592. (string :tag "Use other tag")))
  11593. (defun org-toggle-ordered-property ()
  11594. "Toggle the ORDERED property of the current entry.
  11595. For better visibility, you can track the value of this property with a tag.
  11596. See variable `org-track-ordered-property-with-tag'."
  11597. (interactive)
  11598. (let* ((t1 org-track-ordered-property-with-tag)
  11599. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11600. (save-excursion
  11601. (org-back-to-heading)
  11602. (if (org-entry-get nil "ORDERED")
  11603. (progn
  11604. (org-delete-property "ORDERED")
  11605. (and tag (org-toggle-tag tag 'off))
  11606. (message "Subtasks can be completed in arbitrary order"))
  11607. (org-entry-put nil "ORDERED" "t")
  11608. (and tag (org-toggle-tag tag 'on))
  11609. (message "Subtasks must be completed in sequence")))))
  11610. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11611. (defun org-block-todo-from-checkboxes (change-plist)
  11612. "Block turning an entry into a TODO, using checkboxes.
  11613. This checks whether the current task should be blocked from state
  11614. changes because there are unchecked boxes in this entry."
  11615. (if (not org-enforce-todo-checkbox-dependencies)
  11616. t ; if locally turned off don't block
  11617. (catch 'dont-block
  11618. ;; If this is not a todo state change, or if this entry is already DONE,
  11619. ;; do not block
  11620. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11621. (member (plist-get change-plist :from)
  11622. (cons 'done org-done-keywords))
  11623. (member (plist-get change-plist :to)
  11624. (cons 'todo org-not-done-keywords))
  11625. (not (plist-get change-plist :to)))
  11626. (throw 'dont-block t))
  11627. ;; If this task has checkboxes that are not checked, it's blocked
  11628. (save-excursion
  11629. (org-back-to-heading t)
  11630. (let ((beg (point)) end)
  11631. (outline-next-heading)
  11632. (setq end (point))
  11633. (goto-char beg)
  11634. (if (org-list-search-forward
  11635. (concat (org-item-beginning-re)
  11636. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11637. "\\[[- ]\\]")
  11638. end t)
  11639. (progn
  11640. (if (boundp 'org-blocked-by-checkboxes)
  11641. (setq org-blocked-by-checkboxes t))
  11642. (throw 'dont-block nil)))))
  11643. t))) ; do not block
  11644. (defun org-entry-blocked-p ()
  11645. "Non-nil if entry at point is blocked."
  11646. (and (not (org-entry-get nil "NOBLOCKING"))
  11647. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11648. (not (run-hook-with-args-until-failure
  11649. 'org-blocker-hook
  11650. (list :type 'todo-state-change
  11651. :position (point)
  11652. :from 'todo
  11653. :to 'done)))))
  11654. (defun org-update-statistics-cookies (all)
  11655. "Update the statistics cookie, either from TODO or from checkboxes.
  11656. This should be called with the cursor in a line with a statistics cookie."
  11657. (interactive "P")
  11658. (if all
  11659. (progn
  11660. (org-update-checkbox-count 'all)
  11661. (org-map-entries 'org-update-parent-todo-statistics))
  11662. (if (not (org-at-heading-p))
  11663. (org-update-checkbox-count)
  11664. (let ((pos (point-marker))
  11665. end l1 l2)
  11666. (ignore-errors (org-back-to-heading t))
  11667. (if (not (org-at-heading-p))
  11668. (org-update-checkbox-count)
  11669. (setq l1 (org-outline-level))
  11670. (setq end (save-excursion
  11671. (outline-next-heading)
  11672. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11673. (point)))
  11674. (if (and (save-excursion
  11675. (re-search-forward
  11676. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11677. (not (save-excursion (re-search-forward
  11678. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11679. (org-update-checkbox-count)
  11680. (if (and l2 (> l2 l1))
  11681. (progn
  11682. (goto-char end)
  11683. (org-update-parent-todo-statistics))
  11684. (goto-char pos)
  11685. (beginning-of-line 1)
  11686. (while (re-search-forward
  11687. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11688. (point-at-eol) t)
  11689. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11690. (goto-char pos)
  11691. (move-marker pos nil)))))
  11692. (defvar org-entry-property-inherited-from) ;; defined below
  11693. (defun org-update-parent-todo-statistics ()
  11694. "Update any statistics cookie in the parent of the current headline.
  11695. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11696. the entire subtree and this will travel up the hierarchy and update
  11697. statistics everywhere."
  11698. (let* ((prop (save-excursion (org-up-heading-safe)
  11699. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11700. (recursive (or (not org-hierarchical-todo-statistics)
  11701. (and prop (string-match "\\<recursive\\>" prop))))
  11702. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11703. 0))
  11704. (first t)
  11705. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11706. level ltoggle l1 new ndel
  11707. (cnt-all 0) (cnt-done 0) is-percent kwd
  11708. checkbox-beg ov ovs ove cookie-present)
  11709. (catch 'exit
  11710. (save-excursion
  11711. (beginning-of-line 1)
  11712. (setq ltoggle (funcall outline-level))
  11713. ;; Three situations are to consider:
  11714. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11715. ;; to the top-level ancestor on the headline;
  11716. ;; 2. If parent has "recursive" property, repeat up to the
  11717. ;; headline setting that property, taking inheritance into
  11718. ;; account;
  11719. ;; 3. Else, move up to direct parent and proceed only once.
  11720. (while (and (setq level (org-up-heading-safe))
  11721. (or recursive first)
  11722. (>= (point) lim))
  11723. (setq first nil cookie-present nil)
  11724. (unless (and level
  11725. (not (string-match
  11726. "\\<checkbox\\>"
  11727. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11728. "")))))
  11729. (throw 'exit nil))
  11730. (while (re-search-forward box-re (point-at-eol) t)
  11731. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11732. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11733. (save-match-data
  11734. (unless (outline-next-heading) (throw 'exit nil))
  11735. (while (and (looking-at org-complex-heading-regexp)
  11736. (> (setq l1 (length (match-string 1))) level))
  11737. (setq kwd (and (or recursive (= l1 ltoggle))
  11738. (match-string 2)))
  11739. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11740. (and (eq org-provide-todo-statistics t)
  11741. (or (member kwd org-done-keywords)))
  11742. (and (listp org-provide-todo-statistics)
  11743. (stringp (car org-provide-todo-statistics))
  11744. (or (member kwd org-provide-todo-statistics)
  11745. (member kwd org-done-keywords)))
  11746. (and (listp org-provide-todo-statistics)
  11747. (listp (car org-provide-todo-statistics))
  11748. (or (member kwd (car org-provide-todo-statistics))
  11749. (and (member kwd org-done-keywords)
  11750. (member kwd (cadr org-provide-todo-statistics))))))
  11751. (setq cnt-all (1+ cnt-all))
  11752. (if (eq org-provide-todo-statistics t)
  11753. (and kwd (setq cnt-all (1+ cnt-all)))))
  11754. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11755. (member kwd org-done-keywords))
  11756. (and (listp org-provide-todo-statistics)
  11757. (listp (car org-provide-todo-statistics))
  11758. (member kwd org-done-keywords)
  11759. (member kwd (cadr org-provide-todo-statistics)))
  11760. (and (listp org-provide-todo-statistics)
  11761. (stringp (car org-provide-todo-statistics))
  11762. (member kwd org-done-keywords)))
  11763. (setq cnt-done (1+ cnt-done)))
  11764. (outline-next-heading)))
  11765. (setq new
  11766. (if is-percent
  11767. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11768. (max 1 cnt-all)))
  11769. (format "[%d/%d]" cnt-done cnt-all))
  11770. ndel (- (match-end 0) checkbox-beg))
  11771. ;; handle overlays when updating cookie from column view
  11772. (when (setq ov (car (overlays-at checkbox-beg)))
  11773. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11774. (delete-overlay ov))
  11775. (goto-char checkbox-beg)
  11776. (insert new)
  11777. (delete-region (point) (+ (point) ndel))
  11778. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11779. (when ov (move-overlay ov ovs ove)))
  11780. (when cookie-present
  11781. (run-hook-with-args 'org-after-todo-statistics-hook
  11782. cnt-done (- cnt-all cnt-done))))))
  11783. (run-hooks 'org-todo-statistics-hook)))
  11784. (defvar org-after-todo-statistics-hook nil
  11785. "Hook that is called after a TODO statistics cookie has been updated.
  11786. Each function is called with two arguments: the number of not-done entries
  11787. and the number of done entries.
  11788. For example, the following function, when added to this hook, will switch
  11789. an entry to DONE when all children are done, and back to TODO when new
  11790. entries are set to a TODO status. Note that this hook is only called
  11791. when there is a statistics cookie in the headline!
  11792. (defun org-summary-todo (n-done n-not-done)
  11793. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11794. (let (org-log-done org-log-states) ; turn off logging
  11795. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11796. ")
  11797. (defvar org-todo-statistics-hook nil
  11798. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11799. This hook runs even if there is no statistics cookie present, in which case
  11800. `org-after-todo-statistics-hook' would not run.")
  11801. (defun org-todo-trigger-tag-changes (state)
  11802. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11803. (let ((l org-todo-state-tags-triggers)
  11804. changes)
  11805. (when (or (not state) (equal state ""))
  11806. (setq changes (append changes (cdr (assoc "" l)))))
  11807. (when (and (stringp state) (> (length state) 0))
  11808. (setq changes (append changes (cdr (assoc state l)))))
  11809. (when (member state org-not-done-keywords)
  11810. (setq changes (append changes (cdr (assoc 'todo l)))))
  11811. (when (member state org-done-keywords)
  11812. (setq changes (append changes (cdr (assoc 'done l)))))
  11813. (dolist (c changes)
  11814. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11815. (defun org-local-logging (value)
  11816. "Get logging settings from a property VALUE."
  11817. ;; Directly set the variables, they are already local.
  11818. (setq org-log-done nil
  11819. org-log-repeat nil
  11820. org-todo-log-states nil)
  11821. (dolist (w (org-split-string value))
  11822. (let (a)
  11823. (cond
  11824. ((setq a (assoc w org-startup-options))
  11825. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11826. (set (nth 1 a) (nth 2 a))))
  11827. ((setq a (org-extract-log-state-settings w))
  11828. (and (member (car a) org-todo-keywords-1)
  11829. (push a org-todo-log-states)))))))
  11830. (defun org-get-todo-sequence-head (kwd)
  11831. "Return the head of the TODO sequence to which KWD belongs.
  11832. If KWD is not set, check if there is a text property remembering the
  11833. right sequence."
  11834. (let (p)
  11835. (cond
  11836. ((not kwd)
  11837. (or (get-text-property (point-at-bol) 'org-todo-head)
  11838. (progn
  11839. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11840. nil (point-at-eol)))
  11841. (get-text-property p 'org-todo-head))))
  11842. ((not (member kwd org-todo-keywords-1))
  11843. (car org-todo-keywords-1))
  11844. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11845. (defun org-fast-todo-selection ()
  11846. "Fast TODO keyword selection with single keys.
  11847. Returns the new TODO keyword, or nil if no state change should occur."
  11848. (let* ((fulltable org-todo-key-alist)
  11849. (done-keywords org-done-keywords) ;; needed for the faces.
  11850. (maxlen (apply 'max (mapcar
  11851. (lambda (x)
  11852. (if (stringp (car x)) (string-width (car x)) 0))
  11853. fulltable)))
  11854. (expert nil)
  11855. (fwidth (+ maxlen 3 1 3))
  11856. (ncol (/ (- (window-width) 4) fwidth))
  11857. tg cnt e c tbl
  11858. groups ingroup)
  11859. (save-excursion
  11860. (save-window-excursion
  11861. (if expert
  11862. (set-buffer (get-buffer-create " *Org todo*"))
  11863. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11864. (erase-buffer)
  11865. (org-set-local 'org-done-keywords done-keywords)
  11866. (setq tbl fulltable cnt 0)
  11867. (while (setq e (pop tbl))
  11868. (cond
  11869. ((equal e '(:startgroup))
  11870. (push '() groups) (setq ingroup t)
  11871. (when (not (= cnt 0))
  11872. (setq cnt 0)
  11873. (insert "\n"))
  11874. (insert "{ "))
  11875. ((equal e '(:endgroup))
  11876. (setq ingroup nil cnt 0)
  11877. (insert "}\n"))
  11878. ((equal e '(:newline))
  11879. (when (not (= cnt 0))
  11880. (setq cnt 0)
  11881. (insert "\n")
  11882. (setq e (car tbl))
  11883. (while (equal (car tbl) '(:newline))
  11884. (insert "\n")
  11885. (setq tbl (cdr tbl)))))
  11886. (t
  11887. (setq tg (car e) c (cdr e))
  11888. (if ingroup (push tg (car groups)))
  11889. (setq tg (org-add-props tg nil 'face
  11890. (org-get-todo-face tg)))
  11891. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11892. (insert "[" c "] " tg (make-string
  11893. (- fwidth 4 (length tg)) ?\ ))
  11894. (when (= (setq cnt (1+ cnt)) ncol)
  11895. (insert "\n")
  11896. (if ingroup (insert " "))
  11897. (setq cnt 0)))))
  11898. (insert "\n")
  11899. (goto-char (point-min))
  11900. (if (not expert) (org-fit-window-to-buffer))
  11901. (message "[a-z..]:Set [SPC]:clear")
  11902. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11903. (cond
  11904. ((or (= c ?\C-g)
  11905. (and (= c ?q) (not (rassoc c fulltable))))
  11906. (setq quit-flag t))
  11907. ((= c ?\ ) nil)
  11908. ((setq e (rassoc c fulltable) tg (car e))
  11909. tg)
  11910. (t (setq quit-flag t)))))))
  11911. (defun org-entry-is-todo-p ()
  11912. (member (org-get-todo-state) org-not-done-keywords))
  11913. (defun org-entry-is-done-p ()
  11914. (member (org-get-todo-state) org-done-keywords))
  11915. (defun org-get-todo-state ()
  11916. "Return the TODO keyword of the current subtree."
  11917. (save-excursion
  11918. (org-back-to-heading t)
  11919. (and (looking-at org-todo-line-regexp)
  11920. (match-end 2)
  11921. (match-string 2))))
  11922. (defun org-at-date-range-p (&optional inactive-ok)
  11923. "Non-nil if point is inside a date range.
  11924. When optional argument INACTIVE-OK is non-nil, also consider
  11925. inactive time ranges.
  11926. When this function returns a non-nil value, match data is set
  11927. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11928. on INACTIVE-OK."
  11929. (interactive)
  11930. (save-excursion
  11931. (catch 'exit
  11932. (let ((pos (point)))
  11933. (skip-chars-backward "^[<\r\n")
  11934. (skip-chars-backward "<[")
  11935. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11936. (>= (match-end 0) pos)
  11937. (throw 'exit t))
  11938. (skip-chars-backward "^<[\r\n")
  11939. (skip-chars-backward "<[")
  11940. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11941. (>= (match-end 0) pos)
  11942. (throw 'exit t)))
  11943. nil)))
  11944. (defun org-get-repeat (&optional tagline)
  11945. "Check if there is a deadline/schedule with repeater in this entry."
  11946. (save-match-data
  11947. (save-excursion
  11948. (org-back-to-heading t)
  11949. (and (re-search-forward (if tagline
  11950. (concat tagline "\\s-*" org-repeat-re)
  11951. org-repeat-re)
  11952. (org-entry-end-position) t)
  11953. (match-string-no-properties 1)))))
  11954. (defvar org-last-changed-timestamp)
  11955. (defvar org-last-inserted-timestamp)
  11956. (defvar org-log-post-message)
  11957. (defvar org-log-note-purpose)
  11958. (defvar org-log-note-how nil)
  11959. (defvar org-log-note-extra)
  11960. (defun org-auto-repeat-maybe (done-word)
  11961. "Check if the current headline contains a repeated deadline/schedule.
  11962. If yes, set TODO state back to what it was and change the base date
  11963. of repeating deadline/scheduled time stamps to new date.
  11964. This function is run automatically after each state change to a DONE state."
  11965. ;; last-state is dynamically scoped into this function
  11966. (let* ((repeat (org-get-repeat))
  11967. (aa (assoc org-last-state org-todo-kwd-alist))
  11968. (interpret (nth 1 aa))
  11969. (head (nth 2 aa))
  11970. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11971. (msg "Entry repeats: ")
  11972. (org-log-done nil)
  11973. (org-todo-log-states nil)
  11974. re type n what ts time to-state)
  11975. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11976. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11977. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11978. org-todo-repeat-to-state))
  11979. (unless (and to-state (member to-state org-todo-keywords-1))
  11980. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11981. (org-todo to-state)
  11982. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11983. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11984. (org-time-stamp-format t t))))
  11985. (when org-log-repeat
  11986. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11987. (memq 'org-add-log-note post-command-hook))
  11988. ;; OK, we are already setup for some record
  11989. (if (eq org-log-repeat 'note)
  11990. ;; make sure we take a note, not only a time stamp
  11991. (setq org-log-note-how 'note))
  11992. ;; Set up for taking a record
  11993. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11994. org-last-state
  11995. 'findpos org-log-repeat)))
  11996. (org-back-to-heading t)
  11997. (org-add-planning-info nil nil 'closed)
  11998. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11999. org-deadline-time-regexp "\\)\\|\\("
  12000. org-ts-regexp "\\)"))
  12001. (while (re-search-forward
  12002. re (save-excursion (outline-next-heading) (point)) t)
  12003. (setq type (if (match-end 1) org-scheduled-string
  12004. (if (match-end 3) org-deadline-string "Plain:"))
  12005. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  12006. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  12007. (setq n (string-to-number (match-string 2 ts))
  12008. what (match-string 3 ts))
  12009. (if (equal what "w") (setq n (* n 7) what "d"))
  12010. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  12011. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  12012. ;; Preparation, see if we need to modify the start date for the change
  12013. (when (match-end 1)
  12014. (setq time (save-match-data (org-time-string-to-time ts)))
  12015. (cond
  12016. ((equal (match-string 1 ts) ".")
  12017. ;; Shift starting date to today
  12018. (org-timestamp-change
  12019. (- (org-today) (time-to-days time))
  12020. 'day))
  12021. ((equal (match-string 1 ts) "+")
  12022. (let ((nshiftmax 10) (nshift 0))
  12023. (while (or (= nshift 0)
  12024. (<= (time-to-days time)
  12025. (time-to-days (current-time))))
  12026. (when (= (incf nshift) nshiftmax)
  12027. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12028. (user-error "Abort")))
  12029. (org-timestamp-change n (cdr (assoc what whata)))
  12030. (org-at-timestamp-p t)
  12031. (setq ts (match-string 1))
  12032. (setq time (save-match-data (org-time-string-to-time ts)))))
  12033. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12034. ;; rematch, so that we have everything in place for the real shift
  12035. (org-at-timestamp-p t)
  12036. (setq ts (match-string 1))
  12037. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12038. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12039. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12040. (setq org-log-post-message msg)
  12041. (message "%s" msg))))
  12042. (defun org-show-todo-tree (arg)
  12043. "Make a compact tree which shows all headlines marked with TODO.
  12044. The tree will show the lines where the regexp matches, and all higher
  12045. headlines above the match.
  12046. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12047. With a numeric prefix N, construct a sparse tree for the Nth element
  12048. of `org-todo-keywords-1'."
  12049. (interactive "P")
  12050. (let ((case-fold-search nil)
  12051. (kwd-re
  12052. (cond ((null arg) org-not-done-regexp)
  12053. ((equal arg '(4))
  12054. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12055. (mapcar 'list org-todo-keywords-1))))
  12056. (concat "\\("
  12057. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12058. "\\)\\>")))
  12059. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12060. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12061. org-todo-keywords-1)))
  12062. (t (user-error "Invalid prefix argument: %s" arg)))))
  12063. (message "%d TODO entries found"
  12064. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12065. (defun org-deadline (arg &optional time)
  12066. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12067. With one universal prefix argument, remove any deadline from the item.
  12068. With two universal prefix arguments, prompt for a warning delay.
  12069. With argument TIME, set the deadline at the corresponding date. TIME
  12070. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12071. (interactive "P")
  12072. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12073. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12074. 'region-start-level 'region))
  12075. org-loop-over-headlines-in-active-region)
  12076. (org-map-entries
  12077. `(org-deadline ',arg ,time)
  12078. org-loop-over-headlines-in-active-region
  12079. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12080. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12081. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12082. (repeater (and old-date
  12083. (string-match
  12084. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12085. old-date)
  12086. (match-string 1 old-date))))
  12087. (cond
  12088. ((equal arg '(4))
  12089. (when (and old-date org-log-redeadline)
  12090. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12091. org-log-redeadline))
  12092. (org-remove-timestamp-with-keyword org-deadline-string)
  12093. (message "Item no longer has a deadline."))
  12094. ((equal arg '(16))
  12095. (save-excursion
  12096. (org-back-to-heading t)
  12097. (if (re-search-forward
  12098. org-deadline-time-regexp
  12099. (save-excursion (outline-next-heading) (point)) t)
  12100. (let* ((rpl0 (match-string 1))
  12101. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12102. (replace-match
  12103. (concat org-deadline-string
  12104. " <" rpl
  12105. (format " -%dd"
  12106. (abs
  12107. (- (time-to-days
  12108. (save-match-data
  12109. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12110. (time-to-days old-date-time))))
  12111. ">") t t))
  12112. (user-error "No deadline information to update"))))
  12113. (t
  12114. (org-add-planning-info 'deadline time 'closed)
  12115. (when (and old-date
  12116. org-log-redeadline
  12117. (not (equal old-date org-last-inserted-timestamp)))
  12118. (org-add-log-setup
  12119. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12120. org-log-redeadline))
  12121. (when repeater
  12122. (save-excursion
  12123. (org-back-to-heading t)
  12124. (when (re-search-forward (concat org-deadline-string " "
  12125. org-last-inserted-timestamp)
  12126. (save-excursion
  12127. (outline-next-heading) (point)) t)
  12128. (goto-char (1- (match-end 0)))
  12129. (insert " " repeater)
  12130. (setq org-last-inserted-timestamp
  12131. (concat (substring org-last-inserted-timestamp 0 -1)
  12132. " " repeater
  12133. (substring org-last-inserted-timestamp -1))))))
  12134. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12135. (defun org-schedule (arg &optional time)
  12136. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12137. With one universal prefix argument, remove any scheduling date from the item.
  12138. With two universal prefix arguments, prompt for a delay cookie.
  12139. With argument TIME, scheduled at the corresponding date. TIME can
  12140. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12141. (interactive "P")
  12142. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12143. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12144. 'region-start-level 'region))
  12145. org-loop-over-headlines-in-active-region)
  12146. (org-map-entries
  12147. `(org-schedule ',arg ,time)
  12148. org-loop-over-headlines-in-active-region
  12149. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12150. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12151. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12152. (repeater (and old-date
  12153. (string-match
  12154. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12155. old-date)
  12156. (match-string 1 old-date))))
  12157. (cond
  12158. ((equal arg '(4))
  12159. (progn
  12160. (when (and old-date org-log-reschedule)
  12161. (org-add-log-setup 'delschedule nil old-date 'findpos
  12162. org-log-reschedule))
  12163. (org-remove-timestamp-with-keyword org-scheduled-string)
  12164. (message "Item is no longer scheduled.")))
  12165. ((equal arg '(16))
  12166. (save-excursion
  12167. (org-back-to-heading t)
  12168. (if (re-search-forward
  12169. org-scheduled-time-regexp
  12170. (save-excursion (outline-next-heading) (point)) t)
  12171. (let* ((rpl0 (match-string 1))
  12172. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12173. (replace-match
  12174. (concat org-scheduled-string
  12175. " <" rpl
  12176. (format " -%dd"
  12177. (abs
  12178. (- (time-to-days
  12179. (save-match-data
  12180. (org-read-date nil t nil "Delay until" old-date-time)))
  12181. (time-to-days old-date-time))))
  12182. ">") t t))
  12183. (user-error "No scheduled information to update"))))
  12184. (t
  12185. (org-add-planning-info 'scheduled time 'closed)
  12186. (when (and old-date
  12187. org-log-reschedule
  12188. (not (equal old-date org-last-inserted-timestamp)))
  12189. (org-add-log-setup
  12190. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12191. org-log-reschedule))
  12192. (when repeater
  12193. (save-excursion
  12194. (org-back-to-heading t)
  12195. (when (re-search-forward (concat org-scheduled-string " "
  12196. org-last-inserted-timestamp)
  12197. (save-excursion
  12198. (outline-next-heading) (point)) t)
  12199. (goto-char (1- (match-end 0)))
  12200. (insert " " repeater)
  12201. (setq org-last-inserted-timestamp
  12202. (concat (substring org-last-inserted-timestamp 0 -1)
  12203. " " repeater
  12204. (substring org-last-inserted-timestamp -1))))))
  12205. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12206. (defun org-get-scheduled-time (pom &optional inherit)
  12207. "Get the scheduled time as a time tuple, of a format suitable
  12208. for calling org-schedule with, or if there is no scheduling,
  12209. returns nil."
  12210. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12211. (when time
  12212. (apply 'encode-time (org-parse-time-string time)))))
  12213. (defun org-get-deadline-time (pom &optional inherit)
  12214. "Get the deadline as a time tuple, of a format suitable for
  12215. calling org-deadline with, or if there is no scheduling, returns
  12216. nil."
  12217. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12218. (when time
  12219. (apply 'encode-time (org-parse-time-string time)))))
  12220. (defun org-remove-timestamp-with-keyword (keyword)
  12221. "Remove all time stamps with KEYWORD in the current entry."
  12222. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12223. beg)
  12224. (save-excursion
  12225. (org-back-to-heading t)
  12226. (setq beg (point))
  12227. (outline-next-heading)
  12228. (while (re-search-backward re beg t)
  12229. (replace-match "")
  12230. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12231. (equal (char-before) ?\ ))
  12232. (backward-delete-char 1)
  12233. (if (string-match "^[ \t]*$" (buffer-substring
  12234. (point-at-bol) (point-at-eol)))
  12235. (delete-region (point-at-bol)
  12236. (min (point-max) (1+ (point-at-eol))))))))))
  12237. (defvar org-time-was-given) ; dynamically scoped parameter
  12238. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12239. (defun org-at-planning-p ()
  12240. "Non-nil when point is on a planning info line."
  12241. ;; This is as accurate and faster than `org-element-at-point' since
  12242. ;; planning info location is fixed in the section.
  12243. (org-with-wide-buffer
  12244. (beginning-of-line)
  12245. (and (org-looking-at-p org-planning-line-re)
  12246. (eq (point)
  12247. (ignore-errors
  12248. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12249. (org-back-to-heading t)
  12250. (org-with-limited-levels (org-back-to-heading t)))
  12251. (line-beginning-position 2))))))
  12252. (defun org-add-planning-info (what &optional time &rest remove)
  12253. "Insert new timestamp with keyword in the planning line.
  12254. WHAT indicates what kind of time stamp to add. It is a symbol
  12255. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12256. the time to use. If none is given, the user is prompted for
  12257. a date. REMOVE indicates what kind of entries to remove. An old
  12258. WHAT entry will also be removed."
  12259. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12260. (catch 'exit
  12261. (when (and (memq what '(scheduled deadline))
  12262. (or (not time)
  12263. (and (stringp time)
  12264. (string-match "^[-+]+[0-9]" time))))
  12265. ;; Try to get a default date/time from existing timestamp
  12266. (save-excursion
  12267. (org-back-to-heading t)
  12268. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12269. (when (re-search-forward (if (eq what 'scheduled)
  12270. org-scheduled-time-regexp
  12271. org-deadline-time-regexp)
  12272. end t)
  12273. (setq ts (match-string 1)
  12274. default-time (apply 'encode-time (org-parse-time-string ts))
  12275. default-input (and ts (org-get-compact-tod ts)))))))
  12276. (when what
  12277. (setq time
  12278. (if (stringp time)
  12279. ;; This is a string (relative or absolute), set
  12280. ;; proper date.
  12281. (apply #'encode-time
  12282. (org-read-date-analyze
  12283. time default-time (decode-time default-time)))
  12284. ;; If necessary, get the time from the user
  12285. (or time (org-read-date nil 'to-time nil nil
  12286. default-time default-input)))))
  12287. (org-with-wide-buffer
  12288. (org-back-to-heading t)
  12289. (forward-line)
  12290. (unless (bolp) (insert "\n"))
  12291. (cond ((org-looking-at-p org-planning-line-re)
  12292. ;; Move to current indentation.
  12293. (skip-chars-forward " \t")
  12294. ;; Check if we have to remove something.
  12295. (dolist (type (if what (cons what remove) remove))
  12296. (save-excursion
  12297. (when (re-search-forward
  12298. (case type
  12299. (closed org-closed-time-regexp)
  12300. (deadline org-deadline-time-regexp)
  12301. (scheduled org-scheduled-time-regexp)
  12302. (otherwise
  12303. (error "Invalid planning type: %s" type)))
  12304. (line-end-position) t)
  12305. ;; Delete until next keyword or end of line.
  12306. (delete-region
  12307. (match-beginning 0)
  12308. (if (re-search-forward org-keyword-time-not-clock-regexp
  12309. (line-end-position)
  12310. t)
  12311. (match-beginning 0)
  12312. (line-end-position))))))
  12313. ;; If there is nothing more to add and no more keyword
  12314. ;; is left, remove the line completely.
  12315. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12316. (delete-region (line-beginning-position)
  12317. (line-beginning-position 2))
  12318. ;; If we removed last keyword, do not leave trailing
  12319. ;; white space at the end of line.
  12320. (let ((p (point)))
  12321. (save-excursion
  12322. (end-of-line)
  12323. (unless (= (skip-chars-backward " \t" p) 0)
  12324. (delete-region (point) (line-end-position)))))))
  12325. ((not what) (throw 'exit nil)) ; Nothing to do.
  12326. (t (insert-before-markers "\n")
  12327. (backward-char 1)
  12328. (when org-adapt-indentation
  12329. (org-indent-to-column (1+ (org-outline-level))))))
  12330. (when what
  12331. ;; Insert planning keyword.
  12332. (insert (case what
  12333. (closed org-closed-string)
  12334. (deadline org-deadline-string)
  12335. (scheduled org-scheduled-string)
  12336. (otherwise (error "Invalid planning type: %s" what)))
  12337. " ")
  12338. ;; Insert associated timestamp.
  12339. (let ((ts (org-insert-time-stamp
  12340. time
  12341. (or org-time-was-given
  12342. (and (eq what 'closed) org-log-done-with-time))
  12343. (eq what 'closed)
  12344. nil nil (list org-end-time-was-given))))
  12345. (unless (eolp) (insert " "))
  12346. ts))))))
  12347. (defvar org-log-note-marker (make-marker))
  12348. (defvar org-log-note-purpose nil)
  12349. (defvar org-log-note-state nil)
  12350. (defvar org-log-note-previous-state nil)
  12351. (defvar org-log-note-extra nil)
  12352. (defvar org-log-note-window-configuration nil)
  12353. (defvar org-log-note-return-to (make-marker))
  12354. (defvar org-log-note-effective-time nil
  12355. "Remembered current time so that dynamically scoped
  12356. `org-extend-today-until' affects tha timestamps in state change
  12357. log")
  12358. (defvar org-log-post-message nil
  12359. "Message to be displayed after a log note has been stored.
  12360. The auto-repeater uses this.")
  12361. (defun org-add-note ()
  12362. "Add a note to the current entry.
  12363. This is done in the same way as adding a state change note."
  12364. (interactive)
  12365. (org-add-log-setup 'note nil nil 'findpos nil))
  12366. (defun org-log-beginning (&optional create)
  12367. "Return expected start of log notes in current entry.
  12368. When optional argument CREATE is non-nil, the function creates
  12369. a drawer to store notes, if necessary. Returned position ignores
  12370. narrowing."
  12371. (org-with-wide-buffer
  12372. (org-end-of-meta-data)
  12373. (let ((end (if (org-at-heading-p) (point)
  12374. (save-excursion (outline-next-heading) (point))))
  12375. (drawer (org-log-into-drawer)))
  12376. (cond
  12377. (drawer
  12378. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12379. (case-fold-search t))
  12380. (catch 'exit
  12381. ;; Try to find existing drawer.
  12382. (while (re-search-forward regexp end t)
  12383. (let ((element (org-element-at-point)))
  12384. (when (eq (org-element-type element) 'drawer)
  12385. (let ((cend (org-element-property :contents-end element)))
  12386. (when (and (not org-log-states-order-reversed) cend)
  12387. (goto-char cend)))
  12388. (throw 'exit nil))))
  12389. ;; No drawer found. Create one, if permitted.
  12390. (when create
  12391. (unless (bolp) (insert "\n"))
  12392. (let ((beg (point)))
  12393. (insert ":" drawer ":\n:END:\n")
  12394. (org-indent-region beg (point)))
  12395. (end-of-line -1)))))
  12396. (org-log-state-notes-insert-after-drawers
  12397. (while (and (looking-at org-drawer-regexp)
  12398. (progn (goto-char (match-end 0))
  12399. (re-search-forward org-property-end-re end t)))
  12400. (forward-line)))))
  12401. (if (bolp) (point) (line-beginning-position 2))))
  12402. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12403. "Set up the post command hook to take a note.
  12404. If this is about to TODO state change, the new state is expected in STATE.
  12405. When FINDPOS is non-nil, find the correct position for the note in
  12406. the current entry. If not, assume that it can be inserted at point.
  12407. HOW is an indicator what kind of note should be created.
  12408. EXTRA is additional text that will be inserted into the notes buffer."
  12409. (org-with-wide-buffer
  12410. (when findpos
  12411. (goto-char (org-log-beginning t))
  12412. (unless org-log-states-order-reversed
  12413. (org-skip-over-state-notes)
  12414. (skip-chars-backward " \t\n\r")
  12415. (forward-line)))
  12416. (move-marker org-log-note-marker (point))
  12417. ;; Preserve position even if a property drawer is inserted in the
  12418. ;; process.
  12419. (set-marker-insertion-type org-log-note-marker t)
  12420. (setq org-log-note-purpose purpose
  12421. org-log-note-state state
  12422. org-log-note-previous-state prev-state
  12423. org-log-note-how how
  12424. org-log-note-extra extra
  12425. org-log-note-effective-time (org-current-effective-time))
  12426. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12427. (defun org-skip-over-state-notes ()
  12428. "Skip past the list of State notes in an entry."
  12429. (when (ignore-errors (goto-char (org-in-item-p)))
  12430. (let* ((struct (org-list-struct))
  12431. (prevs (org-list-prevs-alist struct))
  12432. (regexp
  12433. (concat "[ \t]*- +"
  12434. (replace-regexp-in-string
  12435. " +" " +"
  12436. (org-replace-escapes
  12437. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12438. `(("%d" . ,org-ts-regexp-inactive)
  12439. ("%D" . ,org-ts-regexp)
  12440. ("%s" . "\"\\S-+\"")
  12441. ("%S" . "\"\\S-+\"")
  12442. ("%t" . ,org-ts-regexp-inactive)
  12443. ("%T" . ,org-ts-regexp)
  12444. ("%u" . ".*?")
  12445. ("%U" . ".*?")))))))
  12446. (while (org-looking-at-p regexp)
  12447. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12448. (org-list-get-item-end (point) struct)))))))
  12449. (defun org-add-log-note (&optional purpose)
  12450. "Pop up a window for taking a note, and add this note later at point."
  12451. (remove-hook 'post-command-hook 'org-add-log-note)
  12452. (setq org-log-note-window-configuration (current-window-configuration))
  12453. (delete-other-windows)
  12454. (move-marker org-log-note-return-to (point))
  12455. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12456. (goto-char org-log-note-marker)
  12457. (org-switch-to-buffer-other-window "*Org Note*")
  12458. (erase-buffer)
  12459. (if (memq org-log-note-how '(time state))
  12460. (let (current-prefix-arg) (org-store-log-note))
  12461. (let ((org-inhibit-startup t)) (org-mode))
  12462. (insert (format "# Insert note for %s.
  12463. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12464. (cond
  12465. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12466. ((eq org-log-note-purpose 'done) "closed todo item")
  12467. ((eq org-log-note-purpose 'state)
  12468. (format "state change from \"%s\" to \"%s\""
  12469. (or org-log-note-previous-state "")
  12470. (or org-log-note-state "")))
  12471. ((eq org-log-note-purpose 'reschedule)
  12472. "rescheduling")
  12473. ((eq org-log-note-purpose 'delschedule)
  12474. "no longer scheduled")
  12475. ((eq org-log-note-purpose 'redeadline)
  12476. "changing deadline")
  12477. ((eq org-log-note-purpose 'deldeadline)
  12478. "removing deadline")
  12479. ((eq org-log-note-purpose 'refile)
  12480. "refiling")
  12481. ((eq org-log-note-purpose 'note)
  12482. "this entry")
  12483. (t (error "This should not happen")))))
  12484. (if org-log-note-extra (insert org-log-note-extra))
  12485. (org-set-local 'org-finish-function 'org-store-log-note)
  12486. (run-hooks 'org-log-buffer-setup-hook)))
  12487. (defvar org-note-abort nil) ; dynamically scoped
  12488. (defun org-store-log-note ()
  12489. "Finish taking a log note, and insert it to where it belongs."
  12490. (let ((txt (buffer-string)))
  12491. (kill-buffer (current-buffer))
  12492. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12493. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12494. (setq txt (replace-match "" t t txt)))
  12495. (if (string-match "\\s-+\\'" txt)
  12496. (setq txt (replace-match "" t t txt)))
  12497. (setq lines (org-split-string txt "\n"))
  12498. (when (and note (string-match "\\S-" note))
  12499. (setq note
  12500. (org-replace-escapes
  12501. note
  12502. (list (cons "%u" (user-login-name))
  12503. (cons "%U" user-full-name)
  12504. (cons "%t" (format-time-string
  12505. (org-time-stamp-format 'long 'inactive)
  12506. org-log-note-effective-time))
  12507. (cons "%T" (format-time-string
  12508. (org-time-stamp-format 'long nil)
  12509. org-log-note-effective-time))
  12510. (cons "%d" (format-time-string
  12511. (org-time-stamp-format nil 'inactive)
  12512. org-log-note-effective-time))
  12513. (cons "%D" (format-time-string
  12514. (org-time-stamp-format nil nil)
  12515. org-log-note-effective-time))
  12516. (cons "%s" (cond
  12517. ((not org-log-note-state) "")
  12518. ((org-string-match-p org-ts-regexp
  12519. org-log-note-state)
  12520. (format "\"[%s]\""
  12521. (substring org-log-note-state 1 -1)))
  12522. (t (format "\"%s\"" org-log-note-state))))
  12523. (cons "%S"
  12524. (cond
  12525. ((not org-log-note-previous-state) "")
  12526. ((org-string-match-p org-ts-regexp
  12527. org-log-note-previous-state)
  12528. (format "\"[%s]\""
  12529. (substring
  12530. org-log-note-previous-state 1 -1)))
  12531. (t (format "\"%s\""
  12532. org-log-note-previous-state)))))))
  12533. (when lines (setq note (concat note " \\\\")))
  12534. (push note lines))
  12535. (when (or current-prefix-arg org-note-abort)
  12536. (when (org-log-into-drawer)
  12537. (org-remove-empty-drawer-at org-log-note-marker))
  12538. (setq lines nil))
  12539. (when lines
  12540. (with-current-buffer (marker-buffer org-log-note-marker)
  12541. (org-with-wide-buffer
  12542. (goto-char org-log-note-marker)
  12543. (move-marker org-log-note-marker nil)
  12544. ;; Make sure point is at the beginning of an empty line.
  12545. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12546. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12547. ;; In an existing list, add a new item at the top level.
  12548. ;; Otherwise, indent line like a regular one.
  12549. (let ((itemp (org-in-item-p)))
  12550. (if itemp
  12551. (org-indent-line-to
  12552. (let ((struct (save-excursion
  12553. (goto-char itemp) (org-list-struct))))
  12554. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12555. (org-indent-line)))
  12556. (insert (org-list-bullet-string "-") (pop lines))
  12557. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12558. (dolist (line lines)
  12559. (insert "\n")
  12560. (org-indent-line-to ind)
  12561. (insert line)))
  12562. (message "Note stored")
  12563. (org-back-to-heading t)
  12564. (org-cycle-hide-drawers 'children))
  12565. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12566. ;; from within `org-add-log-note' because `buffer-undo-list'
  12567. ;; is then modified outside of `org-with-remote-undo'.
  12568. (when (eq this-command 'org-agenda-todo)
  12569. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12570. ;; Don't add undo information when called from `org-agenda-todo'
  12571. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12572. (set-window-configuration org-log-note-window-configuration)
  12573. (with-current-buffer (marker-buffer org-log-note-return-to)
  12574. (goto-char org-log-note-return-to))
  12575. (move-marker org-log-note-return-to nil)
  12576. (and org-log-post-message (message "%s" org-log-post-message))))
  12577. (defun org-remove-empty-drawer-at (pos)
  12578. "Remove an empty drawer at position POS.
  12579. POS may also be a marker."
  12580. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12581. (org-with-wide-buffer
  12582. (goto-char pos)
  12583. (let ((drawer (org-element-at-point)))
  12584. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12585. (not (org-element-property :contents-begin drawer)))
  12586. (delete-region (org-element-property :begin drawer)
  12587. (progn (goto-char (org-element-property :end drawer))
  12588. (skip-chars-backward " \r\t\n")
  12589. (forward-line)
  12590. (point))))))))
  12591. (defvar org-ts-type nil)
  12592. (defun org-sparse-tree (&optional arg type)
  12593. "Create a sparse tree, prompt for the details.
  12594. This command can create sparse trees. You first need to select the type
  12595. of match used to create the tree:
  12596. t Show all TODO entries.
  12597. T Show entries with a specific TODO keyword.
  12598. m Show entries selected by a tags/property match.
  12599. p Enter a property name and its value (both with completion on existing
  12600. names/values) and show entries with that property.
  12601. r Show entries matching a regular expression (`/' can be used as well).
  12602. b Show deadlines and scheduled items before a date.
  12603. a Show deadlines and scheduled items after a date.
  12604. d Show deadlines due within `org-deadline-warning-days'.
  12605. D Show deadlines and scheduled items between a date range."
  12606. (interactive "P")
  12607. (setq type (or type org-sparse-tree-default-date-type))
  12608. (setq org-ts-type type)
  12609. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12610. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12611. [c]ycle through date types: %s"
  12612. (case type
  12613. (all "all timestamps")
  12614. (scheduled "only scheduled")
  12615. (deadline "only deadline")
  12616. (active "only active timestamps")
  12617. (inactive "only inactive timestamps")
  12618. (closed "with a closed time-stamp")
  12619. (otherwise "scheduled/deadline")))
  12620. (let ((answer (read-char-exclusive)))
  12621. (case answer
  12622. (?c
  12623. (org-sparse-tree
  12624. arg
  12625. (cadr
  12626. (memq type '(nil all scheduled deadline active inactive closed)))))
  12627. (?d (call-interactively 'org-check-deadlines))
  12628. (?b (call-interactively 'org-check-before-date))
  12629. (?a (call-interactively 'org-check-after-date))
  12630. (?D (call-interactively 'org-check-dates-range))
  12631. (?t (call-interactively 'org-show-todo-tree))
  12632. (?T (org-show-todo-tree '(4)))
  12633. (?m (call-interactively 'org-match-sparse-tree))
  12634. ((?p ?P)
  12635. (let* ((kwd (org-icompleting-read
  12636. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12637. (value (org-icompleting-read
  12638. "Value: " (mapcar 'list (org-property-values kwd)))))
  12639. (unless (string-match "\\`{.*}\\'" value)
  12640. (setq value (concat "\"" value "\"")))
  12641. (org-match-sparse-tree arg (concat kwd "=" value))))
  12642. ((?r ?R ?/) (call-interactively 'org-occur))
  12643. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12644. (defvar org-occur-highlights nil
  12645. "List of overlays used for occur matches.")
  12646. (make-variable-buffer-local 'org-occur-highlights)
  12647. (defvar org-occur-parameters nil
  12648. "Parameters of the active org-occur calls.
  12649. This is a list, each call to org-occur pushes as cons cell,
  12650. containing the regular expression and the callback, onto the list.
  12651. The list can contain several entries if `org-occur' has been called
  12652. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12653. will only contain one set of parameters. When the highlights are
  12654. removed (for example with `C-c C-c', or with the next edit (depending
  12655. on `org-remove-highlights-with-change'), this variable is emptied
  12656. as well.")
  12657. (make-variable-buffer-local 'org-occur-parameters)
  12658. (defun org-occur (regexp &optional keep-previous callback)
  12659. "Make a compact tree which shows all matches of REGEXP.
  12660. The tree will show the lines where the regexp matches, and all higher
  12661. headlines above the match. It will also show the heading after the match,
  12662. to make sure editing the matching entry is easy.
  12663. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12664. call to `org-occur' will be kept, to allow stacking of calls to this
  12665. command.
  12666. If CALLBACK is non-nil, it is a function which is called to confirm
  12667. that the match should indeed be shown."
  12668. (interactive "sRegexp: \nP")
  12669. (when (equal regexp "")
  12670. (user-error "Regexp cannot be empty"))
  12671. (unless keep-previous
  12672. (org-remove-occur-highlights nil nil t))
  12673. (push (cons regexp callback) org-occur-parameters)
  12674. (let ((cnt 0))
  12675. (save-excursion
  12676. (goto-char (point-min))
  12677. (if (or (not keep-previous) ; do not want to keep
  12678. (not org-occur-highlights)) ; no previous matches
  12679. ;; hide everything
  12680. (org-overview))
  12681. (while (re-search-forward regexp nil t)
  12682. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12683. (when (or (not callback)
  12684. (save-match-data (funcall callback)))
  12685. (setq cnt (1+ cnt))
  12686. (when org-highlight-sparse-tree-matches
  12687. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12688. (org-show-context 'occur-tree))))
  12689. (when org-remove-highlights-with-change
  12690. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12691. nil 'local))
  12692. (unless org-sparse-tree-open-archived-trees
  12693. (org-hide-archived-subtrees (point-min) (point-max)))
  12694. (run-hooks 'org-occur-hook)
  12695. (if (org-called-interactively-p 'interactive)
  12696. (message "%d match(es) for regexp %s" cnt regexp))
  12697. cnt))
  12698. (defun org-occur-next-match (&optional n reset)
  12699. "Function for `next-error-function' to find sparse tree matches.
  12700. N is the number of matches to move, when negative move backwards.
  12701. RESET is entirely ignored - this function always goes back to the
  12702. starting point when no match is found."
  12703. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12704. (search-func (if (< n 0)
  12705. 'previous-single-char-property-change
  12706. 'next-single-char-property-change))
  12707. (n (abs n))
  12708. (pos (point))
  12709. p1)
  12710. (catch 'exit
  12711. (while (setq p1 (funcall search-func (point) 'org-type))
  12712. (when (equal p1 limit)
  12713. (goto-char pos)
  12714. (user-error "No more matches"))
  12715. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12716. (setq n (1- n))
  12717. (when (= n 0)
  12718. (goto-char p1)
  12719. (throw 'exit (point))))
  12720. (goto-char p1))
  12721. (goto-char p1)
  12722. (user-error "No more matches"))))
  12723. (defun org-show-context (&optional key)
  12724. "Make sure point and context are visible.
  12725. Optional argument KEY, when non-nil, is a symbol. See
  12726. `org-show-context-detail' for allowed values and how much is to
  12727. be shown."
  12728. (org-show-set-visibility
  12729. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12730. ((cdr (assq key org-show-context-detail)))
  12731. (t (cdr (assq 'default org-show-context-detail))))))
  12732. (defun org-show-set-visibility (detail)
  12733. "Set visibility around point according to DETAIL.
  12734. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12735. `tree', `canonical' or t. See `org-show-context-detail' for more
  12736. information."
  12737. (unless (org-before-first-heading-p)
  12738. ;; Show current heading and possibly its entry, following headline
  12739. ;; or all children.
  12740. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12741. (org-flag-heading nil)
  12742. (org-show-entry)
  12743. (org-with-limited-levels
  12744. (case detail
  12745. ((tree canonical t) (org-show-children))
  12746. ((nil minimal ancestors))
  12747. (t (save-excursion
  12748. (outline-next-heading)
  12749. (org-flag-heading nil))))))
  12750. ;; Show all siblings.
  12751. (when (eq detail 'lineage) (org-show-siblings))
  12752. ;; Show ancestors, possibly with their children.
  12753. (when (memq detail '(ancestors lineage tree canonical t))
  12754. (save-excursion
  12755. (while (org-up-heading-safe)
  12756. (org-flag-heading nil)
  12757. (when (memq detail '(canonical t)) (org-show-entry))
  12758. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12759. (defvar org-reveal-start-hook nil
  12760. "Hook run before revealing a location.")
  12761. (defun org-reveal (&optional siblings)
  12762. "Show current entry, hierarchy above it, and the following headline.
  12763. This can be used to show a consistent set of context around
  12764. locations exposed with `org-show-context'.
  12765. With optional argument SIBLINGS, on each level of the hierarchy all
  12766. siblings are shown. This repairs the tree structure to what it would
  12767. look like when opened with hierarchical calls to `org-cycle'.
  12768. With double optional argument \\[universal-argument] \\[universal-argument], \
  12769. go to the parent and show the
  12770. entire tree."
  12771. (interactive "P")
  12772. (run-hooks 'org-reveal-start-hook)
  12773. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12774. ((equal siblings '(16))
  12775. (save-excursion
  12776. (when (org-up-heading-safe)
  12777. (org-show-subtree)
  12778. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12779. (t (org-show-set-visibility 'lineage))))
  12780. (defun org-highlight-new-match (beg end)
  12781. "Highlight from BEG to END and mark the highlight is an occur headline."
  12782. (let ((ov (make-overlay beg end)))
  12783. (overlay-put ov 'face 'secondary-selection)
  12784. (overlay-put ov 'org-type 'org-occur)
  12785. (push ov org-occur-highlights)))
  12786. (defun org-remove-occur-highlights (&optional beg end noremove)
  12787. "Remove the occur highlights from the buffer.
  12788. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12789. from the `before-change-functions' in the current buffer."
  12790. (interactive)
  12791. (unless org-inhibit-highlight-removal
  12792. (mapc 'delete-overlay org-occur-highlights)
  12793. (setq org-occur-highlights nil)
  12794. (setq org-occur-parameters nil)
  12795. (unless noremove
  12796. (remove-hook 'before-change-functions
  12797. 'org-remove-occur-highlights 'local))))
  12798. ;;;; Priorities
  12799. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12800. "Regular expression matching the priority indicator.")
  12801. (defvar org-remove-priority-next-time nil)
  12802. (defun org-priority-up ()
  12803. "Increase the priority of the current item."
  12804. (interactive)
  12805. (org-priority 'up))
  12806. (defun org-priority-down ()
  12807. "Decrease the priority of the current item."
  12808. (interactive)
  12809. (org-priority 'down))
  12810. (defun org-priority (&optional action show)
  12811. "Change the priority of an item.
  12812. ACTION can be `set', `up', `down', or a character."
  12813. (interactive "P")
  12814. (if (equal action '(4))
  12815. (org-show-priority)
  12816. (unless org-enable-priority-commands
  12817. (user-error "Priority commands are disabled"))
  12818. (setq action (or action 'set))
  12819. (let (current new news have remove)
  12820. (save-excursion
  12821. (org-back-to-heading t)
  12822. (if (looking-at org-priority-regexp)
  12823. (setq current (string-to-char (match-string 2))
  12824. have t))
  12825. (cond
  12826. ((eq action 'remove)
  12827. (setq remove t new ?\ ))
  12828. ((or (eq action 'set)
  12829. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12830. (if (not (eq action 'set))
  12831. (setq new action)
  12832. (message "Priority %c-%c, SPC to remove: "
  12833. org-highest-priority org-lowest-priority)
  12834. (save-match-data
  12835. (setq new (read-char-exclusive))))
  12836. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12837. (= (upcase org-lowest-priority) org-lowest-priority))
  12838. (setq new (upcase new)))
  12839. (cond ((equal new ?\ ) (setq remove t))
  12840. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12841. (user-error "Priority must be between `%c' and `%c'"
  12842. org-highest-priority org-lowest-priority))))
  12843. ((eq action 'up)
  12844. (setq new (if have
  12845. (1- current) ; normal cycling
  12846. ;; last priority was empty
  12847. (if (eq last-command this-command)
  12848. org-lowest-priority ; wrap around empty to lowest
  12849. ;; default
  12850. (if org-priority-start-cycle-with-default
  12851. org-default-priority
  12852. (1- org-default-priority))))))
  12853. ((eq action 'down)
  12854. (setq new (if have
  12855. (1+ current) ; normal cycling
  12856. ;; last priority was empty
  12857. (if (eq last-command this-command)
  12858. org-highest-priority ; wrap around empty to highest
  12859. ;; default
  12860. (if org-priority-start-cycle-with-default
  12861. org-default-priority
  12862. (1+ org-default-priority))))))
  12863. (t (user-error "Invalid action")))
  12864. (if (or (< (upcase new) org-highest-priority)
  12865. (> (upcase new) org-lowest-priority))
  12866. (if (and (memq action '(up down))
  12867. (not have) (not (eq last-command this-command)))
  12868. ;; `new' is from default priority
  12869. (error
  12870. "The default can not be set, see `org-default-priority' why")
  12871. ;; normal cycling: `new' is beyond highest/lowest priority
  12872. ;; and is wrapped around to the empty priority
  12873. (setq remove t)))
  12874. (setq news (format "%c" new))
  12875. (if have
  12876. (if remove
  12877. (replace-match "" t t nil 1)
  12878. (replace-match news t t nil 2))
  12879. (if remove
  12880. (user-error "No priority cookie found in line")
  12881. (let ((case-fold-search nil))
  12882. (looking-at org-todo-line-regexp))
  12883. (if (match-end 2)
  12884. (progn
  12885. (goto-char (match-end 2))
  12886. (insert " [#" news "]"))
  12887. (goto-char (match-beginning 3))
  12888. (insert "[#" news "] "))))
  12889. (org-set-tags nil 'align))
  12890. (if remove
  12891. (message "Priority removed")
  12892. (message "Priority of current item set to %s" news)))))
  12893. (defun org-show-priority ()
  12894. "Show the priority of the current item.
  12895. This priority is composed of the main priority given with the [#A] cookies,
  12896. and by additional input from the age of a schedules or deadline entry."
  12897. (interactive)
  12898. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12899. (org-get-at-bol 'priority)
  12900. (save-excursion
  12901. (save-match-data
  12902. (beginning-of-line)
  12903. (and (looking-at org-heading-regexp)
  12904. (org-get-priority (match-string 0))))))))
  12905. (message "Priority is %d" (if pri pri -1000))))
  12906. (defun org-get-priority (s)
  12907. "Find priority cookie and return priority."
  12908. (save-match-data
  12909. (if (functionp org-get-priority-function)
  12910. (funcall org-get-priority-function)
  12911. (if (not (string-match org-priority-regexp s))
  12912. (* 1000 (- org-lowest-priority org-default-priority))
  12913. (* 1000 (- org-lowest-priority
  12914. (string-to-char (match-string 2 s))))))))
  12915. ;;;; Tags
  12916. (defvar org-agenda-archives-mode)
  12917. (defvar org-map-continue-from nil
  12918. "Position from where mapping should continue.
  12919. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12920. (defvar org-scanner-tags nil
  12921. "The current tag list while the tags scanner is running.")
  12922. (defvar org-trust-scanner-tags nil
  12923. "Should `org-get-tags-at' use the tags for the scanner.
  12924. This is for internal dynamical scoping only.
  12925. When this is non-nil, the function `org-get-tags-at' will return the value
  12926. of `org-scanner-tags' instead of building the list by itself. This
  12927. can lead to large speed-ups when the tags scanner is used in a file with
  12928. many entries, and when the list of tags is retrieved, for example to
  12929. obtain a list of properties. Building the tags list for each entry in such
  12930. a file becomes an N^2 operation - but with this variable set, it scales
  12931. as N.")
  12932. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12933. "Scan headline tags with inheritance and produce output ACTION.
  12934. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12935. or `agenda' to produce an entry list for an agenda view. It can also be
  12936. a Lisp form or a function that should be called at each matched headline, in
  12937. this case the return value is a list of all return values from these calls.
  12938. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12939. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12940. only lines with a not-done TODO keyword are included in the output.
  12941. This should be the same variable that was scoped into
  12942. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12943. START-LEVEL can be a string with asterisks, reducing the scope to
  12944. headlines matching this string."
  12945. (require 'org-agenda)
  12946. (let* ((re (concat "^"
  12947. (if start-level
  12948. ;; Get the correct level to match
  12949. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12950. org-outline-regexp)
  12951. " *\\(\\<\\("
  12952. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12953. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12954. (props (list 'face 'default
  12955. 'done-face 'org-agenda-done
  12956. 'undone-face 'default
  12957. 'mouse-face 'highlight
  12958. 'org-not-done-regexp org-not-done-regexp
  12959. 'org-todo-regexp org-todo-regexp
  12960. 'org-complex-heading-regexp org-complex-heading-regexp
  12961. 'help-echo
  12962. (format "mouse-2 or RET jump to org file %s"
  12963. (abbreviate-file-name
  12964. (or (buffer-file-name (buffer-base-buffer))
  12965. (buffer-name (buffer-base-buffer)))))))
  12966. (org-map-continue-from nil)
  12967. lspos tags tags-list
  12968. (tags-alist (list (cons 0 org-file-tags)))
  12969. (llast 0) rtn rtn1 level category i txt
  12970. todo marker entry priority
  12971. ts-date ts-date-type ts-date-pair)
  12972. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12973. (setq action (list 'lambda nil action)))
  12974. (save-excursion
  12975. (goto-char (point-min))
  12976. (when (eq action 'sparse-tree)
  12977. (org-overview)
  12978. (org-remove-occur-highlights))
  12979. (while (let (case-fold-search)
  12980. (re-search-forward re nil t))
  12981. (setq org-map-continue-from nil)
  12982. (catch :skip
  12983. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12984. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12985. (goto-char (setq lspos (match-beginning 0)))
  12986. (setq level (org-reduced-level (org-outline-level))
  12987. category (org-get-category))
  12988. (when (eq action 'agenda)
  12989. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12990. ts-date (car ts-date-pair)
  12991. ts-date-type (cdr ts-date-pair)))
  12992. (setq i llast llast level)
  12993. ;; remove tag lists from same and sublevels
  12994. (while (>= i level)
  12995. (when (setq entry (assoc i tags-alist))
  12996. (setq tags-alist (delete entry tags-alist)))
  12997. (setq i (1- i)))
  12998. ;; add the next tags
  12999. (when tags
  13000. (setq tags (org-split-string tags ":")
  13001. tags-alist
  13002. (cons (cons level tags) tags-alist)))
  13003. ;; compile tags for current headline
  13004. (setq tags-list
  13005. (if org-use-tag-inheritance
  13006. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  13007. tags)
  13008. org-scanner-tags tags-list)
  13009. (when org-use-tag-inheritance
  13010. (setcdr (car tags-alist)
  13011. (mapcar (lambda (x)
  13012. (setq x (copy-sequence x))
  13013. (org-add-prop-inherited x))
  13014. (cdar tags-alist))))
  13015. (when (and tags org-use-tag-inheritance
  13016. (or (not (eq t org-use-tag-inheritance))
  13017. org-tags-exclude-from-inheritance))
  13018. ;; selective inheritance, remove uninherited ones
  13019. (setcdr (car tags-alist)
  13020. (org-remove-uninherited-tags (cdar tags-alist))))
  13021. (when (and
  13022. ;; eval matcher only when the todo condition is OK
  13023. (and (or (not todo-only) (member todo org-not-done-keywords))
  13024. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13025. (eval matcher)))
  13026. ;; Call the skipper, but return t if it does not skip,
  13027. ;; so that the `and' form continues evaluating
  13028. (progn
  13029. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13030. t)
  13031. ;; Check if timestamps are deselecting this entry
  13032. (or (not todo-only)
  13033. (and (member todo org-not-done-keywords)
  13034. (or (not org-agenda-tags-todo-honor-ignore-options)
  13035. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13036. ;; select this headline
  13037. (cond
  13038. ((eq action 'sparse-tree)
  13039. (and org-highlight-sparse-tree-matches
  13040. (org-get-heading) (match-end 0)
  13041. (org-highlight-new-match
  13042. (match-beginning 1) (match-end 1)))
  13043. (org-show-context 'tags-tree))
  13044. ((eq action 'agenda)
  13045. (setq txt (org-agenda-format-item
  13046. ""
  13047. (concat
  13048. (if (eq org-tags-match-list-sublevels 'indented)
  13049. (make-string (1- level) ?.) "")
  13050. (org-get-heading))
  13051. (make-string level ?\s)
  13052. category
  13053. tags-list)
  13054. priority (org-get-priority txt))
  13055. (goto-char lspos)
  13056. (setq marker (org-agenda-new-marker))
  13057. (org-add-props txt props
  13058. 'org-marker marker 'org-hd-marker marker 'org-category category
  13059. 'todo-state todo
  13060. 'ts-date ts-date
  13061. 'priority priority
  13062. 'type (concat "tagsmatch" ts-date-type))
  13063. (push txt rtn))
  13064. ((functionp action)
  13065. (setq org-map-continue-from nil)
  13066. (save-excursion
  13067. (setq rtn1 (funcall action))
  13068. (push rtn1 rtn)))
  13069. (t (user-error "Invalid action")))
  13070. ;; if we are to skip sublevels, jump to end of subtree
  13071. (unless org-tags-match-list-sublevels
  13072. (org-end-of-subtree t)
  13073. (backward-char 1))))
  13074. ;; Get the correct position from where to continue
  13075. (if org-map-continue-from
  13076. (goto-char org-map-continue-from)
  13077. (and (= (point) lspos) (end-of-line 1)))))
  13078. (when (and (eq action 'sparse-tree)
  13079. (not org-sparse-tree-open-archived-trees))
  13080. (org-hide-archived-subtrees (point-min) (point-max)))
  13081. (nreverse rtn)))
  13082. (defun org-remove-uninherited-tags (tags)
  13083. "Remove all tags that are not inherited from the list TAGS."
  13084. (cond
  13085. ((eq org-use-tag-inheritance t)
  13086. (if org-tags-exclude-from-inheritance
  13087. (org-delete-all org-tags-exclude-from-inheritance tags)
  13088. tags))
  13089. ((not org-use-tag-inheritance) nil)
  13090. ((stringp org-use-tag-inheritance)
  13091. (delq nil (mapcar
  13092. (lambda (x)
  13093. (if (and (string-match org-use-tag-inheritance x)
  13094. (not (member x org-tags-exclude-from-inheritance)))
  13095. x nil))
  13096. tags)))
  13097. ((listp org-use-tag-inheritance)
  13098. (delq nil (mapcar
  13099. (lambda (x)
  13100. (if (member x org-use-tag-inheritance) x nil))
  13101. tags)))))
  13102. (defun org-match-sparse-tree (&optional todo-only match)
  13103. "Create a sparse tree according to tags string MATCH.
  13104. MATCH can contain positive and negative selection of tags, like
  13105. \"+WORK+URGENT-WITHBOSS\".
  13106. If optional argument TODO-ONLY is non-nil, only select lines that are
  13107. also TODO lines."
  13108. (interactive "P")
  13109. (org-agenda-prepare-buffers (list (current-buffer)))
  13110. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13111. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13112. (defvar org-cached-props nil)
  13113. (defun org-cached-entry-get (pom property)
  13114. (if (or (eq t org-use-property-inheritance)
  13115. (and (stringp org-use-property-inheritance)
  13116. (let ((case-fold-search t))
  13117. (org-string-match-p org-use-property-inheritance property)))
  13118. (and (listp org-use-property-inheritance)
  13119. (member-ignore-case property org-use-property-inheritance)))
  13120. ;; Caching is not possible, check it directly.
  13121. (org-entry-get pom property 'inherit)
  13122. ;; Get all properties, so we can do complicated checks easily.
  13123. (cdr (assoc-string property
  13124. (or org-cached-props
  13125. (setq org-cached-props (org-entry-properties pom)))
  13126. t))))
  13127. (defun org-global-tags-completion-table (&optional files)
  13128. "Return the list of all tags in all agenda buffer/files.
  13129. Optional FILES argument is a list of files which can be used
  13130. instead of the agenda files."
  13131. (save-excursion
  13132. (org-uniquify
  13133. (delq nil
  13134. (apply 'append
  13135. (mapcar
  13136. (lambda (file)
  13137. (set-buffer (find-file-noselect file))
  13138. (append (org-get-buffer-tags)
  13139. (mapcar (lambda (x) (if (stringp (car-safe x))
  13140. (list (car-safe x)) nil))
  13141. org-tag-alist)))
  13142. (if (and files (car files))
  13143. files
  13144. (org-agenda-files))))))))
  13145. (defun org-make-tags-matcher (match)
  13146. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13147. The variable `todo-only' is scoped dynamically into this function.
  13148. It will be set to t if the matcher restricts matching to TODO entries,
  13149. otherwise will not be touched.
  13150. Returns a cons of the selection string MATCH and the constructed
  13151. lisp form implementing the matcher. The matcher is to be evaluated
  13152. at an Org entry, with point on the headline, and returns t if the
  13153. entry matches the selection string MATCH. The returned lisp form
  13154. references two variables with information about the entry, which
  13155. must be bound around the form's evaluation: todo, the TODO keyword
  13156. at the entry (or nil of none); and tags-list, the list of all tags
  13157. at the entry including inherited ones. Additionally, the category
  13158. of the entry (if any) must be specified as the text property
  13159. `org-category' on the headline.
  13160. See also `org-scan-tags'.
  13161. "
  13162. (declare (special todo-only))
  13163. (unless (boundp 'todo-only)
  13164. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13165. (unless match
  13166. ;; Get a new match request, with completion against the global
  13167. ;; tags table and the local tags in current buffer
  13168. (let ((org-last-tags-completion-table
  13169. (org-uniquify
  13170. (delq nil (append (org-get-buffer-tags)
  13171. (org-global-tags-completion-table))))))
  13172. (setq match (org-completing-read-no-i
  13173. "Match: " 'org-tags-completion-function nil nil nil
  13174. 'org-tags-history))))
  13175. ;; Parse the string and create a lisp form
  13176. (let ((match0 match)
  13177. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13178. minus tag mm
  13179. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13180. orterms term orlist re-p str-p level-p level-op time-p
  13181. prop-p pn pv po gv rest (start 0) (ss 0))
  13182. ;; Expand group tags
  13183. (setq match (org-tags-expand match))
  13184. ;; Check if there is a TODO part of this match, which would be the
  13185. ;; part after a "/". TO make sure that this slash is not part of
  13186. ;; a property value to be matched against, we also check that there
  13187. ;; is no " after that slash.
  13188. ;; First, find the last slash
  13189. (while (string-match "/+" match ss)
  13190. (setq start (match-beginning 0) ss (match-end 0)))
  13191. (if (and (string-match "/+" match start)
  13192. (not (save-match-data (string-match "\"" match start))))
  13193. ;; match contains also a todo-matching request
  13194. (progn
  13195. (setq tagsmatch (substring match 0 (match-beginning 0))
  13196. todomatch (substring match (match-end 0)))
  13197. (if (string-match "^!" todomatch)
  13198. (setq todo-only t todomatch (substring todomatch 1)))
  13199. (if (string-match "^\\s-*$" todomatch)
  13200. (setq todomatch nil)))
  13201. ;; only matching tags
  13202. (setq tagsmatch match todomatch nil))
  13203. ;; Make the tags matcher
  13204. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13205. (setq tagsmatcher t)
  13206. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13207. (while (setq term (pop orterms))
  13208. (while (and (equal (substring term -1) "\\") orterms)
  13209. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13210. (while (string-match re term)
  13211. (setq rest (substring term (match-end 0))
  13212. minus (and (match-end 1)
  13213. (equal (match-string 1 term) "-"))
  13214. tag (save-match-data (replace-regexp-in-string
  13215. "\\\\-" "-"
  13216. (match-string 2 term)))
  13217. re-p (equal (string-to-char tag) ?{)
  13218. level-p (match-end 4)
  13219. prop-p (match-end 5)
  13220. mm (cond
  13221. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13222. (level-p
  13223. (setq level-op (org-op-to-function (match-string 3 term)))
  13224. `(,level-op level ,(string-to-number
  13225. (match-string 4 term))))
  13226. (prop-p
  13227. (setq pn (match-string 5 term)
  13228. po (match-string 6 term)
  13229. pv (match-string 7 term)
  13230. re-p (equal (string-to-char pv) ?{)
  13231. str-p (equal (string-to-char pv) ?\")
  13232. time-p (save-match-data
  13233. (string-match "^\"[[<].*[]>]\"$" pv))
  13234. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13235. (if time-p (setq pv (org-matcher-time pv)))
  13236. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13237. (cond
  13238. ((equal pn "CATEGORY")
  13239. (setq gv '(get-text-property (point) 'org-category)))
  13240. ((equal pn "TODO")
  13241. (setq gv 'todo))
  13242. (t
  13243. (setq gv `(org-cached-entry-get nil ,pn))))
  13244. (if re-p
  13245. (if (eq po 'org<>)
  13246. `(not (string-match ,pv (or ,gv "")))
  13247. `(string-match ,pv (or ,gv "")))
  13248. (if str-p
  13249. `(,po (or ,gv "") ,pv)
  13250. `(,po (string-to-number (or ,gv ""))
  13251. ,(string-to-number pv) ))))
  13252. (t `(member ,tag tags-list)))
  13253. mm (if minus (list 'not mm) mm)
  13254. term rest)
  13255. (push mm tagsmatcher))
  13256. (push (if (> (length tagsmatcher) 1)
  13257. (cons 'and tagsmatcher)
  13258. (car tagsmatcher))
  13259. orlist)
  13260. (setq tagsmatcher nil))
  13261. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13262. (setq tagsmatcher
  13263. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13264. ;; Make the todo matcher
  13265. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13266. (setq todomatcher t)
  13267. (setq orterms (org-split-string todomatch "|") orlist nil)
  13268. (dolist (term orterms)
  13269. (while (string-match re term)
  13270. (setq minus (and (match-end 1)
  13271. (equal (match-string 1 term) "-"))
  13272. kwd (match-string 2 term)
  13273. re-p (equal (string-to-char kwd) ?{)
  13274. term (substring term (match-end 0))
  13275. mm (if re-p
  13276. `(string-match ,(substring kwd 1 -1) todo)
  13277. (list 'equal 'todo kwd))
  13278. mm (if minus (list 'not mm) mm))
  13279. (push mm todomatcher))
  13280. (push (if (> (length todomatcher) 1)
  13281. (cons 'and todomatcher)
  13282. (car todomatcher))
  13283. orlist)
  13284. (setq todomatcher nil))
  13285. (setq todomatcher (if (> (length orlist) 1)
  13286. (cons 'or orlist) (car orlist))))
  13287. ;; Return the string and lisp forms of the matcher
  13288. (setq matcher (if todomatcher
  13289. (list 'and tagsmatcher todomatcher)
  13290. tagsmatcher))
  13291. (when todo-only
  13292. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13293. matcher)))
  13294. (cons match0 matcher)))
  13295. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13296. "Expand group tags in MATCH.
  13297. This replaces every group tag in MATCH with a regexp tag search.
  13298. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13299. will be replaced like this:
  13300. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13301. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13302. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13303. Replacing by a regexp preserves the structure of the match.
  13304. E.g., this expansion
  13305. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13306. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13307. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13308. A group tag in MATCH can contain regular expressions of its own.
  13309. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13310. will be replaced like this:
  13311. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13312. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13313. assumed to be a single group tag, and the function will return
  13314. the list of tags in this group.
  13315. When DOWNCASE is non-nil, expand downcased TAGS."
  13316. (if org-group-tags
  13317. (let* ((case-fold-search t)
  13318. (stable org-mode-syntax-table)
  13319. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13320. (taggroups (if downcased
  13321. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13322. taggroups)
  13323. taggroups))
  13324. (taggroups-keys (mapcar #'car taggroups))
  13325. (return-match (if downcased (downcase match) match))
  13326. (count 0)
  13327. (work-already-expanded tags-already-expanded)
  13328. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13329. ;; @ and _ are allowed as word-components in tags.
  13330. (modify-syntax-entry ?@ "w" stable)
  13331. (modify-syntax-entry ?_ "w" stable)
  13332. ;; Temporarily replace regexp-expressions in the match-expression.
  13333. (while (string-match "{.+?}" return-match)
  13334. (incf count)
  13335. (push (match-string 0 return-match) regexps-in-match)
  13336. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13337. (while (and taggroups-keys
  13338. (with-syntax-table stable
  13339. (string-match
  13340. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13341. (regexp-opt taggroups-keys) "\\>\\)")
  13342. return-match)))
  13343. (let* ((dir (match-string 1 return-match))
  13344. (tag (match-string 2 return-match))
  13345. (tag (if downcased (downcase tag) tag)))
  13346. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13347. (member tag work-already-expanded))
  13348. (setq tags-in-group (assoc tag taggroups))
  13349. (push tag work-already-expanded)
  13350. ;; Recursively expand each tag in the group, if the tag hasn't
  13351. ;; already been expanded. Restore the match-data after all recursive calls.
  13352. (save-match-data
  13353. (let (tags-expanded)
  13354. (dolist (x (cdr tags-in-group))
  13355. (if (and (member x taggroups-keys)
  13356. (not (member x work-already-expanded)))
  13357. (setq tags-expanded
  13358. (delete-dups
  13359. (append
  13360. (org-tags-expand x t downcased
  13361. work-already-expanded)
  13362. tags-expanded)))
  13363. (setq tags-expanded
  13364. (append (list x) tags-expanded)))
  13365. (setq work-already-expanded
  13366. (delete-dups
  13367. (append tags-expanded
  13368. work-already-expanded))))
  13369. (setq tags-in-group
  13370. (delete-dups (cons (car tags-in-group)
  13371. tags-expanded)))))
  13372. ;; Filter tag-regexps from tags.
  13373. (setq regexp-in-group-escaped
  13374. (delq nil (mapcar (lambda (x)
  13375. (if (stringp x)
  13376. (and (equal "{" (substring x 0 1))
  13377. (equal "}" (substring x -1))
  13378. x)
  13379. x))
  13380. tags-in-group))
  13381. regexp-in-group
  13382. (mapcar (lambda (x)
  13383. (substring x 1 -1))
  13384. regexp-in-group-escaped)
  13385. tags-in-group
  13386. (delq nil (mapcar (lambda (x)
  13387. (if (stringp x)
  13388. (and (not (equal "{" (substring x 0 1)))
  13389. (not (equal "}" (substring x -1)))
  13390. x)
  13391. x))
  13392. tags-in-group)))
  13393. ;; If single-as-list, do no more in the while-loop.
  13394. (if (not single-as-list)
  13395. (progn
  13396. (when regexp-in-group
  13397. (setq regexp-in-group
  13398. (concat "\\|"
  13399. (mapconcat 'identity regexp-in-group
  13400. "\\|"))))
  13401. (setq tags-in-group
  13402. (concat dir
  13403. "{\\<"
  13404. (regexp-opt tags-in-group)
  13405. "\\>"
  13406. regexp-in-group
  13407. "}"))
  13408. (when (stringp tags-in-group)
  13409. (org-add-props tags-in-group '(grouptag t)))
  13410. (setq return-match
  13411. (replace-match tags-in-group t t return-match)))
  13412. (setq tags-in-group
  13413. (append regexp-in-group-escaped tags-in-group))))
  13414. (setq taggroups-keys (delete tag taggroups-keys))))
  13415. ;; Add the regular expressions back into the match-expression again.
  13416. (while regexps-in-match
  13417. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13418. (pop regexps-in-match)
  13419. return-match t t))
  13420. (decf count))
  13421. (if single-as-list
  13422. (if tags-in-group tags-in-group (list return-match))
  13423. return-match))
  13424. (if single-as-list
  13425. (list (if downcased (downcase match) match))
  13426. match)))
  13427. (defun org-op-to-function (op &optional stringp)
  13428. "Turn an operator into the appropriate function."
  13429. (setq op
  13430. (cond
  13431. ((equal op "<" ) '(< string< org-time<))
  13432. ((equal op ">" ) '(> org-string> org-time>))
  13433. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13434. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13435. ((member op '("=" "==")) '(= string= org-time=))
  13436. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13437. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13438. (defun org<> (a b) (not (= a b)))
  13439. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13440. (defun org-string>= (a b) (not (string< a b)))
  13441. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13442. (defun org-string<> (a b) (not (string= a b)))
  13443. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13444. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13445. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13446. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13447. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13448. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13449. (defun org-2ft (s)
  13450. "Convert S to a floating point time.
  13451. If S is already a number, just return it. If it is a string, parse
  13452. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13453. (cond
  13454. ((numberp s) s)
  13455. ((stringp s)
  13456. (condition-case nil
  13457. (float-time (apply 'encode-time (org-parse-time-string s)))
  13458. (error 0.)))
  13459. (t 0.)))
  13460. (defun org-time-today ()
  13461. "Time in seconds today at 0:00.
  13462. Returns the float number of seconds since the beginning of the
  13463. epoch to the beginning of today (00:00)."
  13464. (float-time (apply 'encode-time
  13465. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13466. (defun org-matcher-time (s)
  13467. "Interpret a time comparison value."
  13468. (save-match-data
  13469. (cond
  13470. ((string= s "<now>") (float-time))
  13471. ((string= s "<today>") (org-time-today))
  13472. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13473. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13474. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13475. (+ (org-time-today)
  13476. (* (string-to-number (match-string 1 s))
  13477. (cdr (assoc (match-string 2 s)
  13478. '(("d" . 86400.0) ("w" . 604800.0)
  13479. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13480. (t (org-2ft s)))))
  13481. (defun org-match-any-p (re list)
  13482. "Does re match any element of list?"
  13483. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13484. (delq nil list))
  13485. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13486. (defvar org-tags-overlay (make-overlay 1 1))
  13487. (org-detach-overlay org-tags-overlay)
  13488. (defun org-get-local-tags-at (&optional pos)
  13489. "Get a list of tags defined in the current headline."
  13490. (org-get-tags-at pos 'local))
  13491. (defun org-get-local-tags ()
  13492. "Get a list of tags defined in the current headline."
  13493. (org-get-tags-at nil 'local))
  13494. (defun org-get-tags-at (&optional pos local)
  13495. "Get a list of all headline tags applicable at POS.
  13496. POS defaults to point. If tags are inherited, the list contains
  13497. the targets in the same sequence as the headlines appear, i.e.
  13498. the tags of the current headline come last.
  13499. When LOCAL is non-nil, only return tags from the current headline,
  13500. ignore inherited ones."
  13501. (interactive)
  13502. (if (and org-trust-scanner-tags
  13503. (or (not pos) (equal pos (point)))
  13504. (not local))
  13505. org-scanner-tags
  13506. (let (tags ltags lastpos parent)
  13507. (save-excursion
  13508. (save-restriction
  13509. (widen)
  13510. (goto-char (or pos (point)))
  13511. (save-match-data
  13512. (catch 'done
  13513. (condition-case nil
  13514. (progn
  13515. (org-back-to-heading t)
  13516. (while (not (equal lastpos (point)))
  13517. (setq lastpos (point))
  13518. (when (looking-at
  13519. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13520. (setq ltags (org-split-string
  13521. (org-match-string-no-properties 1) ":"))
  13522. (when parent
  13523. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13524. (setq tags (append
  13525. (if parent
  13526. (org-remove-uninherited-tags ltags)
  13527. ltags)
  13528. tags)))
  13529. (or org-use-tag-inheritance (throw 'done t))
  13530. (if local (throw 'done t))
  13531. (or (org-up-heading-safe) (error nil))
  13532. (setq parent t)))
  13533. (error nil)))))
  13534. (if local
  13535. tags
  13536. (reverse (delete-dups
  13537. (reverse (append
  13538. (org-remove-uninherited-tags
  13539. org-file-tags)
  13540. tags)))))))))
  13541. (defun org-add-prop-inherited (s)
  13542. (add-text-properties 0 (length s) '(inherited t) s)
  13543. s)
  13544. (defun org-toggle-tag (tag &optional onoff)
  13545. "Toggle the tag TAG for the current line.
  13546. If ONOFF is `on' or `off', don't toggle but set to this state."
  13547. (let (res current)
  13548. (save-excursion
  13549. (org-back-to-heading t)
  13550. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13551. (point-at-eol) t)
  13552. (progn
  13553. (setq current (match-string 1))
  13554. (replace-match ""))
  13555. (setq current ""))
  13556. (setq current (nreverse (org-split-string current ":")))
  13557. (cond
  13558. ((eq onoff 'on)
  13559. (setq res t)
  13560. (or (member tag current) (push tag current)))
  13561. ((eq onoff 'off)
  13562. (or (not (member tag current)) (setq current (delete tag current))))
  13563. (t (if (member tag current)
  13564. (setq current (delete tag current))
  13565. (setq res t)
  13566. (push tag current))))
  13567. (end-of-line 1)
  13568. (if current
  13569. (progn
  13570. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13571. (org-set-tags nil t))
  13572. (delete-horizontal-space))
  13573. (run-hooks 'org-after-tags-change-hook))
  13574. res))
  13575. (defun org-align-tags-here (to-col)
  13576. ;; Assumes that this is a headline
  13577. "Align tags on the current headline to TO-COL."
  13578. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13579. (beginning-of-line 1)
  13580. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13581. (< pos (match-beginning 2)))
  13582. (progn
  13583. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13584. (goto-char (match-beginning 1))
  13585. (insert " ")
  13586. (delete-region (point) (1+ (match-beginning 2)))
  13587. (setq ncol (max (current-column)
  13588. (1+ col)
  13589. (if (> to-col 0)
  13590. to-col
  13591. (- (abs to-col) tags-l))))
  13592. (setq p (point))
  13593. (insert (make-string (- ncol (current-column)) ?\ ))
  13594. (setq ncol (current-column))
  13595. (when indent-tabs-mode (tabify p (point-at-eol)))
  13596. (org-move-to-column (min ncol col)))
  13597. (goto-char pos))))
  13598. (defun org-set-tags-command (&optional arg just-align)
  13599. "Call the set-tags command for the current entry."
  13600. (interactive "P")
  13601. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13602. (org-set-tags arg just-align)
  13603. (save-excursion
  13604. (unless (and (org-region-active-p)
  13605. org-loop-over-headlines-in-active-region)
  13606. (org-back-to-heading t))
  13607. (org-set-tags arg just-align))))
  13608. (defun org-set-tags-to (data)
  13609. "Set the tags of the current entry to DATA, replacing the current tags.
  13610. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13611. If DATA is nil or the empty string, any tags will be removed."
  13612. (interactive "sTags: ")
  13613. (setq data
  13614. (cond
  13615. ((eq data nil) "")
  13616. ((equal data "") "")
  13617. ((stringp data)
  13618. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13619. ":"))
  13620. ((listp data)
  13621. (concat ":" (mapconcat 'identity data ":") ":"))))
  13622. (when data
  13623. (save-excursion
  13624. (org-back-to-heading t)
  13625. (when (looking-at org-complex-heading-regexp)
  13626. (if (match-end 5)
  13627. (progn
  13628. (goto-char (match-beginning 5))
  13629. (insert data)
  13630. (delete-region (point) (point-at-eol))
  13631. (org-set-tags nil 'align))
  13632. (goto-char (point-at-eol))
  13633. (insert " " data)
  13634. (org-set-tags nil 'align)))
  13635. (beginning-of-line 1)
  13636. (if (looking-at ".*?\\([ \t]+\\)$")
  13637. (delete-region (match-beginning 1) (match-end 1))))))
  13638. (defun org-align-all-tags ()
  13639. "Align the tags i all headings."
  13640. (interactive)
  13641. (save-excursion
  13642. (or (ignore-errors (org-back-to-heading t))
  13643. (outline-next-heading))
  13644. (if (org-at-heading-p)
  13645. (org-set-tags t)
  13646. (message "No headings"))))
  13647. (defvar org-indent-indentation-per-level)
  13648. (defun org-set-tags (&optional arg just-align)
  13649. "Set the tags for the current headline.
  13650. With prefix ARG, realign all tags in headings in the current buffer.
  13651. When JUST-ALIGN is non-nil, only align tags."
  13652. (interactive "P")
  13653. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13654. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13655. 'region-start-level
  13656. 'region))
  13657. org-loop-over-headlines-in-active-region)
  13658. (org-map-entries
  13659. ;; We don't use ARG and JUST-ALIGN here because these args
  13660. ;; are not useful when looping over headlines.
  13661. #'org-set-tags
  13662. org-loop-over-headlines-in-active-region
  13663. cl
  13664. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13665. (let ((org-setting-tags t))
  13666. (if arg
  13667. (save-excursion
  13668. (goto-char (point-min))
  13669. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13670. (while (re-search-forward org-outline-regexp-bol nil t)
  13671. (org-set-tags nil t)
  13672. (end-of-line)))
  13673. (message "All tags realigned to column %d" org-tags-column))
  13674. (let* ((current (org-get-tags-string))
  13675. (col (current-column))
  13676. (tags
  13677. (if just-align current
  13678. ;; Get a new set of tags from the user.
  13679. (save-excursion
  13680. (let* ((table
  13681. (setq
  13682. org-last-tags-completion-table
  13683. (append
  13684. org-tag-persistent-alist
  13685. (or org-tag-alist (org-get-buffer-tags))
  13686. (and
  13687. org-complete-tags-always-offer-all-agenda-tags
  13688. (org-global-tags-completion-table
  13689. (org-agenda-files))))))
  13690. (current-tags (org-split-string current ":"))
  13691. (inherited-tags
  13692. (nreverse (nthcdr (length current-tags)
  13693. (nreverse (org-get-tags-at))))))
  13694. (replace-regexp-in-string
  13695. "\\([-+&]+\\|,\\)"
  13696. ":"
  13697. (if (or (eq t org-use-fast-tag-selection)
  13698. (and org-use-fast-tag-selection
  13699. (delq nil (mapcar #'cdr table))))
  13700. (org-fast-tag-selection
  13701. current-tags inherited-tags table
  13702. (and org-fast-tag-selection-include-todo
  13703. org-todo-key-alist))
  13704. (let ((org-add-colon-after-tag-completion
  13705. (< 1 (length table))))
  13706. (org-trim
  13707. (completing-read
  13708. "Tags: "
  13709. #'org-tags-completion-function
  13710. nil nil current 'org-tags-history))))))))))
  13711. (when org-tags-sort-function
  13712. (setq tags
  13713. (mapconcat
  13714. #'identity
  13715. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13716. org-tags-sort-function)
  13717. ":")))
  13718. (if (not (org-string-nw-p tags)) (setq tags "")
  13719. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13720. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13721. ;; Insert new tags at the correct column
  13722. (beginning-of-line)
  13723. (let ((level (if (looking-at org-outline-regexp)
  13724. (- (match-end 0) (point) 1)
  13725. 1)))
  13726. (cond
  13727. ((and (equal current "") (equal tags "")))
  13728. ((re-search-forward
  13729. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13730. (line-end-position)
  13731. t)
  13732. (if (equal tags "") (replace-match "" t t)
  13733. (goto-char (match-beginning 0))
  13734. (let* ((c0 (current-column))
  13735. ;; Compute offset for the case of org-indent-mode
  13736. ;; active.
  13737. (di (if (org-bound-and-true-p org-indent-mode)
  13738. (* (1- org-indent-indentation-per-level)
  13739. (1- level))
  13740. 0))
  13741. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13742. (tc (+ org-tags-column
  13743. (if (> org-tags-column 0) (- di) di)))
  13744. (c1 (max (1+ c0)
  13745. (if (> tc 0) tc
  13746. (- (- tc) (string-width tags)))))
  13747. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13748. (replace-match rpl t t)
  13749. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13750. (tabify p0 (point))))))
  13751. (t (error "Tags alignment failed"))))
  13752. (org-move-to-column col))
  13753. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13754. (defun org-change-tag-in-region (beg end tag off)
  13755. "Add or remove TAG for each entry in the region.
  13756. This works in the agenda, and also in an org-mode buffer."
  13757. (interactive
  13758. (list (region-beginning) (region-end)
  13759. (let ((org-last-tags-completion-table
  13760. (if (derived-mode-p 'org-mode)
  13761. (org-uniquify
  13762. (delq nil (append (org-get-buffer-tags)
  13763. (org-global-tags-completion-table))))
  13764. (org-global-tags-completion-table))))
  13765. (org-icompleting-read
  13766. "Tag: " 'org-tags-completion-function nil nil nil
  13767. 'org-tags-history))
  13768. (progn
  13769. (message "[s]et or [r]emove? ")
  13770. (equal (read-char-exclusive) ?r))))
  13771. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13772. (let ((agendap (equal major-mode 'org-agenda-mode))
  13773. l1 l2 m buf pos newhead (cnt 0))
  13774. (goto-char end)
  13775. (setq l2 (1- (org-current-line)))
  13776. (goto-char beg)
  13777. (setq l1 (org-current-line))
  13778. (loop for l from l1 to l2 do
  13779. (org-goto-line l)
  13780. (setq m (get-text-property (point) 'org-hd-marker))
  13781. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13782. (and agendap m))
  13783. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13784. pos (if agendap m (point)))
  13785. (with-current-buffer buf
  13786. (save-excursion
  13787. (save-restriction
  13788. (goto-char pos)
  13789. (setq cnt (1+ cnt))
  13790. (org-toggle-tag tag (if off 'off 'on))
  13791. (setq newhead (org-get-heading)))))
  13792. (and agendap (org-agenda-change-all-lines newhead m))))
  13793. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13794. (defun org-tags-completion-function (string predicate &optional flag)
  13795. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13796. (confirm (lambda (x) (stringp (car x)))))
  13797. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13798. (setq s1 (match-string 1 string)
  13799. s2 (match-string 2 string))
  13800. (setq s1 "" s2 string))
  13801. (cond
  13802. ((eq flag nil)
  13803. ;; try completion
  13804. (setq rtn (try-completion s2 ctable confirm))
  13805. (if (stringp rtn)
  13806. (setq rtn
  13807. (concat s1 s2 (substring rtn (length s2))
  13808. (if (and org-add-colon-after-tag-completion
  13809. (assoc rtn ctable))
  13810. ":" ""))))
  13811. rtn)
  13812. ((eq flag t)
  13813. ;; all-completions
  13814. (all-completions s2 ctable confirm))
  13815. ((eq flag 'lambda)
  13816. ;; exact match?
  13817. (assoc s2 ctable)))))
  13818. (defun org-fast-tag-insert (kwd tags face &optional end)
  13819. "Insert KDW, and the TAGS, the latter with face FACE.
  13820. Also insert END."
  13821. (insert (format "%-12s" (concat kwd ":"))
  13822. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13823. (or end "")))
  13824. (defun org-fast-tag-show-exit (flag)
  13825. (save-excursion
  13826. (org-goto-line 3)
  13827. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13828. (replace-match ""))
  13829. (when flag
  13830. (end-of-line 1)
  13831. (org-move-to-column (- (window-width) 19) t)
  13832. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13833. (defun org-set-current-tags-overlay (current prefix)
  13834. "Add an overlay to CURRENT tag with PREFIX."
  13835. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13836. (if (featurep 'xemacs)
  13837. (org-overlay-display org-tags-overlay (concat prefix s)
  13838. 'secondary-selection)
  13839. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13840. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13841. (defvar org-last-tag-selection-key nil)
  13842. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13843. "Fast tag selection with single keys.
  13844. CURRENT is the current list of tags in the headline, INHERITED is the
  13845. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13846. possibly with grouping information. TODO-TABLE is a similar table with
  13847. TODO keywords, should these have keys assigned to them.
  13848. If the keys are nil, a-z are automatically assigned.
  13849. Returns the new tags string, or nil to not change the current settings."
  13850. (let* ((fulltable (append table todo-table))
  13851. (maxlen (apply 'max (mapcar
  13852. (lambda (x)
  13853. (if (stringp (car x)) (string-width (car x)) 0))
  13854. fulltable)))
  13855. (buf (current-buffer))
  13856. (expert (eq org-fast-tag-selection-single-key 'expert))
  13857. (buffer-tags nil)
  13858. (fwidth (+ maxlen 3 1 3))
  13859. (ncol (/ (- (window-width) 4) fwidth))
  13860. (i-face 'org-done)
  13861. (c-face 'org-todo)
  13862. tg cnt e c char c1 c2 ntable tbl rtn
  13863. ov-start ov-end ov-prefix
  13864. (exit-after-next org-fast-tag-selection-single-key)
  13865. (done-keywords org-done-keywords)
  13866. groups ingroup intaggroup)
  13867. (save-excursion
  13868. (beginning-of-line 1)
  13869. (if (looking-at
  13870. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13871. (setq ov-start (match-beginning 1)
  13872. ov-end (match-end 1)
  13873. ov-prefix "")
  13874. (setq ov-start (1- (point-at-eol))
  13875. ov-end (1+ ov-start))
  13876. (skip-chars-forward "^\n\r")
  13877. (setq ov-prefix
  13878. (concat
  13879. (buffer-substring (1- (point)) (point))
  13880. (if (> (current-column) org-tags-column)
  13881. " "
  13882. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13883. (move-overlay org-tags-overlay ov-start ov-end)
  13884. (save-window-excursion
  13885. (if expert
  13886. (set-buffer (get-buffer-create " *Org tags*"))
  13887. (delete-other-windows)
  13888. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13889. (org-switch-to-buffer-other-window " *Org tags*"))
  13890. (erase-buffer)
  13891. (org-set-local 'org-done-keywords done-keywords)
  13892. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13893. (org-fast-tag-insert "Current" current c-face "\n\n")
  13894. (org-fast-tag-show-exit exit-after-next)
  13895. (org-set-current-tags-overlay current ov-prefix)
  13896. (setq tbl fulltable char ?a cnt 0)
  13897. (while (setq e (pop tbl))
  13898. (cond
  13899. ((eq (car e) :startgroup)
  13900. (push '() groups) (setq ingroup t)
  13901. (unless (zerop cnt)
  13902. (setq cnt 0)
  13903. (insert "\n"))
  13904. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13905. ((eq (car e) :endgroup)
  13906. (setq ingroup nil cnt 0)
  13907. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13908. ((eq (car e) :startgrouptag)
  13909. (setq intaggroup t)
  13910. (unless (zerop cnt)
  13911. (setq cnt 0)
  13912. (insert "\n"))
  13913. (insert "[ "))
  13914. ((eq (car e) :endgrouptag)
  13915. (setq intaggroup nil cnt 0)
  13916. (insert "]\n"))
  13917. ((equal e '(:newline))
  13918. (unless (zerop cnt)
  13919. (setq cnt 0)
  13920. (insert "\n")
  13921. (setq e (car tbl))
  13922. (while (equal (car tbl) '(:newline))
  13923. (insert "\n")
  13924. (setq tbl (cdr tbl)))))
  13925. ((equal e '(:grouptags)) (insert " : "))
  13926. (t
  13927. (setq tg (copy-sequence (car e)) c2 nil)
  13928. (if (cdr e)
  13929. (setq c (cdr e))
  13930. ;; automatically assign a character.
  13931. (setq c1 (string-to-char
  13932. (downcase (substring
  13933. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13934. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13935. (while (or (rassoc char ntable) (rassoc char table))
  13936. (setq char (1+ char)))
  13937. (setq c2 c1))
  13938. (setq c (or c2 char)))
  13939. (when ingroup (push tg (car groups)))
  13940. (setq tg (org-add-props tg nil 'face
  13941. (cond
  13942. ((not (assoc tg table))
  13943. (org-get-todo-face tg))
  13944. ((member tg current) c-face)
  13945. ((member tg inherited) i-face))))
  13946. (when (equal (caar tbl) :grouptags)
  13947. (org-add-props tg nil 'face 'org-tag-group))
  13948. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13949. (insert "[" c "] " tg (make-string
  13950. (- fwidth 4 (length tg)) ?\ ))
  13951. (push (cons tg c) ntable)
  13952. (when (= (incf cnt) ncol)
  13953. (insert "\n")
  13954. (when (or ingroup intaggroup) (insert " "))
  13955. (setq cnt 0)))))
  13956. (setq ntable (nreverse ntable))
  13957. (insert "\n")
  13958. (goto-char (point-min))
  13959. (unless expert (org-fit-window-to-buffer))
  13960. (setq rtn
  13961. (catch 'exit
  13962. (while t
  13963. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13964. (if (not groups) "no " "")
  13965. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13966. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13967. (setq org-last-tag-selection-key c)
  13968. (cond
  13969. ((= c ?\r) (throw 'exit t))
  13970. ((= c ?!)
  13971. (setq groups (not groups))
  13972. (goto-char (point-min))
  13973. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13974. ((= c ?\C-c)
  13975. (if (not expert)
  13976. (org-fast-tag-show-exit
  13977. (setq exit-after-next (not exit-after-next)))
  13978. (setq expert nil)
  13979. (delete-other-windows)
  13980. (set-window-buffer (split-window-vertically) " *Org tags*")
  13981. (org-switch-to-buffer-other-window " *Org tags*")
  13982. (org-fit-window-to-buffer)))
  13983. ((or (= c ?\C-g)
  13984. (and (= c ?q) (not (rassoc c ntable))))
  13985. (org-detach-overlay org-tags-overlay)
  13986. (setq quit-flag t))
  13987. ((= c ?\ )
  13988. (setq current nil)
  13989. (when exit-after-next (setq exit-after-next 'now)))
  13990. ((= c ?\t)
  13991. (condition-case nil
  13992. (setq tg (org-icompleting-read
  13993. "Tag: "
  13994. (or buffer-tags
  13995. (with-current-buffer buf
  13996. (setq buffer-tags
  13997. (org-get-buffer-tags))))))
  13998. (quit (setq tg "")))
  13999. (when (string-match "\\S-" tg)
  14000. (add-to-list 'buffer-tags (list tg))
  14001. (if (member tg current)
  14002. (setq current (delete tg current))
  14003. (push tg current)))
  14004. (when exit-after-next (setq exit-after-next 'now)))
  14005. ((setq e (rassoc c todo-table) tg (car e))
  14006. (with-current-buffer buf
  14007. (save-excursion (org-todo tg)))
  14008. (when exit-after-next (setq exit-after-next 'now)))
  14009. ((setq e (rassoc c ntable) tg (car e))
  14010. (if (member tg current)
  14011. (setq current (delete tg current))
  14012. (loop for g in groups do
  14013. (when (member tg g)
  14014. (dolist (x g) (setq current (delete x current)))))
  14015. (push tg current))
  14016. (when exit-after-next (setq exit-after-next 'now))))
  14017. ;; Create a sorted list
  14018. (setq current
  14019. (sort current
  14020. (lambda (a b)
  14021. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14022. (when (eq exit-after-next 'now) (throw 'exit t))
  14023. (goto-char (point-min))
  14024. (beginning-of-line 2)
  14025. (delete-region (point) (point-at-eol))
  14026. (org-fast-tag-insert "Current" current c-face)
  14027. (org-set-current-tags-overlay current ov-prefix)
  14028. (while (re-search-forward
  14029. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14030. (setq tg (match-string 1))
  14031. (add-text-properties
  14032. (match-beginning 1) (match-end 1)
  14033. (list 'face
  14034. (cond
  14035. ((member tg current) c-face)
  14036. ((member tg inherited) i-face)
  14037. (t (get-text-property (match-beginning 1) 'face))))))
  14038. (goto-char (point-min)))))
  14039. (org-detach-overlay org-tags-overlay)
  14040. (if rtn
  14041. (mapconcat 'identity current ":")
  14042. nil))))
  14043. (defun org-get-tags-string ()
  14044. "Get the TAGS string in the current headline."
  14045. (unless (org-at-heading-p t)
  14046. (user-error "Not on a heading"))
  14047. (save-excursion
  14048. (beginning-of-line 1)
  14049. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14050. (org-match-string-no-properties 1)
  14051. "")))
  14052. (defun org-get-tags ()
  14053. "Get the list of tags specified in the current headline."
  14054. (org-split-string (org-get-tags-string) ":"))
  14055. (defun org-get-buffer-tags ()
  14056. "Get a table of all tags used in the buffer, for completion."
  14057. (org-with-wide-buffer
  14058. (goto-char (point-min))
  14059. (let ((tag-re (concat org-outline-regexp-bol
  14060. "\\(?:.*?[ \t]\\)?"
  14061. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14062. tags)
  14063. (while (re-search-forward tag-re nil t)
  14064. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14065. (push tag tags)))
  14066. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14067. ;;;; The mapping API
  14068. (defun org-map-entries (func &optional match scope &rest skip)
  14069. "Call FUNC at each headline selected by MATCH in SCOPE.
  14070. FUNC is a function or a lisp form. The function will be called without
  14071. arguments, with the cursor positioned at the beginning of the headline.
  14072. The return values of all calls to the function will be collected and
  14073. returned as a list.
  14074. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14075. does not need to preserve point. After evaluation, the cursor will be
  14076. moved to the end of the line (presumably of the headline of the
  14077. processed entry) and search continues from there. Under some
  14078. circumstances, this may not produce the wanted results. For example,
  14079. if you have removed (e.g. archived) the current (sub)tree it could
  14080. mean that the next entry will be skipped entirely. In such cases, you
  14081. can specify the position from where search should continue by making
  14082. FUNC set the variable `org-map-continue-from' to the desired buffer
  14083. position.
  14084. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14085. Only headlines that are matched by this query will be considered during
  14086. the iteration. When MATCH is nil or t, all headlines will be
  14087. visited by the iteration.
  14088. SCOPE determines the scope of this command. It can be any of:
  14089. nil The current buffer, respecting the restriction if any
  14090. tree The subtree started with the entry at point
  14091. region The entries within the active region, if any
  14092. region-start-level
  14093. The entries within the active region, but only those at
  14094. the same level than the first one.
  14095. file The current buffer, without restriction
  14096. file-with-archives
  14097. The current buffer, and any archives associated with it
  14098. agenda All agenda files
  14099. agenda-with-archives
  14100. All agenda files with any archive files associated with them
  14101. \(file1 file2 ...)
  14102. If this is a list, all files in the list will be scanned
  14103. The remaining args are treated as settings for the skipping facilities of
  14104. the scanner. The following items can be given here:
  14105. archive skip trees with the archive tag
  14106. comment skip trees with the COMMENT keyword
  14107. function or Emacs Lisp form:
  14108. will be used as value for `org-agenda-skip-function', so
  14109. whenever the function returns a position, FUNC will not be
  14110. called for that entry and search will continue from the
  14111. position returned
  14112. If your function needs to retrieve the tags including inherited tags
  14113. at the *current* entry, you can use the value of the variable
  14114. `org-scanner-tags' which will be much faster than getting the value
  14115. with `org-get-tags-at'. If your function gets properties with
  14116. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14117. to t around the call to `org-entry-properties' to get the same speedup.
  14118. Note that if your function moves around to retrieve tags and properties at
  14119. a *different* entry, you cannot use these techniques."
  14120. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14121. (not (org-region-active-p)))
  14122. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14123. (org-agenda-skip-archived-trees (memq 'archive skip))
  14124. (org-agenda-skip-comment-trees (memq 'comment skip))
  14125. (org-agenda-skip-function
  14126. (car (org-delete-all '(comment archive) skip)))
  14127. (org-tags-match-list-sublevels t)
  14128. (start-level (eq scope 'region-start-level))
  14129. matcher file res
  14130. org-todo-keywords-for-agenda
  14131. org-done-keywords-for-agenda
  14132. org-todo-keyword-alist-for-agenda
  14133. org-tag-alist-for-agenda
  14134. todo-only)
  14135. (cond
  14136. ((eq match t) (setq matcher t))
  14137. ((eq match nil) (setq matcher t))
  14138. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14139. (save-excursion
  14140. (save-restriction
  14141. (cond ((eq scope 'tree)
  14142. (org-back-to-heading t)
  14143. (org-narrow-to-subtree)
  14144. (setq scope nil))
  14145. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14146. (org-region-active-p))
  14147. ;; If needed, set start-level to a string like "2"
  14148. (when start-level
  14149. (save-excursion
  14150. (goto-char (region-beginning))
  14151. (unless (org-at-heading-p) (outline-next-heading))
  14152. (setq start-level (org-current-level))))
  14153. (narrow-to-region (region-beginning)
  14154. (save-excursion
  14155. (goto-char (region-end))
  14156. (unless (and (bolp) (org-at-heading-p))
  14157. (outline-next-heading))
  14158. (point)))
  14159. (setq scope nil)))
  14160. (if (not scope)
  14161. (progn
  14162. (org-agenda-prepare-buffers
  14163. (and buffer-file-name (list buffer-file-name)))
  14164. (setq res (org-scan-tags func matcher todo-only start-level)))
  14165. ;; Get the right scope
  14166. (cond
  14167. ((and scope (listp scope) (symbolp (car scope)))
  14168. (setq scope (eval scope)))
  14169. ((eq scope 'agenda)
  14170. (setq scope (org-agenda-files t)))
  14171. ((eq scope 'agenda-with-archives)
  14172. (setq scope (org-agenda-files t))
  14173. (setq scope (org-add-archive-files scope)))
  14174. ((eq scope 'file)
  14175. (setq scope (and buffer-file-name (list buffer-file-name))))
  14176. ((eq scope 'file-with-archives)
  14177. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14178. (org-agenda-prepare-buffers scope)
  14179. (dolist (file scope)
  14180. (with-current-buffer (org-find-base-buffer-visiting file)
  14181. (save-excursion
  14182. (save-restriction
  14183. (widen)
  14184. (goto-char (point-min))
  14185. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14186. res)))
  14187. ;;; Properties API
  14188. (defconst org-special-properties
  14189. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14190. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14191. "The special properties valid in Org mode.
  14192. These are properties that are not defined in the property drawer,
  14193. but in some other way.")
  14194. (defconst org-default-properties
  14195. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14196. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14197. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14198. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14199. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14200. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14201. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14202. "Some properties that are used by Org mode for various purposes.
  14203. Being in this list makes sure that they are offered for completion.")
  14204. (defun org--update-property-plist (key val props)
  14205. "Associate KEY to VAL in alist PROPS.
  14206. Modifications are made by side-effect. Return new alist."
  14207. (let* ((appending (string= (substring key -1) "+"))
  14208. (key (if appending (substring key 0 -1) key))
  14209. (old (assoc-string key props t)))
  14210. (if (not old) (cons (cons key val) props)
  14211. (setcdr old (if appending (concat (cdr old) " " val) val))
  14212. props)))
  14213. (defun org-get-property-block (&optional beg force)
  14214. "Return the (beg . end) range of the body of the property drawer.
  14215. BEG is the beginning of the current subtree, or of the part
  14216. before the first headline. If it is not given, it will be found.
  14217. If the drawer does not exist, create it if FORCE is non-nil, or
  14218. return nil."
  14219. (org-with-wide-buffer
  14220. (when beg (goto-char beg))
  14221. (unless (org-before-first-heading-p)
  14222. (let ((beg (cond (beg)
  14223. ((or (not (featurep 'org-inlinetask))
  14224. (org-inlinetask-in-task-p))
  14225. (org-back-to-heading t))
  14226. (t (org-with-limited-levels (org-back-to-heading t))))))
  14227. (forward-line)
  14228. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14229. (cond ((looking-at org-property-drawer-re)
  14230. (forward-line)
  14231. (cons (point) (progn (goto-char (match-end 0))
  14232. (line-beginning-position))))
  14233. (force
  14234. (goto-char beg)
  14235. (org-insert-property-drawer)
  14236. (let ((pos (save-excursion (search-forward ":END:")
  14237. (line-beginning-position))))
  14238. (cons pos pos))))))))
  14239. (defun org-at-property-p ()
  14240. "Non-nil when point is inside a property drawer.
  14241. See `org-property-re' for match data, if applicable."
  14242. (save-excursion
  14243. (beginning-of-line)
  14244. (and (looking-at org-property-re)
  14245. (let ((property-drawer (save-match-data (org-get-property-block))))
  14246. (and property-drawer
  14247. (>= (point) (car property-drawer))
  14248. (< (point) (cdr property-drawer)))))))
  14249. (defun org-property-action ()
  14250. "Do an action on properties."
  14251. (interactive)
  14252. (unless (org-at-property-p) (user-error "Not at a property"))
  14253. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14254. (let ((c (read-char-exclusive)))
  14255. (case c
  14256. (?s (call-interactively #'org-set-property))
  14257. (?d (call-interactively #'org-delete-property))
  14258. (?D (call-interactively #'org-delete-property-globally))
  14259. (?c (call-interactively #'org-compute-property-at-point))
  14260. (otherwise (user-error "No such property action %c" c)))))
  14261. (defun org-inc-effort ()
  14262. "Increment the value of the effort property in the current entry."
  14263. (interactive)
  14264. (org-set-effort nil t))
  14265. (defvar org-clock-effort) ; Defined in org-clock.el.
  14266. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14267. (defun org-set-effort (&optional value increment)
  14268. "Set the effort property of the current entry.
  14269. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14270. 10th allowed value.
  14271. When INCREMENT is non-nil, set the property to the next allowed value."
  14272. (interactive "P")
  14273. (if (equal value 0) (setq value 10))
  14274. (let* ((completion-ignore-case t)
  14275. (prop org-effort-property)
  14276. (cur (org-entry-get nil prop))
  14277. (allowed (org-property-get-allowed-values nil prop 'table))
  14278. (existing (mapcar 'list (org-property-values prop)))
  14279. (heading (nth 4 (org-heading-components)))
  14280. rpl
  14281. (val (cond
  14282. ((stringp value) value)
  14283. ((and allowed (integerp value))
  14284. (or (car (nth (1- value) allowed))
  14285. (car (org-last allowed))))
  14286. ((and allowed increment)
  14287. (or (caadr (member (list cur) allowed))
  14288. (user-error "Allowed effort values are not set")))
  14289. (allowed
  14290. (message "Select 1-9,0, [RET%s]: %s"
  14291. (if cur (concat "=" cur) "")
  14292. (mapconcat 'car allowed " "))
  14293. (setq rpl (read-char-exclusive))
  14294. (if (equal rpl ?\r)
  14295. cur
  14296. (setq rpl (- rpl ?0))
  14297. (if (equal rpl 0) (setq rpl 10))
  14298. (if (and (> rpl 0) (<= rpl (length allowed)))
  14299. (car (nth (1- rpl) allowed))
  14300. (org-completing-read "Effort: " allowed nil))))
  14301. (t
  14302. (let (org-completion-use-ido org-completion-use-iswitchb)
  14303. (org-completing-read
  14304. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14305. (concat "[" cur "]") "")
  14306. ": ")
  14307. existing nil nil "" nil cur))))))
  14308. (unless (equal (org-entry-get nil prop) val)
  14309. (org-entry-put nil prop val))
  14310. (org-refresh-property
  14311. '((effort . identity)
  14312. (effort-minutes . org-duration-string-to-minutes))
  14313. val)
  14314. (when (string= heading org-clock-current-task)
  14315. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14316. (org-clock-update-mode-line))
  14317. (message "%s is now %s" prop val)))
  14318. (defun org-entry-properties (&optional pom which)
  14319. "Get all properties of the current entry.
  14320. When POM is a buffer position, get all properties from the entry
  14321. there instead.
  14322. This includes the TODO keyword, the tags, time strings for
  14323. deadline, scheduled, and clocking, and any additional properties
  14324. defined in the entry.
  14325. If WHICH is nil or `all', get all properties. If WHICH is
  14326. `special' or `standard', only get that subclass. If WHICH is
  14327. a string, only get that property.
  14328. Return value is an alist. Keys are properties, as upcased
  14329. strings."
  14330. (org-with-point-at pom
  14331. (when (and (derived-mode-p 'org-mode)
  14332. (ignore-errors (org-back-to-heading t)))
  14333. (catch 'exit
  14334. (let* ((beg (point))
  14335. (specific (and (stringp which) (upcase which)))
  14336. (which (cond ((not specific) which)
  14337. ((member specific org-special-properties) 'special)
  14338. (t 'standard)))
  14339. props)
  14340. ;; Get the special properties, like TODO and TAGS.
  14341. (when (memq which '(nil all special))
  14342. (when (or (not specific) (string= specific "CLOCKSUM"))
  14343. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14344. (when clocksum
  14345. (push (cons "CLOCKSUM"
  14346. (org-columns-number-to-string
  14347. (/ (float clocksum) 60.) 'add_times))
  14348. props)))
  14349. (when specific (throw 'exit props)))
  14350. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14351. (let ((clocksumt (get-text-property (point)
  14352. :org-clock-minutes-today)))
  14353. (when clocksumt
  14354. (push (cons "CLOCKSUM_T"
  14355. (org-columns-number-to-string
  14356. (/ (float clocksumt) 60.) 'add_times))
  14357. props)))
  14358. (when specific (throw 'exit props)))
  14359. (when (or (not specific) (string= specific "ITEM"))
  14360. (when (looking-at org-complex-heading-regexp)
  14361. (push (cons "ITEM"
  14362. (concat
  14363. (org-match-string-no-properties 1)
  14364. (let ((title (org-match-string-no-properties 4)))
  14365. (when (org-string-nw-p title)
  14366. (concat " " (org-remove-tabs title))))))
  14367. props))
  14368. (when specific (throw 'exit props)))
  14369. (when (or (not specific) (string= specific "TODO"))
  14370. (let ((case-fold-search nil))
  14371. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14372. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14373. (when specific (throw 'exit props)))
  14374. (when (or (not specific) (string= specific "PRIORITY"))
  14375. (push (cons "PRIORITY"
  14376. (if (looking-at org-priority-regexp)
  14377. (org-match-string-no-properties 2)
  14378. (char-to-string org-default-priority)))
  14379. props)
  14380. (when specific (throw 'exit props)))
  14381. (when (or (not specific) (string= specific "FILE"))
  14382. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14383. props)
  14384. (when specific (throw 'exit props)))
  14385. (when (or (not specific) (string= specific "TAGS"))
  14386. (let ((value (org-string-nw-p (org-get-tags-string))))
  14387. (when value (push (cons "TAGS" value) props)))
  14388. (when specific (throw 'exit props)))
  14389. (when (or (not specific) (string= specific "ALLTAGS"))
  14390. (let ((value (org-get-tags-at)))
  14391. (when value
  14392. (push (cons "ALLTAGS"
  14393. (format ":%s:" (mapconcat #'identity value ":")))
  14394. props)))
  14395. (when specific (throw 'exit props)))
  14396. (when (or (not specific) (string= specific "BLOCKED"))
  14397. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14398. (when specific (throw 'exit props)))
  14399. (when (or (not specific)
  14400. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14401. (forward-line)
  14402. (when (org-looking-at-p org-planning-line-re)
  14403. (end-of-line)
  14404. (let ((bol (line-beginning-position))
  14405. ;; Backward compatibility: time keywords used to
  14406. ;; be configurable (before 8.3). Make sure we
  14407. ;; get the correct keyword.
  14408. (key-assoc `(("CLOSED" . ,org-closed-string)
  14409. ("DEADLINE" . ,org-deadline-string)
  14410. ("SCHEDULED" . ,org-scheduled-string))))
  14411. (dolist (pair (if specific (list (assoc specific key-assoc))
  14412. key-assoc))
  14413. (save-excursion
  14414. (when (search-backward (cdr pair) bol t)
  14415. (goto-char (match-end 0))
  14416. (skip-chars-forward " \t")
  14417. (and (looking-at org-ts-regexp-both)
  14418. (push (cons (car pair)
  14419. (org-match-string-no-properties 0))
  14420. props)))))))
  14421. (when specific (throw 'exit props)))
  14422. (when (or (not specific)
  14423. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14424. (let ((find-ts
  14425. (lambda (end ts)
  14426. (let ((regexp (cond
  14427. ((string= specific "TIMESTAMP")
  14428. org-ts-regexp)
  14429. ((string= specific "TIMESTAMP_IA")
  14430. org-ts-regexp-inactive)
  14431. ((assoc "TIMESTAMP_IA" ts)
  14432. org-ts-regexp)
  14433. ((assoc "TIMESTAMP" ts)
  14434. org-ts-regexp-inactive)
  14435. (t org-ts-regexp-both))))
  14436. (catch 'next
  14437. (while (re-search-forward regexp end t)
  14438. (backward-char)
  14439. (let ((object (org-element-context)))
  14440. ;; Accept to match timestamps in node
  14441. ;; properties, too.
  14442. (when (memq (org-element-type object)
  14443. '(node-property timestamp))
  14444. (let ((type
  14445. (org-element-property :type object)))
  14446. (cond
  14447. ((and (memq type '(active active-range))
  14448. (not (equal specific "TIMESTAMP_IA")))
  14449. (unless (assoc "TIMESTAMP" ts)
  14450. (push (cons "TIMESTAMP"
  14451. (org-element-property
  14452. :raw-value object))
  14453. ts)
  14454. (when specific (throw 'exit ts))))
  14455. ((and (memq type '(inactive inactive-range))
  14456. (not (string= specific "TIMESTAMP")))
  14457. (unless (assoc "TIMESTAMP_IA" ts)
  14458. (push (cons "TIMESTAMP_IA"
  14459. (org-element-property
  14460. :raw-value object))
  14461. ts)
  14462. (when specific (throw 'exit ts))))))
  14463. ;; Both timestamp types are found,
  14464. ;; move to next part.
  14465. (when (= (length ts) 2) (throw 'next ts)))))
  14466. ts)))))
  14467. (goto-char beg)
  14468. ;; First look for timestamps within headline.
  14469. (let ((ts (funcall find-ts (line-end-position) nil)))
  14470. (if (= (length ts) 2) (setq props (nconc ts props))
  14471. (forward-line)
  14472. ;; Then find timestamps in the section, skipping
  14473. ;; planning line.
  14474. (when (org-looking-at-p org-planning-line-re)
  14475. (forward-line))
  14476. (let ((end (save-excursion (outline-next-heading))))
  14477. (setq props (nconc (funcall find-ts end ts) props))))))))
  14478. ;; Get the standard properties, like :PROP:.
  14479. (when (memq which '(nil all standard))
  14480. ;; If we are looking after a specific property, delegate
  14481. ;; to `org-entry-get', which is faster. However, make an
  14482. ;; exception for "CATEGORY", since it can be also set
  14483. ;; through keywords (i.e. #+CATEGORY).
  14484. (if (and specific (not (equal specific "CATEGORY")))
  14485. (let ((value (org-entry-get beg specific nil t)))
  14486. (throw 'exit (and value (list (cons specific value)))))
  14487. (let ((range (org-get-property-block beg)))
  14488. (when range
  14489. (let ((end (cdr range)) seen-base)
  14490. (goto-char (car range))
  14491. ;; Unlike to `org--update-property-plist', we
  14492. ;; handle the case where base values is found
  14493. ;; after its extension. We also forbid standard
  14494. ;; properties to be named as special properties.
  14495. (while (re-search-forward org-property-re end t)
  14496. (let* ((key (upcase (org-match-string-no-properties 2)))
  14497. (extendp (org-string-match-p "\\+\\'" key))
  14498. (key-base (if extendp (substring key 0 -1) key))
  14499. (value (org-match-string-no-properties 3)))
  14500. (cond
  14501. ((member-ignore-case key-base org-special-properties))
  14502. (extendp
  14503. (setq props
  14504. (org--update-property-plist key value props)))
  14505. ((member key seen-base))
  14506. (t (push key seen-base)
  14507. (let ((p (assoc-string key props t)))
  14508. (if p (setcdr p (concat value " " (cdr p)))
  14509. (push (cons key value) props))))))))))))
  14510. (unless (assoc "CATEGORY" props)
  14511. (push (cons "CATEGORY" (org-get-category beg)) props)
  14512. (when (string= specific "CATEGORY") (throw 'exit props)))
  14513. ;; Return value.
  14514. props)))))
  14515. (defun org-property--local-values (property literal-nil)
  14516. "Return value for PROPERTY in current entry.
  14517. Value is a list whose care is the base value for PROPERTY and cdr
  14518. a list of accumulated values. Return nil if neither is found in
  14519. the entry. Also return nil when PROPERTY is set to \"nil\",
  14520. unless LITERAL-NIL is non-nil."
  14521. (let ((range (org-get-property-block)))
  14522. (when range
  14523. (goto-char (car range))
  14524. (let* ((case-fold-search t)
  14525. (end (cdr range))
  14526. (value
  14527. ;; Base value.
  14528. (save-excursion
  14529. (let ((v (and (re-search-forward
  14530. (org-re-property property nil t) end t)
  14531. (org-match-string-no-properties 3))))
  14532. (list (if literal-nil v (org-not-nil v)))))))
  14533. ;; Find additional values.
  14534. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14535. (while (re-search-forward property+ end t)
  14536. (push (org-match-string-no-properties 3) value)))
  14537. ;; Return final values.
  14538. (and (not (equal value '(nil))) (nreverse value))))))
  14539. (defun org-entry-get (pom property &optional inherit literal-nil)
  14540. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14541. If INHERIT is non-nil and the entry does not have the property,
  14542. then also check higher levels of the hierarchy. If INHERIT is
  14543. the symbol `selective', use inheritance only if the setting in
  14544. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14545. If the property is present but empty, the return value is the
  14546. empty string. If the property is not present at all, nil is
  14547. returned. In any other case, return the value as a string.
  14548. Search is case-insensitive.
  14549. If LITERAL-NIL is set, return the string value \"nil\" as
  14550. a string, do not interpret it as the list atom nil. This is used
  14551. for inheritance when a \"nil\" value can supersede a non-nil
  14552. value higher up the hierarchy."
  14553. (org-with-point-at pom
  14554. (cond
  14555. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14556. ;; We need a special property. Use `org-entry-properties' to
  14557. ;; retrieve it, but specify the wanted property.
  14558. (cdr (assoc-string property (org-entry-properties nil property))))
  14559. ((and inherit
  14560. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14561. (org-entry-get-with-inheritance property literal-nil))
  14562. (t
  14563. (let* ((local (org-property--local-values property literal-nil))
  14564. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14565. (if literal-nil value (org-not-nil value)))))))
  14566. (defun org-property-or-variable-value (var &optional inherit)
  14567. "Check if there is a property fixing the value of VAR.
  14568. If yes, return this value. If not, return the current value of the variable."
  14569. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14570. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14571. (read prop)
  14572. (symbol-value var))))
  14573. (defun org-entry-delete (pom property)
  14574. "Delete PROPERTY from entry at point-or-marker POM.
  14575. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14576. non-nil when a property was removed."
  14577. (unless (member property org-special-properties)
  14578. (org-with-point-at pom
  14579. (let ((range (org-get-property-block)))
  14580. (when range
  14581. (let* ((begin (car range))
  14582. (origin (cdr range))
  14583. (end (copy-marker origin))
  14584. (re (org-re-property
  14585. (concat (regexp-quote property) "\\+?") t t)))
  14586. (goto-char begin)
  14587. (while (re-search-forward re end t)
  14588. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14589. ;; If drawer is empty, remove it altogether.
  14590. (when (= begin end)
  14591. (delete-region (line-beginning-position 0)
  14592. (line-beginning-position 2)))
  14593. ;; Return non-nil if some property was removed.
  14594. (prog1 (/= end origin) (set-marker end nil))))))))
  14595. ;; Multi-values properties are properties that contain multiple values
  14596. ;; These values are assumed to be single words, separated by whitespace.
  14597. (defun org-entry-add-to-multivalued-property (pom property value)
  14598. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14599. (let* ((old (org-entry-get pom property))
  14600. (values (and old (org-split-string old "[ \t]"))))
  14601. (setq value (org-entry-protect-space value))
  14602. (unless (member value values)
  14603. (setq values (append values (list value)))
  14604. (org-entry-put pom property
  14605. (mapconcat 'identity values " ")))))
  14606. (defun org-entry-remove-from-multivalued-property (pom property value)
  14607. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14608. (let* ((old (org-entry-get pom property))
  14609. (values (and old (org-split-string old "[ \t]"))))
  14610. (setq value (org-entry-protect-space value))
  14611. (when (member value values)
  14612. (setq values (delete value values))
  14613. (org-entry-put pom property
  14614. (mapconcat 'identity values " ")))))
  14615. (defun org-entry-member-in-multivalued-property (pom property value)
  14616. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14617. (let* ((old (org-entry-get pom property))
  14618. (values (and old (org-split-string old "[ \t]"))))
  14619. (setq value (org-entry-protect-space value))
  14620. (member value values)))
  14621. (defun org-entry-get-multivalued-property (pom property)
  14622. "Return a list of values in a multivalued property."
  14623. (let* ((value (org-entry-get pom property))
  14624. (values (and value (org-split-string value "[ \t]"))))
  14625. (mapcar 'org-entry-restore-space values)))
  14626. (defun org-entry-put-multivalued-property (pom property &rest values)
  14627. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14628. VALUES should be a list of strings. Spaces will be protected."
  14629. (org-entry-put pom property
  14630. (mapconcat 'org-entry-protect-space values " "))
  14631. (let* ((value (org-entry-get pom property))
  14632. (values (and value (org-split-string value "[ \t]"))))
  14633. (mapcar 'org-entry-restore-space values)))
  14634. (defun org-entry-protect-space (s)
  14635. "Protect spaces and newline in string S."
  14636. (while (string-match " " s)
  14637. (setq s (replace-match "%20" t t s)))
  14638. (while (string-match "\n" s)
  14639. (setq s (replace-match "%0A" t t s)))
  14640. s)
  14641. (defun org-entry-restore-space (s)
  14642. "Restore spaces and newline in string S."
  14643. (while (string-match "%20" s)
  14644. (setq s (replace-match " " t t s)))
  14645. (while (string-match "%0A" s)
  14646. (setq s (replace-match "\n" t t s)))
  14647. s)
  14648. (defvar org-entry-property-inherited-from (make-marker)
  14649. "Marker pointing to the entry from where a property was inherited.
  14650. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14651. location of the entry where the inheritance search matched. If there was
  14652. no match, the marker will point nowhere.
  14653. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14654. is set.")
  14655. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14656. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14657. The search will stop at the first ancestor which has the property defined.
  14658. If the value found is \"nil\", return nil to show that the property
  14659. should be considered as undefined (this is the meaning of nil here).
  14660. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14661. (move-marker org-entry-property-inherited-from nil)
  14662. (org-with-wide-buffer
  14663. (let (value)
  14664. (catch 'exit
  14665. (while t
  14666. (let ((v (org-property--local-values property literal-nil)))
  14667. (when v
  14668. (setq value
  14669. (concat (mapconcat #'identity (delq nil v) " ")
  14670. (and value " ")
  14671. value)))
  14672. (cond
  14673. ((car v)
  14674. (org-back-to-heading t)
  14675. (move-marker org-entry-property-inherited-from (point))
  14676. (throw 'exit nil))
  14677. ((org-up-heading-safe))
  14678. (t
  14679. (let ((global
  14680. (cdr (or (assoc-string property org-file-properties t)
  14681. (assoc-string property org-global-properties t)
  14682. (assoc-string property org-global-properties-fixed t)))))
  14683. (cond ((not global))
  14684. (value (setq value (concat global " " value)))
  14685. (t (setq value global))))
  14686. (throw 'exit nil))))))
  14687. (if literal-nil value (org-not-nil value)))))
  14688. (defvar org-property-changed-functions nil
  14689. "Hook called when the value of a property has changed.
  14690. Each hook function should accept two arguments, the name of the property
  14691. and the new value.")
  14692. (defun org-entry-put (pom property value)
  14693. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14694. If the value is nil, it is converted to the empty string. If
  14695. it is not a string, an error is raised.
  14696. PROPERTY can be any regular property (see
  14697. `org-special-properties'). It can also be \"TODO\",
  14698. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14699. For the last two properties, VALUE may have any of the special
  14700. values \"earlier\" and \"later\". The function then increases or
  14701. decreases scheduled or deadline date by one day."
  14702. (cond ((null value) (setq value ""))
  14703. ((not (stringp value)) (error "Properties values should be strings")))
  14704. (org-with-point-at pom
  14705. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14706. (org-back-to-heading t)
  14707. (org-with-limited-levels (org-back-to-heading t)))
  14708. (let ((beg (point)))
  14709. (cond
  14710. ((equal property "TODO")
  14711. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14712. ((not (member value org-todo-keywords-1))
  14713. (user-error "\"%s\" is not a valid TODO state" value)))
  14714. (org-todo value)
  14715. (org-set-tags nil 'align))
  14716. ((equal property "PRIORITY")
  14717. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14718. (org-set-tags nil 'align))
  14719. ((equal property "SCHEDULED")
  14720. (forward-line)
  14721. (if (and (org-looking-at-p org-planning-line-re)
  14722. (re-search-forward
  14723. org-scheduled-time-regexp (line-end-position) t))
  14724. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14725. ((string= value "later") (org-timestamp-change 1 'day))
  14726. ((string= value "") (org-schedule '(4)))
  14727. (t (org-schedule nil value)))
  14728. (if (member value '("earlier" "later" ""))
  14729. (call-interactively #'org-schedule)
  14730. (org-schedule nil value))))
  14731. ((equal property "DEADLINE")
  14732. (forward-line)
  14733. (if (and (org-looking-at-p org-planning-line-re)
  14734. (re-search-forward
  14735. org-deadline-time-regexp (line-end-position) t))
  14736. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14737. ((string= value "later") (org-timestamp-change 1 'day))
  14738. ((string= value "") (org-deadline '(4)))
  14739. (t (org-deadline nil value)))
  14740. (if (member value '("earlier" "later" ""))
  14741. (call-interactively #'org-deadline)
  14742. (org-deadline nil value))))
  14743. ((member property org-special-properties)
  14744. (error "The %s property cannot be set with `org-entry-put'" property))
  14745. (t
  14746. (let* ((range (org-get-property-block beg 'force))
  14747. (end (cdr range))
  14748. (case-fold-search t))
  14749. (goto-char (car range))
  14750. (if (re-search-forward (org-re-property property nil t) end t)
  14751. (progn (delete-region (match-beginning 0) (match-end 0))
  14752. (goto-char (match-beginning 0)))
  14753. (goto-char end)
  14754. (insert "\n")
  14755. (backward-char))
  14756. (insert ":" property ":")
  14757. (when value (insert " " value))
  14758. (org-indent-line)))))
  14759. (run-hook-with-args 'org-property-changed-functions property value)))
  14760. (defun org-buffer-property-keys (&optional specials defaults columns)
  14761. "Get all property keys in the current buffer.
  14762. When SPECIALS is non-nil, also list the special properties that
  14763. reflect things like tags and TODO state.
  14764. When DEFAULTS is non-nil, also include properties that has
  14765. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14766. DESCRIPTION, LOCATION, and LOGGING and others.
  14767. When COLUMNS in non-nil, also include property names given in
  14768. COLUMN formats in the current buffer."
  14769. (let ((case-fold-search t)
  14770. (props (append
  14771. (and specials org-special-properties)
  14772. (and defaults (cons org-effort-property org-default-properties))
  14773. nil)))
  14774. (org-with-wide-buffer
  14775. (goto-char (point-min))
  14776. (while (re-search-forward org-property-start-re nil t)
  14777. (let ((range (org-get-property-block)))
  14778. (catch 'skip
  14779. (unless range
  14780. (when (and (not (org-before-first-heading-p))
  14781. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14782. (line-beginning-position))))
  14783. (org-get-property-block nil t))
  14784. (throw 'skip nil))
  14785. (goto-char (car range))
  14786. (let ((begin (car range))
  14787. (end (cdr range)))
  14788. ;; Make sure that found property block is not located
  14789. ;; before current point, as it would generate an infloop.
  14790. ;; It can happen, for example, in the following
  14791. ;; situation:
  14792. ;;
  14793. ;; * Headline
  14794. ;; :PROPERTIES:
  14795. ;; ...
  14796. ;; :END:
  14797. ;; *************** Inlinetask
  14798. ;; #+BEGIN_EXAMPLE
  14799. ;; :PROPERTIES:
  14800. ;; #+END_EXAMPLE
  14801. ;;
  14802. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14803. (while (< (point) end)
  14804. (let ((p (progn (looking-at org-property-re)
  14805. (org-match-string-no-properties 2))))
  14806. ;; Only add true property name, not extension symbol.
  14807. (add-to-list 'props
  14808. (if (not (org-string-match-p "\\+\\'" p)) p
  14809. (substring p 0 -1))))
  14810. (forward-line))))
  14811. (outline-next-heading)))
  14812. (when columns
  14813. (goto-char (point-min))
  14814. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14815. (let ((element (org-element-at-point)))
  14816. (when (memq (org-element-type element) '(keyword node-property))
  14817. (let ((value (org-element-property :value element))
  14818. (start 0))
  14819. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14820. (setq start (match-end 0))
  14821. (let ((p (org-match-string-no-properties 1 value)))
  14822. (unless (member-ignore-case p org-special-properties)
  14823. (add-to-list 'props p))))))))))
  14824. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14825. (defun org-property-values (key)
  14826. "List all non-nil values of property KEY in current buffer."
  14827. (org-with-wide-buffer
  14828. (goto-char (point-min))
  14829. (let ((case-fold-search t)
  14830. (re (org-re-property key))
  14831. values)
  14832. (while (re-search-forward re nil t)
  14833. (add-to-list 'values (org-entry-get (point) key)))
  14834. values)))
  14835. (defun org-insert-property-drawer ()
  14836. "Insert a property drawer into the current entry."
  14837. (org-with-wide-buffer
  14838. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14839. (org-back-to-heading t)
  14840. (org-with-limited-levels (org-back-to-heading t)))
  14841. (forward-line)
  14842. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14843. (unless (org-looking-at-p org-property-drawer-re)
  14844. ;; Make sure we start editing a line from current entry, not from
  14845. ;; next one. It prevents extending text properties or overlays
  14846. ;; belonging to the latter.
  14847. (when (bolp) (backward-char))
  14848. (let ((begin (1+ (point)))
  14849. (inhibit-read-only t))
  14850. (insert "\n:PROPERTIES:\n:END:")
  14851. (when (eobp) (insert "\n"))
  14852. (org-indent-region begin (point))))))
  14853. (defun org-insert-drawer (&optional arg drawer)
  14854. "Insert a drawer at point.
  14855. When optional argument ARG is non-nil, insert a property drawer.
  14856. Optional argument DRAWER, when non-nil, is a string representing
  14857. drawer's name. Otherwise, the user is prompted for a name.
  14858. If a region is active, insert the drawer around that region
  14859. instead.
  14860. Point is left between drawer's boundaries."
  14861. (interactive "P")
  14862. (let* ((drawer (if arg "PROPERTIES"
  14863. (or drawer (read-from-minibuffer "Drawer: ")))))
  14864. (cond
  14865. ;; With C-u, fall back on `org-insert-property-drawer'
  14866. (arg (org-insert-property-drawer))
  14867. ;; Check validity of suggested drawer's name.
  14868. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14869. (user-error "Invalid drawer name"))
  14870. ;; With an active region, insert a drawer at point.
  14871. ((not (org-region-active-p))
  14872. (progn
  14873. (unless (bolp) (insert "\n"))
  14874. (insert (format ":%s:\n\n:END:\n" drawer))
  14875. (forward-line -2)))
  14876. ;; Otherwise, insert the drawer at point
  14877. (t
  14878. (let ((rbeg (region-beginning))
  14879. (rend (copy-marker (region-end))))
  14880. (unwind-protect
  14881. (progn
  14882. (goto-char rbeg)
  14883. (beginning-of-line)
  14884. (when (save-excursion
  14885. (re-search-forward org-outline-regexp-bol rend t))
  14886. (user-error "Drawers cannot contain headlines"))
  14887. ;; Position point at the beginning of the first
  14888. ;; non-blank line in region. Insert drawer's opening
  14889. ;; there, then indent it.
  14890. (org-skip-whitespace)
  14891. (beginning-of-line)
  14892. (insert ":" drawer ":\n")
  14893. (forward-line -1)
  14894. (indent-for-tab-command)
  14895. ;; Move point to the beginning of the first blank line
  14896. ;; after the last non-blank line in region. Insert
  14897. ;; drawer's closing, then indent it.
  14898. (goto-char rend)
  14899. (skip-chars-backward " \r\t\n")
  14900. (insert "\n:END:")
  14901. (deactivate-mark t)
  14902. (indent-for-tab-command)
  14903. (unless (eolp) (insert "\n")))
  14904. ;; Clear marker, whatever the outcome of insertion is.
  14905. (set-marker rend nil)))))))
  14906. (defvar org-property-set-functions-alist nil
  14907. "Property set function alist.
  14908. Each entry should have the following format:
  14909. (PROPERTY . READ-FUNCTION)
  14910. The read function will be called with the same argument as
  14911. `org-completing-read'.")
  14912. (defun org-set-property-function (property)
  14913. "Get the function that should be used to set PROPERTY.
  14914. This is computed according to `org-property-set-functions-alist'."
  14915. (or (cdr (assoc property org-property-set-functions-alist))
  14916. 'org-completing-read))
  14917. (defun org-read-property-value (property)
  14918. "Read PROPERTY value from user."
  14919. (let* ((completion-ignore-case t)
  14920. (allowed (org-property-get-allowed-values nil property 'table))
  14921. (cur (org-entry-get nil property))
  14922. (prompt (concat property " value"
  14923. (if (and cur (string-match "\\S-" cur))
  14924. (concat " [" cur "]") "") ": "))
  14925. (set-function (org-set-property-function property))
  14926. (val (if allowed
  14927. (funcall set-function prompt allowed nil
  14928. (not (get-text-property 0 'org-unrestricted
  14929. (caar allowed))))
  14930. (let (org-completion-use-ido org-completion-use-iswitchb)
  14931. (funcall set-function prompt
  14932. (mapcar 'list (org-property-values property))
  14933. nil nil "" nil cur)))))
  14934. (org-trim val)))
  14935. (defvar org-last-set-property nil)
  14936. (defvar org-last-set-property-value nil)
  14937. (defun org-read-property-name ()
  14938. "Read a property name."
  14939. (let ((completion-ignore-case t)
  14940. (default-prop (or (and (org-at-property-p)
  14941. (org-match-string-no-properties 2))
  14942. org-last-set-property)))
  14943. (org-completing-read
  14944. (concat "Property"
  14945. (if default-prop (concat " [" default-prop "]") "")
  14946. ": ")
  14947. (mapcar #'list (org-buffer-property-keys nil t t))
  14948. nil nil nil nil default-prop)))
  14949. (defun org-set-property-and-value (use-last)
  14950. "Allow to set [PROPERTY]: [value] direction from prompt.
  14951. When use-default, don't even ask, just use the last
  14952. \"[PROPERTY]: [value]\" string from the history."
  14953. (interactive "P")
  14954. (let* ((completion-ignore-case t)
  14955. (pv (or (and use-last org-last-set-property-value)
  14956. (org-completing-read
  14957. "Enter a \"[Property]: [value]\" pair: "
  14958. nil nil nil nil nil
  14959. org-last-set-property-value)))
  14960. prop val)
  14961. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14962. (setq prop (match-string 1 pv)
  14963. val (match-string 2 pv))
  14964. (org-set-property prop val))))
  14965. (defun org-set-property (property value)
  14966. "In the current entry, set PROPERTY to VALUE.
  14967. When called interactively, this will prompt for a property name, offering
  14968. completion on existing and default properties. And then it will prompt
  14969. for a value, offering completion either on allowed values (via an inherited
  14970. xxx_ALL property) or on existing values in other instances of this property
  14971. in the current file."
  14972. (interactive (list nil nil))
  14973. (let* ((property (or property (org-read-property-name)))
  14974. (value (or value (org-read-property-value property)))
  14975. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14976. (setq org-last-set-property property)
  14977. (setq org-last-set-property-value (concat property ": " value))
  14978. ;; Possibly postprocess the inserted value:
  14979. (when fn (setq value (funcall fn value)))
  14980. (unless (equal (org-entry-get nil property) value)
  14981. (org-entry-put nil property value))))
  14982. (defun org-find-property (property &optional value)
  14983. "Find first entry in buffer that sets PROPERTY.
  14984. When optional argument VALUE is non-nil, only consider an entry
  14985. if it contains PROPERTY set to this value. If PROPERTY should be
  14986. explicitly set to nil, use string \"nil\" for VALUE.
  14987. Return position where the entry begins, or nil if there is no
  14988. such entry. If narrowing is in effect, only search the visible
  14989. part of the buffer."
  14990. (save-excursion
  14991. (goto-char (point-min))
  14992. (let ((case-fold-search t)
  14993. (re (org-re-property property nil (not value) value)))
  14994. (catch 'exit
  14995. (while (re-search-forward re nil t)
  14996. (when (if value (org-at-property-p)
  14997. (org-entry-get (point) property nil t))
  14998. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14999. (defun org-delete-property (property)
  15000. "In the current entry, delete PROPERTY."
  15001. (interactive
  15002. (let* ((completion-ignore-case t)
  15003. (cat (org-entry-get (point) "CATEGORY"))
  15004. (props0 (org-entry-properties nil 'standard))
  15005. (props (if cat props0
  15006. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  15007. (prop (if (< 1 (length props))
  15008. (org-icompleting-read "Property: " props nil t)
  15009. (caar props))))
  15010. (list prop)))
  15011. (if (not property)
  15012. (message "No property to delete in this entry")
  15013. (org-entry-delete nil property)
  15014. (message "Property \"%s\" deleted" property)))
  15015. (defun org-delete-property-globally (property)
  15016. "Remove PROPERTY globally, from all entries.
  15017. This function ignores narrowing, if any."
  15018. (interactive
  15019. (let* ((completion-ignore-case t)
  15020. (prop (org-icompleting-read
  15021. "Globally remove property: "
  15022. (mapcar #'list (org-buffer-property-keys)))))
  15023. (list prop)))
  15024. (org-with-wide-buffer
  15025. (goto-char (point-min))
  15026. (let ((count 0)
  15027. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15028. (while (re-search-forward re nil t)
  15029. (when (org-entry-delete (point) property) (incf count)))
  15030. (message "Property \"%s\" removed from %d entries" property count))))
  15031. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15032. (defun org-compute-property-at-point ()
  15033. "Compute the property at point.
  15034. This looks for an enclosing column format, extracts the operator and
  15035. then applies it to the property in the column format's scope."
  15036. (interactive)
  15037. (unless (org-at-property-p)
  15038. (user-error "Not at a property"))
  15039. (let ((prop (org-match-string-no-properties 2)))
  15040. (org-columns-get-format-and-top-level)
  15041. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15042. (user-error "No operator defined for property %s" prop))
  15043. (org-columns-compute prop)))
  15044. (defvar org-property-allowed-value-functions nil
  15045. "Hook for functions supplying allowed values for a specific property.
  15046. The functions must take a single argument, the name of the property, and
  15047. return a flat list of allowed values. If \":ETC\" is one of
  15048. the values, this means that these values are intended as defaults for
  15049. completion, but that other values should be allowed too.
  15050. The functions must return nil if they are not responsible for this
  15051. property.")
  15052. (defun org-property-get-allowed-values (pom property &optional table)
  15053. "Get allowed values for the property PROPERTY.
  15054. When TABLE is non-nil, return an alist that can directly be used for
  15055. completion."
  15056. (let (vals)
  15057. (cond
  15058. ((equal property "TODO")
  15059. (setq vals (org-with-point-at pom
  15060. (append org-todo-keywords-1 '("")))))
  15061. ((equal property "PRIORITY")
  15062. (let ((n org-lowest-priority))
  15063. (while (>= n org-highest-priority)
  15064. (push (char-to-string n) vals)
  15065. (setq n (1- n)))))
  15066. ((equal property "CATEGORY"))
  15067. ((member property org-special-properties))
  15068. ((setq vals (run-hook-with-args-until-success
  15069. 'org-property-allowed-value-functions property)))
  15070. (t
  15071. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15072. (when (and vals (string-match "\\S-" vals))
  15073. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15074. (setq vals (mapcar (lambda (x)
  15075. (cond ((stringp x) x)
  15076. ((numberp x) (number-to-string x))
  15077. ((symbolp x) (symbol-name x))
  15078. (t "???")))
  15079. vals)))))
  15080. (when (member ":ETC" vals)
  15081. (setq vals (remove ":ETC" vals))
  15082. (org-add-props (car vals) '(org-unrestricted t)))
  15083. (if table (mapcar 'list vals) vals)))
  15084. (defun org-property-previous-allowed-value (&optional previous)
  15085. "Switch to the next allowed value for this property."
  15086. (interactive)
  15087. (org-property-next-allowed-value t))
  15088. (defun org-property-next-allowed-value (&optional previous)
  15089. "Switch to the next allowed value for this property."
  15090. (interactive)
  15091. (unless (org-at-property-p)
  15092. (user-error "Not at a property"))
  15093. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15094. (key (match-string 2))
  15095. (value (match-string 3))
  15096. (allowed (or (org-property-get-allowed-values (point) key)
  15097. (and (member value '("[ ]" "[-]" "[X]"))
  15098. '("[ ]" "[X]"))))
  15099. (heading (save-match-data (nth 4 (org-heading-components))))
  15100. nval)
  15101. (unless allowed
  15102. (user-error "Allowed values for this property have not been defined"))
  15103. (if previous (setq allowed (reverse allowed)))
  15104. (if (member value allowed)
  15105. (setq nval (car (cdr (member value allowed)))))
  15106. (setq nval (or nval (car allowed)))
  15107. (if (equal nval value)
  15108. (user-error "Only one allowed value for this property"))
  15109. (org-at-property-p)
  15110. (replace-match (concat " :" key ": " nval) t t)
  15111. (org-indent-line)
  15112. (beginning-of-line 1)
  15113. (skip-chars-forward " \t")
  15114. (when (equal prop org-effort-property)
  15115. (org-refresh-property
  15116. '((effort . identity)
  15117. (effort-minutes . org-duration-string-to-minutes))
  15118. nval)
  15119. (when (string= org-clock-current-task heading)
  15120. (setq org-clock-effort nval)
  15121. (org-clock-update-mode-line)))
  15122. (run-hook-with-args 'org-property-changed-functions key nval)))
  15123. (defun org-find-olp (path &optional this-buffer)
  15124. "Return a marker pointing to the entry at outline path OLP.
  15125. If anything goes wrong, throw an error.
  15126. You can wrap this call to catch the error like this:
  15127. (condition-case msg
  15128. (org-mobile-locate-entry (match-string 4))
  15129. (error (nth 1 msg)))
  15130. The return value will then be either a string with the error message,
  15131. or a marker if everything is OK.
  15132. If THIS-BUFFER is set, the outline path does not contain a file,
  15133. only headings."
  15134. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15135. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15136. (level 1)
  15137. (lmin 1)
  15138. (lmax 1)
  15139. limit re end found pos heading cnt flevel)
  15140. (unless buffer (error "File not found :%s" file))
  15141. (with-current-buffer buffer
  15142. (save-excursion
  15143. (save-restriction
  15144. (widen)
  15145. (setq limit (point-max))
  15146. (goto-char (point-min))
  15147. (dolist (heading path)
  15148. (setq re (format org-complex-heading-regexp-format
  15149. (regexp-quote heading)))
  15150. (setq cnt 0 pos (point))
  15151. (while (re-search-forward re end t)
  15152. (setq level (- (match-end 1) (match-beginning 1)))
  15153. (if (and (>= level lmin) (<= level lmax))
  15154. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15155. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15156. lmax heading))
  15157. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15158. lmax heading))
  15159. (goto-char found)
  15160. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15161. (setq end (save-excursion (org-end-of-subtree t t))))
  15162. (when (org-at-heading-p)
  15163. (point-marker)))))))
  15164. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15165. "Find node HEADING in BUFFER.
  15166. Return a marker to the heading if it was found, or nil if not.
  15167. If POS-ONLY is set, return just the position instead of a marker.
  15168. The heading text must match exact, but it may have a TODO keyword,
  15169. a priority cookie and tags in the standard locations."
  15170. (with-current-buffer (or buffer (current-buffer))
  15171. (save-excursion
  15172. (save-restriction
  15173. (widen)
  15174. (goto-char (point-min))
  15175. (let (case-fold-search)
  15176. (if (re-search-forward
  15177. (format org-complex-heading-regexp-format
  15178. (regexp-quote heading)) nil t)
  15179. (if pos-only
  15180. (match-beginning 0)
  15181. (move-marker (make-marker) (match-beginning 0)))))))))
  15182. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15183. "Find Org node headline HEADING in all .org files in directory DIR.
  15184. When the target headline is found, return a marker to this location."
  15185. (let ((files (directory-files (or dir default-directory)
  15186. t "\\`[^.#].*\\.org\\'"))
  15187. visiting m buffer)
  15188. (catch 'found
  15189. (dolist (file files)
  15190. (message "trying %s" file)
  15191. (setq visiting (org-find-base-buffer-visiting file))
  15192. (setq buffer (or visiting (find-file-noselect file)))
  15193. (setq m (org-find-exact-headline-in-buffer
  15194. heading buffer))
  15195. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15196. (and m (throw 'found m))))))
  15197. (defun org-find-entry-with-id (ident)
  15198. "Locate the entry that contains the ID property with exact value IDENT.
  15199. IDENT can be a string, a symbol or a number, this function will search for
  15200. the string representation of it.
  15201. Return the position where this entry starts, or nil if there is no such entry."
  15202. (interactive "sID: ")
  15203. (let ((id (cond
  15204. ((stringp ident) ident)
  15205. ((symbolp ident) (symbol-name ident))
  15206. ((numberp ident) (number-to-string ident))
  15207. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15208. (org-with-wide-buffer (org-find-property "ID" id))))
  15209. ;;;; Timestamps
  15210. (defvar org-last-changed-timestamp nil)
  15211. (defvar org-last-inserted-timestamp nil
  15212. "The last time stamp inserted with `org-insert-time-stamp'.")
  15213. (defvar org-ts-what) ; dynamically scoped parameter
  15214. (defun org-time-stamp (arg &optional inactive)
  15215. "Prompt for a date/time and insert a time stamp.
  15216. If the user specifies a time like HH:MM or if this command is
  15217. called with at least one prefix argument, the time stamp contains
  15218. the date and the time. Otherwise, only the date is included.
  15219. All parts of a date not specified by the user are filled in from
  15220. the timestamp at point, if any, or the current date/time
  15221. otherwise.
  15222. If there is already a timestamp at the cursor, it is replaced.
  15223. With two universal prefix arguments, insert an active timestamp
  15224. with the current time without prompting the user.
  15225. When called from lisp, the timestamp is inactive if INACTIVE is
  15226. non-nil."
  15227. (interactive "P")
  15228. (let* ((ts (cond
  15229. ((org-at-date-range-p t)
  15230. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15231. ((org-at-timestamp-p t) (match-string 0))))
  15232. ;; Default time is either the timestamp at point or today.
  15233. ;; When entering a range, only the range start is considered.
  15234. (default-time (if (not ts) (current-time)
  15235. (apply #'encode-time (org-parse-time-string ts))))
  15236. (default-input (and ts (org-get-compact-tod ts)))
  15237. (repeater (and ts
  15238. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15239. (match-string 0 ts)))
  15240. org-time-was-given
  15241. org-end-time-was-given
  15242. (time
  15243. (and (if (equal arg '(16)) (current-time)
  15244. ;; Preserve `this-command' and `last-command'.
  15245. (let ((this-command this-command)
  15246. (last-command last-command))
  15247. (org-read-date
  15248. arg 'totime nil nil default-time default-input
  15249. inactive))))))
  15250. (cond
  15251. ((and ts
  15252. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15253. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15254. (insert "--")
  15255. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15256. (ts
  15257. ;; Make sure we're on a timestamp. When in the middle of a date
  15258. ;; range, move arbitrarily to range end.
  15259. (unless (org-at-timestamp-p t)
  15260. (skip-chars-forward "-")
  15261. (org-at-timestamp-p t))
  15262. (replace-match "")
  15263. (setq org-last-changed-timestamp
  15264. (org-insert-time-stamp
  15265. time (or org-time-was-given arg)
  15266. inactive nil nil (list org-end-time-was-given)))
  15267. (when repeater
  15268. (backward-char)
  15269. (insert " " repeater)
  15270. (setq org-last-changed-timestamp
  15271. (concat (substring org-last-inserted-timestamp 0 -1)
  15272. " " repeater ">")))
  15273. (message "Timestamp updated"))
  15274. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15275. (t (org-insert-time-stamp
  15276. time (or org-time-was-given arg) inactive nil nil
  15277. (list org-end-time-was-given))))))
  15278. ;; FIXME: can we use this for something else, like computing time differences?
  15279. (defun org-get-compact-tod (s)
  15280. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15281. (let* ((t1 (match-string 1 s))
  15282. (h1 (string-to-number (match-string 2 s)))
  15283. (m1 (string-to-number (match-string 3 s)))
  15284. (t2 (and (match-end 4) (match-string 5 s)))
  15285. (h2 (and t2 (string-to-number (match-string 6 s))))
  15286. (m2 (and t2 (string-to-number (match-string 7 s))))
  15287. dh dm)
  15288. (if (not t2)
  15289. t1
  15290. (setq dh (- h2 h1) dm (- m2 m1))
  15291. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15292. (concat t1 "+" (number-to-string dh)
  15293. (and (/= 0 dm) (format ":%02d" dm)))))))
  15294. (defun org-time-stamp-inactive (&optional arg)
  15295. "Insert an inactive time stamp.
  15296. An inactive time stamp is enclosed in square brackets instead of angle
  15297. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15298. does not link to the calendar and cannot be changed with the S-cursor keys.
  15299. So these are more for recording a certain time/date."
  15300. (interactive "P")
  15301. (org-time-stamp arg 'inactive))
  15302. (defvar org-date-ovl (make-overlay 1 1))
  15303. (overlay-put org-date-ovl 'face 'org-date-selected)
  15304. (org-detach-overlay org-date-ovl)
  15305. (defvar org-ans1) ; dynamically scoped parameter
  15306. (defvar org-ans2) ; dynamically scoped parameter
  15307. (defvar org-plain-time-of-day-regexp) ; defined below
  15308. (defvar org-overriding-default-time nil) ; dynamically scoped
  15309. (defvar org-read-date-overlay nil)
  15310. (defvar org-dcst nil) ; dynamically scoped
  15311. (defvar org-read-date-history nil)
  15312. (defvar org-read-date-final-answer nil)
  15313. (defvar org-read-date-analyze-futurep nil)
  15314. (defvar org-read-date-analyze-forced-year nil)
  15315. (defvar org-read-date-inactive)
  15316. (defvar org-read-date-minibuffer-local-map
  15317. (let* ((map (make-sparse-keymap)))
  15318. (set-keymap-parent map minibuffer-local-map)
  15319. (org-defkey map (kbd ".")
  15320. (lambda () (interactive)
  15321. ;; Are we at the beginning of the prompt?
  15322. (if (looking-back "^[^:]+: ")
  15323. (org-eval-in-calendar '(calendar-goto-today))
  15324. (insert "."))))
  15325. (org-defkey map (kbd "C-.")
  15326. (lambda () (interactive)
  15327. (org-eval-in-calendar '(calendar-goto-today))))
  15328. (org-defkey map [(meta shift left)]
  15329. (lambda () (interactive)
  15330. (org-eval-in-calendar '(calendar-backward-month 1))))
  15331. (org-defkey map [(meta shift right)]
  15332. (lambda () (interactive)
  15333. (org-eval-in-calendar '(calendar-forward-month 1))))
  15334. (org-defkey map [(meta shift up)]
  15335. (lambda () (interactive)
  15336. (org-eval-in-calendar '(calendar-backward-year 1))))
  15337. (org-defkey map [(meta shift down)]
  15338. (lambda () (interactive)
  15339. (org-eval-in-calendar '(calendar-forward-year 1))))
  15340. (org-defkey map [?\e (shift left)]
  15341. (lambda () (interactive)
  15342. (org-eval-in-calendar '(calendar-backward-month 1))))
  15343. (org-defkey map [?\e (shift right)]
  15344. (lambda () (interactive)
  15345. (org-eval-in-calendar '(calendar-forward-month 1))))
  15346. (org-defkey map [?\e (shift up)]
  15347. (lambda () (interactive)
  15348. (org-eval-in-calendar '(calendar-backward-year 1))))
  15349. (org-defkey map [?\e (shift down)]
  15350. (lambda () (interactive)
  15351. (org-eval-in-calendar '(calendar-forward-year 1))))
  15352. (org-defkey map [(shift up)]
  15353. (lambda () (interactive)
  15354. (org-eval-in-calendar '(calendar-backward-week 1))))
  15355. (org-defkey map [(shift down)]
  15356. (lambda () (interactive)
  15357. (org-eval-in-calendar '(calendar-forward-week 1))))
  15358. (org-defkey map [(shift left)]
  15359. (lambda () (interactive)
  15360. (org-eval-in-calendar '(calendar-backward-day 1))))
  15361. (org-defkey map [(shift right)]
  15362. (lambda () (interactive)
  15363. (org-eval-in-calendar '(calendar-forward-day 1))))
  15364. (org-defkey map "!"
  15365. (lambda () (interactive)
  15366. (org-eval-in-calendar '(diary-view-entries))
  15367. (message "")))
  15368. (org-defkey map ">"
  15369. (lambda () (interactive)
  15370. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15371. (org-defkey map "<"
  15372. (lambda () (interactive)
  15373. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15374. (org-defkey map "\C-v"
  15375. (lambda () (interactive)
  15376. (org-eval-in-calendar
  15377. '(calendar-scroll-left-three-months 1))))
  15378. (org-defkey map "\M-v"
  15379. (lambda () (interactive)
  15380. (org-eval-in-calendar
  15381. '(calendar-scroll-right-three-months 1))))
  15382. map)
  15383. "Keymap for minibuffer commands when using `org-read-date'.")
  15384. (defvar org-def)
  15385. (defvar org-defdecode)
  15386. (defvar org-with-time)
  15387. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15388. default-time default-input inactive)
  15389. "Read a date, possibly a time, and make things smooth for the user.
  15390. The prompt will suggest to enter an ISO date, but you can also enter anything
  15391. which will at least partially be understood by `parse-time-string'.
  15392. Unrecognized parts of the date will default to the current day, month, year,
  15393. hour and minute. If this command is called to replace a timestamp at point,
  15394. or to enter the second timestamp of a range, the default time is taken
  15395. from the existing stamp. Furthermore, the command prefers the future,
  15396. so if you are giving a date where the year is not given, and the day-month
  15397. combination is already past in the current year, it will assume you
  15398. mean next year. For details, see the manual. A few examples:
  15399. 3-2-5 --> 2003-02-05
  15400. feb 15 --> currentyear-02-15
  15401. 2/15 --> currentyear-02-15
  15402. sep 12 9 --> 2009-09-12
  15403. 12:45 --> today 12:45
  15404. 22 sept 0:34 --> currentyear-09-22 0:34
  15405. 12 --> currentyear-currentmonth-12
  15406. Fri --> nearest Friday after today
  15407. -Tue --> last Tuesday
  15408. etc.
  15409. Furthermore you can specify a relative date by giving, as the *first* thing
  15410. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15411. change in days weeks, months, years.
  15412. With a single plus or minus, the date is relative to today. With a double
  15413. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15414. +4d --> four days from today
  15415. +4 --> same as above
  15416. +2w --> two weeks from today
  15417. ++5 --> five days from default date
  15418. The function understands only English month and weekday abbreviations.
  15419. While prompting, a calendar is popped up - you can also select the
  15420. date with the mouse (button 1). The calendar shows a period of three
  15421. months. To scroll it to other months, use the keys `>' and `<'.
  15422. If you don't like the calendar, turn it off with
  15423. (setq org-read-date-popup-calendar nil)
  15424. With optional argument TO-TIME, the date will immediately be converted
  15425. to an internal time.
  15426. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15427. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15428. still enter a time, and this function will inform the calling routine
  15429. about this change. The calling routine may then choose to change the
  15430. format used to insert the time stamp into the buffer to include the time.
  15431. With optional argument FROM-STRING, read from this string instead from
  15432. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15433. the time/date that is used for everything that is not specified by the
  15434. user."
  15435. (require 'parse-time)
  15436. (let* ((org-time-stamp-rounding-minutes
  15437. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15438. (org-dcst org-display-custom-times)
  15439. (ct (org-current-time))
  15440. (org-def (or org-overriding-default-time default-time ct))
  15441. (org-defdecode (decode-time org-def))
  15442. (dummy (progn
  15443. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15444. (setcar (nthcdr 2 org-defdecode) -1)
  15445. (setcar (nthcdr 1 org-defdecode) 59)
  15446. (setq org-def (apply 'encode-time org-defdecode)
  15447. org-defdecode (decode-time org-def)))))
  15448. (cur-frame (selected-frame))
  15449. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15450. (calendar-frame-setup nil)
  15451. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15452. (calendar-move-hook nil)
  15453. (calendar-view-diary-initially-flag nil)
  15454. (calendar-view-holidays-initially-flag nil)
  15455. (timestr (format-time-string
  15456. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15457. (prompt (concat (if prompt (concat prompt " ") "")
  15458. (format "Date+time [%s]: " timestr)))
  15459. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15460. (cond
  15461. (from-string (setq ans from-string))
  15462. (org-read-date-popup-calendar
  15463. (save-excursion
  15464. (save-window-excursion
  15465. (calendar)
  15466. (when (eq calendar-setup 'calendar-only)
  15467. (setq cal-frame
  15468. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15469. (select-frame cal-frame))
  15470. (org-eval-in-calendar '(setq cursor-type nil) t)
  15471. (unwind-protect
  15472. (progn
  15473. (calendar-forward-day (- (time-to-days org-def)
  15474. (calendar-absolute-from-gregorian
  15475. (calendar-current-date))))
  15476. (org-eval-in-calendar nil t)
  15477. (let* ((old-map (current-local-map))
  15478. (map (copy-keymap calendar-mode-map))
  15479. (minibuffer-local-map
  15480. (copy-keymap org-read-date-minibuffer-local-map)))
  15481. (org-defkey map (kbd "RET") 'org-calendar-select)
  15482. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15483. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15484. (unwind-protect
  15485. (progn
  15486. (use-local-map map)
  15487. (setq org-read-date-inactive inactive)
  15488. (add-hook 'post-command-hook 'org-read-date-display)
  15489. (setq org-ans0 (read-string prompt default-input
  15490. 'org-read-date-history nil))
  15491. ;; org-ans0: from prompt
  15492. ;; org-ans1: from mouse click
  15493. ;; org-ans2: from calendar motion
  15494. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15495. (remove-hook 'post-command-hook 'org-read-date-display)
  15496. (use-local-map old-map)
  15497. (when org-read-date-overlay
  15498. (delete-overlay org-read-date-overlay)
  15499. (setq org-read-date-overlay nil)))))
  15500. (bury-buffer "*Calendar*")
  15501. (when cal-frame
  15502. (delete-frame cal-frame)
  15503. (select-frame-set-input-focus cur-frame))))))
  15504. (t ; Naked prompt only
  15505. (unwind-protect
  15506. (setq ans (read-string prompt default-input
  15507. 'org-read-date-history timestr))
  15508. (when org-read-date-overlay
  15509. (delete-overlay org-read-date-overlay)
  15510. (setq org-read-date-overlay nil)))))
  15511. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15512. (when org-read-date-analyze-forced-year
  15513. (message "Year was forced into %s"
  15514. (if org-read-date-force-compatible-dates
  15515. "compatible range (1970-2037)"
  15516. "range representable on this machine"))
  15517. (ding))
  15518. ;; One round trip to get rid of 34th of August and stuff like that....
  15519. (setq final (decode-time (apply 'encode-time final)))
  15520. (setq org-read-date-final-answer ans)
  15521. (if to-time
  15522. (apply 'encode-time final)
  15523. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15524. (format "%04d-%02d-%02d %02d:%02d"
  15525. (nth 5 final) (nth 4 final) (nth 3 final)
  15526. (nth 2 final) (nth 1 final))
  15527. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15528. (defun org-read-date-display ()
  15529. "Display the current date prompt interpretation in the minibuffer."
  15530. (when org-read-date-display-live
  15531. (when org-read-date-overlay
  15532. (delete-overlay org-read-date-overlay))
  15533. (when (minibufferp (current-buffer))
  15534. (save-excursion
  15535. (end-of-line 1)
  15536. (while (not (equal (buffer-substring
  15537. (max (point-min) (- (point) 4)) (point))
  15538. " "))
  15539. (insert " ")))
  15540. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15541. " " (or org-ans1 org-ans2)))
  15542. (org-end-time-was-given nil)
  15543. (f (org-read-date-analyze ans org-def org-defdecode))
  15544. (fmts (if org-dcst
  15545. org-time-stamp-custom-formats
  15546. org-time-stamp-formats))
  15547. (fmt (if (or org-with-time
  15548. (and (boundp 'org-time-was-given) org-time-was-given))
  15549. (cdr fmts)
  15550. (car fmts)))
  15551. (txt (format-time-string fmt (apply 'encode-time f)))
  15552. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15553. (txt (concat "=> " txt)))
  15554. (when (and org-end-time-was-given
  15555. (string-match org-plain-time-of-day-regexp txt))
  15556. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15557. org-end-time-was-given
  15558. (substring txt (match-end 0)))))
  15559. (when org-read-date-analyze-futurep
  15560. (setq txt (concat txt " (=>F)")))
  15561. (setq org-read-date-overlay
  15562. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15563. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15564. (defun org-read-date-analyze (ans org-def org-defdecode)
  15565. "Analyze the combined answer of the date prompt."
  15566. ;; FIXME: cleanup and comment
  15567. ;; Pass `current-time' result to `decode-time' (instead of calling
  15568. ;; without arguments) so that only `current-time' has to be
  15569. ;; overriden in tests.
  15570. (let ((nowdecode (decode-time (current-time)))
  15571. delta deltan deltaw deltadef year month day
  15572. hour minute second wday pm h2 m2 tl wday1
  15573. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15574. (setq org-read-date-analyze-futurep nil
  15575. org-read-date-analyze-forced-year nil)
  15576. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15577. (setq ans "+0"))
  15578. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15579. (setq ans (replace-match "" t t ans)
  15580. deltan (car delta)
  15581. deltaw (nth 1 delta)
  15582. deltadef (nth 2 delta)))
  15583. ;; Check if there is an iso week date in there. If yes, store the
  15584. ;; info and postpone interpreting it until the rest of the parsing
  15585. ;; is done.
  15586. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15587. (setq iso-year (if (match-end 1)
  15588. (org-small-year-to-year
  15589. (string-to-number (match-string 1 ans))))
  15590. iso-weekday (if (match-end 3)
  15591. (string-to-number (match-string 3 ans)))
  15592. iso-week (string-to-number (match-string 2 ans)))
  15593. (setq ans (replace-match "" t t ans)))
  15594. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15595. (when (string-match
  15596. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15597. (setq year (if (match-end 2)
  15598. (string-to-number (match-string 2 ans))
  15599. (progn (setq kill-year t)
  15600. (string-to-number (format-time-string "%Y"))))
  15601. month (string-to-number (match-string 3 ans))
  15602. day (string-to-number (match-string 4 ans)))
  15603. (if (< year 100) (setq year (+ 2000 year)))
  15604. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15605. t nil ans)))
  15606. ;; Help matching dotted european dates
  15607. (when (string-match
  15608. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15609. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15610. (setq kill-year t)
  15611. (string-to-number (format-time-string "%Y")))
  15612. day (string-to-number (match-string 1 ans))
  15613. month (string-to-number (match-string 2 ans))
  15614. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15615. t nil ans)))
  15616. ;; Help matching american dates, like 5/30 or 5/30/7
  15617. (when (string-match
  15618. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15619. (setq year (if (match-end 4)
  15620. (string-to-number (match-string 4 ans))
  15621. (progn (setq kill-year t)
  15622. (string-to-number (format-time-string "%Y"))))
  15623. month (string-to-number (match-string 1 ans))
  15624. day (string-to-number (match-string 2 ans)))
  15625. (if (< year 100) (setq year (+ 2000 year)))
  15626. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15627. t nil ans)))
  15628. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15629. ;; If there is a time with am/pm, and *no* time without it, we convert
  15630. ;; so that matching will be successful.
  15631. (loop for i from 1 to 2 do ; twice, for end time as well
  15632. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15633. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15634. (setq hour (string-to-number (match-string 1 ans))
  15635. minute (if (match-end 3)
  15636. (string-to-number (match-string 3 ans))
  15637. 0)
  15638. pm (equal ?p
  15639. (string-to-char (downcase (match-string 4 ans)))))
  15640. (if (and (= hour 12) (not pm))
  15641. (setq hour 0)
  15642. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15643. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15644. t t ans))))
  15645. ;; Check if a time range is given as a duration
  15646. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15647. (setq hour (string-to-number (match-string 1 ans))
  15648. h2 (+ hour (string-to-number (match-string 3 ans)))
  15649. minute (string-to-number (match-string 2 ans))
  15650. m2 (+ minute (if (match-end 5) (string-to-number
  15651. (match-string 5 ans))0)))
  15652. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15653. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15654. t t ans)))
  15655. ;; Check if there is a time range
  15656. (when (boundp 'org-end-time-was-given)
  15657. (setq org-time-was-given nil)
  15658. (when (and (string-match org-plain-time-of-day-regexp ans)
  15659. (match-end 8))
  15660. (setq org-end-time-was-given (match-string 8 ans))
  15661. (setq ans (concat (substring ans 0 (match-beginning 7))
  15662. (substring ans (match-end 7))))))
  15663. (setq tl (parse-time-string ans)
  15664. day (or (nth 3 tl) (nth 3 org-defdecode))
  15665. month
  15666. (cond ((nth 4 tl))
  15667. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15668. ;; Day was specified. Make sure DAY+MONTH
  15669. ;; combination happens in the future.
  15670. ((nth 3 tl)
  15671. (setq futurep t)
  15672. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15673. (nth 4 nowdecode)))
  15674. (t (nth 4 org-defdecode)))
  15675. year
  15676. (cond ((and (not kill-year) (nth 5 tl)))
  15677. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15678. ;; Month was guessed in the future and is at least
  15679. ;; equal to NOWDECODE's. Fix year accordingly.
  15680. (futurep
  15681. (if (or (> month (nth 4 nowdecode))
  15682. (>= day (nth 3 nowdecode)))
  15683. (nth 5 nowdecode)
  15684. (1+ (nth 5 nowdecode))))
  15685. ;; Month was specified. Make sure MONTH+YEAR
  15686. ;; combination happens in the future.
  15687. ((nth 4 tl)
  15688. (setq futurep t)
  15689. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15690. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15691. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15692. (t (nth 5 nowdecode))))
  15693. (t (nth 5 org-defdecode)))
  15694. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15695. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15696. second (or (nth 0 tl) 0)
  15697. wday (nth 6 tl))
  15698. (when (and (eq org-read-date-prefer-future 'time)
  15699. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15700. (equal day (nth 3 nowdecode))
  15701. (equal month (nth 4 nowdecode))
  15702. (equal year (nth 5 nowdecode))
  15703. (nth 2 tl)
  15704. (or (< (nth 2 tl) (nth 2 nowdecode))
  15705. (and (= (nth 2 tl) (nth 2 nowdecode))
  15706. (nth 1 tl)
  15707. (< (nth 1 tl) (nth 1 nowdecode)))))
  15708. (setq day (1+ day)
  15709. futurep t))
  15710. ;; Special date definitions below
  15711. (cond
  15712. (iso-week
  15713. ;; There was an iso week
  15714. (require 'cal-iso)
  15715. (setq futurep nil)
  15716. (setq year (or iso-year year)
  15717. day (or iso-weekday wday 1)
  15718. wday nil ; to make sure that the trigger below does not match
  15719. iso-date (calendar-gregorian-from-absolute
  15720. (calendar-iso-to-absolute
  15721. (list iso-week day year))))
  15722. ; FIXME: Should we also push ISO weeks into the future?
  15723. ; (when (and org-read-date-prefer-future
  15724. ; (not iso-year)
  15725. ; (< (calendar-absolute-from-gregorian iso-date)
  15726. ; (time-to-days (current-time))))
  15727. ; (setq year (1+ year)
  15728. ; iso-date (calendar-gregorian-from-absolute
  15729. ; (calendar-iso-to-absolute
  15730. ; (list iso-week day year)))))
  15731. (setq month (car iso-date)
  15732. year (nth 2 iso-date)
  15733. day (nth 1 iso-date)))
  15734. (deltan
  15735. (setq futurep nil)
  15736. (unless deltadef
  15737. ;; Pass `current-time' result to `decode-time' (instead of
  15738. ;; calling without arguments) so that only `current-time' has
  15739. ;; to be overriden in tests.
  15740. (let ((now (decode-time (current-time))))
  15741. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15742. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15743. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15744. ((equal deltaw "m") (setq month (+ month deltan)))
  15745. ((equal deltaw "y") (setq year (+ year deltan)))))
  15746. ((and wday (not (nth 3 tl)))
  15747. ;; Weekday was given, but no day, so pick that day in the week
  15748. ;; on or after the derived date.
  15749. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15750. (unless (equal wday wday1)
  15751. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15752. (if (and (boundp 'org-time-was-given)
  15753. (nth 2 tl))
  15754. (setq org-time-was-given t))
  15755. (if (< year 100) (setq year (+ 2000 year)))
  15756. ;; Check of the date is representable
  15757. (if org-read-date-force-compatible-dates
  15758. (progn
  15759. (if (< year 1970)
  15760. (setq year 1970 org-read-date-analyze-forced-year t))
  15761. (if (> year 2037)
  15762. (setq year 2037 org-read-date-analyze-forced-year t)))
  15763. (condition-case nil
  15764. (ignore (encode-time second minute hour day month year))
  15765. (error
  15766. (setq year (nth 5 org-defdecode))
  15767. (setq org-read-date-analyze-forced-year t))))
  15768. (setq org-read-date-analyze-futurep futurep)
  15769. (list second minute hour day month year)))
  15770. (defvar parse-time-weekdays)
  15771. (defun org-read-date-get-relative (s today default)
  15772. "Check string S for special relative date string.
  15773. TODAY and DEFAULT are internal times, for today and for a default.
  15774. Return shift list (N what def-flag)
  15775. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15776. N is the number of WHATs to shift.
  15777. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15778. the DEFAULT date rather than TODAY."
  15779. (require 'parse-time)
  15780. (when (and
  15781. (string-match
  15782. (concat
  15783. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15784. "\\([0-9]+\\)?"
  15785. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15786. "\\([ \t]\\|$\\)") s)
  15787. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15788. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15789. (string-to-char (substring (match-string 1 s) -1))
  15790. ?+))
  15791. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15792. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15793. (what (if (match-end 3) (match-string 3 s) "d"))
  15794. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15795. (date (if rel default today))
  15796. (wday (nth 6 (decode-time date)))
  15797. delta)
  15798. (if wday1
  15799. (progn
  15800. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15801. (if (= delta 0) (setq delta 7))
  15802. (if (= dir ?-)
  15803. (progn
  15804. (setq delta (- delta 7))
  15805. (if (= delta 0) (setq delta -7))))
  15806. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15807. (list delta "d" rel))
  15808. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15809. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15810. "Turn a user-specified date into the internal representation.
  15811. The internal representation needed by the calendar is (month day year).
  15812. This is a wrapper to handle the brain-dead convention in calendar that
  15813. user function argument order change dependent on argument order."
  15814. (if (boundp 'calendar-date-style)
  15815. (cond
  15816. ((eq calendar-date-style 'american)
  15817. (list arg1 arg2 arg3))
  15818. ((eq calendar-date-style 'european)
  15819. (list arg2 arg1 arg3))
  15820. ((eq calendar-date-style 'iso)
  15821. (list arg2 arg3 arg1)))
  15822. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15823. (if (org-bound-and-true-p european-calendar-style)
  15824. (list arg2 arg1 arg3)
  15825. (list arg1 arg2 arg3)))))
  15826. (defun org-eval-in-calendar (form &optional keepdate)
  15827. "Eval FORM in the calendar window and return to current window.
  15828. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15829. otherwise stick to the current value of `org-ans2'."
  15830. (let ((sf (selected-frame))
  15831. (sw (selected-window)))
  15832. (select-window (get-buffer-window "*Calendar*" t))
  15833. (eval form)
  15834. (when (and (not keepdate) (calendar-cursor-to-date))
  15835. (let* ((date (calendar-cursor-to-date))
  15836. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15837. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15838. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15839. (select-window sw)
  15840. (org-select-frame-set-input-focus sf)))
  15841. (defun org-calendar-select ()
  15842. "Return to `org-read-date' with the date currently selected.
  15843. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15844. (interactive)
  15845. (when (calendar-cursor-to-date)
  15846. (let* ((date (calendar-cursor-to-date))
  15847. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15848. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15849. (if (active-minibuffer-window) (exit-minibuffer))))
  15850. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15851. "Insert a date stamp for the date given by the internal TIME.
  15852. See `format-time-string' for the format of TIME.
  15853. WITH-HM means use the stamp format that includes the time of the day.
  15854. INACTIVE means use square brackets instead of angular ones, so that the
  15855. stamp will not contribute to the agenda.
  15856. PRE and POST are optional strings to be inserted before and after the
  15857. stamp.
  15858. The command returns the inserted time stamp."
  15859. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15860. stamp)
  15861. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15862. (insert-before-markers (or pre ""))
  15863. (when (listp extra)
  15864. (setq extra (car extra))
  15865. (if (and (stringp extra)
  15866. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15867. (setq extra (format "-%02d:%02d"
  15868. (string-to-number (match-string 1 extra))
  15869. (string-to-number (match-string 2 extra))))
  15870. (setq extra nil)))
  15871. (when extra
  15872. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15873. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15874. (insert-before-markers (or post ""))
  15875. (setq org-last-inserted-timestamp stamp)))
  15876. (defun org-toggle-time-stamp-overlays ()
  15877. "Toggle the use of custom time stamp formats."
  15878. (interactive)
  15879. (setq org-display-custom-times (not org-display-custom-times))
  15880. (unless org-display-custom-times
  15881. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15882. (while (setq p (next-single-property-change p 'display))
  15883. (if (and (get-text-property p 'display)
  15884. (eq (get-text-property p 'face) 'org-date))
  15885. (remove-text-properties
  15886. p (setq p (next-single-property-change p 'display))
  15887. '(display t))))
  15888. (set-buffer-modified-p bmp)))
  15889. (if (featurep 'xemacs)
  15890. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15891. (org-restart-font-lock)
  15892. (setq org-table-may-need-update t)
  15893. (if org-display-custom-times
  15894. (message "Time stamps are overlaid with custom format")
  15895. (message "Time stamp overlays removed")))
  15896. (defun org-display-custom-time (beg end)
  15897. "Overlay modified time stamp format over timestamp between BEG and END."
  15898. (let* ((ts (buffer-substring beg end))
  15899. t1 w1 with-hm tf time str w2 (off 0))
  15900. (save-match-data
  15901. (setq t1 (org-parse-time-string ts t))
  15902. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15903. (setq off (- (match-end 0) (match-beginning 0)))))
  15904. (setq end (- end off))
  15905. (setq w1 (- end beg)
  15906. with-hm (and (nth 1 t1) (nth 2 t1))
  15907. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15908. time (org-fix-decoded-time t1)
  15909. str (org-add-props
  15910. (format-time-string
  15911. (substring tf 1 -1) (apply 'encode-time time))
  15912. nil 'mouse-face 'highlight)
  15913. w2 (length str))
  15914. (if (not (= w2 w1))
  15915. (add-text-properties (1+ beg) (+ 2 beg)
  15916. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15917. (if (featurep 'xemacs)
  15918. (progn
  15919. (put-text-property beg end 'invisible t)
  15920. (put-text-property beg end 'end-glyph (make-glyph str)))
  15921. (put-text-property beg end 'display str))))
  15922. (defun org-fix-decoded-time (time)
  15923. "Set 0 instead of nil for the first 6 elements of time.
  15924. Don't touch the rest."
  15925. (let ((n 0))
  15926. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15927. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15928. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15929. "Difference between TIMESTAMP-STRING and now in days.
  15930. If SECONDS is non-nil, return the difference in seconds."
  15931. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15932. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15933. (funcall fdiff (current-time)))))
  15934. (defun org-deadline-close (timestamp-string &optional ndays)
  15935. "Is the time in TIMESTAMP-STRING close to the current date?"
  15936. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15937. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15938. (not (org-entry-is-done-p))))
  15939. (defun org-get-wdays (ts &optional delay zero-delay)
  15940. "Get the deadline lead time appropriate for timestring TS.
  15941. When DELAY is non-nil, get the delay time for scheduled items
  15942. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15943. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15944. don't try to find the delay cookie in the scheduled timestamp."
  15945. (let ((tv (if delay org-scheduled-delay-days
  15946. org-deadline-warning-days)))
  15947. (cond
  15948. ((or (and delay (< tv 0))
  15949. (and delay zero-delay (<= tv 0))
  15950. (and (not delay) (<= tv 0)))
  15951. ;; Enforce this value no matter what
  15952. (- tv))
  15953. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15954. ;; lead time is specified.
  15955. (floor (* (string-to-number (match-string 1 ts))
  15956. (cdr (assoc (match-string 2 ts)
  15957. '(("d" . 1) ("w" . 7)
  15958. ("m" . 30.4) ("y" . 365.25)
  15959. ("h" . 0.041667)))))))
  15960. ;; go for the default.
  15961. (t tv))))
  15962. (defun org-calendar-select-mouse (ev)
  15963. "Return to `org-read-date' with the date currently selected.
  15964. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15965. (interactive "e")
  15966. (mouse-set-point ev)
  15967. (when (calendar-cursor-to-date)
  15968. (let* ((date (calendar-cursor-to-date))
  15969. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15970. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15971. (if (active-minibuffer-window) (exit-minibuffer))))
  15972. (defun org-check-deadlines (ndays)
  15973. "Check if there are any deadlines due or past due.
  15974. A deadline is considered due if it happens within `org-deadline-warning-days'
  15975. days from today's date. If the deadline appears in an entry marked DONE,
  15976. it is not shown. The prefix arg NDAYS can be used to test that many
  15977. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15978. (interactive "P")
  15979. (let* ((org-warn-days
  15980. (cond
  15981. ((equal ndays '(4)) 100000)
  15982. (ndays (prefix-numeric-value ndays))
  15983. (t (abs org-deadline-warning-days))))
  15984. (case-fold-search nil)
  15985. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15986. (callback
  15987. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15988. (message "%d deadlines past-due or due within %d days"
  15989. (org-occur regexp nil callback)
  15990. org-warn-days)))
  15991. (defsubst org-re-timestamp (type)
  15992. "Return a regexp for timestamp TYPE.
  15993. Allowed values for TYPE are:
  15994. all: all timestamps
  15995. active: only active timestamps (<...>)
  15996. inactive: only inactive timestamps ([...])
  15997. scheduled: only scheduled timestamps
  15998. deadline: only deadline timestamps
  15999. closed: only closed time-stamps
  16000. When TYPE is nil, fall back on returning a regexp that matches
  16001. both scheduled and deadline timestamps."
  16002. (case type
  16003. (all org-ts-regexp-both)
  16004. (active org-ts-regexp)
  16005. (inactive org-ts-regexp-inactive)
  16006. (scheduled org-scheduled-time-regexp)
  16007. (deadline org-deadline-time-regexp)
  16008. (closed org-closed-time-regexp)
  16009. (otherwise
  16010. (concat "\\<"
  16011. (regexp-opt (list org-deadline-string org-scheduled-string))
  16012. " *<\\([^>]+\\)>"))))
  16013. (defun org-check-before-date (date)
  16014. "Check if there are deadlines or scheduled entries before DATE."
  16015. (interactive (list (org-read-date)))
  16016. (let ((case-fold-search nil)
  16017. (regexp (org-re-timestamp org-ts-type))
  16018. (callback
  16019. `(lambda ()
  16020. (let ((match (match-string 1)))
  16021. (and ,(if (memq org-ts-type '(active inactive all))
  16022. '(eq (org-element-type (org-element-context)) 'timestamp)
  16023. '(org-at-planning-p))
  16024. (time-less-p
  16025. (org-time-string-to-time match)
  16026. (org-time-string-to-time date)))))))
  16027. (message "%d entries before %s"
  16028. (org-occur regexp nil callback) date)))
  16029. (defun org-check-after-date (date)
  16030. "Check if there are deadlines or scheduled entries after DATE."
  16031. (interactive (list (org-read-date)))
  16032. (let ((case-fold-search nil)
  16033. (regexp (org-re-timestamp org-ts-type))
  16034. (callback
  16035. `(lambda ()
  16036. (let ((match (match-string 1)))
  16037. (and ,(if (memq org-ts-type '(active inactive all))
  16038. '(eq (org-element-type (org-element-context)) 'timestamp)
  16039. '(org-at-planning-p))
  16040. (not (time-less-p
  16041. (org-time-string-to-time match)
  16042. (org-time-string-to-time date))))))))
  16043. (message "%d entries after %s"
  16044. (org-occur regexp nil callback) date)))
  16045. (defun org-check-dates-range (start-date end-date)
  16046. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16047. (interactive (list (org-read-date nil nil nil "Range starts")
  16048. (org-read-date nil nil nil "Range end")))
  16049. (let ((case-fold-search nil)
  16050. (regexp (org-re-timestamp org-ts-type))
  16051. (callback
  16052. `(lambda ()
  16053. (let ((match (match-string 1)))
  16054. (and
  16055. ,(if (memq org-ts-type '(active inactive all))
  16056. '(eq (org-element-type (org-element-context)) 'timestamp)
  16057. '(org-at-planning-p))
  16058. (not (time-less-p
  16059. (org-time-string-to-time match)
  16060. (org-time-string-to-time start-date)))
  16061. (time-less-p
  16062. (org-time-string-to-time match)
  16063. (org-time-string-to-time end-date)))))))
  16064. (message "%d entries between %s and %s"
  16065. (org-occur regexp nil callback) start-date end-date)))
  16066. (defun org-evaluate-time-range (&optional to-buffer)
  16067. "Evaluate a time range by computing the difference between start and end.
  16068. Normally the result is just printed in the echo area, but with prefix arg
  16069. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16070. If the time range is actually in a table, the result is inserted into the
  16071. next column.
  16072. For time difference computation, a year is assumed to be exactly 365
  16073. days in order to avoid rounding problems."
  16074. (interactive "P")
  16075. (or
  16076. (org-clock-update-time-maybe)
  16077. (save-excursion
  16078. (unless (org-at-date-range-p t)
  16079. (goto-char (point-at-bol))
  16080. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16081. (if (not (org-at-date-range-p t))
  16082. (user-error "Not at a time-stamp range, and none found in current line")))
  16083. (let* ((ts1 (match-string 1))
  16084. (ts2 (match-string 2))
  16085. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16086. (match-end (match-end 0))
  16087. (time1 (org-time-string-to-time ts1))
  16088. (time2 (org-time-string-to-time ts2))
  16089. (t1 (org-float-time time1))
  16090. (t2 (org-float-time time2))
  16091. (diff (abs (- t2 t1)))
  16092. (negative (< (- t2 t1) 0))
  16093. ;; (ys (floor (* 365 24 60 60)))
  16094. (ds (* 24 60 60))
  16095. (hs (* 60 60))
  16096. (fy "%dy %dd %02d:%02d")
  16097. (fy1 "%dy %dd")
  16098. (fd "%dd %02d:%02d")
  16099. (fd1 "%dd")
  16100. (fh "%02d:%02d")
  16101. y d h m align)
  16102. (if havetime
  16103. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16104. y 0
  16105. d (floor (/ diff ds)) diff (mod diff ds)
  16106. h (floor (/ diff hs)) diff (mod diff hs)
  16107. m (floor (/ diff 60)))
  16108. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16109. y 0
  16110. d (floor (+ (/ diff ds) 0.5))
  16111. h 0 m 0))
  16112. (if (not to-buffer)
  16113. (message "%s" (org-make-tdiff-string y d h m))
  16114. (if (org-at-table-p)
  16115. (progn
  16116. (goto-char match-end)
  16117. (setq align t)
  16118. (and (looking-at " *|") (goto-char (match-end 0))))
  16119. (goto-char match-end))
  16120. (if (looking-at
  16121. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16122. (replace-match ""))
  16123. (if negative (insert " -"))
  16124. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16125. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16126. (insert " " (format fh h m))))
  16127. (if align (org-table-align))
  16128. (message "Time difference inserted")))))
  16129. (defun org-make-tdiff-string (y d h m)
  16130. (let ((fmt "")
  16131. (l nil))
  16132. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16133. l (push y l)))
  16134. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16135. l (push d l)))
  16136. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16137. l (push h l)))
  16138. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16139. l (push m l)))
  16140. (apply 'format fmt (nreverse l))))
  16141. (defun org-time-string-to-time (s &optional buffer pos)
  16142. "Convert a timestamp string into internal time."
  16143. (condition-case errdata
  16144. (apply 'encode-time (org-parse-time-string s))
  16145. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16146. s (if (not (and buffer pos))
  16147. ""
  16148. (format-message " at %d in buffer `%s'" pos buffer))
  16149. (cdr errdata)))))
  16150. (defun org-time-string-to-seconds (s)
  16151. "Convert a timestamp string to a number of seconds."
  16152. (org-float-time (org-time-string-to-time s)))
  16153. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16154. "Convert a time stamp to an absolute day number.
  16155. If there is a specifier for a cyclic time stamp, get the closest
  16156. date to DAYNR.
  16157. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16158. The variable `date' is bound by the calendar when this is called."
  16159. (cond
  16160. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16161. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16162. daynr
  16163. (+ daynr 1000)))
  16164. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16165. (> (string-to-number (match-string 1 s)) 0))
  16166. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16167. (time-to-days (current-time))) (match-string 0 s)
  16168. prefer show-all))
  16169. (t (time-to-days
  16170. (condition-case errdata
  16171. (apply 'encode-time (org-parse-time-string s))
  16172. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16173. s (if (not (and buffer pos))
  16174. ""
  16175. (format-message " at %d in buffer `%s'" pos buffer))
  16176. (cdr errdata))))))))
  16177. (defun org-days-to-iso-week (days)
  16178. "Return the iso week number."
  16179. (require 'cal-iso)
  16180. (car (calendar-iso-from-absolute days)))
  16181. (defun org-small-year-to-year (year)
  16182. "Convert 2-digit years into 4-digit years.
  16183. YEAR is expanded into one of the 30 next years, if possible, or
  16184. into a past one. Any year larger than 99 is returned unchanged."
  16185. (if (>= year 100) year
  16186. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16187. (century (/ current 100))
  16188. (offset (- year (% current 100))))
  16189. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16190. ((> offset -70) (+ (* century 100) year))
  16191. (t (+ (* (1+ century) 100) year))))))
  16192. (defun org-time-from-absolute (d)
  16193. "Return the time corresponding to date D.
  16194. D may be an absolute day number, or a calendar-type list (month day year)."
  16195. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16196. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16197. (defun org-calendar-holiday ()
  16198. "List of holidays, for Diary display in Org-mode."
  16199. (require 'holidays)
  16200. (let ((hl (funcall
  16201. (if (fboundp 'calendar-check-holidays)
  16202. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16203. (if hl (mapconcat 'identity hl "; "))))
  16204. (defun org-diary-sexp-entry (sexp entry date)
  16205. "Process a SEXP diary ENTRY for DATE."
  16206. (require 'diary-lib)
  16207. (let ((result (if calendar-debug-sexp
  16208. (let ((stack-trace-on-error t))
  16209. (eval (car (read-from-string sexp))))
  16210. (condition-case nil
  16211. (eval (car (read-from-string sexp)))
  16212. (error
  16213. (beep)
  16214. (message "Bad sexp at line %d in %s: %s"
  16215. (org-current-line)
  16216. (buffer-file-name) sexp)
  16217. (sleep-for 2))))))
  16218. (cond ((stringp result) (split-string result "; "))
  16219. ((and (consp result)
  16220. (not (consp (cdr result)))
  16221. (stringp (cdr result))) (cdr result))
  16222. ((and (consp result)
  16223. (stringp (car result))) result)
  16224. (result entry))))
  16225. (defun org-diary-to-ical-string (frombuf)
  16226. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16227. This uses the icalendar.el library."
  16228. (let* ((tmpdir (if (featurep 'xemacs)
  16229. (temp-directory)
  16230. temporary-file-directory))
  16231. (tmpfile (make-temp-name
  16232. (expand-file-name "orgics" tmpdir)))
  16233. buf rtn b e)
  16234. (with-current-buffer frombuf
  16235. (icalendar-export-region (point-min) (point-max) tmpfile)
  16236. (setq buf (find-buffer-visiting tmpfile))
  16237. (set-buffer buf)
  16238. (goto-char (point-min))
  16239. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16240. (setq b (match-beginning 0)))
  16241. (goto-char (point-max))
  16242. (if (re-search-backward "^END:VEVENT" nil t)
  16243. (setq e (match-end 0)))
  16244. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16245. (kill-buffer buf)
  16246. (delete-file tmpfile)
  16247. rtn))
  16248. (defun org-closest-date (start current change prefer show-all)
  16249. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16250. When PREFER is `past', return a date that is either CURRENT or past.
  16251. When PREFER is `future', return a date that is either CURRENT or future.
  16252. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16253. ;; Make the proper lists from the dates
  16254. (catch 'exit
  16255. (let ((a1 '(("h" . hour)
  16256. ("d" . day)
  16257. ("w" . week)
  16258. ("m" . month)
  16259. ("y" . year)))
  16260. (shour (nth 2 (org-parse-time-string start)))
  16261. dn dw sday cday n1 n2 n0
  16262. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16263. (setq start (org-date-to-gregorian start)
  16264. current (org-date-to-gregorian
  16265. (if show-all
  16266. current
  16267. (time-to-days (current-time))))
  16268. sday (calendar-absolute-from-gregorian start)
  16269. cday (calendar-absolute-from-gregorian current))
  16270. (if (<= cday sday) (throw 'exit sday))
  16271. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16272. (setq dn (string-to-number (match-string 1 change))
  16273. dw (cdr (assoc (match-string 2 change) a1))))
  16274. (unless (and dn (> dn 0))
  16275. (user-error "Invalid change specifier: %s" change))
  16276. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16277. (cond
  16278. ((eq dw 'hour)
  16279. (let ((missing-hours
  16280. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16281. dn)))
  16282. (setq n1 (if (zerop missing-hours) cday
  16283. (- cday (1+ (floor (/ missing-hours 24)))))
  16284. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16285. ((eq dw 'day)
  16286. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16287. n2 (+ n1 dn)))
  16288. ((eq dw 'year)
  16289. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16290. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16291. (setq date1 (list m d y1)
  16292. n1 (calendar-absolute-from-gregorian date1)
  16293. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16294. n2 (calendar-absolute-from-gregorian date2)))
  16295. ((eq dw 'month)
  16296. ;; approx number of month between the two dates
  16297. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16298. ;; How often does dn fit in there?
  16299. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16300. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16301. m (+ m nm)
  16302. ny (floor (/ m 12))
  16303. y (+ y ny)
  16304. m (- m (* ny 12)))
  16305. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16306. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16307. (setq m2 (+ m dn) y2 y)
  16308. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16309. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16310. (while (<= n2 cday)
  16311. (setq n1 n2 m m2 y y2)
  16312. (setq m2 (+ m dn) y2 y)
  16313. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16314. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16315. ;; Make sure n1 is the earlier date
  16316. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16317. (if show-all
  16318. (cond
  16319. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16320. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16321. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16322. (cond
  16323. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16324. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16325. (t (if (= cday n1) n1 n2)))))))
  16326. (defun org-date-to-gregorian (date)
  16327. "Turn any specification of DATE into a Gregorian date for the calendar."
  16328. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16329. ((and (listp date) (= (length date) 3)) date)
  16330. ((stringp date)
  16331. (setq date (org-parse-time-string date))
  16332. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16333. ((listp date)
  16334. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16335. (defun org-parse-time-string (s &optional nodefault)
  16336. "Parse the standard Org-mode time string.
  16337. This should be a lot faster than the normal `parse-time-string'.
  16338. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16339. hour and minute fields will be nil if not given."
  16340. (cond ((string-match org-ts-regexp0 s)
  16341. (list 0
  16342. (if (or (match-beginning 8) (not nodefault))
  16343. (string-to-number (or (match-string 8 s) "0")))
  16344. (if (or (match-beginning 7) (not nodefault))
  16345. (string-to-number (or (match-string 7 s) "0")))
  16346. (string-to-number (match-string 4 s))
  16347. (string-to-number (match-string 3 s))
  16348. (string-to-number (match-string 2 s))
  16349. nil nil nil))
  16350. ((string-match "^<[^>]+>$" s)
  16351. (decode-time (seconds-to-time (org-matcher-time s))))
  16352. (t (error "Not a standard Org-mode time string: %s" s))))
  16353. (defun org-timestamp-up (&optional arg)
  16354. "Increase the date item at the cursor by one.
  16355. If the cursor is on the year, change the year. If it is on the month,
  16356. the day or the time, change that.
  16357. With prefix ARG, change by that many units."
  16358. (interactive "p")
  16359. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16360. (defun org-timestamp-down (&optional arg)
  16361. "Decrease the date item at the cursor by one.
  16362. If the cursor is on the year, change the year. If it is on the month,
  16363. the day or the time, change that.
  16364. With prefix ARG, change by that many units."
  16365. (interactive "p")
  16366. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16367. (defun org-timestamp-up-day (&optional arg)
  16368. "Increase the date in the time stamp by one day.
  16369. With prefix ARG, change that many days."
  16370. (interactive "p")
  16371. (if (and (not (org-at-timestamp-p t))
  16372. (org-at-heading-p))
  16373. (org-todo 'up)
  16374. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16375. (defun org-timestamp-down-day (&optional arg)
  16376. "Decrease the date in the time stamp by one day.
  16377. With prefix ARG, change that many days."
  16378. (interactive "p")
  16379. (if (and (not (org-at-timestamp-p t))
  16380. (org-at-heading-p))
  16381. (org-todo 'down)
  16382. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16383. (defun org-at-timestamp-p (&optional inactive-ok)
  16384. "Non-nil if point is inside a timestamp.
  16385. When optional argument INACTIVE-OK is non-nil, also consider
  16386. inactive timestamps.
  16387. When this function returns a non-nil value, match data is set
  16388. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16389. INACTIVE-OK."
  16390. (interactive)
  16391. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16392. (pos (point))
  16393. (ans (or (looking-at tsr)
  16394. (save-excursion
  16395. (skip-chars-backward "^[<\n\r\t")
  16396. (if (> (point) (point-min)) (backward-char 1))
  16397. (and (looking-at tsr)
  16398. (> (- (match-end 0) pos) -1))))))
  16399. (and ans
  16400. (boundp 'org-ts-what)
  16401. (setq org-ts-what
  16402. (cond
  16403. ((= pos (match-beginning 0)) 'bracket)
  16404. ;; Point is considered to be "on the bracket" whether
  16405. ;; it's really on it or right after it.
  16406. ((= pos (1- (match-end 0))) 'bracket)
  16407. ((= pos (match-end 0)) 'after)
  16408. ((org-pos-in-match-range pos 2) 'year)
  16409. ((org-pos-in-match-range pos 3) 'month)
  16410. ((org-pos-in-match-range pos 7) 'hour)
  16411. ((org-pos-in-match-range pos 8) 'minute)
  16412. ((or (org-pos-in-match-range pos 4)
  16413. (org-pos-in-match-range pos 5)) 'day)
  16414. ((and (> pos (or (match-end 8) (match-end 5)))
  16415. (< pos (match-end 0)))
  16416. (- pos (or (match-end 8) (match-end 5))))
  16417. (t 'day))))
  16418. ans))
  16419. (defun org-toggle-timestamp-type ()
  16420. "Toggle the type (<active> or [inactive]) of a time stamp."
  16421. (interactive)
  16422. (when (org-at-timestamp-p t)
  16423. (let ((beg (match-beginning 0)) (end (match-end 0))
  16424. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16425. (save-excursion
  16426. (goto-char beg)
  16427. (while (re-search-forward "[][<>]" end t)
  16428. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16429. t t)))
  16430. (message "Timestamp is now %sactive"
  16431. (if (equal (char-after beg) ?<) "" "in")))))
  16432. (defun org-at-clock-log-p nil
  16433. "Is the cursor on the clock log line?"
  16434. (save-excursion
  16435. (move-beginning-of-line 1)
  16436. (looking-at org-clock-line-re)))
  16437. (defvar org-clock-history) ; defined in org-clock.el
  16438. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16439. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16440. "Change the date in the time stamp at point.
  16441. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16442. `year', `minute', `second'. If WHAT is not given, the cursor position
  16443. in the timestamp determines what will be changed.
  16444. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16445. (let ((origin (point)) origin-cat
  16446. with-hm inactive
  16447. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16448. org-ts-what
  16449. extra rem
  16450. ts time time0 fixnext clrgx)
  16451. (if (not (org-at-timestamp-p t))
  16452. (user-error "Not at a timestamp"))
  16453. (if (and (not what) (eq org-ts-what 'bracket))
  16454. (org-toggle-timestamp-type)
  16455. ;; Point isn't on brackets. Remember the part of the time-stamp
  16456. ;; the point was in. Indeed, size of time-stamps may change,
  16457. ;; but point must be kept in the same category nonetheless.
  16458. (setq origin-cat org-ts-what)
  16459. (if (and (not what) (not (eq org-ts-what 'day))
  16460. org-display-custom-times
  16461. (get-text-property (point) 'display)
  16462. (not (get-text-property (1- (point)) 'display)))
  16463. (setq org-ts-what 'day))
  16464. (setq org-ts-what (or what org-ts-what)
  16465. inactive (= (char-after (match-beginning 0)) ?\[)
  16466. ts (match-string 0))
  16467. (replace-match "")
  16468. (when (string-match
  16469. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16470. ts)
  16471. (setq extra (match-string 1 ts))
  16472. (if suppress-tmp-delay
  16473. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16474. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16475. (setq with-hm t))
  16476. (setq time0 (org-parse-time-string ts))
  16477. (when (and updown
  16478. (eq org-ts-what 'minute)
  16479. (not current-prefix-arg))
  16480. ;; This looks like s-up and s-down. Change by one rounding step.
  16481. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16482. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16483. (setcar (cdr time0) (+ (nth 1 time0)
  16484. (if (> n 0) (- rem) (- dm rem))))))
  16485. (setq time
  16486. (encode-time (or (car time0) 0)
  16487. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16488. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16489. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16490. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16491. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16492. (nthcdr 6 time0)))
  16493. (when (and (member org-ts-what '(hour minute))
  16494. extra
  16495. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16496. (setq extra (org-modify-ts-extra
  16497. extra
  16498. (if (eq org-ts-what 'hour) 2 5)
  16499. n dm)))
  16500. (when (integerp org-ts-what)
  16501. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16502. (if (eq what 'calendar)
  16503. (let ((cal-date (org-get-date-from-calendar)))
  16504. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16505. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16506. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16507. (setcar time0 (or (car time0) 0))
  16508. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16509. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16510. (setq time (apply 'encode-time time0))))
  16511. ;; Insert the new time-stamp, and ensure point stays in the same
  16512. ;; category as before (i.e. not after the last position in that
  16513. ;; category).
  16514. (let ((pos (point)))
  16515. ;; Stay before inserted string. `save-excursion' is of no use.
  16516. (setq org-last-changed-timestamp
  16517. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16518. (goto-char pos))
  16519. (save-match-data
  16520. (looking-at org-ts-regexp3)
  16521. (goto-char (cond
  16522. ;; `day' category ends before `hour' if any, or at
  16523. ;; the end of the day name.
  16524. ((eq origin-cat 'day)
  16525. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16526. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16527. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16528. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16529. ;; `year' and `month' have both fixed size: point
  16530. ;; couldn't have moved into another part.
  16531. (t origin))))
  16532. ;; Update clock if on a CLOCK line.
  16533. (org-clock-update-time-maybe)
  16534. ;; Maybe adjust the closest clock in `org-clock-history'
  16535. (when org-clock-adjust-closest
  16536. (if (not (and (org-at-clock-log-p)
  16537. (< 1 (length (delq nil (mapcar 'marker-position
  16538. org-clock-history))))))
  16539. (message "No clock to adjust")
  16540. (cond ((save-excursion ; fix previous clock?
  16541. (re-search-backward org-ts-regexp0 nil t)
  16542. (org-looking-back (concat org-clock-string " \\[")))
  16543. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16544. ((save-excursion ; fix next clock?
  16545. (re-search-backward org-ts-regexp0 nil t)
  16546. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16547. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16548. (save-window-excursion
  16549. ;; Find closest clock to point, adjust the previous/next one in history
  16550. (let* ((p (save-excursion (org-back-to-heading t)))
  16551. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16552. (clfixnth
  16553. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16554. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16555. (if (not clfixpos)
  16556. (message "No clock to adjust")
  16557. (save-excursion
  16558. (org-goto-marker-or-bmk clfixpos)
  16559. (org-show-subtree)
  16560. (when (re-search-forward clrgx nil t)
  16561. (goto-char (match-beginning 1))
  16562. (let (org-clock-adjust-closest)
  16563. (org-timestamp-change n org-ts-what updown))
  16564. (message "Clock adjusted in %s for heading: %s"
  16565. (file-name-nondirectory (buffer-file-name))
  16566. (org-get-heading t t)))))))))
  16567. ;; Try to recenter the calendar window, if any.
  16568. (if (and org-calendar-follow-timestamp-change
  16569. (get-buffer-window "*Calendar*" t)
  16570. (memq org-ts-what '(day month year)))
  16571. (org-recenter-calendar (time-to-days time))))))
  16572. (defun org-modify-ts-extra (s pos n dm)
  16573. "Change the different parts of the lead-time and repeat fields in timestamp."
  16574. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16575. ng h m new rem)
  16576. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16577. (cond
  16578. ((or (org-pos-in-match-range pos 2)
  16579. (org-pos-in-match-range pos 3))
  16580. (setq m (string-to-number (match-string 3 s))
  16581. h (string-to-number (match-string 2 s)))
  16582. (if (org-pos-in-match-range pos 2)
  16583. (setq h (+ h n))
  16584. (setq n (* dm (org-no-warnings (signum n))))
  16585. (when (not (= 0 (setq rem (% m dm))))
  16586. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16587. (setq m (+ m n)))
  16588. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16589. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16590. (setq h (mod h 24))
  16591. (setq ng 1 new (format "-%02d:%02d" h m)))
  16592. ((org-pos-in-match-range pos 6)
  16593. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16594. ((org-pos-in-match-range pos 5)
  16595. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16596. ((org-pos-in-match-range pos 9)
  16597. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16598. ((org-pos-in-match-range pos 8)
  16599. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16600. (when ng
  16601. (setq s (concat
  16602. (substring s 0 (match-beginning ng))
  16603. new
  16604. (substring s (match-end ng))))))
  16605. s))
  16606. (defun org-recenter-calendar (date)
  16607. "If the calendar is visible, recenter it to DATE."
  16608. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16609. (when cwin
  16610. (let ((calendar-move-hook nil))
  16611. (with-selected-window cwin
  16612. (calendar-goto-date (if (listp date) date
  16613. (calendar-gregorian-from-absolute date))))))))
  16614. (defun org-goto-calendar (&optional arg)
  16615. "Go to the Emacs calendar at the current date.
  16616. If there is a time stamp in the current line, go to that date.
  16617. A prefix ARG can be used to force the current date."
  16618. (interactive "P")
  16619. (let ((tsr org-ts-regexp) diff
  16620. (calendar-move-hook nil)
  16621. (calendar-view-holidays-initially-flag nil)
  16622. (calendar-view-diary-initially-flag nil))
  16623. (if (or (org-at-timestamp-p)
  16624. (save-excursion
  16625. (beginning-of-line 1)
  16626. (looking-at (concat ".*" tsr))))
  16627. (let ((d1 (time-to-days (current-time)))
  16628. (d2 (time-to-days
  16629. (org-time-string-to-time (match-string 1)))))
  16630. (setq diff (- d2 d1))))
  16631. (calendar)
  16632. (calendar-goto-today)
  16633. (if (and diff (not arg)) (calendar-forward-day diff))))
  16634. (defun org-get-date-from-calendar ()
  16635. "Return a list (month day year) of date at point in calendar."
  16636. (with-current-buffer "*Calendar*"
  16637. (save-match-data
  16638. (calendar-cursor-to-date))))
  16639. (defun org-date-from-calendar ()
  16640. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16641. If there is already a time stamp at the cursor position, update it."
  16642. (interactive)
  16643. (if (org-at-timestamp-p t)
  16644. (org-timestamp-change 0 'calendar)
  16645. (let ((cal-date (org-get-date-from-calendar)))
  16646. (org-insert-time-stamp
  16647. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16648. (defcustom org-effort-durations
  16649. `(("min" . 1)
  16650. ("h" . 60)
  16651. ("d" . ,(* 60 8))
  16652. ("w" . ,(* 60 8 5))
  16653. ("m" . ,(* 60 8 5 4))
  16654. ("y" . ,(* 60 8 5 40)))
  16655. "Conversion factor to minutes for an effort modifier.
  16656. Each entry has the form (MODIFIER . MINUTES).
  16657. In an effort string, a number followed by MODIFIER is multiplied
  16658. by the specified number of MINUTES to obtain an effort in
  16659. minutes.
  16660. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16661. effort string \"2hours\" is equivalent to 120 minutes."
  16662. :group 'org-agenda
  16663. :version "25.1"
  16664. :package-version '(Org . "8.3")
  16665. :type '(alist :key-type (string :tag "Modifier")
  16666. :value-type (number :tag "Minutes")))
  16667. (defun org-minutes-to-clocksum-string (m)
  16668. "Format number of minutes as a clocksum string.
  16669. The format is determined by `org-time-clocksum-format',
  16670. `org-time-clocksum-use-fractional' and
  16671. `org-time-clocksum-fractional-format' and
  16672. `org-time-clocksum-use-effort-durations'."
  16673. (let ((clocksum "")
  16674. (m (round m)) ; Don't allow fractions of minutes
  16675. h d w mo y fmt n)
  16676. (setq h (if org-time-clocksum-use-effort-durations
  16677. (cdr (assoc "h" org-effort-durations)) 60)
  16678. d (if org-time-clocksum-use-effort-durations
  16679. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16680. w (if org-time-clocksum-use-effort-durations
  16681. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16682. mo (if org-time-clocksum-use-effort-durations
  16683. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16684. y (if org-time-clocksum-use-effort-durations
  16685. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16686. ;; fractional format
  16687. (if org-time-clocksum-use-fractional
  16688. (cond
  16689. ;; single format string
  16690. ((stringp org-time-clocksum-fractional-format)
  16691. (format org-time-clocksum-fractional-format (/ m (float h))))
  16692. ;; choice of fractional formats for different time units
  16693. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16694. (> (/ (truncate m) (* y d h)) 0))
  16695. (format fmt (/ m (* y d (float h)))))
  16696. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16697. (> (/ (truncate m) (* mo d h)) 0))
  16698. (format fmt (/ m (* mo d (float h)))))
  16699. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16700. (> (/ (truncate m) (* w d h)) 0))
  16701. (format fmt (/ m (* w d (float h)))))
  16702. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16703. (> (/ (truncate m) (* d h)) 0))
  16704. (format fmt (/ m (* d (float h)))))
  16705. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16706. (> (/ (truncate m) h) 0))
  16707. (format fmt (/ m (float h))))
  16708. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16709. (format fmt m))
  16710. ;; fall back to smallest time unit with a format
  16711. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16712. (format fmt (/ m (float h))))
  16713. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16714. (format fmt (/ m (* d (float h)))))
  16715. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16716. (format fmt (/ m (* w d (float h)))))
  16717. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16718. (format fmt (/ m (* mo d (float h)))))
  16719. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16720. (format fmt (/ m (* y d (float h))))))
  16721. ;; standard (non-fractional) format, with single format string
  16722. (if (stringp org-time-clocksum-format)
  16723. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16724. ;; separate formats components
  16725. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16726. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16727. (plist-get org-time-clocksum-format :require-years))
  16728. (setq clocksum (concat clocksum (format fmt n))
  16729. m (- m (* n y d h))))
  16730. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16731. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16732. (plist-get org-time-clocksum-format :require-months))
  16733. (setq clocksum (concat clocksum (format fmt n))
  16734. m (- m (* n mo d h))))
  16735. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16736. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16737. (plist-get org-time-clocksum-format :require-weeks))
  16738. (setq clocksum (concat clocksum (format fmt n))
  16739. m (- m (* n w d h))))
  16740. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16741. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16742. (plist-get org-time-clocksum-format :require-days))
  16743. (setq clocksum (concat clocksum (format fmt n))
  16744. m (- m (* n d h))))
  16745. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16746. (or (> (setq n (/ (truncate m) h)) 0)
  16747. (plist-get org-time-clocksum-format :require-hours))
  16748. (setq clocksum (concat clocksum (format fmt n))
  16749. m (- m (* n h))))
  16750. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16751. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16752. (setq clocksum (concat clocksum (format fmt m))))
  16753. ;; return formatted time duration
  16754. clocksum))))
  16755. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16756. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16757. "Org mode version 8.0")
  16758. (defun org-hours-to-clocksum-string (n)
  16759. (org-minutes-to-clocksum-string (* n 60)))
  16760. (defun org-hh:mm-string-to-minutes (s)
  16761. "Convert a string H:MM to a number of minutes.
  16762. If the string is just a number, interpret it as minutes.
  16763. In fact, the first hh:mm or number in the string will be taken,
  16764. there can be extra stuff in the string.
  16765. If no number is found, the return value is 0."
  16766. (cond
  16767. ((integerp s) s)
  16768. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16769. (+ (* (string-to-number (match-string 1 s)) 60)
  16770. (string-to-number (match-string 2 s))))
  16771. ((string-match "\\([0-9]+\\)" s)
  16772. (string-to-number (match-string 1 s)))
  16773. (t 0)))
  16774. (defcustom org-image-actual-width t
  16775. "Should we use the actual width of images when inlining them?
  16776. When set to t, always use the image width.
  16777. When set to a number, use imagemagick (when available) to set
  16778. the image's width to this value.
  16779. When set to a number in a list, try to get the width from any
  16780. #+ATTR.* keyword if it matches a width specification like
  16781. #+ATTR_HTML: :width 300px
  16782. and fall back on that number if none is found.
  16783. When set to nil, try to get the width from an #+ATTR.* keyword
  16784. and fall back on the original width if none is found.
  16785. This requires Emacs >= 24.1, build with imagemagick support."
  16786. :group 'org-appearance
  16787. :version "24.4"
  16788. :package-version '(Org . "8.0")
  16789. :type '(choice
  16790. (const :tag "Use the image width" t)
  16791. (integer :tag "Use a number of pixels")
  16792. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16793. (const :tag "Use #+ATTR* or don't resize" nil)))
  16794. (defcustom org-agenda-inhibit-startup nil
  16795. "Inhibit startup when preparing agenda buffers.
  16796. When this variable is t, the initialization of the Org agenda
  16797. buffers is inhibited: e.g. the visibility state is not set, the
  16798. tables are not re-aligned, etc."
  16799. :type 'boolean
  16800. :version "24.3"
  16801. :group 'org-agenda)
  16802. (define-obsolete-variable-alias
  16803. 'org-agenda-ignore-drawer-properties
  16804. 'org-agenda-ignore-properties "25.1")
  16805. (defcustom org-agenda-ignore-properties nil
  16806. "Avoid updating text properties when building the agenda.
  16807. Properties are used to prepare buffers for effort estimates,
  16808. appointments, statistics and subtree-local categories.
  16809. If you don't use these in the agenda, you can add them to this
  16810. list and agenda building will be a bit faster.
  16811. The value is a list, with zero or more of the symbols `effort', `appt',
  16812. `stats' or `category'."
  16813. :type '(set :greedy t
  16814. (const effort)
  16815. (const appt)
  16816. (const stats)
  16817. (const category))
  16818. :version "25.1"
  16819. :package-version '(Org . "8.3")
  16820. :group 'org-agenda)
  16821. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16822. "Convert a duration string S to minutes.
  16823. A bare number is interpreted as minutes, modifiers can be set by
  16824. customizing `org-effort-durations' (which see).
  16825. Entries containing a colon are interpreted as H:MM by
  16826. `org-hh:mm-string-to-minutes'."
  16827. (let ((result 0)
  16828. (re (concat "\\([0-9.]+\\) *\\("
  16829. (regexp-opt (mapcar 'car org-effort-durations))
  16830. "\\)")))
  16831. (while (string-match re s)
  16832. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16833. (string-to-number (match-string 1 s))))
  16834. (setq s (replace-match "" nil t s)))
  16835. (setq result (floor result))
  16836. (incf result (org-hh:mm-string-to-minutes s))
  16837. (if output-to-string (number-to-string result) result)))
  16838. ;;;; Files
  16839. (defun org-save-all-org-buffers ()
  16840. "Save all Org-mode buffers without user confirmation."
  16841. (interactive)
  16842. (message "Saving all Org-mode buffers...")
  16843. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16844. (when (featurep 'org-id) (org-id-locations-save))
  16845. (message "Saving all Org-mode buffers... done"))
  16846. (defun org-revert-all-org-buffers ()
  16847. "Revert all Org-mode buffers.
  16848. Prompt for confirmation when there are unsaved changes.
  16849. Be sure you know what you are doing before letting this function
  16850. overwrite your changes.
  16851. This function is useful in a setup where one tracks org files
  16852. with a version control system, to revert on one machine after pulling
  16853. changes from another. I believe the procedure must be like this:
  16854. 1. M-x org-save-all-org-buffers
  16855. 2. Pull changes from the other machine, resolve conflicts
  16856. 3. M-x org-revert-all-org-buffers"
  16857. (interactive)
  16858. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16859. (user-error "Abort"))
  16860. (save-excursion
  16861. (save-window-excursion
  16862. (mapc
  16863. (lambda (b)
  16864. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16865. (with-current-buffer b buffer-file-name))
  16866. (org-pop-to-buffer-same-window b)
  16867. (revert-buffer t 'no-confirm)))
  16868. (buffer-list))
  16869. (when (and (featurep 'org-id) org-id-track-globally)
  16870. (org-id-locations-load)))))
  16871. ;;;; Agenda files
  16872. ;;;###autoload
  16873. (defun org-switchb (&optional arg)
  16874. "Switch between Org buffers.
  16875. With one prefix argument, restrict available buffers to files.
  16876. With two prefix arguments, restrict available buffers to agenda files.
  16877. Defaults to `iswitchb' for buffer name completion.
  16878. Set `org-completion-use-ido' to make it use ido instead."
  16879. (interactive "P")
  16880. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16881. ((equal arg '(16)) (org-buffer-list 'agenda))
  16882. (t (org-buffer-list))))
  16883. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16884. (org-completion-use-ido org-completion-use-ido))
  16885. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16886. (setq org-completion-use-iswitchb t))
  16887. (org-pop-to-buffer-same-window
  16888. (org-icompleting-read "Org buffer: "
  16889. (mapcar 'list (mapcar 'buffer-name blist))
  16890. nil t))))
  16891. ;;; Define some older names previously used for this functionality
  16892. ;;;###autoload
  16893. (defalias 'org-ido-switchb 'org-switchb)
  16894. ;;;###autoload
  16895. (defalias 'org-iswitchb 'org-switchb)
  16896. (defun org-buffer-list (&optional predicate exclude-tmp)
  16897. "Return a list of Org buffers.
  16898. PREDICATE can be `export', `files' or `agenda'.
  16899. export restrict the list to Export buffers.
  16900. files restrict the list to buffers visiting Org files.
  16901. agenda restrict the list to buffers visiting agenda files.
  16902. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16903. (let* ((bfn nil)
  16904. (agenda-files (and (eq predicate 'agenda)
  16905. (mapcar 'file-truename (org-agenda-files t))))
  16906. (filter
  16907. (cond
  16908. ((eq predicate 'files)
  16909. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16910. ((eq predicate 'export)
  16911. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16912. ((eq predicate 'agenda)
  16913. (lambda (b)
  16914. (with-current-buffer b
  16915. (and (derived-mode-p 'org-mode)
  16916. (setq bfn (buffer-file-name b))
  16917. (member (file-truename bfn) agenda-files)))))
  16918. (t (lambda (b) (with-current-buffer b
  16919. (or (derived-mode-p 'org-mode)
  16920. (string-match "\\*Org .*Export"
  16921. (buffer-name b)))))))))
  16922. (delq nil
  16923. (mapcar
  16924. (lambda(b)
  16925. (if (and (funcall filter b)
  16926. (or (not exclude-tmp)
  16927. (not (string-match "tmp" (buffer-name b)))))
  16928. b
  16929. nil))
  16930. (buffer-list)))))
  16931. (defun org-agenda-files (&optional unrestricted archives)
  16932. "Get the list of agenda files.
  16933. Optional UNRESTRICTED means return the full list even if a restriction
  16934. is currently in place.
  16935. When ARCHIVES is t, include all archive files that are really being
  16936. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16937. `org-agenda-archives-mode' is t."
  16938. (let ((files
  16939. (cond
  16940. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16941. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16942. ((listp org-agenda-files) org-agenda-files)
  16943. (t (error "Invalid value of `org-agenda-files'")))))
  16944. (setq files (apply 'append
  16945. (mapcar (lambda (f)
  16946. (if (file-directory-p f)
  16947. (directory-files
  16948. f t org-agenda-file-regexp)
  16949. (list f)))
  16950. files)))
  16951. (when org-agenda-skip-unavailable-files
  16952. (setq files (delq nil
  16953. (mapcar (function
  16954. (lambda (file)
  16955. (and (file-readable-p file) file)))
  16956. files))))
  16957. (when (or (eq archives t)
  16958. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16959. (setq files (org-add-archive-files files)))
  16960. files))
  16961. (defun org-agenda-file-p (&optional file)
  16962. "Return non-nil, if FILE is an agenda file.
  16963. If FILE is omitted, use the file associated with the current
  16964. buffer."
  16965. (let ((fname (or file (buffer-file-name))))
  16966. (and fname
  16967. (member (file-truename fname)
  16968. (mapcar #'file-truename (org-agenda-files t))))))
  16969. (defun org-edit-agenda-file-list ()
  16970. "Edit the list of agenda files.
  16971. Depending on setup, this either uses customize to edit the variable
  16972. `org-agenda-files', or it visits the file that is holding the list. In the
  16973. latter case, the buffer is set up in a way that saving it automatically kills
  16974. the buffer and restores the previous window configuration."
  16975. (interactive)
  16976. (if (stringp org-agenda-files)
  16977. (let ((cw (current-window-configuration)))
  16978. (find-file org-agenda-files)
  16979. (org-set-local 'org-window-configuration cw)
  16980. (org-add-hook 'after-save-hook
  16981. (lambda ()
  16982. (set-window-configuration
  16983. (prog1 org-window-configuration
  16984. (kill-buffer (current-buffer))))
  16985. (org-install-agenda-files-menu)
  16986. (message "New agenda file list installed"))
  16987. nil 'local)
  16988. (message "%s" (substitute-command-keys
  16989. "Edit list and finish with \\[save-buffer]")))
  16990. (customize-variable 'org-agenda-files)))
  16991. (defun org-store-new-agenda-file-list (list)
  16992. "Set new value for the agenda file list and save it correctly."
  16993. (if (stringp org-agenda-files)
  16994. (let ((fe (org-read-agenda-file-list t)) b u)
  16995. (while (setq b (find-buffer-visiting org-agenda-files))
  16996. (kill-buffer b))
  16997. (with-temp-file org-agenda-files
  16998. (insert
  16999. (mapconcat
  17000. (lambda (f) ;; Keep un-expanded entries.
  17001. (if (setq u (assoc f fe))
  17002. (cdr u)
  17003. f))
  17004. list "\n")
  17005. "\n")))
  17006. (let ((org-mode-hook nil) (org-inhibit-startup t)
  17007. (org-insert-mode-line-in-empty-file nil))
  17008. (setq org-agenda-files list)
  17009. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17010. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  17011. "Read the list of agenda files from a file.
  17012. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  17013. filenames, used by `org-store-new-agenda-file-list' to write back
  17014. un-expanded file names."
  17015. (when (file-directory-p org-agenda-files)
  17016. (error "`org-agenda-files' cannot be a single directory"))
  17017. (when (stringp org-agenda-files)
  17018. (with-temp-buffer
  17019. (insert-file-contents org-agenda-files)
  17020. (mapcar
  17021. (lambda (f)
  17022. (let ((e (expand-file-name (substitute-in-file-name f)
  17023. org-directory)))
  17024. (if pair-with-expansion
  17025. (cons e f)
  17026. e)))
  17027. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17028. ;;;###autoload
  17029. (defun org-cycle-agenda-files ()
  17030. "Cycle through the files in `org-agenda-files'.
  17031. If the current buffer visits an agenda file, find the next one in the list.
  17032. If the current buffer does not, find the first agenda file."
  17033. (interactive)
  17034. (let* ((fs (or (org-agenda-files t)
  17035. (user-error "No agenda files")))
  17036. (files (copy-sequence fs))
  17037. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17038. file)
  17039. (when tcf
  17040. (while (and (setq file (pop files))
  17041. (not (equal (file-truename file) tcf)))))
  17042. (find-file (car (or files fs)))
  17043. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17044. (defun org-agenda-file-to-front (&optional to-end)
  17045. "Move/add the current file to the top of the agenda file list.
  17046. If the file is not present in the list, it is added to the front. If it is
  17047. present, it is moved there. With optional argument TO-END, add/move to the
  17048. end of the list."
  17049. (interactive "P")
  17050. (let ((org-agenda-skip-unavailable-files nil)
  17051. (file-alist (mapcar (lambda (x)
  17052. (cons (file-truename x) x))
  17053. (org-agenda-files t)))
  17054. (ctf (file-truename
  17055. (or buffer-file-name
  17056. (user-error "Please save the current buffer to a file"))))
  17057. x had)
  17058. (setq x (assoc ctf file-alist) had x)
  17059. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17060. (if to-end
  17061. (setq file-alist (append (delq x file-alist) (list x)))
  17062. (setq file-alist (cons x (delq x file-alist))))
  17063. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17064. (org-install-agenda-files-menu)
  17065. (message "File %s to %s of agenda file list"
  17066. (if had "moved" "added") (if to-end "end" "front"))))
  17067. (defun org-remove-file (&optional file)
  17068. "Remove current file from the list of files in variable `org-agenda-files'.
  17069. These are the files which are being checked for agenda entries.
  17070. Optional argument FILE means use this file instead of the current."
  17071. (interactive)
  17072. (let* ((org-agenda-skip-unavailable-files nil)
  17073. (file (or file buffer-file-name
  17074. (user-error "Current buffer does not visit a file")))
  17075. (true-file (file-truename file))
  17076. (afile (abbreviate-file-name file))
  17077. (files (delq nil (mapcar
  17078. (lambda (x)
  17079. (if (equal true-file
  17080. (file-truename x))
  17081. nil x))
  17082. (org-agenda-files t)))))
  17083. (if (not (= (length files) (length (org-agenda-files t))))
  17084. (progn
  17085. (org-store-new-agenda-file-list files)
  17086. (org-install-agenda-files-menu)
  17087. (message "Removed from Org Agenda list: %s" afile))
  17088. (message "File was not in list: %s (not removed)" afile))))
  17089. (defun org-file-menu-entry (file)
  17090. (vector file (list 'find-file file) t))
  17091. (defun org-check-agenda-file (file)
  17092. "Make sure FILE exists. If not, ask user what to do."
  17093. (when (not (file-exists-p file))
  17094. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17095. (abbreviate-file-name file))
  17096. (let ((r (downcase (read-char-exclusive))))
  17097. (cond
  17098. ((equal r ?r)
  17099. (org-remove-file file)
  17100. (throw 'nextfile t))
  17101. (t (user-error "Abort"))))))
  17102. (defun org-get-agenda-file-buffer (file)
  17103. "Get an agenda buffer visiting FILE.
  17104. If the buffer needs to be created, add it to the list of buffers
  17105. which might be released later."
  17106. (let ((buf (org-find-base-buffer-visiting file)))
  17107. (if buf
  17108. buf ; just return it
  17109. ;; Make a new buffer and remember it
  17110. (setq buf (find-file-noselect file))
  17111. (if buf (push buf org-agenda-new-buffers))
  17112. buf)))
  17113. (defun org-release-buffers (blist)
  17114. "Release all buffers in list, asking the user for confirmation when needed.
  17115. When a buffer is unmodified, it is just killed. When modified, it is saved
  17116. \(if the user agrees) and then killed."
  17117. (let (file)
  17118. (dolist (buf blist)
  17119. (setq file (buffer-file-name buf))
  17120. (when (and (buffer-modified-p buf)
  17121. file
  17122. (y-or-n-p (format "Save file %s? " file)))
  17123. (with-current-buffer buf (save-buffer)))
  17124. (kill-buffer buf))))
  17125. (defun org-agenda-prepare-buffers (files)
  17126. "Create buffers for all agenda files, protect archived trees and comments."
  17127. (interactive)
  17128. (let ((pa '(:org-archived t))
  17129. (pc '(:org-comment t))
  17130. (pall '(:org-archived t :org-comment t))
  17131. (inhibit-read-only t)
  17132. (org-inhibit-startup org-agenda-inhibit-startup)
  17133. (rea (concat ":" org-archive-tag ":"))
  17134. file re pos)
  17135. (setq org-tag-alist-for-agenda nil
  17136. org-tag-groups-alist-for-agenda nil)
  17137. (save-excursion
  17138. (save-restriction
  17139. (dolist (file files)
  17140. (catch 'nextfile
  17141. (if (bufferp file)
  17142. (set-buffer file)
  17143. (org-check-agenda-file file)
  17144. (set-buffer (org-get-agenda-file-buffer file)))
  17145. (widen)
  17146. (org-set-regexps-and-options 'tags-only)
  17147. (setq pos (point))
  17148. (or (memq 'category org-agenda-ignore-properties)
  17149. (org-refresh-category-properties))
  17150. (or (memq 'stats org-agenda-ignore-properties)
  17151. (org-refresh-stats-properties))
  17152. (or (memq 'effort org-agenda-ignore-properties)
  17153. (org-refresh-effort-properties))
  17154. (or (memq 'appt org-agenda-ignore-properties)
  17155. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17156. (setq org-todo-keywords-for-agenda
  17157. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17158. (setq org-done-keywords-for-agenda
  17159. (append org-done-keywords-for-agenda org-done-keywords))
  17160. (setq org-todo-keyword-alist-for-agenda
  17161. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17162. (setq org-tag-alist-for-agenda
  17163. (org-uniquify
  17164. (append org-tag-alist-for-agenda
  17165. org-tag-alist
  17166. org-tag-persistent-alist)))
  17167. (if org-group-tags
  17168. (setq org-tag-groups-alist-for-agenda
  17169. (org-uniquify-alist
  17170. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17171. (org-with-silent-modifications
  17172. (save-excursion
  17173. (remove-text-properties (point-min) (point-max) pall)
  17174. (when org-agenda-skip-archived-trees
  17175. (goto-char (point-min))
  17176. (while (re-search-forward rea nil t)
  17177. (if (org-at-heading-p t)
  17178. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17179. (goto-char (point-min))
  17180. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17181. (while (re-search-forward re nil t)
  17182. (when (save-match-data (org-in-commented-heading-p t))
  17183. (add-text-properties
  17184. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17185. (goto-char pos)))))
  17186. (setq org-todo-keywords-for-agenda
  17187. (org-uniquify org-todo-keywords-for-agenda))
  17188. (setq org-todo-keyword-alist-for-agenda
  17189. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17190. ;;;; CDLaTeX minor mode
  17191. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17192. "Keymap for the minor `org-cdlatex-mode'.")
  17193. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17194. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17195. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17196. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17197. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17198. (defvar org-cdlatex-texmathp-advice-is-done nil
  17199. "Flag remembering if we have applied the advice to texmathp already.")
  17200. (define-minor-mode org-cdlatex-mode
  17201. "Toggle the minor `org-cdlatex-mode'.
  17202. This mode supports entering LaTeX environment and math in LaTeX fragments
  17203. in Org-mode.
  17204. \\{org-cdlatex-mode-map}"
  17205. nil " OCDL" nil
  17206. (when org-cdlatex-mode
  17207. (require 'cdlatex)
  17208. (run-hooks 'cdlatex-mode-hook)
  17209. (cdlatex-compute-tables))
  17210. (unless org-cdlatex-texmathp-advice-is-done
  17211. (setq org-cdlatex-texmathp-advice-is-done t)
  17212. (defadvice texmathp (around org-math-always-on activate)
  17213. "Always return t in org-mode buffers.
  17214. This is because we want to insert math symbols without dollars even outside
  17215. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17216. an embedded LaTeX fragment, let texmathp do its job.
  17217. \\[org-cdlatex-mode-map]"
  17218. (interactive)
  17219. (let (p)
  17220. (cond
  17221. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17222. ((eq this-command 'cdlatex-math-symbol)
  17223. (setq ad-return-value t
  17224. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17225. (t
  17226. (let ((p (org-inside-LaTeX-fragment-p)))
  17227. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17228. (setq ad-return-value t
  17229. texmathp-why '("Org-mode embedded math" . 0))
  17230. (if p ad-do-it)))))))))
  17231. (defun turn-on-org-cdlatex ()
  17232. "Unconditionally turn on `org-cdlatex-mode'."
  17233. (org-cdlatex-mode 1))
  17234. (defun org-try-cdlatex-tab ()
  17235. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17236. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17237. - inside a LaTeX fragment, or
  17238. - after the first word in a line, where an abbreviation expansion could
  17239. insert a LaTeX environment."
  17240. (when org-cdlatex-mode
  17241. (cond
  17242. ;; Before any word on the line: No expansion possible.
  17243. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17244. ;; Just after first word on the line: Expand it. Make sure it
  17245. ;; cannot happen on headlines, though.
  17246. ((save-excursion
  17247. (skip-chars-backward "a-zA-Z0-9*")
  17248. (skip-chars-backward " \t")
  17249. (and (bolp) (not (org-at-heading-p))))
  17250. (cdlatex-tab) t)
  17251. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17252. (defun org-cdlatex-underscore-caret (&optional arg)
  17253. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17254. Revert to the normal definition outside of these fragments."
  17255. (interactive "P")
  17256. (if (org-inside-LaTeX-fragment-p)
  17257. (call-interactively 'cdlatex-sub-superscript)
  17258. (let (org-cdlatex-mode)
  17259. (call-interactively (key-binding (vector last-input-event))))))
  17260. (defun org-cdlatex-math-modify (&optional arg)
  17261. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17262. Revert to the normal definition outside of these fragments."
  17263. (interactive "P")
  17264. (if (org-inside-LaTeX-fragment-p)
  17265. (call-interactively 'cdlatex-math-modify)
  17266. (let (org-cdlatex-mode)
  17267. (call-interactively (key-binding (vector last-input-event))))))
  17268. (defun org-cdlatex-environment-indent (&optional environment item)
  17269. "Execute `cdlatex-environment' and indent the inserted environment.
  17270. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17271. The inserted environment is indented to current indentation
  17272. unless point is at the beginning of the line, in which the
  17273. environment remains unintended."
  17274. (interactive)
  17275. ;; cdlatex-environment always return nil. Therefore, capture output
  17276. ;; first and determine if an environment was selected.
  17277. (let* ((beg (point-marker))
  17278. (end (copy-marker (point) t))
  17279. (inserted (progn
  17280. (ignore-errors (cdlatex-environment environment item))
  17281. (< beg end)))
  17282. ;; Figure out how many lines to move forward after the
  17283. ;; environment has been inserted.
  17284. (lines (when inserted
  17285. (save-excursion
  17286. (- (loop while (< beg (point))
  17287. with x = 0
  17288. do (forward-line -1)
  17289. (incf x)
  17290. finally return x)
  17291. (if (progn (goto-char beg)
  17292. (and (progn (skip-chars-forward " \t") (eolp))
  17293. (progn (skip-chars-backward " \t") (bolp))))
  17294. 1 0)))))
  17295. (env (org-trim (delete-and-extract-region beg end))))
  17296. (when inserted
  17297. ;; Get indentation of next line unless at column 0.
  17298. (let ((ind (if (bolp) 0
  17299. (save-excursion
  17300. (org-return-indent)
  17301. (prog1 (org-get-indentation)
  17302. (when (progn (skip-chars-forward " \t") (eolp))
  17303. (delete-region beg (point)))))))
  17304. (bol (progn (skip-chars-backward " \t") (bolp))))
  17305. ;; Insert a newline before environment unless at column zero
  17306. ;; to "escape" the current line. Insert a newline if
  17307. ;; something is one the same line as \end{ENVIRONMENT}.
  17308. (insert
  17309. (concat (unless bol "\n") env
  17310. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17311. (unless (zerop ind)
  17312. (save-excursion
  17313. (goto-char beg)
  17314. (while (< (point) end)
  17315. (unless (eolp) (org-indent-line-to ind))
  17316. (forward-line))))
  17317. (goto-char beg)
  17318. (forward-line lines)
  17319. (org-indent-line-to ind)))
  17320. (set-marker beg nil)
  17321. (set-marker end nil)))
  17322. ;;;; LaTeX fragments
  17323. (defun org-inside-LaTeX-fragment-p ()
  17324. "Test if point is inside a LaTeX fragment.
  17325. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17326. sequence appearing also before point.
  17327. Even though the matchers for math are configurable, this function assumes
  17328. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17329. delimiters are skipped when they have been removed by customization.
  17330. The return value is nil, or a cons cell with the delimiter and the
  17331. position of this delimiter.
  17332. This function does a reasonably good job, but can locally be fooled by
  17333. for example currency specifications. For example it will assume being in
  17334. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17335. fragments that are properly closed, but during editing, we have to live
  17336. with the uncertainty caused by missing closing delimiters. This function
  17337. looks only before point, not after."
  17338. (catch 'exit
  17339. (let ((pos (point))
  17340. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17341. (lim (progn
  17342. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17343. (point)))
  17344. dd-on str (start 0) m re)
  17345. (goto-char pos)
  17346. (when dodollar
  17347. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17348. re (nth 1 (assoc "$" org-latex-regexps)))
  17349. (while (string-match re str start)
  17350. (cond
  17351. ((= (match-end 0) (length str))
  17352. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17353. ((= (match-end 0) (- (length str) 5))
  17354. (throw 'exit nil))
  17355. (t (setq start (match-end 0))))))
  17356. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17357. (goto-char pos)
  17358. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17359. (and (match-beginning 2) (throw 'exit nil))
  17360. ;; count $$
  17361. (while (re-search-backward "\\$\\$" lim t)
  17362. (setq dd-on (not dd-on)))
  17363. (goto-char pos)
  17364. (if dd-on (cons "$$" m))))))
  17365. (defun org-inside-latex-macro-p ()
  17366. "Is point inside a LaTeX macro or its arguments?"
  17367. (save-match-data
  17368. (org-in-regexp
  17369. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17370. (defvar org-latex-fragment-image-overlays nil
  17371. "List of overlays carrying the images of latex fragments.")
  17372. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17373. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17374. "Remove all overlays with LaTeX fragment images in current buffer.
  17375. When optional arguments BEG and END are non-nil, remove all
  17376. overlays between them instead. Return t when some overlays were
  17377. removed, nil otherwise."
  17378. (let (removedp)
  17379. (setq org-latex-fragment-image-overlays
  17380. (let ((beg (or beg (point-min)))
  17381. (end (or end (point-max))))
  17382. (org-remove-if
  17383. (lambda (o)
  17384. (and (>= (overlay-start o) beg)
  17385. (<= (overlay-end o) end)
  17386. (progn (delete-overlay o)
  17387. (or removedp (setq removedp t)))))
  17388. org-latex-fragment-image-overlays)))
  17389. removedp))
  17390. (define-obsolete-function-alias
  17391. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17392. (defun org-toggle-latex-fragment (&optional arg)
  17393. "Preview the LaTeX fragment at point, or all locally or globally.
  17394. If the cursor is on a LaTeX fragment, create the image and overlay
  17395. it over the source code, if there is none. Remove it otherwise.
  17396. If there is no fragment at point, display all fragments in the
  17397. current section.
  17398. With prefix ARG, preview or clear image for all fragments in the
  17399. current subtree or in the whole buffer when used before the first
  17400. headline. With a double prefix ARG \\[universal-argument] \
  17401. \\[universal-argument] preview or clear images
  17402. for all fragments in the buffer."
  17403. (interactive "P")
  17404. (unless (buffer-file-name (buffer-base-buffer))
  17405. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17406. (when (display-graphic-p)
  17407. (catch 'exit
  17408. (save-excursion
  17409. (let ((window-start (window-start)) msg)
  17410. (save-restriction
  17411. (cond
  17412. ((or (equal arg '(16))
  17413. (and (equal arg '(4))
  17414. (org-with-limited-levels (org-before-first-heading-p))))
  17415. (if (org-remove-latex-fragment-image-overlays)
  17416. (progn (message "LaTeX fragments images removed from buffer")
  17417. (throw 'exit nil))
  17418. (setq msg "Creating images for buffer...")))
  17419. ((equal arg '(4))
  17420. (org-with-limited-levels (org-back-to-heading t))
  17421. (let ((beg (point))
  17422. (end (progn (org-end-of-subtree t) (point))))
  17423. (if (org-remove-latex-fragment-image-overlays beg end)
  17424. (progn
  17425. (message "LaTeX fragment images removed from subtree")
  17426. (throw 'exit nil))
  17427. (setq msg "Creating images for subtree...")
  17428. (narrow-to-region beg end))))
  17429. ((let ((datum (org-element-context)))
  17430. (when (memq (org-element-type datum)
  17431. '(latex-environment latex-fragment))
  17432. (let* ((beg (org-element-property :begin datum))
  17433. (end (org-element-property :end datum)))
  17434. (if (org-remove-latex-fragment-image-overlays beg end)
  17435. (progn (message "LaTeX fragment image removed")
  17436. (throw 'exit nil))
  17437. (narrow-to-region beg end)
  17438. (setq msg "Creating image..."))))))
  17439. (t
  17440. (org-with-limited-levels
  17441. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17442. (outline-previous-heading)
  17443. (point)))
  17444. (end (progn (outline-next-heading) (point))))
  17445. (if (org-remove-latex-fragment-image-overlays beg end)
  17446. (progn
  17447. (message "LaTeX fragment images removed from section")
  17448. (throw 'exit nil))
  17449. (setq msg "Creating images for section...")
  17450. (narrow-to-region beg end))))))
  17451. (let ((file (buffer-file-name (buffer-base-buffer))))
  17452. (org-format-latex
  17453. (concat org-latex-preview-ltxpng-directory
  17454. (file-name-sans-extension (file-name-nondirectory file)))
  17455. ;; Emacs cannot overlay images from remote hosts.
  17456. ;; Create it in `temporary-file-directory' instead.
  17457. (if (file-remote-p file) temporary-file-directory
  17458. default-directory)
  17459. 'overlays msg 'forbuffer
  17460. org-latex-create-formula-image-program)))
  17461. ;; Work around a bug that doesn't restore window's start
  17462. ;; when widening back the buffer.
  17463. (set-window-start nil window-start)
  17464. (message (concat msg "done")))))))
  17465. (defun org-format-latex
  17466. (prefix &optional dir overlays msg forbuffer processing-type)
  17467. "Replace LaTeX fragments with links to an image, and produce images.
  17468. When optional argument OVERLAYS is non-nil, display the image on
  17469. top of the fragment instead of replacing it.
  17470. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17471. Some of the options can be changed using the variable
  17472. `org-format-latex-options', which see."
  17473. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17474. (unless (eq processing-type 'verbatim)
  17475. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17476. (cnt 0)
  17477. checkdir-flag)
  17478. (goto-char (point-min))
  17479. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17480. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17481. (overlay-recenter (point-max)))
  17482. (while (re-search-forward math-regexp nil t)
  17483. (unless (and overlays
  17484. (eq (get-char-property (point) 'org-overlay-type)
  17485. 'org-latex-overlay))
  17486. (let* ((context (org-element-context))
  17487. (type (org-element-type context)))
  17488. (when (memq type '(latex-environment latex-fragment))
  17489. (let ((block-type (eq type 'latex-environment))
  17490. (value (org-element-property :value context))
  17491. (beg (org-element-property :begin context))
  17492. (end (save-excursion
  17493. (goto-char (org-element-property :end context))
  17494. (skip-chars-backward " \r\t\n")
  17495. (point))))
  17496. (case processing-type
  17497. (mathjax
  17498. ;; Prepare for MathJax processing.
  17499. (if (eq (char-after beg) ?$)
  17500. (save-excursion
  17501. (delete-region beg end)
  17502. (insert "\\(" (substring value 1 -1) "\\)"))
  17503. (goto-char end)))
  17504. ((dvipng imagemagick)
  17505. ;; Process to an image.
  17506. (incf cnt)
  17507. (goto-char beg)
  17508. (let* ((face (face-at-point))
  17509. ;; Get the colors from the face at point.
  17510. (fg
  17511. (let ((color (plist-get org-format-latex-options
  17512. :foreground)))
  17513. (if (and forbuffer (eq color 'auto))
  17514. (face-attribute face :foreground nil 'default)
  17515. color)))
  17516. (bg
  17517. (let ((color (plist-get org-format-latex-options
  17518. :background)))
  17519. (if (and forbuffer (eq color 'auto))
  17520. (face-attribute face :background nil 'default)
  17521. color)))
  17522. (hash (sha1 (prin1-to-string
  17523. (list org-format-latex-header
  17524. org-latex-default-packages-alist
  17525. org-latex-packages-alist
  17526. org-format-latex-options
  17527. forbuffer value fg bg))))
  17528. (absprefix (expand-file-name prefix dir))
  17529. (linkfile (format "%s_%s.png" prefix hash))
  17530. (movefile (format "%s_%s.png" absprefix hash))
  17531. (sep (and block-type "\n\n"))
  17532. (link (concat sep "[[file:" linkfile "]]" sep))
  17533. (options
  17534. (org-combine-plists
  17535. org-format-latex-options
  17536. `(:foreground ,fg :background ,bg))))
  17537. (when msg (message msg cnt))
  17538. (unless checkdir-flag ; Ensure the directory exists.
  17539. (setq checkdir-flag t)
  17540. (let ((todir (file-name-directory absprefix)))
  17541. (unless (file-directory-p todir)
  17542. (make-directory todir t))))
  17543. (unless (file-exists-p movefile)
  17544. (org-create-formula-image
  17545. value movefile options forbuffer processing-type))
  17546. (if overlays
  17547. (progn
  17548. (dolist (o (overlays-in beg end))
  17549. (when (eq (overlay-get o 'org-overlay-type)
  17550. 'org-latex-overlay)
  17551. (delete-overlay o)))
  17552. (let ((ov (make-overlay beg end)))
  17553. (overlay-put ov
  17554. 'org-overlay-type
  17555. 'org-latex-overlay)
  17556. (overlay-put ov 'evaporate t)
  17557. (if (featurep 'xemacs)
  17558. (progn
  17559. (overlay-put ov 'invisible t)
  17560. (overlay-put
  17561. ov 'end-glyph
  17562. (make-glyph
  17563. (vector 'png :file movefile))))
  17564. (overlay-put
  17565. ov 'display
  17566. (list 'image
  17567. :type 'png
  17568. :file movefile
  17569. :ascent 'center)))
  17570. (push ov org-latex-fragment-image-overlays))
  17571. (goto-char end))
  17572. (delete-region beg end)
  17573. (insert
  17574. (org-add-props link
  17575. (list 'org-latex-src
  17576. (replace-regexp-in-string "\"" "" value)
  17577. 'org-latex-src-embed-type
  17578. (if block-type 'paragraph 'character)))))))
  17579. (mathml
  17580. ;; Process to MathML.
  17581. (unless (org-format-latex-mathml-available-p)
  17582. (user-error "LaTeX to MathML converter not configured"))
  17583. (incf cnt)
  17584. (when msg (message msg cnt))
  17585. (goto-char beg)
  17586. (delete-region beg end)
  17587. (insert (org-format-latex-as-mathml
  17588. value block-type prefix dir)))
  17589. (otherwise
  17590. (error "Unknown conversion type %s for LaTeX fragments"
  17591. processing-type)))))))))))
  17592. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17593. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17594. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17595. sucessful, return the portion between \"<math...> </math>\"
  17596. elements otherwise return nil. When MATHML-FILE is specified,
  17597. write the results in to that file. When invoked as an
  17598. interactive command, prompt for LATEX-FRAG, with initial value
  17599. set to the current active region and echo the results for user
  17600. inspection."
  17601. (interactive (list (let ((frag (when (org-region-active-p)
  17602. (buffer-substring-no-properties
  17603. (region-beginning) (region-end)))))
  17604. (read-string "LaTeX Fragment: " frag nil frag))))
  17605. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17606. (let* ((tmp-in-file (file-relative-name
  17607. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17608. (ignore (write-region latex-frag nil tmp-in-file))
  17609. (tmp-out-file (file-relative-name
  17610. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17611. (cmd (format-spec
  17612. org-latex-to-mathml-convert-command
  17613. `((?j . ,(and org-latex-to-mathml-jar-file
  17614. (shell-quote-argument
  17615. (expand-file-name
  17616. org-latex-to-mathml-jar-file))))
  17617. (?I . ,(shell-quote-argument tmp-in-file))
  17618. (?i . ,latex-frag)
  17619. (?o . ,(shell-quote-argument tmp-out-file)))))
  17620. mathml shell-command-output)
  17621. (when (org-called-interactively-p 'any)
  17622. (unless (org-format-latex-mathml-available-p)
  17623. (user-error "LaTeX to MathML converter not configured")))
  17624. (message "Running %s" cmd)
  17625. (setq shell-command-output (shell-command-to-string cmd))
  17626. (setq mathml
  17627. (when (file-readable-p tmp-out-file)
  17628. (with-current-buffer (find-file-noselect tmp-out-file t)
  17629. (goto-char (point-min))
  17630. (when (re-search-forward
  17631. (concat
  17632. (regexp-quote
  17633. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17634. "[^>]*?>"
  17635. "\\(.\\|\n\\)*"
  17636. "</math>")
  17637. nil t)
  17638. (prog1 (match-string 0) (kill-buffer))))))
  17639. (cond
  17640. (mathml
  17641. (setq mathml
  17642. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17643. (when mathml-file
  17644. (write-region mathml nil mathml-file))
  17645. (when (org-called-interactively-p 'any)
  17646. (message mathml)))
  17647. ((message "LaTeX to MathML conversion failed")
  17648. (message shell-command-output)))
  17649. (delete-file tmp-in-file)
  17650. (when (file-exists-p tmp-out-file)
  17651. (delete-file tmp-out-file))
  17652. mathml))
  17653. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17654. prefix &optional dir)
  17655. "Use `org-create-math-formula' but check local cache first."
  17656. (let* ((absprefix (expand-file-name prefix dir))
  17657. (print-length nil) (print-level nil)
  17658. (formula-id (concat
  17659. "formula-"
  17660. (sha1
  17661. (prin1-to-string
  17662. (list latex-frag
  17663. org-latex-to-mathml-convert-command)))))
  17664. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17665. (formula-cache-dir (file-name-directory formula-cache)))
  17666. (unless (file-directory-p formula-cache-dir)
  17667. (make-directory formula-cache-dir t))
  17668. (unless (file-exists-p formula-cache)
  17669. (org-create-math-formula latex-frag formula-cache))
  17670. (if (file-exists-p formula-cache)
  17671. ;; Successful conversion. Return the link to MathML file.
  17672. (org-add-props
  17673. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17674. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17675. 'org-latex-src-embed-type (if latex-frag-type
  17676. 'paragraph 'character)))
  17677. ;; Failed conversion. Return the LaTeX fragment verbatim
  17678. latex-frag)))
  17679. (defun org-create-formula-image (string tofile options buffer &optional type)
  17680. "Create an image from LaTeX source using dvipng or convert.
  17681. This function calls either `org-create-formula-image-with-dvipng'
  17682. or `org-create-formula-image-with-imagemagick' depending on the
  17683. value of `org-latex-create-formula-image-program' or on the value
  17684. of the optional TYPE variable.
  17685. Note: ultimately these two function should be combined as they
  17686. share a good deal of logic."
  17687. (org-check-external-command
  17688. "latex" "needed to convert LaTeX fragments to images")
  17689. (funcall
  17690. (case (or type org-latex-create-formula-image-program)
  17691. (dvipng
  17692. (org-check-external-command
  17693. "dvipng" "needed to convert LaTeX fragments to images")
  17694. #'org-create-formula-image-with-dvipng)
  17695. (imagemagick
  17696. (org-check-external-command
  17697. "convert" "you need to install imagemagick")
  17698. #'org-create-formula-image-with-imagemagick)
  17699. (t (error
  17700. "Invalid value of `org-latex-create-formula-image-program'")))
  17701. string tofile options buffer))
  17702. (declare-function org-export-get-backend "ox" (name))
  17703. (declare-function org-export--get-global-options "ox" (&optional backend))
  17704. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17705. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17706. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17707. (defun org-create-formula--latex-header ()
  17708. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17709. (let ((info (org-combine-plists (org-export--get-global-options
  17710. (org-export-get-backend 'latex))
  17711. (org-export--get-inbuffer-options
  17712. (org-export-get-backend 'latex)))))
  17713. (org-latex-guess-babel-language
  17714. (org-latex-guess-inputenc
  17715. (org-splice-latex-header
  17716. org-format-latex-header
  17717. org-latex-default-packages-alist
  17718. org-latex-packages-alist t
  17719. (plist-get info :latex-header)))
  17720. info)))
  17721. (defun org--get-display-dpi ()
  17722. "Get the DPI of the display.
  17723. Assumes that the display has the same pixel width in the
  17724. horizontal and vertical directions."
  17725. (if (display-graphic-p)
  17726. (round (/ (display-pixel-height)
  17727. (/ (display-mm-height) 25.4)))
  17728. (error "Attempt to calculate the dpi of a non-graphic display")))
  17729. ;; This function borrows from Ganesh Swami's latex2png.el
  17730. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17731. "This calls dvipng."
  17732. (require 'ox-latex)
  17733. (let* ((tmpdir (if (featurep 'xemacs)
  17734. (temp-directory)
  17735. temporary-file-directory))
  17736. (texfilebase (make-temp-name
  17737. (expand-file-name "orgtex" tmpdir)))
  17738. (texfile (concat texfilebase ".tex"))
  17739. (dvifile (concat texfilebase ".dvi"))
  17740. (pngfile (concat texfilebase ".png"))
  17741. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17742. ;; This assumes that the display has the same pixel width in
  17743. ;; the horizontal and vertical directions
  17744. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17745. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17746. "Black"))
  17747. (bg (or (plist-get options (if buffer :background :html-background))
  17748. "Transparent")))
  17749. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17750. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17751. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17752. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17753. (let ((latex-header (org-create-formula--latex-header)))
  17754. (with-temp-file texfile
  17755. (insert latex-header)
  17756. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17757. (let ((dir default-directory))
  17758. (ignore-errors
  17759. (cd tmpdir)
  17760. (call-process "latex" nil nil nil texfile))
  17761. (cd dir))
  17762. (if (not (file-exists-p dvifile))
  17763. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17764. (ignore-errors
  17765. (if (featurep 'xemacs)
  17766. (call-process "dvipng" nil nil nil
  17767. "-fg" fg "-bg" bg
  17768. "-T" "tight"
  17769. "-o" pngfile
  17770. dvifile)
  17771. (call-process "dvipng" nil nil nil
  17772. "-fg" fg "-bg" bg
  17773. "-D" dpi
  17774. ;;"-x" scale "-y" scale
  17775. "-T" "tight"
  17776. "-o" pngfile
  17777. dvifile)))
  17778. (if (not (file-exists-p pngfile))
  17779. (if org-format-latex-signal-error
  17780. (error "Failed to create png file from %s" texfile)
  17781. (message "Failed to create png file from %s" texfile)
  17782. nil)
  17783. ;; Use the requested file name and clean up
  17784. (copy-file pngfile tofile 'replace)
  17785. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17786. (if (file-exists-p (concat texfilebase e))
  17787. (delete-file (concat texfilebase e))))
  17788. pngfile))))
  17789. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17790. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17791. "This calls convert, which is included into imagemagick."
  17792. (require 'ox-latex)
  17793. (let* ((tmpdir (if (featurep 'xemacs)
  17794. (temp-directory)
  17795. temporary-file-directory))
  17796. (texfilebase (make-temp-name
  17797. (expand-file-name "orgtex" tmpdir)))
  17798. (texfile (concat texfilebase ".tex"))
  17799. (pdffile (concat texfilebase ".pdf"))
  17800. (pngfile (concat texfilebase ".png"))
  17801. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17802. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17803. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17804. "black"))
  17805. (bg (or (plist-get options (if buffer :background :html-background))
  17806. "white")))
  17807. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17808. (setq fg (org-latex-color-format fg)))
  17809. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17810. (setq bg (org-latex-color-format
  17811. (if (string= bg "Transparent") "white" bg))))
  17812. (let ((latex-header (org-create-formula--latex-header)))
  17813. (with-temp-file texfile
  17814. (insert latex-header)
  17815. (insert "\n\\begin{document}\n"
  17816. "\\definecolor{fg}{rgb}{" fg "}\n"
  17817. "\\definecolor{bg}{rgb}{" bg "}\n"
  17818. "\n\\pagecolor{bg}\n"
  17819. "\n{\\color{fg}\n"
  17820. string
  17821. "\n}\n"
  17822. "\n\\end{document}\n")))
  17823. (org-latex-compile texfile t)
  17824. (if (not (file-exists-p pdffile))
  17825. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17826. (ignore-errors
  17827. (if (featurep 'xemacs)
  17828. (call-process "convert" nil nil nil
  17829. "-density" "96"
  17830. "-trim"
  17831. "-antialias"
  17832. pdffile
  17833. "-quality" "100"
  17834. ;; "-sharpen" "0x1.0"
  17835. pngfile)
  17836. (call-process "convert" nil nil nil
  17837. "-density" dpi
  17838. "-trim"
  17839. "-antialias"
  17840. pdffile
  17841. "-quality" "100"
  17842. ;; "-sharpen" "0x1.0"
  17843. pngfile)))
  17844. (if (not (file-exists-p pngfile))
  17845. (if org-format-latex-signal-error
  17846. (error "Failed to create png file from %s" texfile)
  17847. (message "Failed to create png file from %s" texfile)
  17848. nil)
  17849. ;; Use the requested file name and clean up
  17850. (copy-file pngfile tofile 'replace)
  17851. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17852. (if (file-exists-p (concat texfilebase e))
  17853. (delete-file (concat texfilebase e))))
  17854. pngfile))))
  17855. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17856. "Fill a LaTeX header template TPL.
  17857. In the template, the following place holders will be recognized:
  17858. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17859. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17860. [PACKAGES] \\usepackage statements for PKG
  17861. [NO-PACKAGES] do not include PKG
  17862. [EXTRA] the string EXTRA
  17863. [NO-EXTRA] do not include EXTRA
  17864. For backward compatibility, if both the positive and the negative place
  17865. holder is missing, the positive one (without the \"NO-\") will be
  17866. assumed to be present at the end of the template.
  17867. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17868. EXTRA is a string.
  17869. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17870. (let (rpl (end ""))
  17871. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17872. (setq rpl (if (or (match-end 1) (not def-pkg))
  17873. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17874. tpl (replace-match rpl t t tpl))
  17875. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17876. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17877. (setq rpl (if (or (match-end 1) (not pkg))
  17878. "" (org-latex-packages-to-string pkg snippets-p t))
  17879. tpl (replace-match rpl t t tpl))
  17880. (if pkg (setq end
  17881. (concat end "\n"
  17882. (org-latex-packages-to-string pkg snippets-p)))))
  17883. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17884. (setq rpl (if (or (match-end 1) (not extra))
  17885. "" (concat extra "\n"))
  17886. tpl (replace-match rpl t t tpl))
  17887. (if (and extra (string-match "\\S-" extra))
  17888. (setq end (concat end "\n" extra))))
  17889. (if (string-match "\\S-" end)
  17890. (concat tpl "\n" end)
  17891. tpl)))
  17892. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17893. "Turn an alist of packages into a string with the \\usepackage macros."
  17894. (setq pkg (mapconcat (lambda(p)
  17895. (cond
  17896. ((stringp p) p)
  17897. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17898. (format "%% Package %s omitted" (cadr p)))
  17899. ((equal "" (car p))
  17900. (format "\\usepackage{%s}" (cadr p)))
  17901. (t
  17902. (format "\\usepackage[%s]{%s}"
  17903. (car p) (cadr p)))))
  17904. pkg
  17905. "\n"))
  17906. (if newline (concat pkg "\n") pkg))
  17907. (defun org-dvipng-color (attr)
  17908. "Return a RGB color specification for dvipng."
  17909. (apply 'format "rgb %s %s %s"
  17910. (mapcar 'org-normalize-color
  17911. (if (featurep 'xemacs)
  17912. (color-rgb-components
  17913. (face-property 'default
  17914. (cond ((eq attr :foreground) 'foreground)
  17915. ((eq attr :background) 'background))))
  17916. (color-values (face-attribute 'default attr nil))))))
  17917. (defun org-dvipng-color-format (color-name)
  17918. "Convert COLOR-NAME to a RGB color value for dvipng."
  17919. (apply 'format "rgb %s %s %s"
  17920. (mapcar 'org-normalize-color
  17921. (color-values color-name))))
  17922. (defun org-latex-color (attr)
  17923. "Return a RGB color for the LaTeX color package."
  17924. (apply 'format "%s,%s,%s"
  17925. (mapcar 'org-normalize-color
  17926. (if (featurep 'xemacs)
  17927. (color-rgb-components
  17928. (face-property 'default
  17929. (cond ((eq attr :foreground) 'foreground)
  17930. ((eq attr :background) 'background))))
  17931. (color-values (face-attribute 'default attr nil))))))
  17932. (defun org-latex-color-format (color-name)
  17933. "Convert COLOR-NAME to a RGB color value."
  17934. (apply 'format "%s,%s,%s"
  17935. (mapcar 'org-normalize-color
  17936. (color-values color-name))))
  17937. (defun org-normalize-color (value)
  17938. "Return string to be used as color value for an RGB component."
  17939. (format "%g" (/ value 65535.0)))
  17940. ;; Image display
  17941. (defvar org-inline-image-overlays nil)
  17942. (make-variable-buffer-local 'org-inline-image-overlays)
  17943. (defun org-toggle-inline-images (&optional include-linked)
  17944. "Toggle the display of inline images.
  17945. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17946. (interactive "P")
  17947. (if org-inline-image-overlays
  17948. (progn
  17949. (org-remove-inline-images)
  17950. (when (org-called-interactively-p 'interactive)
  17951. (message "Inline image display turned off")))
  17952. (org-display-inline-images include-linked)
  17953. (when (org-called-interactively-p 'interactive)
  17954. (message (if org-inline-image-overlays
  17955. (format "%d images displayed inline"
  17956. (length org-inline-image-overlays))
  17957. "No images to display inline")))))
  17958. (defun org-redisplay-inline-images ()
  17959. "Refresh the display of inline images."
  17960. (interactive)
  17961. (if (not org-inline-image-overlays)
  17962. (org-toggle-inline-images)
  17963. (org-toggle-inline-images)
  17964. (org-toggle-inline-images)))
  17965. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17966. "Display inline images.
  17967. An inline image is a link which follows either of these
  17968. conventions:
  17969. 1. Its path is a file with an extension matching return value
  17970. from `image-file-name-regexp' and it has no contents.
  17971. 2. Its description consists in a single link of the previous
  17972. type.
  17973. When optional argument INCLUDE-LINKED is non-nil, also links with
  17974. a text description part will be inlined. This can be nice for
  17975. a quick look at those images, but it does not reflect what
  17976. exported files will look like.
  17977. When optional argument REFRESH is non-nil, refresh existing
  17978. images between BEG and END. This will create new image displays
  17979. only if necessary. BEG and END default to the buffer
  17980. boundaries."
  17981. (interactive "P")
  17982. (when (display-graphic-p)
  17983. (unless refresh
  17984. (org-remove-inline-images)
  17985. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17986. (org-with-wide-buffer
  17987. (goto-char (or beg (point-min)))
  17988. (let ((case-fold-search t)
  17989. (file-extension-re (org-image-file-name-regexp)))
  17990. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17991. (let ((link (save-match-data (org-element-context))))
  17992. ;; Check if we're at an inline image.
  17993. (when (and (equal (org-element-property :type link) "file")
  17994. (or include-linked
  17995. (not (org-element-property :contents-begin link)))
  17996. (let ((parent (org-element-property :parent link)))
  17997. (or (not (eq (org-element-type parent) 'link))
  17998. (not (cdr (org-element-contents parent)))))
  17999. (org-string-match-p file-extension-re
  18000. (org-element-property :path link)))
  18001. (let ((file (expand-file-name
  18002. (org-link-unescape
  18003. (org-element-property :path link)))))
  18004. (when (file-exists-p file)
  18005. (let ((width
  18006. ;; Apply `org-image-actual-width' specifications.
  18007. (cond
  18008. ((not (image-type-available-p 'imagemagick)) nil)
  18009. ((eq org-image-actual-width t) nil)
  18010. ((listp org-image-actual-width)
  18011. (or
  18012. ;; First try to find a width among
  18013. ;; attributes associated to the paragraph
  18014. ;; containing link.
  18015. (let ((paragraph
  18016. (let ((e link))
  18017. (while (and (setq e (org-element-property
  18018. :parent e))
  18019. (not (eq (org-element-type e)
  18020. 'paragraph))))
  18021. e)))
  18022. (when paragraph
  18023. (save-excursion
  18024. (goto-char (org-element-property :begin paragraph))
  18025. (when
  18026. (re-search-forward
  18027. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18028. (org-element-property
  18029. :post-affiliated paragraph)
  18030. t)
  18031. (string-to-number (match-string 1))))))
  18032. ;; Otherwise, fall-back to provided number.
  18033. (car org-image-actual-width)))
  18034. ((numberp org-image-actual-width)
  18035. org-image-actual-width)))
  18036. (old (get-char-property-and-overlay
  18037. (org-element-property :begin link)
  18038. 'org-image-overlay)))
  18039. (if (and (car-safe old) refresh)
  18040. (image-refresh (overlay-get (cdr old) 'display))
  18041. (let ((image (create-image file
  18042. (and width 'imagemagick)
  18043. nil
  18044. :width width)))
  18045. (when image
  18046. (let* ((link
  18047. ;; If inline image is the description
  18048. ;; of another link, be sure to
  18049. ;; consider the latter as the one to
  18050. ;; apply the overlay on.
  18051. (let ((parent
  18052. (org-element-property :parent link)))
  18053. (if (eq (org-element-type parent) 'link)
  18054. parent
  18055. link)))
  18056. (ov (make-overlay
  18057. (org-element-property :begin link)
  18058. (progn
  18059. (goto-char
  18060. (org-element-property :end link))
  18061. (skip-chars-backward " \t")
  18062. (point)))))
  18063. (overlay-put ov 'display image)
  18064. (overlay-put ov 'face 'default)
  18065. (overlay-put ov 'org-image-overlay t)
  18066. (overlay-put
  18067. ov 'modification-hooks
  18068. (list 'org-display-inline-remove-overlay))
  18069. (push ov org-inline-image-overlays)))))))))))))))
  18070. (define-obsolete-function-alias
  18071. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18072. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18073. "Remove inline-display overlay if a corresponding region is modified."
  18074. (let ((inhibit-modification-hooks t))
  18075. (when (and ov after)
  18076. (delete ov org-inline-image-overlays)
  18077. (delete-overlay ov))))
  18078. (defun org-remove-inline-images ()
  18079. "Remove inline display of images."
  18080. (interactive)
  18081. (mapc 'delete-overlay org-inline-image-overlays)
  18082. (setq org-inline-image-overlays nil))
  18083. ;;;; Key bindings
  18084. ;; Outline functions from `outline-mode-prefix-map'
  18085. ;; that can be remapped in Org:
  18086. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18087. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18088. (define-key org-mode-map [remap outline-forward-same-level]
  18089. 'org-forward-heading-same-level)
  18090. (define-key org-mode-map [remap outline-backward-same-level]
  18091. 'org-backward-heading-same-level)
  18092. (define-key org-mode-map [remap show-branches]
  18093. 'org-kill-note-or-show-branches)
  18094. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18095. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18096. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18097. (define-key org-mode-map [remap outline-next-visible-heading]
  18098. 'org-next-visible-heading)
  18099. (define-key org-mode-map [remap outline-previous-visible-heading]
  18100. 'org-previous-visible-heading)
  18101. (define-key org-mode-map [remap show-children] 'org-show-children)
  18102. ;; Outline functions from `outline-mode-prefix-map' that can not
  18103. ;; be remapped in Org:
  18104. ;; - the column "key binding" shows whether the Outline function is still
  18105. ;; available in Org mode on the same key that it has been bound to in
  18106. ;; Outline mode:
  18107. ;; - "overridden": key used for a different functionality in Org mode
  18108. ;; - else: key still bound to the same Outline function in Org mode
  18109. ;; | Outline function | key binding | Org replacement |
  18110. ;; |------------------------------------+-------------+--------------------------|
  18111. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18112. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18113. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18114. ;; | `show-entry' | overridden | no replacement |
  18115. ;; | `show-branches' | `C-c C-k' | still same function |
  18116. ;; | `show-subtree' | overridden | visibility cycling |
  18117. ;; | `show-all' | overridden | no replacement |
  18118. ;; | `hide-subtree' | overridden | visibility cycling |
  18119. ;; | `hide-body' | overridden | no replacement |
  18120. ;; | `hide-entry' | overridden | visibility cycling |
  18121. ;; | `hide-leaves' | overridden | no replacement |
  18122. ;; | `hide-sublevels' | overridden | no replacement |
  18123. ;; | `hide-other' | overridden | no replacement |
  18124. ;; Make `C-c C-x' a prefix key
  18125. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18126. ;; TAB key with modifiers
  18127. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18128. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18129. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18130. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18131. ;; The following line is necessary under Suse GNU/Linux
  18132. (unless (featurep 'xemacs)
  18133. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18134. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18135. (define-key org-mode-map [backtab] 'org-shifttab)
  18136. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18137. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18138. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18139. ;; Cursor keys with modifiers
  18140. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18141. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18142. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18143. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18144. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18145. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18146. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18147. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18148. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18149. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18150. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18151. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18152. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18153. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18154. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18155. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18156. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18157. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18158. ;; Babel keys
  18159. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18160. (mapc (lambda (pair)
  18161. (define-key org-babel-map (car pair) (cdr pair)))
  18162. org-babel-key-bindings)
  18163. ;;; Extra keys for tty access.
  18164. ;; We only set them when really needed because otherwise the
  18165. ;; menus don't show the simple keys
  18166. (when (or org-use-extra-keys
  18167. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18168. (not window-system))
  18169. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18170. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18171. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18172. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18173. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18174. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18175. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18176. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18177. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18178. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18179. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18180. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18181. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18182. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18183. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18184. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18185. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18186. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18187. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18188. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18189. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18190. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18191. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18192. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18193. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18194. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18195. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18196. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18197. ;; All the other keys
  18198. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18199. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18200. (if (boundp 'narrow-map)
  18201. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18202. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18203. (if (boundp 'narrow-map)
  18204. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18205. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18206. (if (boundp 'narrow-map)
  18207. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18208. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18209. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18210. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18211. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18212. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18213. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18214. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18215. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18216. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18217. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18218. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18219. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18220. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18221. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18222. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18223. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18224. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18225. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18226. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18227. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18228. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18229. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18230. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18231. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18232. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18233. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18234. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18235. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18236. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18237. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18238. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18239. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18240. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18241. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18242. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18243. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18244. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18245. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18246. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18247. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18248. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18249. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18250. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18251. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18252. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18253. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18254. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18255. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18256. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18257. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18258. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18259. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18260. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18261. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18262. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18263. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18264. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18265. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18266. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18267. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18268. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18269. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18270. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18271. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18272. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18273. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18274. (org-defkey org-mode-map "\C-m" 'org-return)
  18275. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18276. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18277. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18278. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18279. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18280. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18281. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18282. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18283. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18284. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18285. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18286. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18287. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18288. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18289. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18290. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18291. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18293. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18294. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18295. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18296. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18297. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18298. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18299. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18300. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18301. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18302. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18303. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18304. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18305. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18306. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18307. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18308. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18309. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18310. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18311. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18312. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18313. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18314. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18315. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18316. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18317. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18318. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18319. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18320. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18321. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18322. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18323. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18324. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18325. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18326. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18327. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18328. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18329. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18330. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18331. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18332. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18333. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18334. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18335. (when (featurep 'xemacs)
  18336. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18337. (defconst org-speed-commands-default
  18338. '(
  18339. ("Outline Navigation")
  18340. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18341. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18342. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18343. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18344. ("F" . org-next-block)
  18345. ("B" . org-previous-block)
  18346. ("u" . (org-speed-move-safe 'outline-up-heading))
  18347. ("j" . org-goto)
  18348. ("g" . (org-refile t))
  18349. ("Outline Visibility")
  18350. ("c" . org-cycle)
  18351. ("C" . org-shifttab)
  18352. (" " . org-display-outline-path)
  18353. ("s" . org-narrow-to-subtree)
  18354. ("=" . org-columns)
  18355. ("Outline Structure Editing")
  18356. ("U" . org-metaup)
  18357. ("D" . org-metadown)
  18358. ("r" . org-metaright)
  18359. ("l" . org-metaleft)
  18360. ("R" . org-shiftmetaright)
  18361. ("L" . org-shiftmetaleft)
  18362. ("i" . (progn (forward-char 1) (call-interactively
  18363. 'org-insert-heading-respect-content)))
  18364. ("^" . org-sort)
  18365. ("w" . org-refile)
  18366. ("a" . org-archive-subtree-default-with-confirmation)
  18367. ("@" . org-mark-subtree)
  18368. ("#" . org-toggle-comment)
  18369. ("Clock Commands")
  18370. ("I" . org-clock-in)
  18371. ("O" . org-clock-out)
  18372. ("Meta Data Editing")
  18373. ("t" . org-todo)
  18374. ("," . (org-priority))
  18375. ("0" . (org-priority ?\ ))
  18376. ("1" . (org-priority ?A))
  18377. ("2" . (org-priority ?B))
  18378. ("3" . (org-priority ?C))
  18379. (":" . org-set-tags-command)
  18380. ("e" . org-set-effort)
  18381. ("E" . org-inc-effort)
  18382. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18383. (org-entry-put (point) "APPT_WARNTIME" m)))
  18384. ("Agenda Views etc")
  18385. ("v" . org-agenda)
  18386. ("/" . org-sparse-tree)
  18387. ("Misc")
  18388. ("o" . org-open-at-point)
  18389. ("?" . org-speed-command-help)
  18390. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18391. (">" . (org-agenda-remove-restriction-lock))
  18392. )
  18393. "The default speed commands.")
  18394. (defun org-print-speed-command (e)
  18395. (if (> (length (car e)) 1)
  18396. (progn
  18397. (princ "\n")
  18398. (princ (car e))
  18399. (princ "\n")
  18400. (princ (make-string (length (car e)) ?-))
  18401. (princ "\n"))
  18402. (princ (car e))
  18403. (princ " ")
  18404. (if (symbolp (cdr e))
  18405. (princ (symbol-name (cdr e)))
  18406. (prin1 (cdr e)))
  18407. (princ "\n")))
  18408. (defun org-speed-command-help ()
  18409. "Show the available speed commands."
  18410. (interactive)
  18411. (if (not org-use-speed-commands)
  18412. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18413. (with-output-to-temp-buffer "*Help*"
  18414. (princ "User-defined Speed commands\n===========================\n")
  18415. (mapc 'org-print-speed-command org-speed-commands-user)
  18416. (princ "\n")
  18417. (princ "Built-in Speed commands\n=======================\n")
  18418. (mapc 'org-print-speed-command org-speed-commands-default))
  18419. (with-current-buffer "*Help*"
  18420. (setq truncate-lines t))))
  18421. (defun org-speed-move-safe (cmd)
  18422. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18423. If not, return to the original position and throw an error."
  18424. (interactive)
  18425. (let ((pos (point)))
  18426. (call-interactively cmd)
  18427. (unless (and (bolp) (org-at-heading-p))
  18428. (goto-char pos)
  18429. (error "Boundary reached while executing %s" cmd))))
  18430. (defvar org-self-insert-command-undo-counter 0)
  18431. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18432. (defvar org-speed-command nil)
  18433. (define-obsolete-function-alias
  18434. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18435. (defun org-speed-command-activate (keys)
  18436. "Hook for activating single-letter speed commands.
  18437. `org-speed-commands-default' specifies a minimal command set.
  18438. Use `org-speed-commands-user' for further customization."
  18439. (when (or (and (bolp) (looking-at org-outline-regexp))
  18440. (and (functionp org-use-speed-commands)
  18441. (funcall org-use-speed-commands)))
  18442. (cdr (assoc keys (append org-speed-commands-user
  18443. org-speed-commands-default)))))
  18444. (define-obsolete-function-alias
  18445. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18446. (defun org-babel-speed-command-activate (keys)
  18447. "Hook for activating single-letter code block commands."
  18448. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18449. (cdr (assoc keys org-babel-key-bindings))))
  18450. (defcustom org-speed-command-hook
  18451. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18452. "Hook for activating speed commands at strategic locations.
  18453. Hook functions are called in sequence until a valid handler is
  18454. found.
  18455. Each hook takes a single argument, a user-pressed command key
  18456. which is also a `self-insert-command' from the global map.
  18457. Within the hook, examine the cursor position and the command key
  18458. and return nil or a valid handler as appropriate. Handler could
  18459. be one of an interactive command, a function, or a form.
  18460. Set `org-use-speed-commands' to non-nil value to enable this
  18461. hook. The default setting is `org-speed-command-activate'."
  18462. :group 'org-structure
  18463. :version "24.1"
  18464. :type 'hook)
  18465. (defun org-self-insert-command (N)
  18466. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18467. If the cursor is in a table looking at whitespace, the whitespace is
  18468. overwritten, and the table is not marked as requiring realignment."
  18469. (interactive "p")
  18470. (org-check-before-invisible-edit 'insert)
  18471. (cond
  18472. ((and org-use-speed-commands
  18473. (let ((kv (this-command-keys-vector)))
  18474. (setq org-speed-command
  18475. (run-hook-with-args-until-success
  18476. 'org-speed-command-hook
  18477. (make-string 1 (aref kv (1- (length kv))))))))
  18478. (cond
  18479. ((commandp org-speed-command)
  18480. (setq this-command org-speed-command)
  18481. (call-interactively org-speed-command))
  18482. ((functionp org-speed-command)
  18483. (funcall org-speed-command))
  18484. ((and org-speed-command (listp org-speed-command))
  18485. (eval org-speed-command))
  18486. (t (let (org-use-speed-commands)
  18487. (call-interactively 'org-self-insert-command)))))
  18488. ((and
  18489. (org-table-p)
  18490. (progn
  18491. ;; check if we blank the field, and if that triggers align
  18492. (and (featurep 'org-table) org-table-auto-blank-field
  18493. (memq last-command
  18494. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18495. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18496. ;; got extra space, this field does not determine column width
  18497. (let (org-table-may-need-update) (org-table-blank-field))
  18498. ;; no extra space, this field may determine column width
  18499. (org-table-blank-field)))
  18500. t)
  18501. (eq N 1)
  18502. (looking-at "[^|\n]* |"))
  18503. (let (org-table-may-need-update)
  18504. (goto-char (1- (match-end 0)))
  18505. (backward-delete-char 1)
  18506. (goto-char (match-beginning 0))
  18507. (self-insert-command N)))
  18508. (t
  18509. (setq org-table-may-need-update t)
  18510. (self-insert-command N)
  18511. (org-fix-tags-on-the-fly)
  18512. (if org-self-insert-cluster-for-undo
  18513. (if (not (eq last-command 'org-self-insert-command))
  18514. (setq org-self-insert-command-undo-counter 1)
  18515. (if (>= org-self-insert-command-undo-counter 20)
  18516. (setq org-self-insert-command-undo-counter 1)
  18517. (and (> org-self-insert-command-undo-counter 0)
  18518. buffer-undo-list (listp buffer-undo-list)
  18519. (not (cadr buffer-undo-list)) ; remove nil entry
  18520. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18521. (setq org-self-insert-command-undo-counter
  18522. (1+ org-self-insert-command-undo-counter))))))))
  18523. (defun org-check-before-invisible-edit (kind)
  18524. "Check is editing if kind KIND would be dangerous with invisible text around.
  18525. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18526. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18527. (if (and org-catch-invisible-edits
  18528. (or (not (boundp 'visible-mode)) (not visible-mode))
  18529. (or (get-char-property (point) 'invisible)
  18530. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18531. ;; OK, we need to take a closer look
  18532. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18533. (invisible-before-point (if (bobp) nil (get-char-property
  18534. (1- (point)) 'invisible)))
  18535. (border-and-ok-direction
  18536. (or
  18537. ;; Check if we are acting predictably before invisible text
  18538. (and invisible-at-point (not invisible-before-point)
  18539. (memq kind '(insert delete-backward)))
  18540. ;; Check if we are acting predictably after invisible text
  18541. ;; This works not well, and I have turned it off. It seems
  18542. ;; better to always show and stop after invisible text.
  18543. ;; (and (not invisible-at-point) invisible-before-point
  18544. ;; (memq kind '(insert delete)))
  18545. )))
  18546. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18547. (memq invisible-before-point '(outline org-hide-block t)))
  18548. (if (eq org-catch-invisible-edits 'error)
  18549. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18550. (if (and org-custom-properties-overlays
  18551. (y-or-n-p "Display invisible properties in this buffer? "))
  18552. (org-toggle-custom-properties-visibility)
  18553. ;; Make the area visible
  18554. (save-excursion
  18555. (if invisible-before-point
  18556. (goto-char (previous-single-char-property-change
  18557. (point) 'invisible)))
  18558. (show-subtree))
  18559. (cond
  18560. ((eq org-catch-invisible-edits 'show)
  18561. ;; That's it, we do the edit after showing
  18562. (message
  18563. "Unfolding invisible region around point before editing")
  18564. (sit-for 1))
  18565. ((and (eq org-catch-invisible-edits 'smart)
  18566. border-and-ok-direction)
  18567. (message "Unfolding invisible region around point before editing"))
  18568. (t
  18569. ;; Don't do the edit, make the user repeat it in full visibility
  18570. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18571. (defun org-fix-tags-on-the-fly ()
  18572. (when (and (equal (char-after (point-at-bol)) ?*)
  18573. (org-at-heading-p))
  18574. (org-align-tags-here org-tags-column)))
  18575. (defun org-delete-backward-char (N)
  18576. "Like `delete-backward-char', insert whitespace at field end in tables.
  18577. When deleting backwards, in tables this function will insert whitespace in
  18578. front of the next \"|\" separator, to keep the table aligned. The table will
  18579. still be marked for re-alignment if the field did fill the entire column,
  18580. because, in this case the deletion might narrow the column."
  18581. (interactive "p")
  18582. (save-match-data
  18583. (org-check-before-invisible-edit 'delete-backward)
  18584. (if (and (org-table-p)
  18585. (eq N 1)
  18586. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18587. (looking-at ".*?|"))
  18588. (let ((pos (point))
  18589. (noalign (looking-at "[^|\n\r]* |"))
  18590. (c org-table-may-need-update))
  18591. (backward-delete-char N)
  18592. (if (not overwrite-mode)
  18593. (progn
  18594. (skip-chars-forward "^|")
  18595. (insert " ")
  18596. (goto-char (1- pos))))
  18597. ;; noalign: if there were two spaces at the end, this field
  18598. ;; does not determine the width of the column.
  18599. (if noalign (setq org-table-may-need-update c)))
  18600. (backward-delete-char N)
  18601. (org-fix-tags-on-the-fly))))
  18602. (defun org-delete-char (N)
  18603. "Like `delete-char', but insert whitespace at field end in tables.
  18604. When deleting characters, in tables this function will insert whitespace in
  18605. front of the next \"|\" separator, to keep the table aligned. The table will
  18606. still be marked for re-alignment if the field did fill the entire column,
  18607. because, in this case the deletion might narrow the column."
  18608. (interactive "p")
  18609. (save-match-data
  18610. (org-check-before-invisible-edit 'delete)
  18611. (if (and (org-table-p)
  18612. (not (bolp))
  18613. (not (= (char-after) ?|))
  18614. (eq N 1))
  18615. (if (looking-at ".*?|")
  18616. (let ((pos (point))
  18617. (noalign (looking-at "[^|\n\r]* |"))
  18618. (c org-table-may-need-update))
  18619. (replace-match
  18620. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18621. (goto-char pos)
  18622. ;; noalign: if there were two spaces at the end, this field
  18623. ;; does not determine the width of the column.
  18624. (if noalign (setq org-table-may-need-update c)))
  18625. (delete-char N))
  18626. (delete-char N)
  18627. (org-fix-tags-on-the-fly))))
  18628. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18629. (put 'org-self-insert-command 'delete-selection
  18630. (lambda ()
  18631. (not (run-hook-with-args-until-success
  18632. 'self-insert-uses-region-functions))))
  18633. (put 'orgtbl-self-insert-command 'delete-selection
  18634. (lambda ()
  18635. (not (run-hook-with-args-until-success
  18636. 'self-insert-uses-region-functions))))
  18637. (put 'org-delete-char 'delete-selection 'supersede)
  18638. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18639. (put 'org-yank 'delete-selection 'yank)
  18640. ;; Make `flyspell-mode' delay after some commands
  18641. (put 'org-self-insert-command 'flyspell-delayed t)
  18642. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18643. (put 'org-delete-char 'flyspell-delayed t)
  18644. (put 'org-delete-backward-char 'flyspell-delayed t)
  18645. ;; Make pabbrev-mode expand after org-mode commands
  18646. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18647. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18648. (defun org-remap (map &rest commands)
  18649. "In MAP, remap the functions given in COMMANDS.
  18650. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18651. (let (new old)
  18652. (while commands
  18653. (setq old (pop commands) new (pop commands))
  18654. (if (fboundp 'command-remapping)
  18655. (org-defkey map (vector 'remap old) new)
  18656. (substitute-key-definition old new map global-map)))))
  18657. (defun org-transpose-words ()
  18658. "Transpose words for Org.
  18659. This uses the `org-mode-transpose-word-syntax-table' syntax
  18660. table, which interprets characters in `org-emphasis-alist' as
  18661. word constituents."
  18662. (interactive)
  18663. (with-syntax-table org-mode-transpose-word-syntax-table
  18664. (call-interactively 'transpose-words)))
  18665. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18666. (when (eq org-enable-table-editor 'optimized)
  18667. ;; If the user wants maximum table support, we need to hijack
  18668. ;; some standard editing functions
  18669. (org-remap org-mode-map
  18670. 'self-insert-command 'org-self-insert-command
  18671. 'delete-char 'org-delete-char
  18672. 'delete-backward-char 'org-delete-backward-char)
  18673. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18674. (defvar org-ctrl-c-ctrl-c-hook nil
  18675. "Hook for functions attaching themselves to `C-c C-c'.
  18676. This can be used to add additional functionality to the C-c C-c
  18677. key which executes context-dependent commands. This hook is run
  18678. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18679. run after the last test.
  18680. Each function will be called with no arguments. The function
  18681. must check if the context is appropriate for it to act. If yes,
  18682. it should do its thing and then return a non-nil value. If the
  18683. context is wrong, just do nothing and return nil.")
  18684. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18685. "Hook for functions attaching themselves to `C-c C-c'.
  18686. This can be used to add additional functionality to the C-c C-c
  18687. key which executes context-dependent commands. This hook is run
  18688. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18689. before the first test.
  18690. Each function will be called with no arguments. The function
  18691. must check if the context is appropriate for it to act. If yes,
  18692. it should do its thing and then return a non-nil value. If the
  18693. context is wrong, just do nothing and return nil.")
  18694. (defvar org-tab-first-hook nil
  18695. "Hook for functions to attach themselves to TAB.
  18696. See `org-ctrl-c-ctrl-c-hook' for more information.
  18697. This hook runs as the first action when TAB is pressed, even before
  18698. `org-cycle' messes around with the `outline-regexp' to cater for
  18699. inline tasks and plain list item folding.
  18700. If any function in this hook returns t, any other actions that
  18701. would have been caused by TAB (such as table field motion or visibility
  18702. cycling) will not occur.")
  18703. (defvar org-tab-after-check-for-table-hook nil
  18704. "Hook for functions to attach themselves to TAB.
  18705. See `org-ctrl-c-ctrl-c-hook' for more information.
  18706. This hook runs after it has been established that the cursor is not in a
  18707. table, but before checking if the cursor is in a headline or if global cycling
  18708. should be done.
  18709. If any function in this hook returns t, not other actions like visibility
  18710. cycling will be done.")
  18711. (defvar org-tab-after-check-for-cycling-hook nil
  18712. "Hook for functions to attach themselves to TAB.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.
  18714. This hook runs after it has been established that not table field motion and
  18715. not visibility should be done because of current context. This is probably
  18716. the place where a package like yasnippets can hook in.")
  18717. (defvar org-tab-before-tab-emulation-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 after every other options for TAB have been exhausted, but
  18721. before indentation and \t insertion takes place.")
  18722. (defvar org-metaleft-hook nil
  18723. "Hook for functions attaching themselves to `M-left'.
  18724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18725. (defvar org-metaright-hook nil
  18726. "Hook for functions attaching themselves to `M-right'.
  18727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18728. (defvar org-metaup-hook nil
  18729. "Hook for functions attaching themselves to `M-up'.
  18730. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18731. (defvar org-metadown-hook nil
  18732. "Hook for functions attaching themselves to `M-down'.
  18733. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18734. (defvar org-shiftmetaleft-hook nil
  18735. "Hook for functions attaching themselves to `M-S-left'.
  18736. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18737. (defvar org-shiftmetaright-hook nil
  18738. "Hook for functions attaching themselves to `M-S-right'.
  18739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18740. (defvar org-shiftmetaup-hook nil
  18741. "Hook for functions attaching themselves to `M-S-up'.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18743. (defvar org-shiftmetadown-hook nil
  18744. "Hook for functions attaching themselves to `M-S-down'.
  18745. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18746. (defvar org-metareturn-hook nil
  18747. "Hook for functions attaching themselves to `M-RET'.
  18748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18749. (defvar org-shiftup-hook nil
  18750. "Hook for functions attaching themselves to `S-up'.
  18751. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18752. (defvar org-shiftup-final-hook nil
  18753. "Hook for functions attaching themselves to `S-up'.
  18754. This one runs after all other options except shift-select have been excluded.
  18755. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18756. (defvar org-shiftdown-hook nil
  18757. "Hook for functions attaching themselves to `S-down'.
  18758. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18759. (defvar org-shiftdown-final-hook nil
  18760. "Hook for functions attaching themselves to `S-down'.
  18761. This one runs after all other options except shift-select have been excluded.
  18762. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18763. (defvar org-shiftleft-hook nil
  18764. "Hook for functions attaching themselves to `S-left'.
  18765. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18766. (defvar org-shiftleft-final-hook nil
  18767. "Hook for functions attaching themselves to `S-left'.
  18768. This one runs after all other options except shift-select have been excluded.
  18769. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18770. (defvar org-shiftright-hook nil
  18771. "Hook for functions attaching themselves to `S-right'.
  18772. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18773. (defvar org-shiftright-final-hook nil
  18774. "Hook for functions attaching themselves to `S-right'.
  18775. This one runs after all other options except shift-select have been excluded.
  18776. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18777. (defun org-modifier-cursor-error ()
  18778. "Throw an error, a modified cursor command was applied in wrong context."
  18779. (user-error "This command is active in special context like tables, headlines or items"))
  18780. (defun org-shiftselect-error ()
  18781. "Throw an error because Shift-Cursor command was applied in wrong context."
  18782. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18783. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18784. (user-error "This command works only in special context like headlines or timestamps")))
  18785. (defun org-call-for-shift-select (cmd)
  18786. (let ((this-command-keys-shift-translated t))
  18787. (call-interactively cmd)))
  18788. (defun org-shifttab (&optional arg)
  18789. "Global visibility cycling or move to previous table field.
  18790. Call `org-table-previous-field' within a table.
  18791. When ARG is nil, cycle globally through visibility states.
  18792. When ARG is a numeric prefix, show contents of this level."
  18793. (interactive "P")
  18794. (cond
  18795. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18796. ((integerp arg)
  18797. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18798. (message "Content view to level: %d" arg)
  18799. (org-content (prefix-numeric-value arg2))
  18800. (org-cycle-show-empty-lines t)
  18801. (setq org-cycle-global-status 'overview)))
  18802. (t (call-interactively 'org-global-cycle))))
  18803. (defun org-shiftmetaleft ()
  18804. "Promote subtree or delete table column.
  18805. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18806. `org-table-delete-column', depending on context. See the
  18807. individual commands for more information."
  18808. (interactive)
  18809. (cond
  18810. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18811. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18812. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18813. ((if (not (org-region-active-p)) (org-at-item-p)
  18814. (save-excursion (goto-char (region-beginning))
  18815. (org-at-item-p)))
  18816. (call-interactively 'org-outdent-item-tree))
  18817. (t (org-modifier-cursor-error))))
  18818. (defun org-shiftmetaright ()
  18819. "Demote subtree or insert table column.
  18820. Calls `org-demote-subtree', `org-indent-item-tree', or
  18821. `org-table-insert-column', depending on context. See the
  18822. individual commands for more information."
  18823. (interactive)
  18824. (cond
  18825. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18826. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18827. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18828. ((if (not (org-region-active-p)) (org-at-item-p)
  18829. (save-excursion (goto-char (region-beginning))
  18830. (org-at-item-p)))
  18831. (call-interactively 'org-indent-item-tree))
  18832. (t (org-modifier-cursor-error))))
  18833. (defun org-shiftmetaup (&optional arg)
  18834. "Drag the line at point up.
  18835. In a table, kill the current row.
  18836. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18837. but also adjust the previous clocked item in the clock history.
  18838. Everywhere else, drag the line at point up."
  18839. (interactive "P")
  18840. (cond
  18841. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18842. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18843. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18844. (call-interactively 'org-timestamp-up)))
  18845. (t (call-interactively 'org-drag-line-backward))))
  18846. (defun org-shiftmetadown (&optional arg)
  18847. "Drag the line at point down.
  18848. In a table, insert an empty row at the current line.
  18849. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18850. but also adjust the previous clocked item in the clock history.
  18851. Everywhere else, drag the line at point down."
  18852. (interactive "P")
  18853. (cond
  18854. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18855. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18856. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18857. (call-interactively 'org-timestamp-down)))
  18858. (t (call-interactively 'org-drag-line-forward))))
  18859. (defsubst org-hidden-tree-error ()
  18860. (user-error
  18861. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18862. (defun org-metaleft (&optional arg)
  18863. "Promote heading, list item at point or move table column left.
  18864. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18865. depending on context. With no specific context, calls the Emacs
  18866. default `backward-word'. See the individual commands for more
  18867. information.
  18868. This function runs the hook `org-metaleft-hook' as a first step,
  18869. and returns at first non-nil value."
  18870. (interactive "P")
  18871. (cond
  18872. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18873. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18874. ((org-with-limited-levels
  18875. (or (org-at-heading-p)
  18876. (and (org-region-active-p)
  18877. (save-excursion
  18878. (goto-char (region-beginning))
  18879. (org-at-heading-p)))))
  18880. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18881. (call-interactively 'org-do-promote))
  18882. ;; At an inline task.
  18883. ((org-at-heading-p)
  18884. (call-interactively 'org-inlinetask-promote))
  18885. ((or (org-at-item-p)
  18886. (and (org-region-active-p)
  18887. (save-excursion
  18888. (goto-char (region-beginning))
  18889. (org-at-item-p))))
  18890. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18891. (call-interactively 'org-outdent-item))
  18892. (t (call-interactively 'backward-word))))
  18893. (defun org-metaright (&optional arg)
  18894. "Demote heading, list item at point or move table column right.
  18895. In front of a drawer or a block keyword, indent it correctly.
  18896. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18897. `org-indnet-drawer' or `org-indent-block' depending on context.
  18898. With no specific context, calls the Emacs default `forward-word'.
  18899. See the individual commands for more information.
  18900. This function runs the hook `org-metaright-hook' as a first step,
  18901. and returns at first non-nil value."
  18902. (interactive "P")
  18903. (cond
  18904. ((run-hook-with-args-until-success 'org-metaright-hook))
  18905. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18906. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18907. ((org-at-block-p) (call-interactively 'org-indent-block))
  18908. ((org-with-limited-levels
  18909. (or (org-at-heading-p)
  18910. (and (org-region-active-p)
  18911. (save-excursion
  18912. (goto-char (region-beginning))
  18913. (org-at-heading-p)))))
  18914. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18915. (call-interactively 'org-do-demote))
  18916. ;; At an inline task.
  18917. ((org-at-heading-p)
  18918. (call-interactively 'org-inlinetask-demote))
  18919. ((or (org-at-item-p)
  18920. (and (org-region-active-p)
  18921. (save-excursion
  18922. (goto-char (region-beginning))
  18923. (org-at-item-p))))
  18924. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18925. (call-interactively 'org-indent-item))
  18926. (t (call-interactively 'forward-word))))
  18927. (defun org-check-for-hidden (what)
  18928. "Check if there are hidden headlines/items in the current visual line.
  18929. WHAT can be either `headlines' or `items'. If the current line is
  18930. an outline or item heading and it has a folded subtree below it,
  18931. this function returns t, nil otherwise."
  18932. (let ((re (cond
  18933. ((eq what 'headlines) org-outline-regexp-bol)
  18934. ((eq what 'items) (org-item-beginning-re))
  18935. (t (error "This should not happen"))))
  18936. beg end)
  18937. (save-excursion
  18938. (catch 'exit
  18939. (unless (org-region-active-p)
  18940. (setq beg (point-at-bol))
  18941. (beginning-of-line 2)
  18942. (while (and (not (eobp)) ;; this is like `next-line'
  18943. (get-char-property (1- (point)) 'invisible))
  18944. (beginning-of-line 2))
  18945. (setq end (point))
  18946. (goto-char beg)
  18947. (goto-char (point-at-eol))
  18948. (setq end (max end (point)))
  18949. (while (re-search-forward re end t)
  18950. (if (get-char-property (match-beginning 0) 'invisible)
  18951. (throw 'exit t))))
  18952. nil))))
  18953. (defun org-metaup (&optional arg)
  18954. "Move subtree up or move table row up.
  18955. Calls `org-move-subtree-up' or `org-table-move-row' or
  18956. `org-move-item-up', depending on context. See the individual commands
  18957. for more information."
  18958. (interactive "P")
  18959. (cond
  18960. ((run-hook-with-args-until-success 'org-metaup-hook))
  18961. ((org-region-active-p)
  18962. (let* ((a (min (region-beginning) (region-end)))
  18963. (b (1- (max (region-beginning) (region-end))))
  18964. (c (save-excursion (goto-char a)
  18965. (move-beginning-of-line 0)))
  18966. (d (save-excursion (goto-char a)
  18967. (move-end-of-line 0) (point))))
  18968. (transpose-regions a b c d)
  18969. (goto-char c)))
  18970. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18971. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18972. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18973. (t (org-drag-element-backward))))
  18974. (defun org-metadown (&optional arg)
  18975. "Move subtree down or move table row down.
  18976. Calls `org-move-subtree-down' or `org-table-move-row' or
  18977. `org-move-item-down', depending on context. See the individual
  18978. commands for more information."
  18979. (interactive "P")
  18980. (cond
  18981. ((run-hook-with-args-until-success 'org-metadown-hook))
  18982. ((org-region-active-p)
  18983. (let* ((a (min (region-beginning) (region-end)))
  18984. (b (max (region-beginning) (region-end)))
  18985. (c (save-excursion (goto-char b)
  18986. (move-beginning-of-line 1)))
  18987. (d (save-excursion (goto-char b)
  18988. (move-end-of-line 1) (1+ (point)))))
  18989. (transpose-regions a b c d)
  18990. (goto-char d)))
  18991. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18992. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18993. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18994. (t (org-drag-element-forward))))
  18995. (defun org-shiftup (&optional arg)
  18996. "Increase item in timestamp or increase priority of current headline.
  18997. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18998. depending on context. See the individual commands for more information."
  18999. (interactive "P")
  19000. (cond
  19001. ((run-hook-with-args-until-success 'org-shiftup-hook))
  19002. ((and org-support-shift-select (org-region-active-p))
  19003. (org-call-for-shift-select 'previous-line))
  19004. ((org-at-timestamp-p t)
  19005. (call-interactively (if org-edit-timestamp-down-means-later
  19006. 'org-timestamp-down 'org-timestamp-up)))
  19007. ((and (not (eq org-support-shift-select 'always))
  19008. org-enable-priority-commands
  19009. (org-at-heading-p))
  19010. (call-interactively 'org-priority-up))
  19011. ((and (not org-support-shift-select) (org-at-item-p))
  19012. (call-interactively 'org-previous-item))
  19013. ((org-clocktable-try-shift 'up arg))
  19014. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  19015. (org-support-shift-select
  19016. (org-call-for-shift-select 'previous-line))
  19017. (t (org-shiftselect-error))))
  19018. (defun org-shiftdown (&optional arg)
  19019. "Decrease item in timestamp or decrease priority of current headline.
  19020. Calls `org-timestamp-down' or `org-priority-down', or `org-next-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-shiftdown-hook))
  19025. ((and org-support-shift-select (org-region-active-p))
  19026. (org-call-for-shift-select 'next-line))
  19027. ((org-at-timestamp-p t)
  19028. (call-interactively (if org-edit-timestamp-down-means-later
  19029. 'org-timestamp-up 'org-timestamp-down)))
  19030. ((and (not (eq org-support-shift-select 'always))
  19031. org-enable-priority-commands
  19032. (org-at-heading-p))
  19033. (call-interactively 'org-priority-down))
  19034. ((and (not org-support-shift-select) (org-at-item-p))
  19035. (call-interactively 'org-next-item))
  19036. ((org-clocktable-try-shift 'down arg))
  19037. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19038. (org-support-shift-select
  19039. (org-call-for-shift-select 'next-line))
  19040. (t (org-shiftselect-error))))
  19041. (defun org-shiftright (&optional arg)
  19042. "Cycle the thing at point or in the current line, depending on context.
  19043. Depending on context, this does one of the following:
  19044. - switch a timestamp at point one day into the future
  19045. - on a headline, switch to the next TODO keyword.
  19046. - on an item, switch entire list to the next bullet type
  19047. - on a property line, switch to the next allowed value
  19048. - on a clocktable definition line, move time block into the future"
  19049. (interactive "P")
  19050. (cond
  19051. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19052. ((and org-support-shift-select (org-region-active-p))
  19053. (org-call-for-shift-select 'forward-char))
  19054. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19055. ((and (not (eq org-support-shift-select 'always))
  19056. (org-at-heading-p))
  19057. (let ((org-inhibit-logging
  19058. (not org-treat-S-cursor-todo-selection-as-state-change))
  19059. (org-inhibit-blocking
  19060. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19061. (org-call-with-arg 'org-todo 'right)))
  19062. ((or (and org-support-shift-select
  19063. (not (eq org-support-shift-select 'always))
  19064. (org-at-item-bullet-p))
  19065. (and (not org-support-shift-select) (org-at-item-p)))
  19066. (org-call-with-arg 'org-cycle-list-bullet nil))
  19067. ((and (not (eq org-support-shift-select 'always))
  19068. (org-at-property-p))
  19069. (call-interactively 'org-property-next-allowed-value))
  19070. ((org-clocktable-try-shift 'right arg))
  19071. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19072. (org-support-shift-select
  19073. (org-call-for-shift-select 'forward-char))
  19074. (t (org-shiftselect-error))))
  19075. (defun org-shiftleft (&optional arg)
  19076. "Cycle the thing at point or in the current line, depending on context.
  19077. Depending on context, this does one of the following:
  19078. - switch a timestamp at point one day into the past
  19079. - on a headline, switch to the previous TODO keyword.
  19080. - on an item, switch entire list to the previous bullet type
  19081. - on a property line, switch to the previous allowed value
  19082. - on a clocktable definition line, move time block into the past"
  19083. (interactive "P")
  19084. (cond
  19085. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19086. ((and org-support-shift-select (org-region-active-p))
  19087. (org-call-for-shift-select 'backward-char))
  19088. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19089. ((and (not (eq org-support-shift-select 'always))
  19090. (org-at-heading-p))
  19091. (let ((org-inhibit-logging
  19092. (not org-treat-S-cursor-todo-selection-as-state-change))
  19093. (org-inhibit-blocking
  19094. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19095. (org-call-with-arg 'org-todo 'left)))
  19096. ((or (and org-support-shift-select
  19097. (not (eq org-support-shift-select 'always))
  19098. (org-at-item-bullet-p))
  19099. (and (not org-support-shift-select) (org-at-item-p)))
  19100. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19101. ((and (not (eq org-support-shift-select 'always))
  19102. (org-at-property-p))
  19103. (call-interactively 'org-property-previous-allowed-value))
  19104. ((org-clocktable-try-shift 'left arg))
  19105. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19106. (org-support-shift-select
  19107. (org-call-for-shift-select 'backward-char))
  19108. (t (org-shiftselect-error))))
  19109. (defun org-shiftcontrolright ()
  19110. "Switch to next TODO set."
  19111. (interactive)
  19112. (cond
  19113. ((and org-support-shift-select (org-region-active-p))
  19114. (org-call-for-shift-select 'forward-word))
  19115. ((and (not (eq org-support-shift-select 'always))
  19116. (org-at-heading-p))
  19117. (org-call-with-arg 'org-todo 'nextset))
  19118. (org-support-shift-select
  19119. (org-call-for-shift-select 'forward-word))
  19120. (t (org-shiftselect-error))))
  19121. (defun org-shiftcontrolleft ()
  19122. "Switch to previous TODO set."
  19123. (interactive)
  19124. (cond
  19125. ((and org-support-shift-select (org-region-active-p))
  19126. (org-call-for-shift-select 'backward-word))
  19127. ((and (not (eq org-support-shift-select 'always))
  19128. (org-at-heading-p))
  19129. (org-call-with-arg 'org-todo 'previousset))
  19130. (org-support-shift-select
  19131. (org-call-for-shift-select 'backward-word))
  19132. (t (org-shiftselect-error))))
  19133. (defun org-shiftcontrolup (&optional n)
  19134. "Change timestamps synchronously up in CLOCK log lines.
  19135. Optional argument N tells to change by that many units."
  19136. (interactive "P")
  19137. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19138. (let (org-support-shift-select)
  19139. (org-clock-timestamps-up n))
  19140. (user-error "Not at a clock log")))
  19141. (defun org-shiftcontroldown (&optional n)
  19142. "Change timestamps synchronously down in CLOCK log lines.
  19143. Optional argument N tells to change by that many units."
  19144. (interactive "P")
  19145. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19146. (let (org-support-shift-select)
  19147. (org-clock-timestamps-down n))
  19148. (user-error "Not at a clock log")))
  19149. (defun org-increase-number-at-point (&optional inc)
  19150. "Increment the number at point.
  19151. With an optional prefix numeric argument INC, increment using
  19152. this numeric value."
  19153. (interactive "p")
  19154. (if (not (number-at-point))
  19155. (user-error "Not on a number")
  19156. (unless inc (setq inc 1))
  19157. (let ((pos (point))
  19158. (beg (skip-chars-backward "-+^/*0-9eE."))
  19159. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19160. (setq nap (buffer-substring-no-properties
  19161. (+ pos beg) (+ pos beg end)))
  19162. (delete-region (+ pos beg) (+ pos beg end))
  19163. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19164. (when (org-at-table-p)
  19165. (org-table-align)
  19166. (org-table-end-of-field 1))))
  19167. (defun org-decrease-number-at-point (&optional inc)
  19168. "Decrement the number at point.
  19169. With an optional prefix numeric argument INC, decrement using
  19170. this numeric value."
  19171. (interactive "p")
  19172. (org-increase-number-at-point (- (or inc 1))))
  19173. (defun org-ctrl-c-ret ()
  19174. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19175. (interactive)
  19176. (cond
  19177. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19178. (t (call-interactively 'org-insert-heading))))
  19179. (defun org-find-visible ()
  19180. (let ((s (point)))
  19181. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19182. (get-char-property s 'invisible)))
  19183. s))
  19184. (defun org-find-invisible ()
  19185. (let ((s (point)))
  19186. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19187. (not (get-char-property s 'invisible))))
  19188. s))
  19189. (defun org-copy-visible (beg end)
  19190. "Copy the visible parts of the region."
  19191. (interactive "r")
  19192. (let (snippets s)
  19193. (save-excursion
  19194. (save-restriction
  19195. (narrow-to-region beg end)
  19196. (setq s (goto-char (point-min)))
  19197. (while (not (= (point) (point-max)))
  19198. (goto-char (org-find-invisible))
  19199. (push (buffer-substring s (point)) snippets)
  19200. (setq s (goto-char (org-find-visible))))))
  19201. (kill-new (apply 'concat (nreverse snippets)))))
  19202. (defun org-copy-special ()
  19203. "Copy region in table or copy current subtree.
  19204. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19205. See the individual commands for more information."
  19206. (interactive)
  19207. (call-interactively
  19208. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19209. (defun org-cut-special ()
  19210. "Cut region in table or cut current subtree.
  19211. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19212. See the individual commands for more information."
  19213. (interactive)
  19214. (call-interactively
  19215. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19216. (defun org-paste-special (arg)
  19217. "Paste rectangular region into table, or past subtree relative to level.
  19218. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19219. See the individual commands for more information."
  19220. (interactive "P")
  19221. (if (org-at-table-p)
  19222. (org-table-paste-rectangle)
  19223. (org-paste-subtree arg)))
  19224. (defsubst org-in-fixed-width-region-p ()
  19225. "Is point in a fixed-width region?"
  19226. (save-match-data
  19227. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19228. (defun org-edit-special (&optional arg)
  19229. "Call a special editor for the element at point.
  19230. When at a table, call the formula editor with `org-table-edit-formulas'.
  19231. When in a source code block, call `org-edit-src-code'.
  19232. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19233. When in an export block, call `org-edit-export-block'.
  19234. When at an #+INCLUDE keyword, visit the included file.
  19235. When at a footnote reference, call `org-edit-footnote-reference'
  19236. On a link, call `ffap' to visit the link at point.
  19237. Otherwise, return a user error."
  19238. (interactive "P")
  19239. (let ((element (org-element-at-point)))
  19240. (assert (not buffer-read-only) nil
  19241. "Buffer is read-only: %s" (buffer-name))
  19242. (case (org-element-type element)
  19243. (src-block
  19244. (if (not arg) (org-edit-src-code)
  19245. (let* ((info (org-babel-get-src-block-info))
  19246. (lang (nth 0 info))
  19247. (params (nth 2 info))
  19248. (session (cdr (assq :session params))))
  19249. (if (not session) (org-edit-src-code)
  19250. ;; At a src-block with a session and function called with
  19251. ;; an ARG: switch to the buffer related to the inferior
  19252. ;; process.
  19253. (switch-to-buffer
  19254. (funcall (intern (concat "org-babel-prep-session:" lang))
  19255. session params))))))
  19256. (keyword
  19257. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19258. (org-open-link-from-string
  19259. (format "[[%s]]"
  19260. (expand-file-name
  19261. (let ((value (org-element-property :value element)))
  19262. (cond ((not (org-string-nw-p value))
  19263. (user-error "No file to edit"))
  19264. ((string-match "\\`\"\\(.*?\\)\"" value)
  19265. (match-string 1 value))
  19266. ((string-match "\\`[^ \t\"]\\S-*" value)
  19267. (match-string 0 value))
  19268. (t (user-error "No valid file specified")))))))
  19269. (user-error "No special environment to edit here")))
  19270. (table
  19271. (if (eq (org-element-property :type element) 'table.el)
  19272. (org-edit-table.el)
  19273. (call-interactively 'org-table-edit-formulas)))
  19274. ;; Only Org tables contain `table-row' type elements.
  19275. (table-row (call-interactively 'org-table-edit-formulas))
  19276. (example-block (org-edit-src-code))
  19277. (export-block (org-edit-export-block))
  19278. (fixed-width (org-edit-fixed-width-region))
  19279. (otherwise
  19280. ;; No notable element at point. Though, we may be at a link or
  19281. ;; a footnote reference, which are objects. Thus, scan deeper.
  19282. (let ((context (org-element-context element)))
  19283. (case (org-element-type context)
  19284. (link (call-interactively #'ffap))
  19285. (footnote-reference (org-edit-footnote-reference))
  19286. (t (user-error "No special environment to edit here"))))))))
  19287. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19288. (defun org-ctrl-c-ctrl-c (&optional arg)
  19289. "Set tags in headline, or update according to changed information at point.
  19290. This command does many different things, depending on context:
  19291. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19292. this is what we do.
  19293. - If the cursor is on a statistics cookie, update it.
  19294. - If the cursor is in a headline, prompt for tags and insert them
  19295. into the current line, aligned to `org-tags-column'. When called
  19296. with prefix arg, realign all tags in the current buffer.
  19297. - If the cursor is in one of the special #+KEYWORD lines, this
  19298. triggers scanning the buffer for these lines and updating the
  19299. information.
  19300. - If the cursor is inside a table, realign the table. This command
  19301. works even if the automatic table editor has been turned off.
  19302. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19303. the entire table.
  19304. - If the cursor is at a footnote reference or definition, jump to
  19305. the corresponding definition or references, respectively.
  19306. - If the cursor is a the beginning of a dynamic block, update it.
  19307. - If the current buffer is a capture buffer, close note and file it.
  19308. - If the cursor is on a <<<target>>>, update radio targets and
  19309. corresponding links in this buffer.
  19310. - If the cursor is on a numbered item in a plain list, renumber the
  19311. ordered list.
  19312. - If the cursor is on a checkbox, toggle it.
  19313. - If the cursor is on a code block, evaluate it. The variable
  19314. `org-confirm-babel-evaluate' can be used to control prompting
  19315. before code block evaluation, by default every code block
  19316. evaluation requires confirmation. Code block evaluation can be
  19317. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19318. (interactive "P")
  19319. (cond
  19320. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19321. org-occur-highlights)
  19322. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19323. (org-remove-occur-highlights)
  19324. (message "Temporary highlights/overlays removed from current buffer"))
  19325. ((and (local-variable-p 'org-finish-function (current-buffer))
  19326. (fboundp org-finish-function))
  19327. (funcall org-finish-function))
  19328. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19329. (t
  19330. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19331. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19332. (user-error "C-c C-c can do nothing useful at this location"))
  19333. (let* ((context (org-element-context))
  19334. (type (org-element-type context)))
  19335. (case type
  19336. ;; When at a link, act according to the parent instead.
  19337. (link (setq context (org-element-property :parent context))
  19338. (setq type (org-element-type context)))
  19339. ;; Unsupported object types: refer to the first supported
  19340. ;; element or object containing it.
  19341. ((bold code entity export-snippet inline-babel-call inline-src-block
  19342. italic latex-fragment line-break macro strike-through subscript
  19343. superscript underline verbatim)
  19344. (setq context
  19345. (org-element-lineage
  19346. context '(radio-target paragraph verse-block table-cell)))))
  19347. ;; For convenience: at the first line of a paragraph on the
  19348. ;; same line as an item, apply function on that item instead.
  19349. (when (eq type 'paragraph)
  19350. (let ((parent (org-element-property :parent context)))
  19351. (when (and (eq (org-element-type parent) 'item)
  19352. (= (line-beginning-position)
  19353. (org-element-property :begin parent)))
  19354. (setq context parent type 'item))))
  19355. ;; Act according to type of element or object at point.
  19356. (case type
  19357. (clock (org-clock-update-time-maybe))
  19358. (dynamic-block
  19359. (save-excursion
  19360. (goto-char (org-element-property :post-affiliated context))
  19361. (org-update-dblock)))
  19362. (footnote-definition
  19363. (goto-char (org-element-property :post-affiliated context))
  19364. (call-interactively 'org-footnote-action))
  19365. (footnote-reference (call-interactively 'org-footnote-action))
  19366. ((headline inlinetask)
  19367. (save-excursion (goto-char (org-element-property :begin context))
  19368. (call-interactively 'org-set-tags)))
  19369. (item
  19370. ;; At an item: a double C-u set checkbox to "[-]"
  19371. ;; unconditionally, whereas a single one will toggle its
  19372. ;; presence. Without a universal argument, if the item
  19373. ;; has a checkbox, toggle it. Otherwise repair the list.
  19374. (let* ((box (org-element-property :checkbox context))
  19375. (struct (org-element-property :structure context))
  19376. (old-struct (copy-tree struct))
  19377. (parents (org-list-parents-alist struct))
  19378. (prevs (org-list-prevs-alist struct))
  19379. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19380. (org-list-set-checkbox
  19381. (org-element-property :begin context) struct
  19382. (cond ((equal arg '(16)) "[-]")
  19383. ((and (not box) (equal arg '(4))) "[ ]")
  19384. ((or (not box) (equal arg '(4))) nil)
  19385. ((eq box 'on) "[ ]")
  19386. (t "[X]")))
  19387. ;; Mimic `org-list-write-struct' but with grabbing
  19388. ;; a return value from `org-list-struct-fix-box'.
  19389. (org-list-struct-fix-ind struct parents 2)
  19390. (org-list-struct-fix-item-end struct)
  19391. (org-list-struct-fix-bul struct prevs)
  19392. (org-list-struct-fix-ind struct parents)
  19393. (let ((block-item
  19394. (org-list-struct-fix-box struct parents prevs orderedp)))
  19395. (if (and box (equal struct old-struct))
  19396. (if (equal arg '(16))
  19397. (message "Checkboxes already reset")
  19398. (user-error "Cannot toggle this checkbox: %s"
  19399. (if (eq box 'on)
  19400. "all subitems checked"
  19401. "unchecked subitems")))
  19402. (org-list-struct-apply-struct struct old-struct)
  19403. (org-update-checkbox-count-maybe))
  19404. (when block-item
  19405. (message "Checkboxes were removed due to empty box at line %d"
  19406. (org-current-line block-item))))))
  19407. (keyword
  19408. (let ((org-inhibit-startup-visibility-stuff t)
  19409. (org-startup-align-all-tables nil))
  19410. (when (boundp 'org-table-coordinate-overlays)
  19411. (mapc 'delete-overlay org-table-coordinate-overlays)
  19412. (setq org-table-coordinate-overlays nil))
  19413. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19414. (message "Local setup has been refreshed"))
  19415. (plain-list
  19416. ;; At a plain list, with a double C-u argument, set
  19417. ;; checkboxes of each item to "[-]", whereas a single one
  19418. ;; will toggle their presence according to the state of the
  19419. ;; first item in the list. Without an argument, repair the
  19420. ;; list.
  19421. (let* ((begin (org-element-property :contents-begin context))
  19422. (beginm (move-marker (make-marker) begin))
  19423. (struct (org-element-property :structure context))
  19424. (old-struct (copy-tree struct))
  19425. (first-box (save-excursion
  19426. (goto-char begin)
  19427. (looking-at org-list-full-item-re)
  19428. (match-string-no-properties 3)))
  19429. (new-box (cond ((equal arg '(16)) "[-]")
  19430. ((equal arg '(4)) (unless first-box "[ ]"))
  19431. ((equal first-box "[X]") "[ ]")
  19432. (t "[X]"))))
  19433. (cond
  19434. (arg
  19435. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19436. (org-list-get-all-items
  19437. begin struct (org-list-prevs-alist struct))))
  19438. ((and first-box (eq (point) begin))
  19439. ;; For convenience, when point is at bol on the first
  19440. ;; item of the list and no argument is provided, simply
  19441. ;; toggle checkbox of that item, if any.
  19442. (org-list-set-checkbox begin struct new-box)))
  19443. (org-list-write-struct
  19444. struct (org-list-parents-alist struct) old-struct)
  19445. (org-update-checkbox-count-maybe)
  19446. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19447. ((property-drawer node-property)
  19448. (call-interactively 'org-property-action))
  19449. ((radio-target target)
  19450. (call-interactively 'org-update-radio-target-regexp))
  19451. (statistics-cookie
  19452. (call-interactively 'org-update-statistics-cookies))
  19453. ((table table-cell table-row)
  19454. ;; At a table, recalculate every field and align it. Also
  19455. ;; send the table if necessary. If the table has
  19456. ;; a `table.el' type, just give up. At a table row or
  19457. ;; cell, maybe recalculate line but always align table.
  19458. (if (eq (org-element-property :type context) 'table.el)
  19459. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19460. Use \\[org-edit-special] to edit table.el tables"))
  19461. (let ((org-enable-table-editor t))
  19462. (if (or (eq type 'table)
  19463. ;; Check if point is at a TBLFM line.
  19464. (and (eq type 'table-row)
  19465. (= (point) (org-element-property :end context))))
  19466. (save-excursion
  19467. (if (org-at-TBLFM-p)
  19468. (progn (require 'org-table)
  19469. (org-table-calc-current-TBLFM))
  19470. (goto-char (org-element-property :contents-begin context))
  19471. (org-call-with-arg 'org-table-recalculate (or arg t))
  19472. (orgtbl-send-table 'maybe)))
  19473. (org-table-maybe-eval-formula)
  19474. (cond (arg (call-interactively 'org-table-recalculate))
  19475. ((org-table-maybe-recalculate-line))
  19476. (t (org-table-align)))))))
  19477. (timestamp (org-timestamp-change 0 'day))
  19478. (otherwise
  19479. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19480. (user-error
  19481. "C-c C-c can do nothing useful at this location")))))))))
  19482. (defun org-mode-restart ()
  19483. (interactive)
  19484. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19485. (funcall major-mode)
  19486. (hack-local-variables)
  19487. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19488. (org-indent-mode -1)))
  19489. (message "%s restarted" major-mode))
  19490. (defun org-kill-note-or-show-branches ()
  19491. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19492. (interactive)
  19493. (if (not org-finish-function)
  19494. (progn
  19495. (hide-subtree)
  19496. (call-interactively 'show-branches))
  19497. (let ((org-note-abort t))
  19498. (funcall org-finish-function))))
  19499. (defun org-delete-indentation (&optional ARG)
  19500. "Join current line to previous and fix whitespace at join.
  19501. If previous line is a headline add to headline title. Otherwise
  19502. the function calls `delete-indentation'.
  19503. With argument, join this line to following line."
  19504. (interactive "*P")
  19505. (if (save-excursion
  19506. (if ARG (beginning-of-line)
  19507. (forward-line -1))
  19508. (looking-at org-complex-heading-regexp))
  19509. ;; At headline.
  19510. (let ((tags-column (when (match-beginning 5)
  19511. (save-excursion (goto-char (match-beginning 5))
  19512. (current-column))))
  19513. (string (concat " " (progn (when ARG (forward-line 1))
  19514. (org-trim (delete-and-extract-region
  19515. (line-beginning-position)
  19516. (line-end-position)))))))
  19517. (unless (bobp) (delete-region (point) (1- (point))))
  19518. (goto-char (or (match-end 4)
  19519. (match-beginning 5)
  19520. (match-end 0)))
  19521. (skip-chars-backward " \t")
  19522. (save-excursion (insert string))
  19523. ;; Adjust alignment of tags.
  19524. (when tags-column
  19525. (org-align-tags-here (if org-auto-align-tags
  19526. org-tags-column
  19527. tags-column))))
  19528. (delete-indentation ARG)))
  19529. (defun org-open-line (n)
  19530. "Insert a new row in tables, call `open-line' elsewhere.
  19531. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19532. (interactive "*p")
  19533. (cond
  19534. ((not org-special-ctrl-o)
  19535. (open-line n))
  19536. ((org-at-table-p)
  19537. (org-table-insert-row))
  19538. (t
  19539. (open-line n))))
  19540. (defun org-return (&optional indent)
  19541. "Goto next table row or insert a newline.
  19542. Calls `org-table-next-row' or `newline', depending on context.
  19543. When optional INDENT argument is non-nil, call
  19544. `newline-and-indent' instead of `newline'.
  19545. When `org-return-follows-link' is non-nil and point is on
  19546. a timestamp or a link, call `org-open-at-point'. However, it
  19547. will not happen if point is in a table or on a \"dead\"
  19548. object (e.g., within a comment). In these case, you need to use
  19549. `org-open-at-point' directly."
  19550. (interactive)
  19551. (let* ((context (if org-return-follows-link (org-element-context)
  19552. (org-element-at-point)))
  19553. (type (org-element-type context)))
  19554. (cond
  19555. ;; In a table, call `org-table-next-row'.
  19556. ((or (and (eq type 'table)
  19557. (>= (point) (org-element-property :contents-begin context))
  19558. (< (point) (org-element-property :contents-end context)))
  19559. (org-element-lineage context '(table-row table-cell) t))
  19560. (org-table-justify-field-maybe)
  19561. (call-interactively #'org-table-next-row))
  19562. ;; On a link or a timestamp but not on white spaces after it,
  19563. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19564. ((and org-return-follows-link
  19565. (memq type '(link timestamp))
  19566. (< (point)
  19567. (save-excursion (goto-char (org-element-property :end context))
  19568. (skip-chars-backward " \t")
  19569. (point))))
  19570. (call-interactively #'org-open-at-point))
  19571. ;; Insert newline in heading, but preserve tags.
  19572. ((and (not (bolp))
  19573. (save-excursion (beginning-of-line)
  19574. (looking-at org-complex-heading-regexp)))
  19575. ;; At headline. Split line. However, if point is on keyword,
  19576. ;; priority cookie or tags, do not break any of them: add
  19577. ;; a newline after the headline instead.
  19578. (let ((tags-column (and (match-beginning 5)
  19579. (save-excursion (goto-char (match-beginning 5))
  19580. (current-column))))
  19581. (string
  19582. (when (and (match-end 4)
  19583. (>= (point)
  19584. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19585. (<= (point) (match-end 4)))
  19586. (delete-and-extract-region (point) (match-end 4)))))
  19587. (when (and tags-column string) ; Adjust tag alignment.
  19588. (org-align-tags-here
  19589. (if org-auto-align-tags org-tags-column tags-column)))
  19590. (end-of-line)
  19591. (org-show-entry)
  19592. (if indent (newline-and-indent) (newline))
  19593. (when string (save-excursion (insert (org-trim string))))))
  19594. ;; In a list, make sure indenting keeps trailing text within.
  19595. ((and indent
  19596. (not (eolp))
  19597. (org-element-lineage context '(item)))
  19598. (let ((trailing-data
  19599. (delete-and-extract-region (point) (line-end-position))))
  19600. (newline-and-indent)
  19601. (save-excursion (insert trailing-data))))
  19602. (t (if indent (newline-and-indent) (newline))))))
  19603. (defun org-return-indent ()
  19604. "Goto next table row or insert a newline and indent.
  19605. Calls `org-table-next-row' or `newline-and-indent', depending on
  19606. context. See the individual commands for more information."
  19607. (interactive)
  19608. (org-return t))
  19609. (defun org-ctrl-c-star ()
  19610. "Compute table, or change heading status of lines.
  19611. Calls `org-table-recalculate' or `org-toggle-heading',
  19612. depending on context."
  19613. (interactive)
  19614. (cond
  19615. ((org-at-table-p)
  19616. (call-interactively 'org-table-recalculate))
  19617. (t
  19618. ;; Convert all lines in region to list items
  19619. (call-interactively 'org-toggle-heading))))
  19620. (defun org-ctrl-c-minus ()
  19621. "Insert separator line in table or modify bullet status of line.
  19622. Also turns a plain line or a region of lines into list items.
  19623. Calls `org-table-insert-hline', `org-toggle-item', or
  19624. `org-cycle-list-bullet', depending on context."
  19625. (interactive)
  19626. (cond
  19627. ((org-at-table-p)
  19628. (call-interactively 'org-table-insert-hline))
  19629. ((org-region-active-p)
  19630. (call-interactively 'org-toggle-item))
  19631. ((org-in-item-p)
  19632. (call-interactively 'org-cycle-list-bullet))
  19633. (t
  19634. (call-interactively 'org-toggle-item))))
  19635. (defun org-toggle-item (arg)
  19636. "Convert headings or normal lines to items, items to normal lines.
  19637. If there is no active region, only the current line is considered.
  19638. If the first non blank line in the region is a headline, convert
  19639. all headlines to items, shifting text accordingly.
  19640. If it is an item, convert all items to normal lines.
  19641. If it is normal text, change region into a list of items.
  19642. With a prefix argument ARG, change the region in a single item."
  19643. (interactive "P")
  19644. (let ((shift-text
  19645. (function
  19646. ;; Shift text in current section to IND, from point to END.
  19647. ;; The function leaves point to END line.
  19648. (lambda (ind end)
  19649. (let ((min-i 1000) (end (copy-marker end)))
  19650. ;; First determine the minimum indentation (MIN-I) of
  19651. ;; the text.
  19652. (save-excursion
  19653. (catch 'exit
  19654. (while (< (point) end)
  19655. (let ((i (org-get-indentation)))
  19656. (cond
  19657. ;; Skip blank lines and inline tasks.
  19658. ((looking-at "^[ \t]*$"))
  19659. ((looking-at org-outline-regexp-bol))
  19660. ;; We can't find less than 0 indentation.
  19661. ((zerop i) (throw 'exit (setq min-i 0)))
  19662. ((< i min-i) (setq min-i i))))
  19663. (forward-line))))
  19664. ;; Then indent each line so that a line indented to
  19665. ;; MIN-I becomes indented to IND. Ignore blank lines
  19666. ;; and inline tasks in the process.
  19667. (let ((delta (- ind min-i)))
  19668. (while (< (point) end)
  19669. (unless (or (looking-at "^[ \t]*$")
  19670. (looking-at org-outline-regexp-bol))
  19671. (org-indent-line-to (+ (org-get-indentation) delta)))
  19672. (forward-line)))))))
  19673. (skip-blanks
  19674. (function
  19675. ;; Return beginning of first non-blank line, starting from
  19676. ;; line at POS.
  19677. (lambda (pos)
  19678. (save-excursion
  19679. (goto-char pos)
  19680. (skip-chars-forward " \r\t\n")
  19681. (point-at-bol)))))
  19682. beg end)
  19683. ;; Determine boundaries of changes.
  19684. (if (org-region-active-p)
  19685. (setq beg (funcall skip-blanks (region-beginning))
  19686. end (copy-marker (region-end)))
  19687. (setq beg (funcall skip-blanks (point-at-bol))
  19688. end (copy-marker (point-at-eol))))
  19689. ;; Depending on the starting line, choose an action on the text
  19690. ;; between BEG and END.
  19691. (org-with-limited-levels
  19692. (save-excursion
  19693. (goto-char beg)
  19694. (cond
  19695. ;; Case 1. Start at an item: de-itemize. Note that it only
  19696. ;; happens when a region is active: `org-ctrl-c-minus'
  19697. ;; would call `org-cycle-list-bullet' otherwise.
  19698. ((org-at-item-p)
  19699. (while (< (point) end)
  19700. (when (org-at-item-p)
  19701. (skip-chars-forward " \t")
  19702. (delete-region (point) (match-end 0)))
  19703. (forward-line)))
  19704. ;; Case 2. Start at an heading: convert to items.
  19705. ((org-at-heading-p)
  19706. (let* ((bul (org-list-bullet-string "-"))
  19707. (bul-len (length bul))
  19708. (done (org-entry-is-done-p))
  19709. (todo (org-entry-is-todo-p))
  19710. ;; Indentation of the first heading. It should be
  19711. ;; relative to the indentation of its parent, if any.
  19712. (start-ind (save-excursion
  19713. (cond
  19714. ((not org-adapt-indentation) 0)
  19715. ((not (outline-previous-heading)) 0)
  19716. (t (length (match-string 0))))))
  19717. ;; Level of first heading. Further headings will be
  19718. ;; compared to it to determine hierarchy in the list.
  19719. (ref-level (org-reduced-level (org-outline-level))))
  19720. (when (or done todo) (org-todo ""))
  19721. (while (< (point) end)
  19722. (let* ((level (org-reduced-level (org-outline-level)))
  19723. (delta (max 0 (- level ref-level))))
  19724. ;; If current headline is less indented than the first
  19725. ;; one, set it as reference, in order to preserve
  19726. ;; subtrees.
  19727. (when (< level ref-level) (setq ref-level level))
  19728. (replace-match bul t t)
  19729. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19730. (when (or done todo)
  19731. (let* ((struct (org-list-struct))
  19732. (old (copy-tree struct)))
  19733. (org-list-set-checkbox (line-beginning-position)
  19734. struct
  19735. (if done "[X]" "[ ]"))
  19736. (org-list-write-struct struct
  19737. (org-list-parents-alist struct)
  19738. old)))
  19739. ;; Ensure all text down to END (or SECTION-END) belongs
  19740. ;; to the newly created item.
  19741. (let ((section-end (save-excursion
  19742. (or (outline-next-heading) (point)))))
  19743. (forward-line)
  19744. (funcall shift-text
  19745. (+ start-ind (* (1+ delta) bul-len))
  19746. (min end section-end)))))))
  19747. ;; Case 3. Normal line with ARG: make the first line of region
  19748. ;; an item, and shift indentation of others lines to
  19749. ;; set them as item's body.
  19750. (arg (let* ((bul (org-list-bullet-string "-"))
  19751. (bul-len (length bul))
  19752. (ref-ind (org-get-indentation)))
  19753. (skip-chars-forward " \t")
  19754. (insert bul)
  19755. (forward-line)
  19756. (while (< (point) end)
  19757. ;; Ensure that lines less indented than first one
  19758. ;; still get included in item body.
  19759. (funcall shift-text
  19760. (+ ref-ind bul-len)
  19761. (min end (save-excursion (or (outline-next-heading)
  19762. (point)))))
  19763. (forward-line))))
  19764. ;; Case 4. Normal line without ARG: turn each non-item line
  19765. ;; into an item.
  19766. (t
  19767. (while (< (point) end)
  19768. (unless (or (org-at-heading-p) (org-at-item-p))
  19769. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19770. (replace-match
  19771. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19772. (forward-line))))))))
  19773. (defun org-toggle-heading (&optional nstars)
  19774. "Convert headings to normal text, or items or text to headings.
  19775. If there is no active region, only convert the current line.
  19776. With a \\[universal-argument] prefix, convert the whole list at
  19777. point into heading.
  19778. In a region:
  19779. - If the first non blank line is a headline, remove the stars
  19780. from all headlines in the region.
  19781. - If it is a normal line, turn each and every normal line (i.e.,
  19782. not an heading or an item) in the region into headings. If you
  19783. want to convert only the first line of this region, use one
  19784. universal prefix argument.
  19785. - If it is a plain list item, turn all plain list items into headings.
  19786. When converting a line into a heading, the number of stars is chosen
  19787. such that the lines become children of the current entry. However,
  19788. when a numeric prefix argument is given, its value determines the
  19789. number of stars to add."
  19790. (interactive "P")
  19791. (let ((skip-blanks
  19792. (function
  19793. ;; Return beginning of first non-blank line, starting from
  19794. ;; line at POS.
  19795. (lambda (pos)
  19796. (save-excursion
  19797. (goto-char pos)
  19798. (while (org-at-comment-p) (forward-line))
  19799. (skip-chars-forward " \r\t\n")
  19800. (point-at-bol)))))
  19801. beg end toggled)
  19802. ;; Determine boundaries of changes. If a universal prefix has
  19803. ;; been given, put the list in a region. If region ends at a bol,
  19804. ;; do not consider the last line to be in the region.
  19805. (when (and current-prefix-arg (org-at-item-p))
  19806. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19807. (org-mark-element))
  19808. (if (org-region-active-p)
  19809. (setq beg (funcall skip-blanks (region-beginning))
  19810. end (copy-marker (save-excursion
  19811. (goto-char (region-end))
  19812. (if (bolp) (point) (point-at-eol)))))
  19813. (setq beg (funcall skip-blanks (point-at-bol))
  19814. end (copy-marker (point-at-eol))))
  19815. ;; Ensure inline tasks don't count as headings.
  19816. (org-with-limited-levels
  19817. (save-excursion
  19818. (goto-char beg)
  19819. (cond
  19820. ;; Case 1. Started at an heading: de-star headings.
  19821. ((org-at-heading-p)
  19822. (while (< (point) end)
  19823. (when (org-at-heading-p t)
  19824. (looking-at org-outline-regexp) (replace-match "")
  19825. (setq toggled t))
  19826. (forward-line)))
  19827. ;; Case 2. Started at an item: change items into headlines.
  19828. ;; One star will be added by `org-list-to-subtree'.
  19829. ((org-at-item-p)
  19830. (while (< (point) end)
  19831. (when (org-at-item-p)
  19832. ;; Pay attention to cases when region ends before list.
  19833. (let* ((struct (org-list-struct))
  19834. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19835. (save-restriction
  19836. (narrow-to-region (point) list-end)
  19837. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19838. (setq toggled t))
  19839. (forward-line)))
  19840. ;; Case 3. Started at normal text: make every line an heading,
  19841. ;; skipping headlines and items.
  19842. (t (let* ((stars
  19843. (make-string
  19844. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19845. (add-stars
  19846. (cond (nstars "") ; stars from prefix only
  19847. ((equal stars "") "*") ; before first heading
  19848. (org-odd-levels-only "**") ; inside heading, odd
  19849. (t "*"))) ; inside heading, oddeven
  19850. (rpl (concat stars add-stars " "))
  19851. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19852. (while (< (point) (if (equal nstars '(4)) lend end))
  19853. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19854. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19855. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19856. (forward-line)))))))
  19857. (unless toggled (message "Cannot toggle heading from here"))))
  19858. (defun org-meta-return (&optional _arg)
  19859. "Insert a new heading or wrap a region in a table.
  19860. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19861. on context. See the individual commands for more information."
  19862. (interactive)
  19863. (org-check-before-invisible-edit 'insert)
  19864. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19865. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19866. #'org-insert-heading))))
  19867. ;;; Menu entries
  19868. (defsubst org-in-subtree-not-table-p ()
  19869. "Are we in a subtree and not in a table?"
  19870. (and (not (org-before-first-heading-p))
  19871. (not (org-at-table-p))))
  19872. ;; Define the Org-mode menus
  19873. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19874. '("Tbl"
  19875. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19876. ["Next Field" org-cycle (org-at-table-p)]
  19877. ["Previous Field" org-shifttab (org-at-table-p)]
  19878. ["Next Row" org-return (org-at-table-p)]
  19879. "--"
  19880. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19881. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19882. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19883. "--"
  19884. ("Column"
  19885. ["Move Column Left" org-metaleft (org-at-table-p)]
  19886. ["Move Column Right" org-metaright (org-at-table-p)]
  19887. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19888. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19889. ("Row"
  19890. ["Move Row Up" org-metaup (org-at-table-p)]
  19891. ["Move Row Down" org-metadown (org-at-table-p)]
  19892. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19893. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19894. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19895. "--"
  19896. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19897. ("Rectangle"
  19898. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19899. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19900. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19901. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19902. "--"
  19903. ("Calculate"
  19904. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19905. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19906. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19907. "--"
  19908. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19909. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19910. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19911. "--"
  19912. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19913. "--"
  19914. ["Sum Column/Rectangle" org-table-sum
  19915. (or (org-at-table-p) (org-region-active-p))]
  19916. ["Which Column?" org-table-current-column (org-at-table-p)])
  19917. ["Debug Formulas"
  19918. org-table-toggle-formula-debugger
  19919. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19920. ["Show Col/Row Numbers"
  19921. org-table-toggle-coordinate-overlays
  19922. :style toggle
  19923. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19924. "--"
  19925. ["Create" org-table-create (and (not (org-at-table-p))
  19926. org-enable-table-editor)]
  19927. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19928. ["Import from File" org-table-import (not (org-at-table-p))]
  19929. ["Export to File" org-table-export (org-at-table-p)]
  19930. "--"
  19931. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19932. "--"
  19933. ("Plot"
  19934. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19935. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19936. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19937. '("Org"
  19938. ("Show/Hide"
  19939. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19940. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19941. ["Sparse Tree..." org-sparse-tree t]
  19942. ["Reveal Context" org-reveal t]
  19943. ["Show All" show-all t]
  19944. "--"
  19945. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19946. "--"
  19947. ["New Heading" org-insert-heading t]
  19948. ("Navigate Headings"
  19949. ["Up" outline-up-heading t]
  19950. ["Next" outline-next-visible-heading t]
  19951. ["Previous" outline-previous-visible-heading t]
  19952. ["Next Same Level" outline-forward-same-level t]
  19953. ["Previous Same Level" outline-backward-same-level t]
  19954. "--"
  19955. ["Jump" org-goto t])
  19956. ("Edit Structure"
  19957. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19958. "--"
  19959. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19960. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19961. "--"
  19962. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19963. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19964. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19965. "--"
  19966. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19967. "--"
  19968. ["Copy visible text" org-copy-visible t]
  19969. "--"
  19970. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19971. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19972. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19973. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19974. "--"
  19975. ["Sort Region/Children" org-sort t]
  19976. "--"
  19977. ["Convert to odd levels" org-convert-to-odd-levels t]
  19978. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19979. ("Editing"
  19980. ["Emphasis..." org-emphasize t]
  19981. ["Edit Source Example" org-edit-special t]
  19982. "--"
  19983. ["Footnote new/jump" org-footnote-action t]
  19984. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19985. ("Archive"
  19986. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19987. "--"
  19988. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19989. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19990. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19991. )
  19992. "--"
  19993. ("Hyperlinks"
  19994. ["Store Link (Global)" org-store-link t]
  19995. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19996. ["Insert Link" org-insert-link t]
  19997. ["Follow Link" org-open-at-point t]
  19998. "--"
  19999. ["Next link" org-next-link t]
  20000. ["Previous link" org-previous-link t]
  20001. "--"
  20002. ["Descriptive Links"
  20003. org-toggle-link-display
  20004. :style radio
  20005. :selected org-descriptive-links
  20006. ]
  20007. ["Literal Links"
  20008. org-toggle-link-display
  20009. :style radio
  20010. :selected (not org-descriptive-links)])
  20011. "--"
  20012. ("TODO Lists"
  20013. ["TODO/DONE/-" org-todo t]
  20014. ("Select keyword"
  20015. ["Next keyword" org-shiftright (org-at-heading-p)]
  20016. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  20017. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20018. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20019. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20020. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20021. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20022. "--"
  20023. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20024. :selected org-enforce-todo-dependencies :style toggle :active t]
  20025. "Settings for tree at point"
  20026. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20027. :selected (org-entry-get nil "ORDERED")
  20028. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20029. ["Do Children parallel" org-toggle-ordered-property :style radio
  20030. :selected (not (org-entry-get nil "ORDERED"))
  20031. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20032. "--"
  20033. ["Set Priority" org-priority t]
  20034. ["Priority Up" org-shiftup t]
  20035. ["Priority Down" org-shiftdown t]
  20036. "--"
  20037. ["Get news from all feeds" org-feed-update-all t]
  20038. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20039. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20040. ("TAGS and Properties"
  20041. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20042. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20043. "--"
  20044. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20045. ["Column view of properties" org-columns t]
  20046. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20047. ("Dates and Scheduling"
  20048. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20049. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20050. ("Change Date"
  20051. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20052. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20053. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20054. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20055. ["Compute Time Range" org-evaluate-time-range t]
  20056. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20057. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20058. "--"
  20059. ["Custom time format" org-toggle-time-stamp-overlays
  20060. :style radio :selected org-display-custom-times]
  20061. "--"
  20062. ["Goto Calendar" org-goto-calendar t]
  20063. ["Date from Calendar" org-date-from-calendar t]
  20064. "--"
  20065. ["Start/Restart Timer" org-timer-start t]
  20066. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20067. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20068. ["Insert Timer String" org-timer t]
  20069. ["Insert Timer Item" org-timer-item t])
  20070. ("Logging work"
  20071. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20072. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20073. ["Clock out" org-clock-out t]
  20074. ["Clock cancel" org-clock-cancel t]
  20075. "--"
  20076. ["Mark as default task" org-clock-mark-default-task t]
  20077. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20078. ["Goto running clock" org-clock-goto t]
  20079. "--"
  20080. ["Display times" org-clock-display t]
  20081. ["Create clock table" org-clock-report t]
  20082. "--"
  20083. ["Record DONE time"
  20084. (progn (setq org-log-done (not org-log-done))
  20085. (message "Switching to %s will %s record a timestamp"
  20086. (car org-done-keywords)
  20087. (if org-log-done "automatically" "not")))
  20088. :style toggle :selected org-log-done])
  20089. "--"
  20090. ["Agenda Command..." org-agenda t]
  20091. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20092. ("File List for Agenda")
  20093. ("Special views current file"
  20094. ["TODO Tree" org-show-todo-tree t]
  20095. ["Check Deadlines" org-check-deadlines t]
  20096. ["Timeline" org-timeline t]
  20097. ["Tags/Property tree" org-match-sparse-tree t])
  20098. "--"
  20099. ["Export/Publish..." org-export-dispatch t]
  20100. ("LaTeX"
  20101. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20102. :selected org-cdlatex-mode]
  20103. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20104. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20105. ["Modify math symbol" org-cdlatex-math-modify
  20106. (org-inside-LaTeX-fragment-p)]
  20107. ["Insert citation" org-reftex-citation t]
  20108. "--"
  20109. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20110. "--"
  20111. ("MobileOrg"
  20112. ["Push Files and Views" org-mobile-push t]
  20113. ["Get Captured and Flagged" org-mobile-pull t]
  20114. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20115. "--"
  20116. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20117. "--"
  20118. ("Documentation"
  20119. ["Show Version" org-version t]
  20120. ["Info Documentation" org-info t])
  20121. ("Customize"
  20122. ["Browse Org Group" org-customize t]
  20123. "--"
  20124. ["Expand This Menu" org-create-customize-menu
  20125. (fboundp 'customize-menu-create)])
  20126. ["Send bug report" org-submit-bug-report t]
  20127. "--"
  20128. ("Refresh/Reload"
  20129. ["Refresh setup current buffer" org-mode-restart t]
  20130. ["Reload Org (after update)" org-reload t]
  20131. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20132. ))
  20133. (defun org-info (&optional node)
  20134. "Read documentation for Org-mode in the info system.
  20135. With optional NODE, go directly to that node."
  20136. (interactive)
  20137. (info (format "(org)%s" (or node ""))))
  20138. ;;;###autoload
  20139. (defun org-submit-bug-report ()
  20140. "Submit a bug report on Org-mode via mail.
  20141. Don't hesitate to report any problems or inaccurate documentation.
  20142. If you don't have setup sending mail from (X)Emacs, please copy the
  20143. output buffer into your mail program, as it gives us important
  20144. information about your Org-mode version and configuration."
  20145. (interactive)
  20146. (require 'reporter)
  20147. (defvar reporter-prompt-for-summary-p)
  20148. (org-load-modules-maybe)
  20149. (org-require-autoloaded-modules)
  20150. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20151. (reporter-submit-bug-report
  20152. "emacs-orgmode@gnu.org"
  20153. (org-version nil 'full)
  20154. (let (list)
  20155. (save-window-excursion
  20156. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20157. (delete-other-windows)
  20158. (erase-buffer)
  20159. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20160. We would like to add your full Org-mode and Outline configuration to the
  20161. bug report. This greatly simplifies the work of the maintainer and
  20162. other experts on the mailing list.
  20163. HOWEVER, some variables you have customized may contain private
  20164. information. The names of customers, colleagues, or friends, might
  20165. appear in the form of file names, tags, todo states, or search strings.
  20166. If you answer yes to the prompt, you might want to check and remove
  20167. such private information before sending the email.")
  20168. (add-text-properties (point-min) (point-max) '(face org-warning))
  20169. (when (yes-or-no-p "Include your Org-mode configuration ")
  20170. (mapatoms
  20171. (lambda (v)
  20172. (and (boundp v)
  20173. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20174. (or (and (symbol-value v)
  20175. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20176. (and
  20177. (get v 'custom-type) (get v 'standard-value)
  20178. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20179. (push v list)))))
  20180. (kill-buffer (get-buffer "*Warn about privacy*"))
  20181. list))
  20182. nil nil
  20183. "Remember to cover the basics, that is, what you expected to happen and
  20184. what in fact did happen. You don't know how to make a good report? See
  20185. http://orgmode.org/manual/Feedback.html#Feedback
  20186. Your bug report will be posted to the Org-mode mailing list.
  20187. ------------------------------------------------------------------------")
  20188. (save-excursion
  20189. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20190. (replace-match "\\1Bug: \\3 [\\2]")))))
  20191. (defun org-install-agenda-files-menu ()
  20192. (let ((bl (buffer-list)))
  20193. (save-excursion
  20194. (while bl
  20195. (set-buffer (pop bl))
  20196. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20197. (when (derived-mode-p 'org-mode)
  20198. (easy-menu-change
  20199. '("Org") "File List for Agenda"
  20200. (append
  20201. (list
  20202. ["Edit File List" (org-edit-agenda-file-list) t]
  20203. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20204. ["Remove Current File from List" org-remove-file t]
  20205. ["Cycle through agenda files" org-cycle-agenda-files t]
  20206. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20207. "--")
  20208. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20209. ;;;; Documentation
  20210. (defun org-require-autoloaded-modules ()
  20211. (interactive)
  20212. (mapc 'require
  20213. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20214. org-table org-timer)))
  20215. ;;;###autoload
  20216. (defun org-reload (&optional uncompiled)
  20217. "Reload all org lisp files.
  20218. With prefix arg UNCOMPILED, load the uncompiled versions."
  20219. (interactive "P")
  20220. (require 'loadhist)
  20221. (let* ((org-dir (org-find-library-dir "org"))
  20222. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20223. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20224. (remove-re (mapconcat 'identity
  20225. (mapcar (lambda (f) (concat "^" f "$"))
  20226. (list (if (featurep 'xemacs)
  20227. "org-colview"
  20228. "org-colview-xemacs")
  20229. "org" "org-loaddefs" "org-version"))
  20230. "\\|"))
  20231. (feats (delete-dups
  20232. (mapcar 'file-name-sans-extension
  20233. (mapcar 'file-name-nondirectory
  20234. (delq nil
  20235. (mapcar 'feature-file
  20236. features))))))
  20237. (lfeat (append
  20238. (sort
  20239. (setq feats
  20240. (delq nil (mapcar
  20241. (lambda (f)
  20242. (if (and (string-match feature-re f)
  20243. (not (string-match remove-re f)))
  20244. f nil))
  20245. feats)))
  20246. 'string-lessp)
  20247. (list "org-version" "org")))
  20248. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20249. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20250. load-uncore load-misses)
  20251. (setq load-misses
  20252. (delq 't
  20253. (mapcar (lambda (f)
  20254. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20255. (and (string= org-dir contrib-dir)
  20256. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20257. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20258. (add-to-list 'load-uncore f 'append)
  20259. 't)
  20260. f))
  20261. lfeat)))
  20262. (if load-uncore
  20263. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20264. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20265. (if load-misses
  20266. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20267. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20268. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20269. ;;;###autoload
  20270. (defun org-customize ()
  20271. "Call the customize function with org as argument."
  20272. (interactive)
  20273. (org-load-modules-maybe)
  20274. (org-require-autoloaded-modules)
  20275. (customize-browse 'org))
  20276. (defun org-create-customize-menu ()
  20277. "Create a full customization menu for Org-mode, insert it into the menu."
  20278. (interactive)
  20279. (org-load-modules-maybe)
  20280. (org-require-autoloaded-modules)
  20281. (if (fboundp 'customize-menu-create)
  20282. (progn
  20283. (easy-menu-change
  20284. '("Org") "Customize"
  20285. `(["Browse Org group" org-customize t]
  20286. "--"
  20287. ,(customize-menu-create 'org)
  20288. ["Set" Custom-set t]
  20289. ["Save" Custom-save t]
  20290. ["Reset to Current" Custom-reset-current t]
  20291. ["Reset to Saved" Custom-reset-saved t]
  20292. ["Reset to Standard Settings" Custom-reset-standard t]))
  20293. (message "\"Org\"-menu now contains full customization menu"))
  20294. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20295. ;;;; Miscellaneous stuff
  20296. ;;; Generally useful functions
  20297. (defsubst org-get-at-eol (property n)
  20298. "Get text property PROPERTY at the end of line less N characters."
  20299. (get-text-property (- (point-at-eol) n) property))
  20300. (defun org-find-text-property-in-string (prop s)
  20301. "Return the first non-nil value of property PROP in string S."
  20302. (or (get-text-property 0 prop s)
  20303. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20304. prop s)))
  20305. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20306. "Display the given MESSAGE as a warning."
  20307. (if (fboundp 'display-warning)
  20308. (display-warning 'org message
  20309. (if (featurep 'xemacs) 'warning :warning))
  20310. (let ((buf (get-buffer-create "*Org warnings*")))
  20311. (with-current-buffer buf
  20312. (goto-char (point-max))
  20313. (insert "Warning (Org): " message)
  20314. (unless (bolp)
  20315. (newline)))
  20316. (display-buffer buf)
  20317. (sit-for 0))))
  20318. (defun org-eval (form)
  20319. "Eval FORM and return result."
  20320. (condition-case error
  20321. (eval form)
  20322. (error (format "%%![Error: %s]" error))))
  20323. (defun org-in-clocktable-p ()
  20324. "Check if the cursor is in a clocktable."
  20325. (let ((pos (point)) start)
  20326. (save-excursion
  20327. (end-of-line 1)
  20328. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20329. (setq start (match-beginning 0))
  20330. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20331. (>= (match-end 0) pos)
  20332. start))))
  20333. (defun org-in-verbatim-emphasis ()
  20334. (save-match-data
  20335. (and (org-in-regexp org-emph-re 2)
  20336. (>= (point) (match-beginning 3))
  20337. (<= (point) (match-end 4))
  20338. (member (match-string 3) '("=" "~")))))
  20339. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20340. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20341. (if (and marker (marker-buffer marker)
  20342. (buffer-live-p (marker-buffer marker)))
  20343. (progn
  20344. (org-pop-to-buffer-same-window (marker-buffer marker))
  20345. (if (or (> marker (point-max)) (< marker (point-min)))
  20346. (widen))
  20347. (goto-char marker)
  20348. (org-show-context 'org-goto))
  20349. (if bookmark
  20350. (bookmark-jump bookmark)
  20351. (error "Cannot find location"))))
  20352. (defun org-quote-csv-field (s)
  20353. "Quote field for inclusion in CSV material."
  20354. (if (string-match "[\",]" s)
  20355. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20356. s))
  20357. (defun org-force-self-insert (N)
  20358. "Needed to enforce self-insert under remapping."
  20359. (interactive "p")
  20360. (self-insert-command N))
  20361. (defun org-string-width (s)
  20362. "Compute width of string, ignoring invisible characters.
  20363. This ignores character with invisibility property `org-link', and also
  20364. characters with property `org-cwidth', because these will become invisible
  20365. upon the next fontification round."
  20366. (let (b l)
  20367. (when (or (eq t buffer-invisibility-spec)
  20368. (assq 'org-link buffer-invisibility-spec))
  20369. (while (setq b (text-property-any 0 (length s)
  20370. 'invisible 'org-link s))
  20371. (setq s (concat (substring s 0 b)
  20372. (substring s (or (next-single-property-change
  20373. b 'invisible s)
  20374. (length s)))))))
  20375. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20376. (setq s (concat (substring s 0 b)
  20377. (substring s (or (next-single-property-change
  20378. b 'org-cwidth s)
  20379. (length s))))))
  20380. (setq l (string-width s) b -1)
  20381. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20382. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20383. l))
  20384. (defun org-shorten-string (s maxlength)
  20385. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20386. If the string is shorter or has length MAXLENGTH, just return the
  20387. original string. If it is longer, the functions finds a space in the
  20388. string, breaks this string off at that locations and adds three dots
  20389. as ellipsis. Including the ellipsis, the string will not be longer
  20390. than MAXLENGTH. If finding a good breaking point in the string does
  20391. not work, the string is just chopped off in the middle of a word
  20392. if necessary."
  20393. (if (<= (length s) maxlength)
  20394. s
  20395. (let* ((n (max (- maxlength 4) 1))
  20396. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20397. (if (string-match re s)
  20398. (concat (match-string 1 s) "...")
  20399. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20400. (defun org-get-indentation (&optional line)
  20401. "Get the indentation of the current line, interpreting tabs.
  20402. When LINE is given, assume it represents a line and compute its indentation."
  20403. (if line
  20404. (if (string-match "^ *" (org-remove-tabs line))
  20405. (match-end 0))
  20406. (save-excursion
  20407. (beginning-of-line 1)
  20408. (skip-chars-forward " \t")
  20409. (current-column))))
  20410. (defun org-get-string-indentation (s)
  20411. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20412. (let ((n -1) (i 0) (w tab-width) c)
  20413. (catch 'exit
  20414. (while (< (setq n (1+ n)) (length s))
  20415. (setq c (aref s n))
  20416. (cond ((= c ?\ ) (setq i (1+ i)))
  20417. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20418. (t (throw 'exit t)))))
  20419. i))
  20420. (defun org-remove-tabs (s &optional width)
  20421. "Replace tabulators in S with spaces.
  20422. Assumes that s is a single line, starting in column 0."
  20423. (setq width (or width tab-width))
  20424. (while (string-match "\t" s)
  20425. (setq s (replace-match
  20426. (make-string
  20427. (- (* width (/ (+ (match-beginning 0) width) width))
  20428. (match-beginning 0)) ?\ )
  20429. t t s)))
  20430. s)
  20431. (defun org-fix-indentation (line ind)
  20432. "Fix indentation in LINE.
  20433. IND is a cons cell with target and minimum indentation.
  20434. If the current indentation in LINE is smaller than the minimum,
  20435. leave it alone. If it is larger than ind, set it to the target."
  20436. (let* ((l (org-remove-tabs line))
  20437. (i (org-get-indentation l))
  20438. (i1 (car ind)) (i2 (cdr ind)))
  20439. (if (>= i i2) (setq l (substring line i2)))
  20440. (if (> i1 0)
  20441. (concat (make-string i1 ?\ ) l)
  20442. l)))
  20443. (defun org-remove-indentation (code &optional n)
  20444. "Remove the maximum common indentation from the lines in CODE.
  20445. N may optionally be the number of spaces to remove."
  20446. (with-temp-buffer
  20447. (insert code)
  20448. (org-do-remove-indentation n)
  20449. (buffer-string)))
  20450. (defun org-do-remove-indentation (&optional n)
  20451. "Remove the maximum common indentation from the buffer."
  20452. (untabify (point-min) (point-max))
  20453. (let ((min 10000) re)
  20454. (if n
  20455. (setq min n)
  20456. (goto-char (point-min))
  20457. (while (re-search-forward "^ *[^ \n]" nil t)
  20458. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20459. (unless (or (= min 0) (= min 10000))
  20460. (setq re (format "^ \\{%d\\}" min))
  20461. (goto-char (point-min))
  20462. (while (re-search-forward re nil t)
  20463. (replace-match "")
  20464. (end-of-line 1))
  20465. min)))
  20466. (defun org-fill-template (template alist)
  20467. "Find each %key of ALIST in TEMPLATE and replace it."
  20468. (let ((case-fold-search nil))
  20469. (dolist (entry (sort (copy-sequence alist)
  20470. (lambda (a b) (< (length (car a)) (length (car b))))))
  20471. (setq template
  20472. (replace-regexp-in-string
  20473. (concat "%" (regexp-quote (car entry)))
  20474. (or (cdr entry) "") template t t)))
  20475. template))
  20476. (defun org-base-buffer (buffer)
  20477. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20478. (if (not buffer)
  20479. buffer
  20480. (or (buffer-base-buffer buffer)
  20481. buffer)))
  20482. (defun org-wrap (string &optional width lines)
  20483. "Wrap string to either a number of lines, or a width in characters.
  20484. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20485. that costs. If there is a word longer than WIDTH, the text is actually
  20486. wrapped to the length of that word.
  20487. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20488. many lines, whatever width that takes.
  20489. The return value is a list of lines, without newlines at the end."
  20490. (let* ((words (org-split-string string "[ \t\n]+"))
  20491. (maxword (apply 'max (mapcar 'org-string-width words)))
  20492. w ll)
  20493. (cond (width
  20494. (org-do-wrap words (max maxword width)))
  20495. (lines
  20496. (setq w maxword)
  20497. (setq ll (org-do-wrap words maxword))
  20498. (if (<= (length ll) lines)
  20499. ll
  20500. (setq ll words)
  20501. (while (> (length ll) lines)
  20502. (setq w (1+ w))
  20503. (setq ll (org-do-wrap words w)))
  20504. ll))
  20505. (t (error "Cannot wrap this")))))
  20506. (defun org-do-wrap (words width)
  20507. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20508. (let (lines line)
  20509. (while words
  20510. (setq line (pop words))
  20511. (while (and words (< (+ (length line) (length (car words))) width))
  20512. (setq line (concat line " " (pop words))))
  20513. (setq lines (push line lines)))
  20514. (nreverse lines)))
  20515. (defun org-split-string (string &optional separators)
  20516. "Splits STRING into substrings at SEPARATORS.
  20517. SEPARATORS is a regular expression.
  20518. No empty strings are returned if there are matches at the beginning
  20519. and end of string."
  20520. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20521. (let ((start 0) notfirst list)
  20522. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20523. (if (and notfirst
  20524. (= start (match-beginning 0))
  20525. (< start (length string)))
  20526. (1+ start) start))
  20527. (< (match-beginning 0) (length string)))
  20528. (setq notfirst t)
  20529. (or (eq (match-beginning 0) 0)
  20530. (and (eq (match-beginning 0) (match-end 0))
  20531. (eq (match-beginning 0) start))
  20532. (push (substring string start (match-beginning 0)) list))
  20533. (setq start (match-end 0)))
  20534. (or (eq start (length string))
  20535. (push (substring string start) list))
  20536. (nreverse list)))
  20537. (defun org-quote-vert (s)
  20538. "Replace \"|\" with \"\\vert\"."
  20539. (while (string-match "|" s)
  20540. (setq s (replace-match "\\vert" t t s)))
  20541. s)
  20542. (defun org-uuidgen-p (s)
  20543. "Is S an ID created by UUIDGEN?"
  20544. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20545. (defun org-in-src-block-p (&optional inside)
  20546. "Whether point is in a code source block.
  20547. When INSIDE is non-nil, don't consider we are within a src block
  20548. when point is at #+BEGIN_SRC or #+END_SRC."
  20549. (let ((case-fold-search t) ov)
  20550. (or (and (eq (get-char-property (point) 'src-block) t))
  20551. (and (not inside)
  20552. (save-match-data
  20553. (save-excursion
  20554. (beginning-of-line)
  20555. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20556. (defun org-context ()
  20557. "Return a list of contexts of the current cursor position.
  20558. If several contexts apply, all are returned.
  20559. Each context entry is a list with a symbol naming the context, and
  20560. two positions indicating start and end of the context. Possible
  20561. contexts are:
  20562. :headline anywhere in a headline
  20563. :headline-stars on the leading stars in a headline
  20564. :todo-keyword on a TODO keyword (including DONE) in a headline
  20565. :tags on the TAGS in a headline
  20566. :priority on the priority cookie in a headline
  20567. :item on the first line of a plain list item
  20568. :item-bullet on the bullet/number of a plain list item
  20569. :checkbox on the checkbox in a plain list item
  20570. :table in an org-mode table
  20571. :table-special on a special filed in a table
  20572. :table-table in a table.el table
  20573. :clocktable in a clocktable
  20574. :src-block in a source block
  20575. :link on a hyperlink
  20576. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20577. :target on a <<target>>
  20578. :radio-target on a <<<radio-target>>>
  20579. :latex-fragment on a LaTeX fragment
  20580. :latex-preview on a LaTeX fragment with overlaid preview image
  20581. This function expects the position to be visible because it uses font-lock
  20582. faces as a help to recognize the following contexts: :table-special, :link,
  20583. and :keyword."
  20584. (let* ((f (get-text-property (point) 'face))
  20585. (faces (if (listp f) f (list f)))
  20586. (case-fold-search t)
  20587. (p (point)) clist o)
  20588. ;; First the large context
  20589. (cond
  20590. ((org-at-heading-p t)
  20591. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20592. (when (progn
  20593. (beginning-of-line 1)
  20594. (looking-at org-todo-line-tags-regexp))
  20595. (push (org-point-in-group p 1 :headline-stars) clist)
  20596. (push (org-point-in-group p 2 :todo-keyword) clist)
  20597. (push (org-point-in-group p 4 :tags) clist))
  20598. (goto-char p)
  20599. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20600. (if (looking-at "\\[#[A-Z0-9]\\]")
  20601. (push (org-point-in-group p 0 :priority) clist)))
  20602. ((org-at-item-p)
  20603. (push (org-point-in-group p 2 :item-bullet) clist)
  20604. (push (list :item (point-at-bol)
  20605. (save-excursion (org-end-of-item) (point)))
  20606. clist)
  20607. (and (org-at-item-checkbox-p)
  20608. (push (org-point-in-group p 0 :checkbox) clist)))
  20609. ((org-at-table-p)
  20610. (push (list :table (org-table-begin) (org-table-end)) clist)
  20611. (if (memq 'org-formula faces)
  20612. (push (list :table-special
  20613. (previous-single-property-change p 'face)
  20614. (next-single-property-change p 'face)) clist)))
  20615. ((org-at-table-p 'any)
  20616. (push (list :table-table) clist)))
  20617. (goto-char p)
  20618. (let ((case-fold-search t))
  20619. ;; New the "medium" contexts: clocktables, source blocks
  20620. (cond ((org-in-clocktable-p)
  20621. (push (list :clocktable
  20622. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20623. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20624. (match-beginning 1))
  20625. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20626. (match-end 0))) clist))
  20627. ((org-in-src-block-p)
  20628. (push (list :src-block
  20629. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20630. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20631. (match-beginning 1))
  20632. (and (search-forward "#+END_SRC" nil t)
  20633. (match-beginning 0))) clist))))
  20634. (goto-char p)
  20635. ;; Now the small context
  20636. (cond
  20637. ((org-at-timestamp-p)
  20638. (push (org-point-in-group p 0 :timestamp) clist))
  20639. ((memq 'org-link faces)
  20640. (push (list :link
  20641. (previous-single-property-change p 'face)
  20642. (next-single-property-change p 'face)) clist))
  20643. ((memq 'org-special-keyword faces)
  20644. (push (list :keyword
  20645. (previous-single-property-change p 'face)
  20646. (next-single-property-change p 'face)) clist))
  20647. ((org-at-target-p)
  20648. (push (org-point-in-group p 0 :target) clist)
  20649. (goto-char (1- (match-beginning 0)))
  20650. (if (looking-at org-radio-target-regexp)
  20651. (push (org-point-in-group p 0 :radio-target) clist))
  20652. (goto-char p))
  20653. ((setq o (car (delq nil
  20654. (mapcar
  20655. (lambda (x)
  20656. (if (memq x org-latex-fragment-image-overlays) x))
  20657. (overlays-at (point))))))
  20658. (push (list :latex-fragment
  20659. (overlay-start o) (overlay-end o)) clist)
  20660. (push (list :latex-preview
  20661. (overlay-start o) (overlay-end o)) clist))
  20662. ((org-inside-LaTeX-fragment-p)
  20663. ;; FIXME: positions wrong.
  20664. (push (list :latex-fragment (point) (point)) clist)))
  20665. (setq clist (nreverse (delq nil clist)))
  20666. clist))
  20667. (defun org-in-regexp (re &optional nlines visually)
  20668. "Check if point is inside a match of RE.
  20669. Normally only the current line is checked, but you can include
  20670. NLINES extra lines after point into the search. If VISUALLY is
  20671. set, require that the cursor is not after the match but really
  20672. on, so that the block visually is on the match."
  20673. (catch 'exit
  20674. (let ((pos (point))
  20675. (eol (point-at-eol (+ 1 (or nlines 0))))
  20676. (inc (if visually 1 0)))
  20677. (save-excursion
  20678. (beginning-of-line (- 1 (or nlines 0)))
  20679. (while (re-search-forward re eol t)
  20680. (if (and (<= (match-beginning 0) pos)
  20681. (>= (+ inc (match-end 0)) pos))
  20682. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20683. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20684. "Org mode 8.3")
  20685. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20686. "Non-nil when point is between matches of START-RE and END-RE.
  20687. Also return a non-nil value when point is on one of the matches.
  20688. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20689. buffer positions. Default values are the positions of headlines
  20690. surrounding the point.
  20691. The functions returns a cons cell whose car (resp. cdr) is the
  20692. position before START-RE (resp. after END-RE)."
  20693. (save-match-data
  20694. (let ((pos (point))
  20695. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20696. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20697. beg end)
  20698. (save-excursion
  20699. ;; Point is on a block when on START-RE or if START-RE can be
  20700. ;; found before it...
  20701. (and (or (org-in-regexp start-re)
  20702. (re-search-backward start-re limit-up t))
  20703. (setq beg (match-beginning 0))
  20704. ;; ... and END-RE after it...
  20705. (goto-char (match-end 0))
  20706. (re-search-forward end-re limit-down t)
  20707. (> (setq end (match-end 0)) pos)
  20708. ;; ... without another START-RE in-between.
  20709. (goto-char (match-beginning 0))
  20710. (not (re-search-backward start-re (1+ beg) t))
  20711. ;; Return value.
  20712. (cons beg end))))))
  20713. (defun org-in-block-p (names)
  20714. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20715. NAMES is a list of strings containing names of blocks.
  20716. Return first block name matched, or nil. Beware that in case of
  20717. nested blocks, the returned name may not belong to the closest
  20718. block from point."
  20719. (save-match-data
  20720. (catch 'exit
  20721. (let ((case-fold-search t)
  20722. (lim-up (save-excursion (outline-previous-heading)))
  20723. (lim-down (save-excursion (outline-next-heading))))
  20724. (mapc (lambda (name)
  20725. (let ((n (regexp-quote name)))
  20726. (when (org-between-regexps-p
  20727. (concat "^[ \t]*#\\+begin_" n)
  20728. (concat "^[ \t]*#\\+end_" n)
  20729. lim-up lim-down)
  20730. (throw 'exit n))))
  20731. names))
  20732. nil)))
  20733. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20734. "Call `multi-occur' with buffers for all agenda files."
  20735. (interactive "sOrg-files matching: ")
  20736. (let* ((files (org-agenda-files))
  20737. (tnames (mapcar #'file-truename files))
  20738. (extra org-agenda-text-search-extra-files))
  20739. (when (eq (car extra) 'agenda-archives)
  20740. (setq extra (cdr extra))
  20741. (setq files (org-add-archive-files files)))
  20742. (dolist (f extra)
  20743. (unless (member (file-truename f) tnames)
  20744. (unless (member f files) (setq files (append files (list f))))
  20745. (setq tnames (append tnames (list (file-truename f))))))
  20746. (multi-occur
  20747. (mapcar (lambda (x)
  20748. (with-current-buffer
  20749. ;; FIXME: Why not just (find-file-noselect x)?
  20750. ;; Is it to avoid the "revert buffer" prompt?
  20751. (or (get-file-buffer x) (find-file-noselect x))
  20752. (widen)
  20753. (current-buffer)))
  20754. files)
  20755. regexp)))
  20756. (if (boundp 'occur-mode-find-occurrence-hook)
  20757. ;; Emacs 23
  20758. (add-hook 'occur-mode-find-occurrence-hook
  20759. (lambda ()
  20760. (when (derived-mode-p 'org-mode)
  20761. (org-reveal))))
  20762. ;; Emacs 22
  20763. (defadvice occur-mode-goto-occurrence
  20764. (after org-occur-reveal activate)
  20765. (and (derived-mode-p 'org-mode) (org-reveal)))
  20766. (defadvice occur-mode-goto-occurrence-other-window
  20767. (after org-occur-reveal activate)
  20768. (and (derived-mode-p 'org-mode) (org-reveal)))
  20769. (defadvice occur-mode-display-occurrence
  20770. (after org-occur-reveal activate)
  20771. (when (derived-mode-p 'org-mode)
  20772. (let ((pos (occur-mode-find-occurrence)))
  20773. (with-current-buffer (marker-buffer pos)
  20774. (save-excursion
  20775. (goto-char pos)
  20776. (org-reveal)))))))
  20777. (defun org-occur-link-in-agenda-files ()
  20778. "Create a link and search for it in the agendas.
  20779. The link is not stored in `org-stored-links', it is just created
  20780. for the search purpose."
  20781. (interactive)
  20782. (let ((link (condition-case nil
  20783. (org-store-link nil)
  20784. (error "Unable to create a link to here"))))
  20785. (org-occur-in-agenda-files (regexp-quote link))))
  20786. (defun org-reverse-string (string)
  20787. "Return the reverse of STRING."
  20788. (apply 'string (reverse (string-to-list string))))
  20789. ;; defsubst org-uniquify must be defined before first use
  20790. (defun org-uniquify-alist (alist)
  20791. "Merge elements of ALIST with the same key.
  20792. For example, in this alist:
  20793. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20794. => \\='((a 1 3) (b 2))
  20795. merge (a 1) and (a 3) into (a 1 3).
  20796. The function returns the new ALIST."
  20797. (let (rtn)
  20798. (mapc
  20799. (lambda (e)
  20800. (let (n)
  20801. (if (not (assoc (car e) rtn))
  20802. (push e rtn)
  20803. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20804. (setq rtn (assq-delete-all (car e) rtn))
  20805. (push n rtn))))
  20806. alist)
  20807. rtn))
  20808. (defun org-delete-all (elts list)
  20809. "Remove all elements in ELTS from LIST."
  20810. (while elts
  20811. (setq list (delete (pop elts) list)))
  20812. list)
  20813. (defun org-count (cl-item cl-seq)
  20814. "Count the number of occurrences of ITEM in SEQ.
  20815. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20816. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20817. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20818. (while (< cl-start cl-end)
  20819. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20820. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20821. (setq cl-start (1+ cl-start)))
  20822. cl-count))
  20823. (defun org-remove-if (predicate seq)
  20824. "Remove everything from SEQ that fulfills PREDICATE."
  20825. (let (res e)
  20826. (while seq
  20827. (setq e (pop seq))
  20828. (if (not (funcall predicate e)) (push e res)))
  20829. (nreverse res)))
  20830. (defun org-remove-if-not (predicate seq)
  20831. "Remove everything from SEQ that does not fulfill PREDICATE."
  20832. (let (res e)
  20833. (while seq
  20834. (setq e (pop seq))
  20835. (if (funcall predicate e) (push e res)))
  20836. (nreverse res)))
  20837. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20838. "Reduce two-argument FUNCTION across SEQ.
  20839. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20840. \":initial-value\" removed."
  20841. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20842. (cadr (memq :initial-value cl-keys)))
  20843. (cl-seq (pop cl-seq))
  20844. (t (funcall cl-func)))))
  20845. (while cl-seq
  20846. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20847. cl-accum))
  20848. (defun org-every (pred seq)
  20849. "Return true if PREDICATE is true of every element of SEQ.
  20850. Adapted from `every' in cl.el."
  20851. (catch 'org-every
  20852. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20853. t))
  20854. (defun org-some (pred seq)
  20855. "Return true if PREDICATE is true of any element of SEQ.
  20856. Adapted from `some' in cl.el."
  20857. (catch 'org-some
  20858. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20859. nil))
  20860. (defun org-back-over-empty-lines ()
  20861. "Move backwards over whitespace, to the beginning of the first empty line.
  20862. Returns the number of empty lines passed."
  20863. (let ((pos (point)))
  20864. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20865. (skip-chars-backward " \t\n\r")
  20866. (unless (eobp)
  20867. (forward-line -1)))
  20868. (beginning-of-line 2)
  20869. (goto-char (min (point) pos))
  20870. (count-lines (point) pos)))
  20871. (defun org-skip-whitespace ()
  20872. (skip-chars-forward " \t\n\r"))
  20873. (defun org-point-in-group (point group &optional context)
  20874. "Check if POINT is in match-group GROUP.
  20875. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20876. match. If the match group does not exist or point is not inside it,
  20877. return nil."
  20878. (and (match-beginning group)
  20879. (>= point (match-beginning group))
  20880. (<= point (match-end group))
  20881. (if context
  20882. (list context (match-beginning group) (match-end group))
  20883. t)))
  20884. (defun org-switch-to-buffer-other-window (&rest args)
  20885. "Switch to buffer in a second window on the current frame.
  20886. In particular, do not allow pop-up frames.
  20887. Returns the newly created buffer."
  20888. (org-no-popups
  20889. (apply 'switch-to-buffer-other-window args)))
  20890. (defun org-combine-plists (&rest plists)
  20891. "Create a single property list from all plists in PLISTS.
  20892. The process starts by copying the first list, and then setting properties
  20893. from the other lists. Settings in the last list are the most significant
  20894. ones and overrule settings in the other lists."
  20895. (let ((rtn (copy-sequence (pop plists)))
  20896. p v ls)
  20897. (while plists
  20898. (setq ls (pop plists))
  20899. (while ls
  20900. (setq p (pop ls) v (pop ls))
  20901. (setq rtn (plist-put rtn p v))))
  20902. rtn))
  20903. (defun org-replace-escapes (string table)
  20904. "Replace %-escapes in STRING with values in TABLE.
  20905. TABLE is an association list with keys like \"%a\" and string values.
  20906. The sequences in STRING may contain normal field width and padding information,
  20907. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20908. so values can contain further %-escapes if they are define later in TABLE."
  20909. (let ((tbl (copy-alist table))
  20910. (case-fold-search nil)
  20911. (pchg 0)
  20912. e re rpl)
  20913. (dolist (e tbl)
  20914. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20915. (when (and (cdr e) (string-match re (cdr e)))
  20916. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20917. (safe "SREF"))
  20918. (add-text-properties 0 3 (list 'sref sref) safe)
  20919. (setcdr e (replace-match safe t t (cdr e)))))
  20920. (while (string-match re string)
  20921. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20922. (cdr e)))
  20923. (setq string (replace-match rpl t t string))))
  20924. (while (setq pchg (next-property-change pchg string))
  20925. (let ((sref (get-text-property pchg 'sref string)))
  20926. (when (and sref (string-match "SREF" string pchg))
  20927. (setq string (replace-match sref t t string)))))
  20928. string))
  20929. (defun org-sublist (list start end)
  20930. "Return a section of LIST, from START to END.
  20931. Counting starts at 1."
  20932. (let (rtn (c start))
  20933. (setq list (nthcdr (1- start) list))
  20934. (while (and list (<= c end))
  20935. (push (pop list) rtn)
  20936. (setq c (1+ c)))
  20937. (nreverse rtn)))
  20938. (defun org-find-base-buffer-visiting (file)
  20939. "Like `find-buffer-visiting' but always return the base buffer and
  20940. not an indirect buffer."
  20941. (let ((buf (or (get-file-buffer file)
  20942. (find-buffer-visiting file))))
  20943. (if buf
  20944. (or (buffer-base-buffer buf) buf)
  20945. nil)))
  20946. (defun org-image-file-name-regexp (&optional extensions)
  20947. "Return regexp matching the file names of images.
  20948. If EXTENSIONS is given, only match these."
  20949. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20950. (image-file-name-regexp)
  20951. (let ((image-file-name-extensions
  20952. (or extensions
  20953. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20954. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20955. (concat "\\."
  20956. (regexp-opt (nconc (mapcar 'upcase
  20957. image-file-name-extensions)
  20958. image-file-name-extensions)
  20959. t)
  20960. "\\'"))))
  20961. (defun org-file-image-p (file &optional extensions)
  20962. "Return non-nil if FILE is an image."
  20963. (save-match-data
  20964. (string-match (org-image-file-name-regexp extensions) file)))
  20965. (defun org-get-cursor-date (&optional with-time)
  20966. "Return the date at cursor in as a time.
  20967. This works in the calendar and in the agenda, anywhere else it just
  20968. returns the current time.
  20969. If WITH-TIME is non-nil, returns the time of the event at point (in
  20970. the agenda) or the current time of the day."
  20971. (let (date day defd tp hod mod)
  20972. (when with-time
  20973. (setq tp (get-text-property (point) 'time))
  20974. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20975. (setq hod (string-to-number (match-string 1 tp))
  20976. mod (string-to-number (match-string 2 tp))))
  20977. (or tp (let ((now (decode-time)))
  20978. (setq hod (nth 2 now)
  20979. mod (nth 1 now)))))
  20980. (cond
  20981. ((eq major-mode 'calendar-mode)
  20982. (setq date (calendar-cursor-to-date)
  20983. defd (encode-time 0 (or mod 0) (or hod 0)
  20984. (nth 1 date) (nth 0 date) (nth 2 date))))
  20985. ((eq major-mode 'org-agenda-mode)
  20986. (setq day (get-text-property (point) 'day))
  20987. (if day
  20988. (setq date (calendar-gregorian-from-absolute day)
  20989. defd (encode-time 0 (or mod 0) (or hod 0)
  20990. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20991. (or defd (current-time))))
  20992. (defun org-mark-subtree (&optional up)
  20993. "Mark the current subtree.
  20994. This puts point at the start of the current subtree, and mark at
  20995. the end. If a numeric prefix UP is given, move up into the
  20996. hierarchy of headlines by UP levels before marking the subtree."
  20997. (interactive "P")
  20998. (org-with-limited-levels
  20999. (cond ((org-at-heading-p) (beginning-of-line))
  21000. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  21001. (t (outline-previous-visible-heading 1))))
  21002. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  21003. (if (org-called-interactively-p 'any)
  21004. (call-interactively 'org-mark-element)
  21005. (org-mark-element)))
  21006. ;;; Indentation
  21007. (defun org--get-expected-indentation (element contentsp)
  21008. "Expected indentation column for current line, according to ELEMENT.
  21009. ELEMENT is an element containing point. CONTENTSP is non-nil
  21010. when indentation is to be computed according to contents of
  21011. ELEMENT."
  21012. (let ((type (org-element-type element))
  21013. (start (org-element-property :begin element))
  21014. (post-affiliated (org-element-property :post-affiliated element)))
  21015. (org-with-wide-buffer
  21016. (cond
  21017. (contentsp
  21018. (case type
  21019. ((diary-sexp footnote-definition) 0)
  21020. ((headline inlinetask nil)
  21021. (if (not org-adapt-indentation) 0
  21022. (let ((level (org-current-level)))
  21023. (if level (1+ level) 0))))
  21024. ((item plain-list) (org-list-item-body-column post-affiliated))
  21025. (t
  21026. (goto-char start)
  21027. (org-get-indentation))))
  21028. ((memq type '(headline inlinetask nil))
  21029. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21030. (org--get-expected-indentation element t)
  21031. 0))
  21032. ((memq type '(diary-sexp footnote-definition)) 0)
  21033. ;; First paragraph of a footnote definition or an item.
  21034. ;; Indent like parent.
  21035. ((< (line-beginning-position) start)
  21036. (org--get-expected-indentation
  21037. (org-element-property :parent element) t))
  21038. ;; At first line: indent according to previous sibling, if any,
  21039. ;; ignoring footnote definitions and inline tasks, or parent's
  21040. ;; contents.
  21041. ((= (line-beginning-position) start)
  21042. (catch 'exit
  21043. (while t
  21044. (if (= (point-min) start) (throw 'exit 0)
  21045. (goto-char (1- start))
  21046. (let* ((previous (org-element-at-point))
  21047. (parent previous))
  21048. (while (and parent (<= (org-element-property :end parent) start))
  21049. (setq previous parent
  21050. parent (org-element-property :parent parent)))
  21051. (cond
  21052. ((not previous) (throw 'exit 0))
  21053. ((> (org-element-property :end previous) start)
  21054. (throw 'exit (org--get-expected-indentation previous t)))
  21055. ((memq (org-element-type previous)
  21056. '(footnote-definition inlinetask))
  21057. (setq start (org-element-property :begin previous)))
  21058. (t (goto-char (org-element-property :begin previous))
  21059. (throw 'exit
  21060. (if (bolp) (org-get-indentation)
  21061. ;; At first paragraph in an item or
  21062. ;; a footnote definition.
  21063. (org--get-expected-indentation
  21064. (org-element-property :parent previous) t))))))))))
  21065. ;; Otherwise, move to the first non-blank line above.
  21066. (t
  21067. (beginning-of-line)
  21068. (let ((pos (point)))
  21069. (skip-chars-backward " \r\t\n")
  21070. (cond
  21071. ;; Two blank lines end a footnote definition or a plain
  21072. ;; list. When we indent an empty line after them, the
  21073. ;; containing list or footnote definition is over, so it
  21074. ;; qualifies as a previous sibling. Therefore, we indent
  21075. ;; like its first line.
  21076. ((and (memq type '(footnote-definition plain-list))
  21077. (> (count-lines (point) pos) 2))
  21078. (goto-char start)
  21079. (org-get-indentation))
  21080. ;; Line above is the first one of a paragraph at the
  21081. ;; beginning of an item or a footnote definition. Indent
  21082. ;; like parent.
  21083. ((< (line-beginning-position) start)
  21084. (org--get-expected-indentation
  21085. (org-element-property :parent element) t))
  21086. ;; Line above is the beginning of an element, i.e., point
  21087. ;; was originally on the blank lines between element's start
  21088. ;; and contents.
  21089. ((= (line-beginning-position) post-affiliated)
  21090. (org--get-expected-indentation element t))
  21091. ;; POS is after contents in a greater element. Indent like
  21092. ;; the beginning of the element.
  21093. ;;
  21094. ;; As a special case, if point is at the end of a footnote
  21095. ;; definition or an item, indent like the very last element
  21096. ;; within.
  21097. ((and (not (eq type 'paragraph))
  21098. (let ((cend (org-element-property :contents-end element)))
  21099. (and cend (<= cend pos))))
  21100. (if (memq type '(footnote-definition item plain-list))
  21101. (let ((last (org-element-at-point)))
  21102. (org--get-expected-indentation
  21103. last
  21104. (memq (org-element-type last)
  21105. '(footnote-definition item plain-list))))
  21106. (goto-char start)
  21107. (org-get-indentation)))
  21108. ;; In any other case, indent like the current line.
  21109. (t (org-get-indentation)))))))))
  21110. (defun org--align-node-property ()
  21111. "Align node property at point.
  21112. Alignment is done according to `org-property-format', which see."
  21113. (when (save-excursion
  21114. (beginning-of-line)
  21115. (looking-at org-property-re))
  21116. (replace-match
  21117. (concat (match-string 4)
  21118. (org-trim
  21119. (format org-property-format (match-string 1) (match-string 3))))
  21120. t t)))
  21121. (defun org-indent-line ()
  21122. "Indent line depending on context.
  21123. Indentation is done according to the following rules:
  21124. - Footnote definitions, diary sexps, headlines and inline tasks
  21125. have to start at column 0.
  21126. - On the very first line of an element, consider, in order, the
  21127. next rules until one matches:
  21128. 1. If there's a sibling element before, ignoring footnote
  21129. definitions and inline tasks, indent like its first line.
  21130. 2. If element has a parent, indent like its contents. More
  21131. precisely, if parent is an item, indent after the
  21132. description part, if any, or the bullet (see
  21133. `org-list-description-max-indent'). Else, indent like
  21134. parent's first line.
  21135. 3. Otherwise, indent relatively to current level, if
  21136. `org-adapt-indentation' is non-nil, or to left margin.
  21137. - On a blank line at the end of an element, indent according to
  21138. the type of the element. More precisely
  21139. 1. If element is a plain list, an item, or a footnote
  21140. definition, indent like the very last element within.
  21141. 2. If element is a paragraph, indent like its last non blank
  21142. line.
  21143. 3. Otherwise, indent like its very first line.
  21144. - In the code part of a source block, use language major mode
  21145. to indent current line if `org-src-tab-acts-natively' is
  21146. non-nil. If it is nil, do nothing.
  21147. - Otherwise, indent like the first non-blank line above.
  21148. The function doesn't indent an item as it could break the whole
  21149. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21150. \\[org-shiftmetaright].
  21151. Also align node properties according to `org-property-format'."
  21152. (interactive)
  21153. (cond
  21154. (orgstruct-is-++
  21155. (let ((indent-line-function
  21156. (cadadr (assq 'indent-line-function org-fb-vars))))
  21157. (indent-according-to-mode)))
  21158. ((org-at-heading-p) 'noindent)
  21159. (t
  21160. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21161. (type (org-element-type element)))
  21162. (cond ((and (memq type '(plain-list item))
  21163. (= (line-beginning-position)
  21164. (org-element-property :post-affiliated element)))
  21165. 'noindent)
  21166. ((and (eq type 'src-block)
  21167. org-src-tab-acts-natively
  21168. (> (line-beginning-position)
  21169. (org-element-property :post-affiliated element))
  21170. (< (line-beginning-position)
  21171. (org-with-wide-buffer
  21172. (goto-char (org-element-property :end element))
  21173. (skip-chars-backward " \r\t\n")
  21174. (line-beginning-position))))
  21175. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21176. (t
  21177. (let ((column (org--get-expected-indentation element nil)))
  21178. ;; Preserve current column.
  21179. (if (<= (current-column) (current-indentation))
  21180. (org-indent-line-to column)
  21181. (save-excursion (org-indent-line-to column))))
  21182. ;; Align node property. Also preserve current column.
  21183. (when (eq type 'node-property)
  21184. (let ((column (current-column)))
  21185. (org--align-node-property)
  21186. (org-move-to-column column)))))))))
  21187. (defun org-indent-region (start end)
  21188. "Indent each non-blank line in the region.
  21189. Called from a program, START and END specify the region to
  21190. indent. The function will not indent contents of example blocks,
  21191. verse blocks and export blocks as leading white spaces are
  21192. assumed to be significant there."
  21193. (interactive "r")
  21194. (save-excursion
  21195. (goto-char start)
  21196. (skip-chars-forward " \r\t\n")
  21197. (unless (eobp) (beginning-of-line))
  21198. (let ((indent-to
  21199. (lambda (ind pos)
  21200. ;; Set IND as indentation for all lines between point and
  21201. ;; POS or END, whichever comes first. Blank lines are
  21202. ;; ignored. Leave point after POS once done.
  21203. (let ((limit (copy-marker (min end pos))))
  21204. (while (< (point) limit)
  21205. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21206. (forward-line))
  21207. (set-marker limit nil))))
  21208. (end (copy-marker end)))
  21209. (while (< (point) end)
  21210. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21211. (let* ((element (org-element-at-point))
  21212. (type (org-element-type element))
  21213. (element-end (copy-marker (org-element-property :end element)))
  21214. (ind (org--get-expected-indentation element nil)))
  21215. (cond
  21216. ((or (memq type '(paragraph table table-row))
  21217. (not (or (org-element-property :contents-begin element)
  21218. (memq type
  21219. '(example-block export-block src-block)))))
  21220. ;; Elements here are indented as a single block. Also
  21221. ;; align node properties.
  21222. (when (eq type 'node-property)
  21223. (org--align-node-property)
  21224. (beginning-of-line))
  21225. (funcall indent-to ind element-end))
  21226. (t
  21227. ;; Elements in this category consist of three parts:
  21228. ;; before the contents, the contents, and after the
  21229. ;; contents. The contents are treated specially,
  21230. ;; according to the element type, or not indented at
  21231. ;; all. Other parts are indented as a single block.
  21232. (let* ((post (copy-marker
  21233. (org-element-property :post-affiliated element)))
  21234. (cbeg
  21235. (copy-marker
  21236. (cond
  21237. ((not (org-element-property :contents-begin element))
  21238. ;; Fake contents for source blocks.
  21239. (org-with-wide-buffer
  21240. (goto-char post)
  21241. (forward-line)
  21242. (point)))
  21243. ((memq type '(footnote-definition item plain-list))
  21244. ;; Contents in these elements could start on
  21245. ;; the same line as the beginning of the
  21246. ;; element. Make sure we start indenting
  21247. ;; from the second line.
  21248. (org-with-wide-buffer
  21249. (goto-char post)
  21250. (end-of-line)
  21251. (skip-chars-forward " \r\t\n")
  21252. (if (eobp) (point) (line-beginning-position))))
  21253. (t (org-element-property :contents-begin element)))))
  21254. (cend (copy-marker
  21255. (or (org-element-property :contents-end element)
  21256. ;; Fake contents for source blocks.
  21257. (org-with-wide-buffer
  21258. (goto-char element-end)
  21259. (skip-chars-backward " \r\t\n")
  21260. (line-beginning-position)))
  21261. t)))
  21262. ;; Do not change items indentation individually as it
  21263. ;; might break the list as a whole. On the other
  21264. ;; hand, when at a plain list, indent it as a whole.
  21265. (cond ((eq type 'plain-list)
  21266. (let ((offset (- ind (org-get-indentation))))
  21267. (unless (zerop offset)
  21268. (indent-rigidly (org-element-property :begin element)
  21269. (org-element-property :end element)
  21270. offset))
  21271. (goto-char cbeg)))
  21272. ((eq type 'item) (goto-char cbeg))
  21273. (t (funcall indent-to ind cbeg)))
  21274. (when (< (point) end)
  21275. (case type
  21276. ((example-block export-block verse-block))
  21277. (src-block
  21278. ;; In a source block, indent source code
  21279. ;; according to language major mode, but only if
  21280. ;; `org-src-tab-acts-natively' is non-nil.
  21281. (when (and (< (point) end) org-src-tab-acts-natively)
  21282. (ignore-errors
  21283. (org-babel-do-in-edit-buffer
  21284. (indent-region (point-min) (point-max))))))
  21285. (t (org-indent-region (point) (min cend end))))
  21286. (goto-char (min cend end))
  21287. (when (< (point) end) (funcall indent-to ind element-end)))
  21288. (set-marker post nil)
  21289. (set-marker cbeg nil)
  21290. (set-marker cend nil))))
  21291. (set-marker element-end nil))))
  21292. (set-marker end nil))))
  21293. (defun org-indent-drawer ()
  21294. "Indent the drawer at point."
  21295. (interactive)
  21296. (unless (save-excursion
  21297. (beginning-of-line)
  21298. (org-looking-at-p org-drawer-regexp))
  21299. (user-error "Not at a drawer"))
  21300. (let ((element (org-element-at-point)))
  21301. (unless (memq (org-element-type element) '(drawer property-drawer))
  21302. (user-error "Not at a drawer"))
  21303. (org-with-wide-buffer
  21304. (org-indent-region (org-element-property :begin element)
  21305. (org-element-property :end element))))
  21306. (message "Drawer at point indented"))
  21307. (defun org-indent-block ()
  21308. "Indent the block at point."
  21309. (interactive)
  21310. (unless (save-excursion
  21311. (beginning-of-line)
  21312. (let ((case-fold-search t))
  21313. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21314. (user-error "Not at a block"))
  21315. (let ((element (org-element-at-point)))
  21316. (unless (memq (org-element-type element)
  21317. '(comment-block center-block dynamic-block example-block
  21318. export-block quote-block special-block
  21319. src-block verse-block))
  21320. (user-error "Not at a block"))
  21321. (org-with-wide-buffer
  21322. (org-indent-region (org-element-property :begin element)
  21323. (org-element-property :end element))))
  21324. (message "Block at point indented"))
  21325. ;;; Filling
  21326. ;; We use our own fill-paragraph and auto-fill functions.
  21327. ;; `org-fill-paragraph' relies on adaptive filling and context
  21328. ;; checking. Appropriate `fill-prefix' is computed with
  21329. ;; `org-adaptive-fill-function'.
  21330. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21331. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21332. ;; `org-adaptive-fill-function' value. Internally,
  21333. ;; `org-comment-line-break-function' breaks the line.
  21334. ;; `org-setup-filling' installs filling and auto-filling related
  21335. ;; variables during `org-mode' initialization.
  21336. (defvar org-element-paragraph-separate) ; org-element.el
  21337. (defun org-setup-filling ()
  21338. (require 'org-element)
  21339. ;; Prevent auto-fill from inserting unwanted new items.
  21340. (when (boundp 'fill-nobreak-predicate)
  21341. (org-set-local
  21342. 'fill-nobreak-predicate
  21343. (org-uniquify
  21344. (append fill-nobreak-predicate
  21345. '(org-fill-line-break-nobreak-p
  21346. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21347. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21348. (org-set-local 'paragraph-start paragraph-ending)
  21349. (org-set-local 'paragraph-separate paragraph-ending))
  21350. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21351. (org-set-local 'auto-fill-inhibit-regexp nil)
  21352. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21353. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21354. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21355. (defun org-fill-line-break-nobreak-p ()
  21356. "Non-nil when a new line at point would create an Org line break."
  21357. (save-excursion
  21358. (skip-chars-backward "[ \t]")
  21359. (skip-chars-backward "\\\\")
  21360. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21361. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21362. "Non-nil when a new line at point would split a timestamp."
  21363. (and (org-at-timestamp-p t)
  21364. (not (looking-at org-ts-regexp-both))))
  21365. (declare-function message-in-body-p "message" ())
  21366. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21367. (defun org-adaptive-fill-function ()
  21368. "Compute a fill prefix for the current line.
  21369. Return fill prefix, as a string, or nil if current line isn't
  21370. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21371. matches in paragraphs or comments, use it."
  21372. (catch 'exit
  21373. (when (derived-mode-p 'message-mode)
  21374. (save-excursion
  21375. (beginning-of-line)
  21376. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21377. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21378. ((looking-at message-cite-prefix-regexp)
  21379. (throw 'exit (match-string-no-properties 0)))
  21380. ((looking-at org-outline-regexp)
  21381. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21382. (org-with-wide-buffer
  21383. (unless (org-at-heading-p)
  21384. (let* ((p (line-beginning-position))
  21385. (element (save-excursion
  21386. (beginning-of-line)
  21387. (org-element-at-point)))
  21388. (type (org-element-type element))
  21389. (post-affiliated (org-element-property :post-affiliated element)))
  21390. (unless (< p post-affiliated)
  21391. (case type
  21392. (comment
  21393. (save-excursion
  21394. (beginning-of-line)
  21395. (looking-at "[ \t]*")
  21396. (concat (match-string 0) "# ")))
  21397. (footnote-definition "")
  21398. ((item plain-list)
  21399. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21400. (paragraph
  21401. ;; Fill prefix is usually the same as the current line,
  21402. ;; unless the paragraph is at the beginning of an item.
  21403. (let ((parent (org-element-property :parent element)))
  21404. (save-excursion
  21405. (beginning-of-line)
  21406. (cond ((eq (org-element-type parent) 'item)
  21407. (make-string (org-list-item-body-column
  21408. (org-element-property :begin parent))
  21409. ?\s))
  21410. ((and adaptive-fill-regexp
  21411. ;; Locally disable
  21412. ;; `adaptive-fill-function' to let
  21413. ;; `fill-context-prefix' handle
  21414. ;; `adaptive-fill-regexp' variable.
  21415. (let (adaptive-fill-function)
  21416. (fill-context-prefix
  21417. post-affiliated
  21418. (org-element-property :end element)))))
  21419. ((looking-at "[ \t]+") (match-string 0))
  21420. (t "")))))
  21421. (comment-block
  21422. ;; Only fill contents if P is within block boundaries.
  21423. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21424. (forward-line)
  21425. (point)))
  21426. (cend (save-excursion
  21427. (goto-char (org-element-property :end element))
  21428. (skip-chars-backward " \r\t\n")
  21429. (line-beginning-position))))
  21430. (when (and (>= p cbeg) (< p cend))
  21431. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21432. (match-string 0)
  21433. "")))))))))))
  21434. (declare-function message-goto-body "message" ())
  21435. (defvar message-cite-prefix-regexp) ; From message.el
  21436. (defun org-fill-paragraph (&optional justify)
  21437. "Fill element at point, when applicable.
  21438. This function only applies to comment blocks, comments, example
  21439. blocks and paragraphs. Also, as a special case, re-align table
  21440. when point is at one.
  21441. If JUSTIFY is non-nil (interactively, with prefix argument),
  21442. justify as well. If `sentence-end-double-space' is non-nil, then
  21443. period followed by one space does not end a sentence, so don't
  21444. break a line there. The variable `fill-column' controls the
  21445. width for filling.
  21446. For convenience, when point is at a plain list, an item or
  21447. a footnote definition, try to fill the first paragraph within."
  21448. (interactive)
  21449. (if (and (derived-mode-p 'message-mode)
  21450. (or (not (message-in-body-p))
  21451. (save-excursion (move-beginning-of-line 1)
  21452. (looking-at message-cite-prefix-regexp))))
  21453. ;; First ensure filling is correct in message-mode.
  21454. (let ((fill-paragraph-function
  21455. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21456. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21457. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21458. (paragraph-separate
  21459. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21460. (fill-paragraph nil))
  21461. (with-syntax-table org-mode-transpose-word-syntax-table
  21462. ;; Move to end of line in order to get the first paragraph
  21463. ;; within a plain list or a footnote definition.
  21464. (let ((element (save-excursion
  21465. (end-of-line)
  21466. (or (ignore-errors (org-element-at-point))
  21467. (user-error "An element cannot be parsed line %d"
  21468. (line-number-at-pos (point)))))))
  21469. ;; First check if point is in a blank line at the beginning of
  21470. ;; the buffer. In that case, ignore filling.
  21471. (case (org-element-type element)
  21472. ;; Use major mode filling function is src blocks.
  21473. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21474. ;; Align Org tables, leave table.el tables as-is.
  21475. (table-row (org-table-align) t)
  21476. (table
  21477. (when (eq (org-element-property :type element) 'org)
  21478. (save-excursion
  21479. (goto-char (org-element-property :post-affiliated element))
  21480. (org-table-align)))
  21481. t)
  21482. (paragraph
  21483. ;; Paragraphs may contain `line-break' type objects.
  21484. (let ((beg (max (point-min)
  21485. (org-element-property :contents-begin element)))
  21486. (end (min (point-max)
  21487. (org-element-property :contents-end element))))
  21488. ;; Do nothing if point is at an affiliated keyword.
  21489. (if (< (line-end-position) beg) t
  21490. (when (derived-mode-p 'message-mode)
  21491. ;; In `message-mode', do not fill following citation
  21492. ;; in current paragraph nor text before message body.
  21493. (let ((body-start (save-excursion (message-goto-body))))
  21494. (when body-start (setq beg (max body-start beg))))
  21495. (when (save-excursion
  21496. (re-search-forward
  21497. (concat "^" message-cite-prefix-regexp) end t))
  21498. (setq end (match-beginning 0))))
  21499. ;; Fill paragraph, taking line breaks into account.
  21500. (save-excursion
  21501. (goto-char beg)
  21502. (let ((cuts (list beg)))
  21503. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21504. (when (eq 'line-break
  21505. (org-element-type
  21506. (save-excursion (backward-char)
  21507. (org-element-context))))
  21508. (push (point) cuts)))
  21509. (dolist (c (delq end cuts))
  21510. (fill-region-as-paragraph c end justify)
  21511. (setq end c))))
  21512. t)))
  21513. ;; Contents of `comment-block' type elements should be
  21514. ;; filled as plain text, but only if point is within block
  21515. ;; markers.
  21516. (comment-block
  21517. (let* ((case-fold-search t)
  21518. (beg (save-excursion
  21519. (goto-char (org-element-property :begin element))
  21520. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21521. (forward-line)
  21522. (point)))
  21523. (end (save-excursion
  21524. (goto-char (org-element-property :end element))
  21525. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21526. (line-beginning-position))))
  21527. (if (or (< (point) beg) (> (point) end)) t
  21528. (fill-region-as-paragraph
  21529. (save-excursion (end-of-line)
  21530. (re-search-backward "^[ \t]*$" beg 'move)
  21531. (line-beginning-position))
  21532. (save-excursion (beginning-of-line)
  21533. (re-search-forward "^[ \t]*$" end 'move)
  21534. (line-beginning-position))
  21535. justify))))
  21536. ;; Fill comments.
  21537. (comment
  21538. (let ((begin (org-element-property :post-affiliated element))
  21539. (end (org-element-property :end element)))
  21540. (when (and (>= (point) begin) (<= (point) end))
  21541. (let ((begin (save-excursion
  21542. (end-of-line)
  21543. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21544. (progn (forward-line) (point))
  21545. begin)))
  21546. (end (save-excursion
  21547. (end-of-line)
  21548. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21549. (1- (line-beginning-position))
  21550. (skip-chars-backward " \r\t\n")
  21551. (line-end-position)))))
  21552. ;; Do not fill comments when at a blank line.
  21553. (when (> end begin)
  21554. (let ((fill-prefix
  21555. (save-excursion
  21556. (beginning-of-line)
  21557. (looking-at "[ \t]*#")
  21558. (let ((comment-prefix (match-string 0)))
  21559. (goto-char (match-end 0))
  21560. (if (looking-at adaptive-fill-regexp)
  21561. (concat comment-prefix (match-string 0))
  21562. (concat comment-prefix " "))))))
  21563. (save-excursion
  21564. (fill-region-as-paragraph begin end justify))))))
  21565. t))
  21566. ;; Ignore every other element.
  21567. (otherwise t))))))
  21568. (defun org-auto-fill-function ()
  21569. "Auto-fill function."
  21570. ;; Check if auto-filling is meaningful.
  21571. (let ((fc (current-fill-column)))
  21572. (when (and fc (> (current-column) fc))
  21573. (let* ((fill-prefix (org-adaptive-fill-function))
  21574. ;; Enforce empty fill prefix, if required. Otherwise, it
  21575. ;; will be computed again.
  21576. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21577. (when fill-prefix (do-auto-fill))))))
  21578. (defun org-comment-line-break-function (&optional soft)
  21579. "Break line at point and indent, continuing comment if within one.
  21580. The inserted newline is marked hard if variable
  21581. `use-hard-newlines' is true, unless optional argument SOFT is
  21582. non-nil."
  21583. (if soft (insert-and-inherit ?\n) (newline 1))
  21584. (save-excursion (forward-char -1) (delete-horizontal-space))
  21585. (delete-horizontal-space)
  21586. (indent-to-left-margin)
  21587. (insert-before-markers-and-inherit fill-prefix))
  21588. ;;; Fixed Width Areas
  21589. (defun org-toggle-fixed-width ()
  21590. "Toggle fixed-width markup.
  21591. Add or remove fixed-width markup on current line, whenever it
  21592. makes sense. Return an error otherwise.
  21593. If a region is active and if it contains only fixed-width areas
  21594. or blank lines, remove all fixed-width markup in it. If the
  21595. region contains anything else, convert all non-fixed-width lines
  21596. to fixed-width ones.
  21597. Blank lines at the end of the region are ignored unless the
  21598. region only contains such lines."
  21599. (interactive)
  21600. (if (not (org-region-active-p))
  21601. ;; No region:
  21602. ;;
  21603. ;; Remove fixed width marker only in a fixed-with element.
  21604. ;;
  21605. ;; Add fixed width maker in paragraphs, in blank lines after
  21606. ;; elements or at the beginning of a headline or an inlinetask,
  21607. ;; and before any one-line elements (e.g., a clock).
  21608. (progn
  21609. (beginning-of-line)
  21610. (let* ((element (org-element-at-point))
  21611. (type (org-element-type element)))
  21612. (cond
  21613. ((and (eq type 'fixed-width)
  21614. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21615. (replace-match
  21616. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21617. ((and (memq type '(babel-call clock comment diary-sexp headline
  21618. horizontal-rule keyword paragraph
  21619. planning))
  21620. (<= (org-element-property :post-affiliated element) (point)))
  21621. (skip-chars-forward " \t")
  21622. (insert ": "))
  21623. ((and (org-looking-at-p "[ \t]*$")
  21624. (or (eq type 'inlinetask)
  21625. (save-excursion
  21626. (skip-chars-forward " \r\t\n")
  21627. (<= (org-element-property :end element) (point)))))
  21628. (delete-region (point) (line-end-position))
  21629. (org-indent-line)
  21630. (insert ": "))
  21631. (t (user-error "Cannot insert a fixed-width line here")))))
  21632. ;; Region active.
  21633. (let* ((begin (save-excursion
  21634. (goto-char (region-beginning))
  21635. (line-beginning-position)))
  21636. (end (copy-marker
  21637. (save-excursion
  21638. (goto-char (region-end))
  21639. (unless (eolp) (beginning-of-line))
  21640. (if (save-excursion (re-search-backward "\\S-" begin t))
  21641. (progn (skip-chars-backward " \r\t\n") (point))
  21642. (point)))))
  21643. (all-fixed-width-p
  21644. (catch 'not-all-p
  21645. (save-excursion
  21646. (goto-char begin)
  21647. (skip-chars-forward " \r\t\n")
  21648. (when (eobp) (throw 'not-all-p nil))
  21649. (while (< (point) end)
  21650. (let ((element (org-element-at-point)))
  21651. (if (eq (org-element-type element) 'fixed-width)
  21652. (goto-char (org-element-property :end element))
  21653. (throw 'not-all-p nil))))
  21654. t))))
  21655. (if all-fixed-width-p
  21656. (save-excursion
  21657. (goto-char begin)
  21658. (while (< (point) end)
  21659. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21660. (replace-match
  21661. "" nil nil nil
  21662. (if (= (line-end-position) (match-end 0)) 0 1)))
  21663. (forward-line)))
  21664. (let ((min-ind (point-max)))
  21665. ;; Find minimum indentation across all lines.
  21666. (save-excursion
  21667. (goto-char begin)
  21668. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21669. (setq min-ind 0)
  21670. (catch 'zerop
  21671. (while (< (point) end)
  21672. (unless (org-looking-at-p "[ \t]*$")
  21673. (let ((ind (org-get-indentation)))
  21674. (setq min-ind (min min-ind ind))
  21675. (when (zerop ind) (throw 'zerop t))))
  21676. (forward-line)))))
  21677. ;; Loop over all lines and add fixed-width markup everywhere
  21678. ;; but in fixed-width lines.
  21679. (save-excursion
  21680. (goto-char begin)
  21681. (while (< (point) end)
  21682. (cond
  21683. ((org-at-heading-p)
  21684. (insert ": ")
  21685. (forward-line)
  21686. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21687. (insert ":")
  21688. (forward-line)))
  21689. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21690. (let* ((element (org-element-at-point))
  21691. (element-end (org-element-property :end element)))
  21692. (if (eq (org-element-type element) 'fixed-width)
  21693. (progn (goto-char element-end)
  21694. (skip-chars-backward " \r\t\n")
  21695. (forward-line))
  21696. (let ((limit (min end element-end)))
  21697. (while (< (point) limit)
  21698. (org-move-to-column min-ind t)
  21699. (insert ": ")
  21700. (forward-line))))))
  21701. (t
  21702. (org-move-to-column min-ind t)
  21703. (insert ": ")
  21704. (forward-line)))))))
  21705. (set-marker end nil))))
  21706. ;;; Comments
  21707. ;; Org comments syntax is quite complex. It requires the entire line
  21708. ;; to be just a comment. Also, even with the right syntax at the
  21709. ;; beginning of line, some some elements (i.e. verse-block or
  21710. ;; example-block) don't accept comments. Usual Emacs comment commands
  21711. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21712. ;; Org still relies on `comment-dwim', but cannot trust
  21713. ;; `comment-only-p'. So, `comment-region-function' and
  21714. ;; `uncomment-region-function' both point
  21715. ;; to`org-comment-or-uncomment-region'. Eventually,
  21716. ;; `org-insert-comment' takes care of insertion of comments at the
  21717. ;; beginning of line.
  21718. ;; `org-setup-comments-handling' install comments related variables
  21719. ;; during `org-mode' initialization.
  21720. (defun org-setup-comments-handling ()
  21721. (interactive)
  21722. (org-set-local 'comment-use-syntax nil)
  21723. (org-set-local 'comment-start "# ")
  21724. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21725. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21726. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21727. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21728. (defun org-insert-comment ()
  21729. "Insert an empty comment above current line.
  21730. If the line is empty, insert comment at its beginning. When
  21731. point is within a source block, comment according to the related
  21732. major mode."
  21733. (if (let ((element (org-element-at-point)))
  21734. (and (eq (org-element-type element) 'src-block)
  21735. (< (save-excursion
  21736. (goto-char (org-element-property :post-affiliated element))
  21737. (line-end-position))
  21738. (point))
  21739. (> (save-excursion
  21740. (goto-char (org-element-property :end element))
  21741. (skip-chars-backward " \r\t\n")
  21742. (line-beginning-position))
  21743. (point))))
  21744. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21745. (beginning-of-line)
  21746. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21747. (open-line 1))
  21748. (org-indent-line)
  21749. (insert "# ")))
  21750. (defvar comment-empty-lines) ; From newcomment.el.
  21751. (defun org-comment-or-uncomment-region (beg end &rest _)
  21752. "Comment or uncomment each non-blank line in the region.
  21753. Uncomment each non-blank line between BEG and END if it only
  21754. contains commented lines. Otherwise, comment them. If region is
  21755. strictly within a source block, use appropriate comment syntax."
  21756. (if (let ((element (org-element-at-point)))
  21757. (and (eq (org-element-type element) 'src-block)
  21758. (< (save-excursion
  21759. (goto-char (org-element-property :post-affiliated element))
  21760. (line-end-position))
  21761. beg)
  21762. (>= (save-excursion
  21763. (goto-char (org-element-property :end element))
  21764. (skip-chars-backward " \r\t\n")
  21765. (line-beginning-position))
  21766. end)))
  21767. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21768. (save-restriction
  21769. ;; Restrict region
  21770. (narrow-to-region (save-excursion (goto-char beg)
  21771. (skip-chars-forward " \r\t\n" end)
  21772. (line-beginning-position))
  21773. (save-excursion (goto-char end)
  21774. (skip-chars-backward " \r\t\n" beg)
  21775. (line-end-position)))
  21776. (let ((uncommentp
  21777. ;; UNCOMMENTP is non-nil when every non blank line between
  21778. ;; BEG and END is a comment.
  21779. (save-excursion
  21780. (goto-char (point-min))
  21781. (while (and (not (eobp))
  21782. (let ((element (org-element-at-point)))
  21783. (and (eq (org-element-type element) 'comment)
  21784. (goto-char (min (point-max)
  21785. (org-element-property
  21786. :end element)))))))
  21787. (eobp))))
  21788. (if uncommentp
  21789. ;; Only blank lines and comments in region: uncomment it.
  21790. (save-excursion
  21791. (goto-char (point-min))
  21792. (while (not (eobp))
  21793. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21794. (replace-match "" nil nil nil 1))
  21795. (forward-line)))
  21796. ;; Comment each line in region.
  21797. (let ((min-indent (point-max)))
  21798. ;; First find the minimum indentation across all lines.
  21799. (save-excursion
  21800. (goto-char (point-min))
  21801. (while (and (not (eobp)) (not (zerop min-indent)))
  21802. (unless (looking-at "[ \t]*$")
  21803. (setq min-indent (min min-indent (current-indentation))))
  21804. (forward-line)))
  21805. ;; Then loop over all lines.
  21806. (save-excursion
  21807. (goto-char (point-min))
  21808. (while (not (eobp))
  21809. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21810. ;; Don't get fooled by invisible text (e.g. link path)
  21811. ;; when moving to column MIN-INDENT.
  21812. (let ((buffer-invisibility-spec nil))
  21813. (org-move-to-column min-indent t))
  21814. (insert comment-start))
  21815. (forward-line)))))))))
  21816. (defun org-comment-dwim (arg)
  21817. "Call `comment-dwim' within a source edit buffer if needed."
  21818. (interactive "*P")
  21819. (if (org-in-src-block-p)
  21820. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21821. (call-interactively 'comment-dwim)))
  21822. ;;; Timestamps API
  21823. ;; This section contains tools to operate on timestamp objects, as
  21824. ;; returned by, e.g. `org-element-context'.
  21825. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21826. "Encode TIMESTAMP object into Emacs internal time.
  21827. Use end of date range or time range when END is non-nil."
  21828. (apply #'encode-time
  21829. (cons 0
  21830. (mapcar
  21831. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21832. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21833. '(:minute-start :hour-start :day-start :month-start
  21834. :year-start))))))
  21835. (defun org-timestamp-has-time-p (timestamp)
  21836. "Non-nil when TIMESTAMP has a time specified."
  21837. (org-element-property :hour-start timestamp))
  21838. (defun org-timestamp-format (timestamp format &optional end utc)
  21839. "Format a TIMESTAMP object into a string.
  21840. FORMAT is a format specifier to be passed to
  21841. `format-time-string'.
  21842. When optional argument END is non-nil, use end of date-range or
  21843. time-range, if possible.
  21844. When optional argument UTC is non-nil, time will be expressed as
  21845. Universal Time."
  21846. (format-time-string
  21847. format (org-timestamp--to-internal-time timestamp end) utc))
  21848. (defun org-timestamp-split-range (timestamp &optional end)
  21849. "Extract a TIMESTAMP object from a date or time range.
  21850. END, when non-nil, means extract the end of the range.
  21851. Otherwise, extract its start.
  21852. Return a new timestamp object."
  21853. (let ((type (org-element-property :type timestamp)))
  21854. (if (memq type '(active inactive diary)) timestamp
  21855. (let ((split-ts (org-element-copy timestamp)))
  21856. ;; Set new type.
  21857. (org-element-put-property
  21858. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21859. ;; Copy start properties over end properties if END is
  21860. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21861. (let ((p-alist '((:minute-start . :minute-end)
  21862. (:hour-start . :hour-end)
  21863. (:day-start . :day-end)
  21864. (:month-start . :month-end)
  21865. (:year-start . :year-end))))
  21866. (dolist (p-cell p-alist)
  21867. (org-element-put-property
  21868. split-ts
  21869. (funcall (if end #'car #'cdr) p-cell)
  21870. (org-element-property
  21871. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21872. ;; Eventually refresh `:raw-value'.
  21873. (org-element-put-property split-ts :raw-value nil)
  21874. (org-element-put-property
  21875. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21876. (defun org-timestamp-translate (timestamp &optional boundary)
  21877. "Translate TIMESTAMP object to custom format.
  21878. Format string is defined in `org-time-stamp-custom-formats',
  21879. which see.
  21880. When optional argument BOUNDARY is non-nil, it is either the
  21881. symbol `start' or `end'. In this case, only translate the
  21882. starting or ending part of TIMESTAMP if it is a date or time
  21883. range. Otherwise, translate both parts.
  21884. Return timestamp as-is if `org-display-custom-times' is nil or if
  21885. it has a `diary' type."
  21886. (let ((type (org-element-property :type timestamp)))
  21887. (if (or (not org-display-custom-times) (eq type 'diary))
  21888. (org-element-interpret-data timestamp)
  21889. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21890. org-time-stamp-custom-formats)))
  21891. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21892. (concat (org-timestamp-format timestamp fmt)
  21893. "--"
  21894. (org-timestamp-format timestamp fmt t))
  21895. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21896. ;;; Other stuff.
  21897. (defvar reftex-docstruct-symbol)
  21898. (defvar reftex-cite-format)
  21899. (defvar org--rds)
  21900. (defun org-reftex-citation ()
  21901. "Use reftex-citation to insert a citation into the buffer.
  21902. This looks for a line like
  21903. #+BIBLIOGRAPHY: foo plain option:-d
  21904. and derives from it that foo.bib is the bibliography file relevant
  21905. for this document. It then installs the necessary environment for RefTeX
  21906. to work in this buffer and calls `reftex-citation' to insert a citation
  21907. into the buffer.
  21908. Export of such citations to both LaTeX and HTML is handled by the contributed
  21909. package ox-bibtex by Taru Karttunen."
  21910. (interactive)
  21911. (let ((reftex-docstruct-symbol 'org--rds)
  21912. (reftex-cite-format "\\cite{%l}")
  21913. org--rds bib)
  21914. (save-excursion
  21915. (save-restriction
  21916. (widen)
  21917. (let ((case-fold-search t)
  21918. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21919. (if (not (save-excursion
  21920. (or (re-search-forward re nil t)
  21921. (re-search-backward re nil t))))
  21922. (user-error "No bibliography defined in file")
  21923. (setq bib (concat (match-string 1) ".bib")
  21924. org--rds (list (list 'bib bib)))))))
  21925. (call-interactively 'reftex-citation)))
  21926. ;;;; Functions extending outline functionality
  21927. (defun org-beginning-of-line (&optional arg)
  21928. "Go to the beginning of the current line. If that is invisible, continue
  21929. to a visible line beginning. This makes the function of C-a more intuitive.
  21930. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21931. first attempt, and only move to after the tags when the cursor is already
  21932. beyond the end of the headline."
  21933. (interactive "P")
  21934. (let ((pos (point))
  21935. (special (if (consp org-special-ctrl-a/e)
  21936. (car org-special-ctrl-a/e)
  21937. org-special-ctrl-a/e))
  21938. deactivate-mark refpos)
  21939. (if (org-bound-and-true-p visual-line-mode)
  21940. (beginning-of-visual-line 1)
  21941. (beginning-of-line 1))
  21942. (if (and arg (fboundp 'move-beginning-of-line))
  21943. (call-interactively 'move-beginning-of-line)
  21944. (if (bobp)
  21945. nil
  21946. (backward-char 1)
  21947. (if (org-truely-invisible-p)
  21948. (while (and (not (bobp)) (org-truely-invisible-p))
  21949. (backward-char 1)
  21950. (beginning-of-line 1))
  21951. (forward-char 1))))
  21952. (when special
  21953. (cond
  21954. ((and (looking-at org-complex-heading-regexp)
  21955. (eq (char-after (match-end 1)) ?\s))
  21956. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21957. (point-at-eol)))
  21958. (goto-char
  21959. (if (eq special t)
  21960. (cond ((> pos refpos) refpos)
  21961. ((= pos (point)) refpos)
  21962. (t (point)))
  21963. (cond ((> pos (point)) (point))
  21964. ((not (eq last-command this-command)) (point))
  21965. (t refpos)))))
  21966. ((org-at-item-p)
  21967. ;; Being at an item and not looking at an the item means point
  21968. ;; was previously moved to beginning of a visual line, which
  21969. ;; doesn't contain the item. Therefore, do nothing special,
  21970. ;; just stay here.
  21971. (when (looking-at org-list-full-item-re)
  21972. ;; Set special position at first white space character after
  21973. ;; bullet, and check-box, if any.
  21974. (let ((after-bullet
  21975. (let ((box (match-end 3)))
  21976. (if (not box) (match-end 1)
  21977. (let ((after (char-after box)))
  21978. (if (and after (= after ? )) (1+ box) box))))))
  21979. ;; Special case: Move point to special position when
  21980. ;; currently after it or at beginning of line.
  21981. (if (eq special t)
  21982. (when (or (> pos after-bullet) (= (point) pos))
  21983. (goto-char after-bullet))
  21984. ;; Reversed case: Move point to special position when
  21985. ;; point was already at beginning of line and command is
  21986. ;; repeated.
  21987. (when (and (= (point) pos) (eq last-command this-command))
  21988. (goto-char after-bullet))))))))
  21989. (org-no-warnings
  21990. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21991. (setq disable-point-adjustment
  21992. (or (not (invisible-p (point)))
  21993. (not (invisible-p (max (point-min) (1- (point))))))))
  21994. (defun org-end-of-line (&optional arg)
  21995. "Go to the end of the line.
  21996. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21997. tags on the first attempt, and only move to after the tags when
  21998. the cursor is already beyond the end of the headline."
  21999. (interactive "P")
  22000. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  22001. org-special-ctrl-a/e))
  22002. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  22003. 'end-of-visual-line)
  22004. ((fboundp 'move-end-of-line) 'move-end-of-line)
  22005. (t 'end-of-line)))
  22006. deactivate-mark)
  22007. (if (or (not special) arg) (call-interactively move-fun)
  22008. (let* ((element (save-excursion (beginning-of-line)
  22009. (org-element-at-point)))
  22010. (type (org-element-type element)))
  22011. (cond
  22012. ((memq type '(headline inlinetask))
  22013. (let ((pos (point)))
  22014. (beginning-of-line 1)
  22015. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  22016. (if (eq special t)
  22017. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  22018. (goto-char (match-beginning 1))
  22019. (goto-char (match-end 0)))
  22020. (if (or (< pos (match-end 0))
  22021. (not (eq this-command last-command)))
  22022. (goto-char (match-end 0))
  22023. (goto-char (match-beginning 1))))
  22024. (call-interactively move-fun))))
  22025. ((outline-invisible-p (line-end-position))
  22026. ;; If element is hidden, `move-end-of-line' would put point
  22027. ;; after it. Use `end-of-line' to stay on current line.
  22028. (call-interactively 'end-of-line))
  22029. (t (call-interactively move-fun)))))
  22030. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22031. (setq disable-point-adjustment
  22032. (or (not (invisible-p (point)))
  22033. (not (invisible-p (max (point-min) (1- (point))))))))
  22034. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22035. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22036. (defun org-backward-sentence (&optional _arg)
  22037. "Go to beginning of sentence, or beginning of table field.
  22038. This will call `backward-sentence' or `org-table-beginning-of-field',
  22039. depending on context."
  22040. (interactive)
  22041. (let* ((element (org-element-at-point))
  22042. (contents-begin (org-element-property :contents-begin element))
  22043. (table (org-element-lineage element '(table) t)))
  22044. (if (and table
  22045. (> (point) contents-begin)
  22046. (<= (point) (org-element-property :contents-end table)))
  22047. (call-interactively #'org-table-beginning-of-field)
  22048. (save-restriction
  22049. (when (and contents-begin
  22050. (< (point-min) contents-begin)
  22051. (> (point) contents-begin))
  22052. (narrow-to-region contents-begin
  22053. (org-element-property :contents-end element)))
  22054. (call-interactively #'backward-sentence)))))
  22055. (defun org-forward-sentence (&optional _arg)
  22056. "Go to end of sentence, or end of table field.
  22057. This will call `forward-sentence' or `org-table-end-of-field',
  22058. depending on context."
  22059. (interactive)
  22060. (let* ((element (org-element-at-point))
  22061. (contents-end (org-element-property :contents-end element))
  22062. (table (org-element-lineage element '(table) t)))
  22063. (if (and table
  22064. (>= (point) (org-element-property :contents-begin table))
  22065. (< (point) contents-end))
  22066. (call-interactively #'org-table-end-of-field)
  22067. (save-restriction
  22068. (when (and contents-end
  22069. (> (point-max) contents-end)
  22070. ;; Skip blank lines between elements.
  22071. (< (org-element-property :end element)
  22072. (save-excursion (goto-char contents-end)
  22073. (skip-chars-forward " \r\t\n"))))
  22074. (narrow-to-region (org-element-property :contents-begin element)
  22075. contents-end))
  22076. (call-interactively #'forward-sentence)))))
  22077. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22078. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22079. (defun org-kill-line (&optional _arg)
  22080. "Kill line, to tags or end of line."
  22081. (interactive)
  22082. (cond
  22083. ((or (not org-special-ctrl-k)
  22084. (bolp)
  22085. (not (org-at-heading-p)))
  22086. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22087. org-ctrl-k-protect-subtree)
  22088. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22089. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22090. (user-error "C-k aborted as it would kill a hidden subtree")))
  22091. (call-interactively
  22092. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22093. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22094. (kill-region (point) (match-beginning 1))
  22095. (org-set-tags nil t))
  22096. (t (kill-region (point) (point-at-eol)))))
  22097. (define-key org-mode-map "\C-k" 'org-kill-line)
  22098. (defun org-yank (&optional arg)
  22099. "Yank. If the kill is a subtree, treat it specially.
  22100. This command will look at the current kill and check if is a single
  22101. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22102. cursor is at the beginning of a line or after the stars of a currently
  22103. empty headline, then the yank is handled specially. How exactly depends
  22104. on the value of the following variables, both set by default.
  22105. `org-yank-folded-subtrees'
  22106. When set, the subtree(s) will be folded after insertion, but only
  22107. if doing so would now swallow text after the yanked text.
  22108. `org-yank-adjusted-subtrees'
  22109. When set, the subtree will be promoted or demoted in order to
  22110. fit into the local outline tree structure, which means that the
  22111. level will be adjusted so that it becomes the smaller one of the
  22112. two *visible* surrounding headings.
  22113. Any prefix to this command will cause `yank' to be called directly with
  22114. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22115. will just
  22116. plainly yank the text as it is.
  22117. \[1] The test checks if the first non-white line is a heading
  22118. and if there are no other headings with fewer stars."
  22119. (interactive "P")
  22120. (org-yank-generic 'yank arg))
  22121. (defun org-yank-generic (command arg)
  22122. "Perform some yank-like command.
  22123. This function implements the behavior described in the `org-yank'
  22124. documentation. However, it has been generalized to work for any
  22125. interactive command with similar behavior."
  22126. ;; pretend to be command COMMAND
  22127. (setq this-command command)
  22128. (if arg
  22129. (call-interactively command)
  22130. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22131. (and (org-kill-is-subtree-p)
  22132. (or (bolp)
  22133. (and (looking-at "[ \t]*$")
  22134. (string-match
  22135. "\\`\\*+\\'"
  22136. (buffer-substring (point-at-bol) (point)))))))
  22137. swallowp)
  22138. (cond
  22139. ((and subtreep org-yank-folded-subtrees)
  22140. (let ((beg (point))
  22141. end)
  22142. (if (and subtreep org-yank-adjusted-subtrees)
  22143. (org-paste-subtree nil nil 'for-yank)
  22144. (call-interactively command))
  22145. (setq end (point))
  22146. (goto-char beg)
  22147. (when (and (bolp) subtreep
  22148. (not (setq swallowp
  22149. (org-yank-folding-would-swallow-text beg end))))
  22150. (org-with-limited-levels
  22151. (or (looking-at org-outline-regexp)
  22152. (re-search-forward org-outline-regexp-bol end t))
  22153. (while (and (< (point) end) (looking-at org-outline-regexp))
  22154. (hide-subtree)
  22155. (org-cycle-show-empty-lines 'folded)
  22156. (condition-case nil
  22157. (outline-forward-same-level 1)
  22158. (error (goto-char end))))))
  22159. (when swallowp
  22160. (message
  22161. "Inserted text not folded because that would swallow text"))
  22162. (goto-char end)
  22163. (skip-chars-forward " \t\n\r")
  22164. (beginning-of-line 1)
  22165. (push-mark beg 'nomsg)))
  22166. ((and subtreep org-yank-adjusted-subtrees)
  22167. (let ((beg (point-at-bol)))
  22168. (org-paste-subtree nil nil 'for-yank)
  22169. (push-mark beg 'nomsg)))
  22170. (t
  22171. (call-interactively command))))))
  22172. (defun org-yank-folding-would-swallow-text (beg end)
  22173. "Would hide-subtree at BEG swallow any text after END?"
  22174. (let (level)
  22175. (org-with-limited-levels
  22176. (save-excursion
  22177. (goto-char beg)
  22178. (when (or (looking-at org-outline-regexp)
  22179. (re-search-forward org-outline-regexp-bol end t))
  22180. (setq level (org-outline-level)))
  22181. (goto-char end)
  22182. (skip-chars-forward " \t\r\n\v\f")
  22183. (if (or (eobp)
  22184. (and (bolp) (looking-at org-outline-regexp)
  22185. (<= (org-outline-level) level)))
  22186. nil ; Nothing would be swallowed
  22187. t))))) ; something would swallow
  22188. (define-key org-mode-map "\C-y" 'org-yank)
  22189. (defun org-truely-invisible-p ()
  22190. "Check if point is at a character currently not visible.
  22191. This version does not only check the character property, but also
  22192. `visible-mode'."
  22193. ;; Early versions of noutline don't have `outline-invisible-p'.
  22194. (if (org-bound-and-true-p visible-mode)
  22195. nil
  22196. (outline-invisible-p)))
  22197. (defun org-invisible-p2 ()
  22198. "Check if point is at a character currently not visible."
  22199. (save-excursion
  22200. (if (and (eolp) (not (bobp))) (backward-char 1))
  22201. ;; Early versions of noutline don't have `outline-invisible-p'.
  22202. (outline-invisible-p)))
  22203. (defun org-back-to-heading (&optional invisible-ok)
  22204. "Call `outline-back-to-heading', but provide a better error message."
  22205. (condition-case nil
  22206. (outline-back-to-heading invisible-ok)
  22207. (error (error "Before first headline at position %d in buffer %s"
  22208. (point) (current-buffer)))))
  22209. (defun org-before-first-heading-p ()
  22210. "Before first heading?"
  22211. (save-excursion
  22212. (end-of-line)
  22213. (null (re-search-backward org-outline-regexp-bol nil t))))
  22214. (defun org-at-heading-p (&optional ignored)
  22215. (outline-on-heading-p t))
  22216. ;; Compatibility alias with Org versions < 7.8.03
  22217. (defalias 'org-on-heading-p 'org-at-heading-p)
  22218. (defun org-in-commented-heading-p (&optional no-inheritance)
  22219. "Non-nil if point is under a commented heading.
  22220. This function also checks ancestors of the current headline,
  22221. unless optional argument NO-INHERITANCE is non-nil."
  22222. (cond
  22223. ((org-before-first-heading-p) nil)
  22224. ((let ((headline (nth 4 (org-heading-components))))
  22225. (and headline
  22226. (let ((case-fold-search nil))
  22227. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22228. headline)))))
  22229. (no-inheritance nil)
  22230. (t
  22231. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22232. (defun org-at-comment-p nil
  22233. "Is cursor in a commented line?"
  22234. (save-excursion
  22235. (save-match-data
  22236. (beginning-of-line)
  22237. (looking-at "^[ \t]*# "))))
  22238. (defun org-at-drawer-p nil
  22239. "Is cursor at a drawer keyword?"
  22240. (save-excursion
  22241. (move-beginning-of-line 1)
  22242. (looking-at org-drawer-regexp)))
  22243. (defun org-at-block-p nil
  22244. "Is cursor at a block keyword?"
  22245. (save-excursion
  22246. (move-beginning-of-line 1)
  22247. (looking-at org-block-regexp)))
  22248. (defun org-point-at-end-of-empty-headline ()
  22249. "If point is at the end of an empty headline, return t, else nil.
  22250. If the heading only contains a TODO keyword, it is still still considered
  22251. empty."
  22252. (and (looking-at "[ \t]*$")
  22253. (when org-todo-line-regexp
  22254. (save-excursion
  22255. (beginning-of-line 1)
  22256. (let ((case-fold-search nil))
  22257. (looking-at org-todo-line-regexp)
  22258. (string= (match-string 3) ""))))))
  22259. (defun org-at-heading-or-item-p ()
  22260. (or (org-at-heading-p) (org-at-item-p)))
  22261. (defun org-at-target-p ()
  22262. (or (org-in-regexp org-radio-target-regexp)
  22263. (org-in-regexp org-target-regexp)))
  22264. ;; Compatibility alias with Org versions < 7.8.03
  22265. (defalias 'org-on-target-p 'org-at-target-p)
  22266. (defun org-up-heading-all (arg)
  22267. "Move to the heading line of which the present line is a subheading.
  22268. This function considers both visible and invisible heading lines.
  22269. With argument, move up ARG levels."
  22270. (if (fboundp 'outline-up-heading-all)
  22271. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22272. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22273. (defun org-up-heading-safe ()
  22274. "Move to the heading line of which the present line is a subheading.
  22275. This version will not throw an error. It will return the level of the
  22276. headline found, or nil if no higher level is found.
  22277. Also, this function will be a lot faster than `outline-up-heading',
  22278. because it relies on stars being the outline starters. This can really
  22279. make a significant difference in outlines with very many siblings."
  22280. (when (ignore-errors (org-back-to-heading t))
  22281. (let ((level-up (1- (funcall outline-level))))
  22282. (and (> level-up 0)
  22283. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22284. (funcall outline-level)))))
  22285. (defun org-first-sibling-p ()
  22286. "Is this heading the first child of its parents?"
  22287. (interactive)
  22288. (let ((re org-outline-regexp-bol)
  22289. level l)
  22290. (unless (org-at-heading-p t)
  22291. (user-error "Not at a heading"))
  22292. (setq level (funcall outline-level))
  22293. (save-excursion
  22294. (if (not (re-search-backward re nil t))
  22295. t
  22296. (setq l (funcall outline-level))
  22297. (< l level)))))
  22298. (defun org-goto-sibling (&optional previous)
  22299. "Goto the next sibling, even if it is invisible.
  22300. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22301. when a sibling was found. When none is found, return nil and don't
  22302. move point."
  22303. (let ((fun (if previous 're-search-backward 're-search-forward))
  22304. (pos (point))
  22305. (re org-outline-regexp-bol)
  22306. level l)
  22307. (when (ignore-errors (org-back-to-heading t))
  22308. (setq level (funcall outline-level))
  22309. (catch 'exit
  22310. (or previous (forward-char 1))
  22311. (while (funcall fun re nil t)
  22312. (setq l (funcall outline-level))
  22313. (when (< l level) (goto-char pos) (throw 'exit nil))
  22314. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22315. (goto-char pos)
  22316. nil))))
  22317. (defun org-show-siblings ()
  22318. "Show all siblings of the current headline."
  22319. (save-excursion
  22320. (while (org-goto-sibling) (org-flag-heading nil)))
  22321. (save-excursion
  22322. (while (org-goto-sibling 'previous)
  22323. (org-flag-heading nil))))
  22324. (defun org-goto-first-child ()
  22325. "Goto the first child, even if it is invisible.
  22326. Return t when a child was found. Otherwise don't move point and
  22327. return nil."
  22328. (let (level (pos (point)) (re org-outline-regexp-bol))
  22329. (when (ignore-errors (org-back-to-heading t))
  22330. (setq level (outline-level))
  22331. (forward-char 1)
  22332. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22333. (progn (goto-char (match-beginning 0)) t)
  22334. (goto-char pos) nil))))
  22335. (defun org-show-hidden-entry ()
  22336. "Show an entry where even the heading is hidden."
  22337. (save-excursion
  22338. (org-show-entry)))
  22339. (defun org-flag-heading (flag &optional entry)
  22340. "Flag the current heading. FLAG non-nil means make invisible.
  22341. When ENTRY is non-nil, show the entire entry."
  22342. (save-excursion
  22343. (org-back-to-heading t)
  22344. ;; Check if we should show the entire entry
  22345. (if entry
  22346. (progn
  22347. (org-show-entry)
  22348. (save-excursion
  22349. (and (outline-next-heading)
  22350. (org-flag-heading nil))))
  22351. (outline-flag-region (max (point-min) (1- (point)))
  22352. (save-excursion (outline-end-of-heading) (point))
  22353. flag))))
  22354. (defun org-get-next-sibling ()
  22355. "Move to next heading of the same level, and return point.
  22356. If there is no such heading, return nil.
  22357. This is like outline-next-sibling, but invisible headings are ok."
  22358. (let ((level (funcall outline-level)))
  22359. (outline-next-heading)
  22360. (while (and (not (eobp)) (> (funcall outline-level) level))
  22361. (outline-next-heading))
  22362. (if (or (eobp) (< (funcall outline-level) level))
  22363. nil
  22364. (point))))
  22365. (defun org-get-last-sibling ()
  22366. "Move to previous heading of the same level, and return point.
  22367. If there is no such heading, return nil."
  22368. (let ((opoint (point))
  22369. (level (funcall outline-level)))
  22370. (outline-previous-heading)
  22371. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22372. (while (and (> (funcall outline-level) level)
  22373. (not (bobp)))
  22374. (outline-previous-heading))
  22375. (if (< (funcall outline-level) level)
  22376. nil
  22377. (point)))))
  22378. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22379. "Goto to the end of a subtree."
  22380. ;; This contains an exact copy of the original function, but it uses
  22381. ;; `org-back-to-heading', to make it work also in invisible
  22382. ;; trees. And is uses an invisible-ok argument.
  22383. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22384. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22385. ;; with many children and grandchildren etc, this can be much faster
  22386. ;; than the outline version.
  22387. (org-back-to-heading invisible-ok)
  22388. (let ((first t)
  22389. (level (funcall outline-level)))
  22390. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22391. ;; A true heading (not a plain list item), in Org-mode
  22392. ;; This means we can easily find the end by looking
  22393. ;; only for the right number of stars. Using a regexp to do
  22394. ;; this is so much faster than using a Lisp loop.
  22395. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22396. (forward-char 1)
  22397. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22398. ;; something else, do it the slow way
  22399. (while (and (not (eobp))
  22400. (or first (> (funcall outline-level) level)))
  22401. (setq first nil)
  22402. (outline-next-heading)))
  22403. (unless to-heading
  22404. (if (memq (preceding-char) '(?\n ?\^M))
  22405. (progn
  22406. ;; Go to end of line before heading
  22407. (forward-char -1)
  22408. (if (memq (preceding-char) '(?\n ?\^M))
  22409. ;; leave blank line before heading
  22410. (forward-char -1))))))
  22411. (point))
  22412. (defun org-end-of-meta-data (&optional full)
  22413. "Skip planning line and properties drawer in current entry.
  22414. When optional argument FULL is non-nil, also skip empty lines,
  22415. clocking lines and regular drawers at the beginning of the
  22416. entry."
  22417. (org-back-to-heading t)
  22418. (forward-line)
  22419. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22420. (when (looking-at org-property-drawer-re)
  22421. (goto-char (match-end 0))
  22422. (forward-line))
  22423. (when (and full (not (org-at-heading-p)))
  22424. (catch 'exit
  22425. (let ((end (save-excursion (outline-next-heading) (point)))
  22426. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22427. (while (not (eobp))
  22428. (cond ((org-looking-at-p org-drawer-regexp)
  22429. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22430. (forward-line)
  22431. (throw 'exit t)))
  22432. ((org-looking-at-p re) (forward-line))
  22433. (t (throw 'exit t))))))))
  22434. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22435. "Move forward to the ARG'th subheading at same level as this one.
  22436. Stop at the first and last subheadings of a superior heading.
  22437. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22438. non-nil it will also look at invisible ones."
  22439. (interactive "p")
  22440. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22441. (if (and arg (< arg 0))
  22442. (goto-char (point-min))
  22443. (outline-next-heading))
  22444. (org-at-heading-p)
  22445. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22446. (f (if (and arg (< arg 0))
  22447. 're-search-backward
  22448. 're-search-forward))
  22449. (count (if arg (abs arg) 1))
  22450. (result (point)))
  22451. (while (and (prog1 (> count 0)
  22452. (forward-char (if (and arg (< arg 0)) -1 1)))
  22453. (funcall f org-outline-regexp-bol nil 'move))
  22454. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22455. (cond ((< l level) (setq count 0))
  22456. ((and (= l level)
  22457. (or invisible-ok
  22458. (progn
  22459. (goto-char (line-beginning-position))
  22460. (not (outline-invisible-p)))))
  22461. (setq count (1- count))
  22462. (when (eq l level)
  22463. (setq result (point)))))))
  22464. (goto-char result))
  22465. (beginning-of-line 1)))
  22466. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22467. "Move backward to the ARG'th subheading at same level as this one.
  22468. Stop at the first and last subheadings of a superior heading."
  22469. (interactive "p")
  22470. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22471. (defun org-next-visible-heading (arg)
  22472. "Move to the next visible heading.
  22473. This function wraps `outline-next-visible-heading' with
  22474. `org-with-limited-levels' in order to skip over inline tasks and
  22475. respect customization of `org-odd-levels-only'."
  22476. (interactive "p")
  22477. (org-with-limited-levels
  22478. (outline-next-visible-heading arg)))
  22479. (defun org-previous-visible-heading (arg)
  22480. "Move to the next visible heading.
  22481. This function wraps `outline-previous-visible-heading' with
  22482. `org-with-limited-levels' in order to skip over inline tasks and
  22483. respect customization of `org-odd-levels-only'."
  22484. (interactive "p")
  22485. (org-with-limited-levels
  22486. (outline-previous-visible-heading arg)))
  22487. (defun org-next-block (arg &optional backward block-regexp)
  22488. "Jump to the next block.
  22489. With a prefix argument ARG, jump forward ARG many blocks.
  22490. When BACKWARD is non-nil, jump to the previous block.
  22491. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22492. Match data is set according to this regexp when the function
  22493. returns.
  22494. Return point at beginning of the opening line of found block.
  22495. Throw an error if no block is found."
  22496. (interactive "p")
  22497. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22498. (case-fold-search t)
  22499. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22500. (count (or arg 1))
  22501. (origin (point))
  22502. last-element)
  22503. (if backward (beginning-of-line) (end-of-line))
  22504. (while (and (> count 0) (funcall search-fn re nil t))
  22505. (let ((element (save-excursion
  22506. (goto-char (match-beginning 0))
  22507. (save-match-data (org-element-at-point)))))
  22508. (when (and (memq (org-element-type element)
  22509. '(center-block comment-block dynamic-block
  22510. example-block export-block quote-block
  22511. special-block src-block verse-block))
  22512. (<= (match-beginning 0)
  22513. (org-element-property :post-affiliated element)))
  22514. (setq last-element element)
  22515. (decf count))))
  22516. (if (= count 0)
  22517. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22518. (save-match-data (org-show-context)))
  22519. (goto-char origin)
  22520. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22521. (defun org-previous-block (arg &optional block-regexp)
  22522. "Jump to the previous block.
  22523. With a prefix argument ARG, jump backward ARG many source blocks.
  22524. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22525. (interactive "p")
  22526. (org-next-block arg t block-regexp))
  22527. (defun org-forward-paragraph ()
  22528. "Move forward to beginning of next paragraph or equivalent.
  22529. The function moves point to the beginning of the next visible
  22530. structural element, which can be a paragraph, a table, a list
  22531. item, etc. It also provides some special moves for convenience:
  22532. - On an affiliated keyword, jump to the beginning of the
  22533. relative element.
  22534. - On an item or a footnote definition, move to the second
  22535. element inside, if any.
  22536. - On a table or a property drawer, jump after it.
  22537. - On a verse or source block, stop after blank lines."
  22538. (interactive)
  22539. (when (eobp) (user-error "Cannot move further down"))
  22540. (let* ((deactivate-mark nil)
  22541. (element (org-element-at-point))
  22542. (type (org-element-type element))
  22543. (post-affiliated (org-element-property :post-affiliated element))
  22544. (contents-begin (org-element-property :contents-begin element))
  22545. (contents-end (org-element-property :contents-end element))
  22546. (end (let ((end (org-element-property :end element)) (parent element))
  22547. (while (and (setq parent (org-element-property :parent parent))
  22548. (= (org-element-property :contents-end parent) end))
  22549. (setq end (org-element-property :end parent)))
  22550. end)))
  22551. (cond ((not element)
  22552. (skip-chars-forward " \r\t\n")
  22553. (or (eobp) (beginning-of-line)))
  22554. ;; On affiliated keywords, move to element's beginning.
  22555. ((< (point) post-affiliated)
  22556. (goto-char post-affiliated))
  22557. ;; At a table row, move to the end of the table. Similarly,
  22558. ;; at a node property, move to the end of the property
  22559. ;; drawer.
  22560. ((memq type '(node-property table-row))
  22561. (goto-char (org-element-property
  22562. :end (org-element-property :parent element))))
  22563. ((memq type '(property-drawer table)) (goto-char end))
  22564. ;; Consider blank lines as separators in verse and source
  22565. ;; blocks to ease editing.
  22566. ((memq type '(src-block verse-block))
  22567. (when (eq type 'src-block)
  22568. (setq contents-end
  22569. (save-excursion (goto-char end)
  22570. (skip-chars-backward " \r\t\n")
  22571. (line-beginning-position))))
  22572. (beginning-of-line)
  22573. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22574. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22575. (goto-char end)
  22576. (skip-chars-forward " \r\t\n")
  22577. (if (= (point) contents-end) (goto-char end)
  22578. (beginning-of-line))))
  22579. ;; With no contents, just skip element.
  22580. ((not contents-begin) (goto-char end))
  22581. ;; If contents are invisible, skip the element altogether.
  22582. ((outline-invisible-p (line-end-position))
  22583. (case type
  22584. (headline
  22585. (org-with-limited-levels (outline-next-visible-heading 1)))
  22586. ;; At a plain list, make sure we move to the next item
  22587. ;; instead of skipping the whole list.
  22588. (plain-list (forward-char)
  22589. (org-forward-paragraph))
  22590. (otherwise (goto-char end))))
  22591. ((>= (point) contents-end) (goto-char end))
  22592. ((>= (point) contents-begin)
  22593. ;; This can only happen on paragraphs and plain lists.
  22594. (case type
  22595. (paragraph (goto-char end))
  22596. ;; At a plain list, try to move to second element in
  22597. ;; first item, if possible.
  22598. (plain-list (end-of-line)
  22599. (org-forward-paragraph))))
  22600. ;; When contents start on the middle of a line (e.g. in
  22601. ;; items and footnote definitions), try to reach first
  22602. ;; element starting after current line.
  22603. ((> (line-end-position) contents-begin)
  22604. (end-of-line)
  22605. (org-forward-paragraph))
  22606. (t (goto-char contents-begin)))))
  22607. (defun org-backward-paragraph ()
  22608. "Move backward to start of previous paragraph or equivalent.
  22609. The function moves point to the beginning of the current
  22610. structural element, which can be a paragraph, a table, a list
  22611. item, etc., or to the beginning of the previous visible one if
  22612. point is already there. It also provides some special moves for
  22613. convenience:
  22614. - On an affiliated keyword, jump to the first one.
  22615. - On a table or a property drawer, move to its beginning.
  22616. - On a verse or source block, stop before blank lines."
  22617. (interactive)
  22618. (when (bobp) (user-error "Cannot move further up"))
  22619. (let* ((deactivate-mark nil)
  22620. (element (org-element-at-point))
  22621. (type (org-element-type element))
  22622. (contents-begin (org-element-property :contents-begin element))
  22623. (contents-end (org-element-property :contents-end element))
  22624. (post-affiliated (org-element-property :post-affiliated element))
  22625. (begin (org-element-property :begin element)))
  22626. (cond
  22627. ((not element) (goto-char (point-min)))
  22628. ((= (point) begin)
  22629. (backward-char)
  22630. (org-backward-paragraph))
  22631. ((<= (point) post-affiliated) (goto-char begin))
  22632. ((memq type '(node-property table-row))
  22633. (goto-char (org-element-property
  22634. :post-affiliated (org-element-property :parent element))))
  22635. ((memq type '(property-drawer table)) (goto-char begin))
  22636. ((memq type '(src-block verse-block))
  22637. (when (eq type 'src-block)
  22638. (setq contents-begin
  22639. (save-excursion (goto-char begin) (forward-line) (point))))
  22640. (if (= (point) contents-begin) (goto-char post-affiliated)
  22641. ;; Inside a verse block, see blank lines as paragraph
  22642. ;; separators.
  22643. (let ((origin (point)))
  22644. (skip-chars-backward " \r\t\n" contents-begin)
  22645. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22646. (skip-chars-forward " \r\t\n" origin)
  22647. (if (= (point) origin) (goto-char contents-begin)
  22648. (beginning-of-line))))))
  22649. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22650. ((eq type 'paragraph)
  22651. (goto-char contents-begin)
  22652. ;; When at first paragraph in an item or a footnote definition,
  22653. ;; move directly to beginning of line.
  22654. (let ((parent-contents
  22655. (org-element-property
  22656. :contents-begin (org-element-property :parent element))))
  22657. (when (and parent-contents (= parent-contents contents-begin))
  22658. (beginning-of-line))))
  22659. ;; At the end of a greater element, move to the beginning of the
  22660. ;; last element within.
  22661. ((>= (point) contents-end)
  22662. (goto-char (1- contents-end))
  22663. (org-backward-paragraph))
  22664. (t (goto-char (or post-affiliated begin))))
  22665. ;; Ensure we never leave point invisible.
  22666. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22667. (defun org-forward-element ()
  22668. "Move forward by one element.
  22669. Move to the next element at the same level, when possible."
  22670. (interactive)
  22671. (cond ((eobp) (user-error "Cannot move further down"))
  22672. ((org-with-limited-levels (org-at-heading-p))
  22673. (let ((origin (point)))
  22674. (goto-char (org-end-of-subtree nil t))
  22675. (unless (org-with-limited-levels (org-at-heading-p))
  22676. (goto-char origin)
  22677. (user-error "Cannot move further down"))))
  22678. (t
  22679. (let* ((elem (org-element-at-point))
  22680. (end (org-element-property :end elem))
  22681. (parent (org-element-property :parent elem)))
  22682. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22683. (goto-char (org-element-property :end parent)))
  22684. ((integer-or-marker-p end) (goto-char end))
  22685. (t (message "No element at point")))))))
  22686. (defun org-backward-element ()
  22687. "Move backward by one element.
  22688. Move to the previous element at the same level, when possible."
  22689. (interactive)
  22690. (cond ((bobp) (user-error "Cannot move further up"))
  22691. ((org-with-limited-levels (org-at-heading-p))
  22692. ;; At a headline, move to the previous one, if any, or stay
  22693. ;; here.
  22694. (let ((origin (point)))
  22695. (org-with-limited-levels (org-backward-heading-same-level 1))
  22696. ;; When current headline has no sibling above, move to its
  22697. ;; parent.
  22698. (when (= (point) origin)
  22699. (or (org-with-limited-levels (org-up-heading-safe))
  22700. (progn (goto-char origin)
  22701. (user-error "Cannot move further up"))))))
  22702. (t
  22703. (let* ((elem (org-element-at-point))
  22704. (beg (org-element-property :begin elem)))
  22705. (cond
  22706. ;; Move to beginning of current element if point isn't
  22707. ;; there already.
  22708. ((null beg) (message "No element at point"))
  22709. ((/= (point) beg) (goto-char beg))
  22710. (t (goto-char beg)
  22711. (skip-chars-backward " \r\t\n")
  22712. (unless (bobp)
  22713. (let ((prev (org-element-at-point)))
  22714. (goto-char (org-element-property :begin prev))
  22715. (while (and (setq prev (org-element-property :parent prev))
  22716. (<= (org-element-property :end prev) beg))
  22717. (goto-char (org-element-property :begin prev)))))))))))
  22718. (defun org-up-element ()
  22719. "Move to upper element."
  22720. (interactive)
  22721. (if (org-with-limited-levels (org-at-heading-p))
  22722. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22723. (let* ((elem (org-element-at-point))
  22724. (parent (org-element-property :parent elem)))
  22725. (if parent (goto-char (org-element-property :begin parent))
  22726. (if (org-with-limited-levels (org-before-first-heading-p))
  22727. (user-error "No surrounding element")
  22728. (org-with-limited-levels (org-back-to-heading)))))))
  22729. (defvar org-element-greater-elements)
  22730. (defun org-down-element ()
  22731. "Move to inner element."
  22732. (interactive)
  22733. (let ((element (org-element-at-point)))
  22734. (cond
  22735. ((memq (org-element-type element) '(plain-list table))
  22736. (goto-char (org-element-property :contents-begin element))
  22737. (forward-char))
  22738. ((memq (org-element-type element) org-element-greater-elements)
  22739. ;; If contents are hidden, first disclose them.
  22740. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22741. (goto-char (or (org-element-property :contents-begin element)
  22742. (user-error "No content for this element"))))
  22743. (t (user-error "No inner element")))))
  22744. (defun org-drag-element-backward ()
  22745. "Move backward element at point."
  22746. (interactive)
  22747. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22748. (let* ((elem (org-element-at-point))
  22749. (prev-elem
  22750. (save-excursion
  22751. (goto-char (org-element-property :begin elem))
  22752. (skip-chars-backward " \r\t\n")
  22753. (unless (bobp)
  22754. (let* ((beg (org-element-property :begin elem))
  22755. (prev (org-element-at-point))
  22756. (up prev))
  22757. (while (and (setq up (org-element-property :parent up))
  22758. (<= (org-element-property :end up) beg))
  22759. (setq prev up))
  22760. prev)))))
  22761. ;; Error out if no previous element or previous element is
  22762. ;; a parent of the current one.
  22763. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22764. (user-error "Cannot drag element backward")
  22765. (let ((pos (point)))
  22766. (org-element-swap-A-B prev-elem elem)
  22767. (goto-char (+ (org-element-property :begin prev-elem)
  22768. (- pos (org-element-property :begin elem)))))))))
  22769. (defun org-drag-element-forward ()
  22770. "Move forward element at point."
  22771. (interactive)
  22772. (let* ((pos (point))
  22773. (elem (org-element-at-point)))
  22774. (when (= (point-max) (org-element-property :end elem))
  22775. (user-error "Cannot drag element forward"))
  22776. (goto-char (org-element-property :end elem))
  22777. (let ((next-elem (org-element-at-point)))
  22778. (when (or (org-element-nested-p elem next-elem)
  22779. (and (eq (org-element-type next-elem) 'headline)
  22780. (not (eq (org-element-type elem) 'headline))))
  22781. (goto-char pos)
  22782. (user-error "Cannot drag element forward"))
  22783. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22784. ;; body's length (without final blanks) and by the length of
  22785. ;; blanks between ELEM and NEXT-ELEM.
  22786. (let ((size-next (- (save-excursion
  22787. (goto-char (org-element-property :end next-elem))
  22788. (skip-chars-backward " \r\t\n")
  22789. (forward-line)
  22790. ;; Small correction if buffer doesn't end
  22791. ;; with a newline character.
  22792. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22793. (org-element-property :begin next-elem)))
  22794. (size-blank (- (org-element-property :end elem)
  22795. (save-excursion
  22796. (goto-char (org-element-property :end elem))
  22797. (skip-chars-backward " \r\t\n")
  22798. (forward-line)
  22799. (point)))))
  22800. (org-element-swap-A-B elem next-elem)
  22801. (goto-char (+ pos size-next size-blank))))))
  22802. (defun org-drag-line-forward (arg)
  22803. "Drag the line at point ARG lines forward."
  22804. (interactive "p")
  22805. (dotimes (n (abs arg))
  22806. (let ((c (current-column)))
  22807. (if (< 0 arg)
  22808. (progn
  22809. (beginning-of-line 2)
  22810. (transpose-lines 1)
  22811. (beginning-of-line 0))
  22812. (transpose-lines 1)
  22813. (beginning-of-line -1))
  22814. (org-move-to-column c))))
  22815. (defun org-drag-line-backward (arg)
  22816. "Drag the line at point ARG lines backward."
  22817. (interactive "p")
  22818. (org-drag-line-forward (- arg)))
  22819. (defun org-mark-element ()
  22820. "Put point at beginning of this element, mark at end.
  22821. Interactively, if this command is repeated or (in Transient Mark
  22822. mode) if the mark is active, it marks the next element after the
  22823. ones already marked."
  22824. (interactive)
  22825. (let (deactivate-mark)
  22826. (if (and (org-called-interactively-p 'any)
  22827. (or (and (eq last-command this-command) (mark t))
  22828. (and transient-mark-mode mark-active)))
  22829. (set-mark
  22830. (save-excursion
  22831. (goto-char (mark))
  22832. (goto-char (org-element-property :end (org-element-at-point)))))
  22833. (let ((element (org-element-at-point)))
  22834. (end-of-line)
  22835. (push-mark (org-element-property :end element) t t)
  22836. (goto-char (org-element-property :begin element))))))
  22837. (defun org-narrow-to-element ()
  22838. "Narrow buffer to current element."
  22839. (interactive)
  22840. (let ((elem (org-element-at-point)))
  22841. (cond
  22842. ((eq (car elem) 'headline)
  22843. (narrow-to-region
  22844. (org-element-property :begin elem)
  22845. (org-element-property :end elem)))
  22846. ((memq (car elem) org-element-greater-elements)
  22847. (narrow-to-region
  22848. (org-element-property :contents-begin elem)
  22849. (org-element-property :contents-end elem)))
  22850. (t
  22851. (narrow-to-region
  22852. (org-element-property :begin elem)
  22853. (org-element-property :end elem))))))
  22854. (defun org-transpose-element ()
  22855. "Transpose current and previous elements, keeping blank lines between.
  22856. Point is moved after both elements."
  22857. (interactive)
  22858. (org-skip-whitespace)
  22859. (let ((end (org-element-property :end (org-element-at-point))))
  22860. (org-drag-element-backward)
  22861. (goto-char end)))
  22862. (defun org-unindent-buffer ()
  22863. "Un-indent the visible part of the buffer.
  22864. Relative indentation (between items, inside blocks, etc.) isn't
  22865. modified."
  22866. (interactive)
  22867. (unless (eq major-mode 'org-mode)
  22868. (user-error "Cannot un-indent a buffer not in Org mode"))
  22869. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22870. unindent-tree ; For byte-compiler.
  22871. (unindent-tree
  22872. (function
  22873. (lambda (contents)
  22874. (mapc
  22875. (lambda (element)
  22876. (if (memq (org-element-type element) '(headline section))
  22877. (funcall unindent-tree (org-element-contents element))
  22878. (save-excursion
  22879. (save-restriction
  22880. (narrow-to-region
  22881. (org-element-property :begin element)
  22882. (org-element-property :end element))
  22883. (org-do-remove-indentation)))))
  22884. (reverse contents))))))
  22885. (funcall unindent-tree (org-element-contents parse-tree))))
  22886. (defun org-show-children (&optional level)
  22887. "Show all direct subheadings of this heading.
  22888. Prefix arg LEVEL is how many levels below the current level
  22889. should be shown. Default is enough to cause the following
  22890. heading to appear."
  22891. (interactive "p")
  22892. (save-excursion
  22893. (org-back-to-heading t)
  22894. (let* ((current-level (funcall outline-level))
  22895. (max-level (org-get-valid-level
  22896. current-level (if level (prefix-numeric-value level) 1)))
  22897. (end (save-excursion (org-end-of-subtree t t)))
  22898. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22899. (past-first-child nil)
  22900. ;; Make sure to skip inlinetasks.
  22901. (re (format regexp-fmt
  22902. current-level
  22903. (cond
  22904. ((not (featurep 'org-inlinetask)) "")
  22905. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22906. 3))
  22907. (t (1- org-inlinetask-min-level))))))
  22908. ;; Display parent heading.
  22909. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22910. (forward-line)
  22911. ;; Display children. First child may be deeper than expected
  22912. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22913. ;; MAX-LEVEL accordingly.
  22914. (while (re-search-forward re end t)
  22915. (unless past-first-child
  22916. (setq re (format regexp-fmt
  22917. current-level
  22918. (max (funcall outline-level) max-level)))
  22919. (setq past-first-child t))
  22920. (outline-flag-region (line-end-position 0) (line-end-position) nil)))))
  22921. (defun org-show-subtree ()
  22922. "Show everything after this heading at deeper levels."
  22923. (interactive)
  22924. (outline-flag-region
  22925. (point)
  22926. (save-excursion
  22927. (org-end-of-subtree t t))
  22928. nil))
  22929. (defun org-show-entry ()
  22930. "Show the body directly following this heading.
  22931. Show the heading too, if it is currently invisible."
  22932. (interactive)
  22933. (save-excursion
  22934. (ignore-errors
  22935. (org-back-to-heading t)
  22936. (outline-flag-region
  22937. (max (point-min) (1- (point)))
  22938. (save-excursion
  22939. (if (re-search-forward
  22940. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22941. (match-beginning 1)
  22942. (point-max)))
  22943. nil)
  22944. (org-cycle-hide-drawers 'children))))
  22945. (defun org-make-options-regexp (kwds &optional extra)
  22946. "Make a regular expression for keyword lines.
  22947. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22948. when non-nil, is a regexp matching keywords names."
  22949. (concat "^[ \t]*#\\+\\("
  22950. (regexp-opt kwds)
  22951. (and extra (concat (and kwds "\\|") extra))
  22952. "\\):[ \t]*\\(.*\\)"))
  22953. ;; Make isearch reveal the necessary context
  22954. (defun org-isearch-end ()
  22955. "Reveal context after isearch exits."
  22956. (when isearch-success ; only if search was successful
  22957. (if (featurep 'xemacs)
  22958. ;; Under XEmacs, the hook is run in the correct place,
  22959. ;; we directly show the context.
  22960. (org-show-context 'isearch)
  22961. ;; In Emacs the hook runs *before* restoring the overlays.
  22962. ;; So we have to use a one-time post-command-hook to do this.
  22963. ;; (Emacs 22 has a special variable, see function `org-mode')
  22964. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22965. isearch-mode-end-hook-quit)
  22966. ;; Only when the isearch was not quitted.
  22967. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22968. 'append 'local)))))
  22969. (defun org-isearch-post-command ()
  22970. "Remove self from hook, and show context."
  22971. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22972. (org-show-context 'isearch))
  22973. ;;;; Integration with and fixes for other packages
  22974. ;;; Imenu support
  22975. (defvar org-imenu-markers nil
  22976. "All markers currently used by Imenu.")
  22977. (make-variable-buffer-local 'org-imenu-markers)
  22978. (defun org-imenu-new-marker (&optional pos)
  22979. "Return a new marker for use by Imenu, and remember the marker."
  22980. (let ((m (make-marker)))
  22981. (move-marker m (or pos (point)))
  22982. (push m org-imenu-markers)
  22983. m))
  22984. (defun org-imenu-get-tree ()
  22985. "Produce the index for Imenu."
  22986. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22987. (setq org-imenu-markers nil)
  22988. (let* ((n org-imenu-depth)
  22989. (re (concat "^" (org-get-limited-outline-regexp)))
  22990. (subs (make-vector (1+ n) nil))
  22991. (last-level 0)
  22992. m level head0 head)
  22993. (save-excursion
  22994. (save-restriction
  22995. (widen)
  22996. (goto-char (point-max))
  22997. (while (re-search-backward re nil t)
  22998. (setq level (org-reduced-level (funcall outline-level)))
  22999. (when (and (<= level n)
  23000. (looking-at org-complex-heading-regexp)
  23001. (setq head0 (org-match-string-no-properties 4)))
  23002. (setq head (org-link-display-format head0)
  23003. m (org-imenu-new-marker))
  23004. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  23005. (if (>= level last-level)
  23006. (push (cons head m) (aref subs level))
  23007. (push (cons head (aref subs (1+ level))) (aref subs level))
  23008. (loop for i from (1+ level) to n do (aset subs i nil)))
  23009. (setq last-level level)))))
  23010. (aref subs 1)))
  23011. (eval-after-load "imenu"
  23012. '(progn
  23013. (add-hook 'imenu-after-jump-hook
  23014. (lambda ()
  23015. (if (derived-mode-p 'org-mode)
  23016. (org-show-context 'org-goto))))))
  23017. (defun org-link-display-format (link)
  23018. "Replace a link with its the description.
  23019. If there is no description, use the link target."
  23020. (save-match-data
  23021. (if (string-match org-bracket-link-analytic-regexp link)
  23022. (replace-match (if (match-end 5)
  23023. (match-string 5 link)
  23024. (concat (match-string 1 link)
  23025. (match-string 3 link)))
  23026. nil t link)
  23027. link)))
  23028. (defun org-toggle-link-display ()
  23029. "Toggle the literal or descriptive display of links."
  23030. (interactive)
  23031. (if org-descriptive-links
  23032. (progn (org-remove-from-invisibility-spec '(org-link))
  23033. (org-restart-font-lock)
  23034. (setq org-descriptive-links nil))
  23035. (progn (add-to-invisibility-spec '(org-link))
  23036. (org-restart-font-lock)
  23037. (setq org-descriptive-links t))))
  23038. ;; Speedbar support
  23039. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23040. "Overlay marking the agenda restriction line in speedbar.")
  23041. (overlay-put org-speedbar-restriction-lock-overlay
  23042. 'face 'org-agenda-restriction-lock)
  23043. (overlay-put org-speedbar-restriction-lock-overlay
  23044. 'help-echo "Agendas are currently limited to this item.")
  23045. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  23046. (defun org-speedbar-set-agenda-restriction ()
  23047. "Restrict future agenda commands to the location at point in speedbar.
  23048. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23049. (interactive)
  23050. (require 'org-agenda)
  23051. (let (p m tp np dir txt)
  23052. (cond
  23053. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23054. 'org-imenu t))
  23055. (setq m (get-text-property p 'org-imenu-marker))
  23056. (with-current-buffer (marker-buffer m)
  23057. (goto-char m)
  23058. (org-agenda-set-restriction-lock 'subtree)))
  23059. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23060. 'speedbar-function 'speedbar-find-file))
  23061. (setq tp (previous-single-property-change
  23062. (1+ p) 'speedbar-function)
  23063. np (next-single-property-change
  23064. tp 'speedbar-function)
  23065. dir (speedbar-line-directory)
  23066. txt (buffer-substring-no-properties (or tp (point-min))
  23067. (or np (point-max))))
  23068. (with-current-buffer (find-file-noselect
  23069. (let ((default-directory dir))
  23070. (expand-file-name txt)))
  23071. (unless (derived-mode-p 'org-mode)
  23072. (user-error "Cannot restrict to non-Org-mode file"))
  23073. (org-agenda-set-restriction-lock 'file)))
  23074. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23075. (move-overlay org-speedbar-restriction-lock-overlay
  23076. (point-at-bol) (point-at-eol))
  23077. (setq current-prefix-arg nil)
  23078. (org-agenda-maybe-redo)))
  23079. (defvar speedbar-file-key-map)
  23080. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23081. (eval-after-load "speedbar"
  23082. '(progn
  23083. (speedbar-add-supported-extension ".org")
  23084. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23085. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23086. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23087. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23088. (add-hook 'speedbar-visiting-tag-hook
  23089. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23090. ;;; Fixes and Hacks for problems with other packages
  23091. (defun org--flyspell-object-check-p (element)
  23092. "Non-nil when Flyspell can check object at point.
  23093. ELEMENT is the element at point."
  23094. (let ((object (save-excursion
  23095. (when (org-looking-at-p "\\>") (backward-char))
  23096. (org-element-context element))))
  23097. (case (org-element-type object)
  23098. ;; Prevent checks in links due to keybinding conflict with
  23099. ;; Flyspell.
  23100. ((code entity export-snippet inline-babel-call
  23101. inline-src-block line-break latex-fragment link macro
  23102. statistics-cookie target timestamp verbatim)
  23103. nil)
  23104. (footnote-reference
  23105. ;; Only in inline footnotes, within the definition.
  23106. (and (eq (org-element-property :type object) 'inline)
  23107. (< (save-excursion
  23108. (goto-char (org-element-property :begin object))
  23109. (search-forward ":" nil t 2))
  23110. (point))))
  23111. (otherwise t))))
  23112. (defun org-mode-flyspell-verify ()
  23113. "Function used for `flyspell-generic-check-word-predicate'."
  23114. (if (org-at-heading-p)
  23115. ;; At a headline or an inlinetask, check title only. This is
  23116. ;; faster than relying on `org-element-at-point'.
  23117. (and (save-excursion (beginning-of-line)
  23118. (and (let ((case-fold-search t))
  23119. (not (looking-at "\\*+ END[ \t]*$")))
  23120. (looking-at org-complex-heading-regexp)))
  23121. (match-beginning 4)
  23122. (>= (point) (match-beginning 4))
  23123. (or (not (match-beginning 5))
  23124. (< (point) (match-beginning 5))))
  23125. (let* ((element (org-element-at-point))
  23126. (post-affiliated (org-element-property :post-affiliated element)))
  23127. (cond
  23128. ;; Ignore checks in all affiliated keywords but captions.
  23129. ((< (point) post-affiliated)
  23130. (and (save-excursion
  23131. (beginning-of-line)
  23132. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23133. (> (point) (match-end 0))
  23134. (org--flyspell-object-check-p element)))
  23135. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23136. ((let ((log (org-log-into-drawer)))
  23137. (and log
  23138. (let ((drawer (org-element-lineage element '(drawer))))
  23139. (and drawer
  23140. (eq (compare-strings
  23141. log nil nil
  23142. (org-element-property :drawer-name drawer) nil nil t)
  23143. t)))))
  23144. nil)
  23145. (t
  23146. (case (org-element-type element)
  23147. ((comment quote-section) t)
  23148. (comment-block
  23149. ;; Allow checks between block markers, not on them.
  23150. (and (> (line-beginning-position) post-affiliated)
  23151. (save-excursion
  23152. (end-of-line)
  23153. (skip-chars-forward " \r\t\n")
  23154. (< (point) (org-element-property :end element)))))
  23155. ;; Arbitrary list of keywords where checks are meaningful.
  23156. ;; Make sure point is on the value part of the element.
  23157. (keyword
  23158. (and (member (org-element-property :key element)
  23159. '("DESCRIPTION" "TITLE"))
  23160. (save-excursion
  23161. (search-backward ":" (line-beginning-position) t))))
  23162. ;; Check is globally allowed in paragraphs verse blocks and
  23163. ;; table rows (after affiliated keywords) but some objects
  23164. ;; must not be affected.
  23165. ((paragraph table-row verse-block)
  23166. (let ((cbeg (org-element-property :contents-begin element))
  23167. (cend (org-element-property :contents-end element)))
  23168. (and cbeg (>= (point) cbeg) (< (point) cend)
  23169. (org--flyspell-object-check-p element))))))))))
  23170. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23171. (defun org-remove-flyspell-overlays-in (beg end)
  23172. "Remove flyspell overlays in region."
  23173. (and (org-bound-and-true-p flyspell-mode)
  23174. (fboundp 'flyspell-delete-region-overlays)
  23175. (flyspell-delete-region-overlays beg end)))
  23176. (defvar flyspell-delayed-commands)
  23177. (eval-after-load "flyspell"
  23178. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23179. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23180. (eval-after-load "bookmark"
  23181. '(if (boundp 'bookmark-after-jump-hook)
  23182. ;; We can use the hook
  23183. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23184. ;; Hook not available, use advice
  23185. (defadvice bookmark-jump (after org-make-visible activate)
  23186. "Make the position visible."
  23187. (org-bookmark-jump-unhide))))
  23188. ;; Make sure saveplace shows the location if it was hidden
  23189. (eval-after-load "saveplace"
  23190. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23191. "Make the position visible."
  23192. (org-bookmark-jump-unhide)))
  23193. ;; Make sure ecb shows the location if it was hidden
  23194. (eval-after-load "ecb"
  23195. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23196. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23197. (if (derived-mode-p 'org-mode)
  23198. (org-show-context))))
  23199. (defun org-bookmark-jump-unhide ()
  23200. "Unhide the current position, to show the bookmark location."
  23201. (and (derived-mode-p 'org-mode)
  23202. (or (outline-invisible-p)
  23203. (save-excursion (goto-char (max (point-min) (1- (point))))
  23204. (outline-invisible-p)))
  23205. (org-show-context 'bookmark-jump)))
  23206. (defun org-mark-jump-unhide ()
  23207. "Make the point visible with `org-show-context' after jumping to the mark."
  23208. (when (and (derived-mode-p 'org-mode)
  23209. (outline-invisible-p))
  23210. (org-show-context 'mark-goto)))
  23211. (eval-after-load "simple"
  23212. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23213. "Make the point visible with `org-show-context'."
  23214. (org-mark-jump-unhide)))
  23215. (eval-after-load "simple"
  23216. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23217. "Make the point visible with `org-show-context'."
  23218. (org-mark-jump-unhide)))
  23219. (eval-after-load "simple"
  23220. '(defadvice pop-global-mark (after org-make-visible activate)
  23221. "Make the point visible with `org-show-context'."
  23222. (org-mark-jump-unhide)))
  23223. ;; Make session.el ignore our circular variable
  23224. (defvar session-globals-exclude)
  23225. (eval-after-load "session"
  23226. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23227. ;;;; Finish up
  23228. (provide 'org)
  23229. (run-hooks 'org-load-hook)
  23230. ;;; org.el ends here