org.el 938 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-set-constants "org-table" ())
  133. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  134. (declare-function org-id-get-create "org-id" (&optional force))
  135. (declare-function org-add-archive-files "org-archive" (files))
  136. (declare-function org-id-find-id-file "org-id" (id))
  137. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  138. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  139. (declare-function org-agenda-redo "org-agenda" (&optional all))
  140. (declare-function org-table-align "org-table" ())
  141. (declare-function org-table-begin "org-table" (&optional table-type))
  142. (declare-function org-table-blank-field "org-table" ())
  143. (declare-function org-table-end "org-table" (&optional table-type))
  144. (declare-function org-table-end-of-field "org-table" (&optional n))
  145. (declare-function org-table-insert-row "org-table" (&optional arg))
  146. (declare-function org-table-paste-rectangle "org-table" ())
  147. (declare-function org-table-maybe-eval-formula "org-table" ())
  148. (declare-function org-table-maybe-recalculate-line "org-table" ())
  149. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  150. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  151. (declare-function org-element-at-point "org-element" ())
  152. (declare-function org-element-cache-reset "org-element" (&optional all))
  153. (declare-function org-element-cache-refresh "org-element" (pos))
  154. (declare-function org-element-contents "org-element" (element))
  155. (declare-function org-element-context "org-element" (&optional element))
  156. (declare-function org-element-interpret-data "org-element"
  157. (data &optional parent))
  158. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  159. (declare-function org-element-parse-buffer "org-element"
  160. (&optional granularity visible-only))
  161. (declare-function org-element-property "org-element" (property element))
  162. (declare-function org-element-put-property "org-element"
  163. (element property value))
  164. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  165. (declare-function org-element-parse-buffer "org-element"
  166. (&optional granularity visible-only))
  167. (declare-function org-element-type "org-element" (element))
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. (setq exported-file
  216. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  217. (message "%s %s"
  218. (if compile
  219. (progn (byte-compile-file exported-file 'load)
  220. "Compiled and loaded")
  221. (progn (load-file exported-file) "Loaded"))
  222. exported-file)))
  223. (defcustom org-babel-load-languages '((emacs-lisp . t))
  224. "Languages which can be evaluated in Org-mode buffers.
  225. This list can be used to load support for any of the languages
  226. below, note that each language will depend on a different set of
  227. system executables and/or Emacs modes. When a language is
  228. \"loaded\", then code blocks in that language can be evaluated
  229. with `org-babel-execute-src-block' bound by default to C-c
  230. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  231. be set to remove code block evaluation from the C-c C-c
  232. keybinding. By default only Emacs Lisp (which has no
  233. requirements) is loaded."
  234. :group 'org-babel
  235. :set 'org-babel-do-load-languages
  236. :version "24.1"
  237. :type '(alist :tag "Babel Languages"
  238. :key-type
  239. (choice
  240. (const :tag "Awk" awk)
  241. (const :tag "C" C)
  242. (const :tag "R" R)
  243. (const :tag "Asymptote" asymptote)
  244. (const :tag "Calc" calc)
  245. (const :tag "Clojure" clojure)
  246. (const :tag "CSS" css)
  247. (const :tag "Ditaa" ditaa)
  248. (const :tag "Dot" dot)
  249. (const :tag "Emacs Lisp" emacs-lisp)
  250. (const :tag "Forth" forth)
  251. (const :tag "Fortran" fortran)
  252. (const :tag "Gnuplot" gnuplot)
  253. (const :tag "Haskell" haskell)
  254. (const :tag "IO" io)
  255. (const :tag "J" J)
  256. (const :tag "Java" java)
  257. (const :tag "Javascript" js)
  258. (const :tag "LaTeX" latex)
  259. (const :tag "Ledger" ledger)
  260. (const :tag "Lilypond" lilypond)
  261. (const :tag "Lisp" lisp)
  262. (const :tag "Makefile" makefile)
  263. (const :tag "Maxima" maxima)
  264. (const :tag "Matlab" matlab)
  265. (const :tag "Mscgen" mscgen)
  266. (const :tag "Ocaml" ocaml)
  267. (const :tag "Octave" octave)
  268. (const :tag "Org" org)
  269. (const :tag "Perl" perl)
  270. (const :tag "Pico Lisp" picolisp)
  271. (const :tag "PlantUML" plantuml)
  272. (const :tag "Python" python)
  273. (const :tag "Ruby" ruby)
  274. (const :tag "Sass" sass)
  275. (const :tag "Scala" scala)
  276. (const :tag "Scheme" scheme)
  277. (const :tag "Screen" screen)
  278. (const :tag "Shell Script" shell)
  279. (const :tag "Shen" shen)
  280. (const :tag "Sql" sql)
  281. (const :tag "Sqlite" sqlite)
  282. (const :tag "ebnf2ps" ebnf2ps))
  283. :value-type (boolean :tag "Activate" :value t)))
  284. ;;;; Customization variables
  285. (defcustom org-clone-delete-id nil
  286. "Remove ID property of clones of a subtree.
  287. When non-nil, clones of a subtree don't inherit the ID property.
  288. Otherwise they inherit the ID property with a new unique
  289. identifier."
  290. :type 'boolean
  291. :version "24.1"
  292. :group 'org-id)
  293. ;;; Version
  294. (org-check-version)
  295. ;;;###autoload
  296. (defun org-version (&optional here full message)
  297. "Show the org-mode version.
  298. Interactively, or when MESSAGE is non-nil, show it in echo area.
  299. With prefix argument, or when HERE is non-nil, insert it at point.
  300. In non-interactive uses, a reduced version string is output unless
  301. FULL is given."
  302. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  303. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  304. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  305. (load-suffixes (list ".el"))
  306. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  307. (org-trash (or
  308. (and (fboundp 'org-release) (fboundp 'org-git-version))
  309. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  310. (load-suffixes save-load-suffixes)
  311. (org-version (org-release))
  312. (git-version (org-git-version))
  313. (version (format "Org-mode version %s (%s @ %s)"
  314. org-version
  315. git-version
  316. (if org-install-dir
  317. (if (string= org-dir org-install-dir)
  318. org-install-dir
  319. (concat "mixed installation! " org-install-dir " and " org-dir))
  320. "org-loaddefs.el can not be found!")))
  321. (version1 (if full version org-version)))
  322. (when here (insert version1))
  323. (when message (message "%s" version1))
  324. version1))
  325. (defconst org-version (org-version))
  326. ;;; Syntax Constants
  327. ;;;; Block
  328. (defconst org-block-regexp
  329. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  330. "Regular expression for hiding blocks.")
  331. (defconst org-dblock-start-re
  332. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  333. "Matches the start line of a dynamic block, with parameters.")
  334. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  335. "Matches the end of a dynamic block.")
  336. ;;;; Clock and Planning
  337. (defconst org-clock-string "CLOCK:"
  338. "String used as prefix for timestamps clocking work hours on an item.")
  339. (defconst org-closed-string "CLOSED:"
  340. "String used as the prefix for timestamps logging closing a TODO entry.")
  341. (defconst org-deadline-string "DEADLINE:"
  342. "String to mark deadline entries.
  343. A deadline is this string, followed by a time stamp. Should be a word,
  344. terminated by a colon. You can insert a schedule keyword and
  345. a timestamp with \\[org-deadline].")
  346. (defconst org-scheduled-string "SCHEDULED:"
  347. "String to mark scheduled TODO entries.
  348. A schedule is this string, followed by a time stamp. Should be a word,
  349. terminated by a colon. You can insert a schedule keyword and
  350. a timestamp with \\[org-schedule].")
  351. (defconst org-ds-keyword-length
  352. (+ 2
  353. (apply #'max
  354. (mapcar #'length
  355. (list org-deadline-string org-scheduled-string
  356. org-clock-string org-closed-string))))
  357. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  358. (defconst org-planning-line-re
  359. (concat "^[ \t]*"
  360. (regexp-opt
  361. (list org-closed-string org-deadline-string org-scheduled-string)
  362. t))
  363. "Matches a line with planning info.
  364. Matched keyword is in group 1.")
  365. (defconst org-clock-line-re
  366. (concat "^[ \t]*" org-clock-string)
  367. "Matches a line with clock info.")
  368. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  369. "Matches the DEADLINE keyword.")
  370. (defconst org-deadline-time-regexp
  371. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  372. "Matches the DEADLINE keyword together with a time stamp.")
  373. (defconst org-deadline-time-hour-regexp
  374. (concat "\\<" org-deadline-string
  375. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  376. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  377. (defconst org-deadline-line-regexp
  378. (concat "\\<\\(" org-deadline-string "\\).*")
  379. "Matches the DEADLINE keyword and the rest of the line.")
  380. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  381. "Matches the SCHEDULED keyword.")
  382. (defconst org-scheduled-time-regexp
  383. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  384. "Matches the SCHEDULED keyword together with a time stamp.")
  385. (defconst org-scheduled-time-hour-regexp
  386. (concat "\\<" org-scheduled-string
  387. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  388. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  389. (defconst org-closed-time-regexp
  390. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  391. "Matches the CLOSED keyword together with a time stamp.")
  392. (defconst org-keyword-time-regexp
  393. (concat "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string
  396. org-clock-string)
  397. t)
  398. " *[[<]\\([^]>]+\\)[]>]")
  399. "Matches any of the 4 keywords, together with the time stamp.")
  400. (defconst org-keyword-time-not-clock-regexp
  401. (concat
  402. "\\<"
  403. (regexp-opt
  404. (list org-scheduled-string org-deadline-string org-closed-string) t)
  405. " *[[<]\\([^]>]+\\)[]>]")
  406. "Matches any of the 3 keywords, together with the time stamp.")
  407. (defconst org-maybe-keyword-time-regexp
  408. (concat "\\(\\<"
  409. (regexp-opt
  410. (list org-scheduled-string org-deadline-string org-closed-string
  411. org-clock-string)
  412. t)
  413. "\\)?"
  414. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  415. "\\|"
  416. "<%%([^\r\n>]*>\\)")
  417. "Matches a timestamp, possibly preceded by a keyword.")
  418. (defconst org-all-time-keywords
  419. (mapcar (lambda (w) (substring w 0 -1))
  420. (list org-scheduled-string org-deadline-string
  421. org-clock-string org-closed-string))
  422. "List of time keywords.")
  423. ;;;; Drawer
  424. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  425. "Matches first or last line of a hidden block.
  426. Group 1 contains drawer's name or \"END\".")
  427. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  428. "Regular expression matching the first line of a property drawer.")
  429. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  430. "Regular expression matching the last line of a property drawer.")
  431. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  432. "Regular expression matching the first line of a clock drawer.")
  433. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  434. "Regular expression matching the last line of a clock drawer.")
  435. (defconst org-property-drawer-re
  436. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  437. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  438. "[ \t]*:END:[ \t]*$")
  439. "Matches an entire property drawer.")
  440. (defconst org-clock-drawer-re
  441. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  442. org-clock-drawer-end-re "\\)\n?")
  443. "Matches an entire clock drawer.")
  444. ;;;; Headline
  445. (defconst org-heading-keyword-regexp-format
  446. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  447. "Printf format for a regexp matching a headline with some keyword.
  448. This regexp will match the headline of any node which has the
  449. exact keyword that is put into the format. The keyword isn't in
  450. any group by default, but the stars and the body are.")
  451. (defconst org-heading-keyword-maybe-regexp-format
  452. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  453. "Printf format for a regexp matching a headline, possibly with some keyword.
  454. This regexp can match any headline with the specified keyword, or
  455. without a keyword. The keyword isn't in any group by default,
  456. but the stars and the body are.")
  457. (defconst org-archive-tag "ARCHIVE"
  458. "The tag that marks a subtree as archived.
  459. An archived subtree does not open during visibility cycling, and does
  460. not contribute to the agenda listings.")
  461. (defconst org-comment-string "COMMENT"
  462. "Entries starting with this keyword will never be exported.
  463. An entry can be toggled between COMMENT and normal with
  464. \\[org-toggle-comment].")
  465. ;;;; LaTeX Environments and Fragments
  466. (defconst org-latex-regexps
  467. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  468. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  469. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  470. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  471. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  472. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  473. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  474. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  475. "Regular expressions for matching embedded LaTeX.")
  476. ;;;; Node Property
  477. (defconst org-effort-property "Effort"
  478. "The property that is being used to keep track of effort estimates.
  479. Effort estimates given in this property need to have the format H:MM.")
  480. ;;;; Table
  481. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  482. "Detect an org-type or table-type table.")
  483. (defconst org-table-line-regexp "^[ \t]*|"
  484. "Detect an org-type table line.")
  485. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  486. "Detect an org-type table line.")
  487. (defconst org-table-hline-regexp "^[ \t]*|-"
  488. "Detect an org-type table hline.")
  489. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  490. "Detect a table-type table hline.")
  491. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  492. "Detect the first line outside a table when searching from within it.
  493. This works for both table types.")
  494. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  495. "Detect a #+TBLFM line.")
  496. ;;;; Timestamp
  497. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  498. "Regular expression for fast time stamp matching.")
  499. (defconst org-ts-regexp-inactive
  500. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  501. "Regular expression for fast inactive time stamp matching.")
  502. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  503. "Regular expression for fast time stamp matching.")
  504. (defconst org-ts-regexp0
  505. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  506. "Regular expression matching time strings for analysis.
  507. This one does not require the space after the date, so it can be used
  508. on a string that terminates immediately after the date.")
  509. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  510. "Regular expression matching time strings for analysis.")
  511. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  512. "Regular expression matching time stamps, with groups.")
  513. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  514. "Regular expression matching time stamps (also [..]), with groups.")
  515. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  516. "Regular expression matching a time stamp range.")
  517. (defconst org-tr-regexp-both
  518. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  519. "Regular expression matching a time stamp range.")
  520. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  521. org-ts-regexp "\\)?")
  522. "Regular expression matching a time stamp or time stamp range.")
  523. (defconst org-tsr-regexp-both
  524. (concat org-ts-regexp-both "\\(--?-?"
  525. org-ts-regexp-both "\\)?")
  526. "Regular expression matching a time stamp or time stamp range.
  527. The time stamps may be either active or inactive.")
  528. (defconst org-repeat-re
  529. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  530. "Regular expression for specifying repeated events.
  531. After a match, group 1 contains the repeat expression.")
  532. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  533. "Formats for `format-time-string' which are used for time stamps.")
  534. ;;; The custom variables
  535. (defgroup org nil
  536. "Outline-based notes management and organizer."
  537. :tag "Org"
  538. :group 'outlines
  539. :group 'calendar)
  540. (defcustom org-mode-hook nil
  541. "Mode hook for Org-mode, run after the mode was turned on."
  542. :group 'org
  543. :type 'hook)
  544. (defcustom org-load-hook nil
  545. "Hook that is run after org.el has been loaded."
  546. :group 'org
  547. :type 'hook)
  548. (defcustom org-log-buffer-setup-hook nil
  549. "Hook that is run after an Org log buffer is created."
  550. :group 'org
  551. :version "24.1"
  552. :type 'hook)
  553. (defvar org-modules) ; defined below
  554. (defvar org-modules-loaded nil
  555. "Have the modules been loaded already?")
  556. (defun org-load-modules-maybe (&optional force)
  557. "Load all extensions listed in `org-modules'."
  558. (when (or force (not org-modules-loaded))
  559. (mapc (lambda (ext)
  560. (condition-case nil (require ext)
  561. (error (message "Problems while trying to load feature `%s'" ext))))
  562. org-modules)
  563. (setq org-modules-loaded t)))
  564. (defun org-set-modules (var value)
  565. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  566. (set var value)
  567. (when (featurep 'org)
  568. (org-load-modules-maybe 'force)
  569. (org-element-cache-reset 'all)))
  570. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  571. "Modules that should always be loaded together with org.el.
  572. If a description starts with <C>, the file is not part of Emacs
  573. and loading it will require that you have downloaded and properly
  574. installed the Org mode distribution.
  575. You can also use this system to load external packages (i.e. neither Org
  576. core modules, nor modules from the CONTRIB directory). Just add symbols
  577. to the end of the list. If the package is called org-xyz.el, then you need
  578. to add the symbol `xyz', and the package must have a call to:
  579. \(provide 'org-xyz)
  580. For export specific modules, see also `org-export-backends'."
  581. :group 'org
  582. :set 'org-set-modules
  583. :version "24.4"
  584. :package-version '(Org . "8.0")
  585. :type
  586. '(set :greedy t
  587. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  588. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  589. (const :tag " crypt: Encryption of subtrees" org-crypt)
  590. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  591. (const :tag " docview: Links to doc-view buffers" org-docview)
  592. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  593. (const :tag " habit: Track your consistency with habits" org-habit)
  594. (const :tag " id: Global IDs for identifying entries" org-id)
  595. (const :tag " info: Links to Info nodes" org-info)
  596. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  597. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  598. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  599. (const :tag " mouse: Additional mouse support" org-mouse)
  600. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  601. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  602. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  603. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  604. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  605. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  606. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  607. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  608. (const :tag "C collector: Collect properties into tables" org-collector)
  609. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  610. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  611. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  612. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  613. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  614. (const :tag "C eval: Include command output as text" org-eval)
  615. (const :tag "C eww: Store link to url of eww" org-eww)
  616. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  617. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  618. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  619. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  620. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  621. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  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. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  740. start selecting a region, or enlarge regions started in this way.
  741. In Org-mode, in special contexts, these same keys are used for
  742. other purposes, important enough to compete with shift selection.
  743. Org tries to balance these needs by supporting `shift-select-mode'
  744. outside these special contexts, under control of this variable.
  745. The default of this variable is nil, to avoid confusing behavior. Shifted
  746. cursor keys will then execute Org commands in the following contexts:
  747. - on a headline, changing TODO state (left/right) and priority (up/down)
  748. - on a time stamp, changing the time
  749. - in a plain list item, changing the bullet type
  750. - in a property definition line, switching between allowed values
  751. - in the BEGIN line of a clock table (changing the time block).
  752. Outside these contexts, the commands will throw an error.
  753. When this variable is t and the cursor is not in a special
  754. context, Org-mode will support shift-selection for making and
  755. enlarging regions. To make this more effective, the bullet
  756. cycling will no longer happen anywhere in an item line, but only
  757. if the cursor is exactly on the bullet.
  758. If you set this variable to the symbol `always', then the keys
  759. will not be special in headlines, property lines, and item lines,
  760. to make shift selection work there as well. If this is what you
  761. want, you can use the following alternative commands: `C-c C-t'
  762. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  763. can be used to switch TODO sets, `C-c -' to cycle item bullet
  764. types, and properties can be edited by hand or in column view.
  765. However, when the cursor is on a timestamp, shift-cursor commands
  766. will still edit the time stamp - this is just too good to give up.
  767. XEmacs user should have this variable set to nil, because
  768. `shift-select-mode' is in Emacs 23 or later only."
  769. :group 'org
  770. :type '(choice
  771. (const :tag "Never" nil)
  772. (const :tag "When outside special context" t)
  773. (const :tag "Everywhere except timestamps" always)))
  774. (defcustom org-loop-over-headlines-in-active-region nil
  775. "Shall some commands act upon headlines in the active region?
  776. When set to `t', some commands will be performed in all headlines
  777. within the active region.
  778. When set to `start-level', some commands will be performed in all
  779. headlines within the active region, provided that these headlines
  780. are of the same level than the first one.
  781. When set to a string, those commands will be performed on the
  782. matching headlines within the active region. Such string must be
  783. a tags/property/todo match as it is used in the agenda tags view.
  784. The list of commands is: `org-schedule', `org-deadline',
  785. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  786. `org-archive-to-archive-sibling'. The archiving commands skip
  787. already archived entries."
  788. :type '(choice (const :tag "Don't loop" nil)
  789. (const :tag "All headlines in active region" t)
  790. (const :tag "In active region, headlines at the same level than the first one" start-level)
  791. (string :tag "Tags/Property/Todo matcher"))
  792. :version "24.1"
  793. :group 'org-todo
  794. :group 'org-archive)
  795. (defgroup org-startup nil
  796. "Options concerning startup of Org-mode."
  797. :tag "Org Startup"
  798. :group 'org)
  799. (defcustom org-startup-folded t
  800. "Non-nil means entering Org-mode will switch to OVERVIEW.
  801. This can also be configured on a per-file basis by adding one of
  802. the following lines anywhere in the buffer:
  803. #+STARTUP: fold (or `overview', this is equivalent)
  804. #+STARTUP: nofold (or `showall', this is equivalent)
  805. #+STARTUP: content
  806. #+STARTUP: showeverything
  807. By default, this option is ignored when Org opens agenda files
  808. for the first time. If you want the agenda to honor the startup
  809. option, set `org-agenda-inhibit-startup' to nil."
  810. :group 'org-startup
  811. :type '(choice
  812. (const :tag "nofold: show all" nil)
  813. (const :tag "fold: overview" t)
  814. (const :tag "content: all headlines" content)
  815. (const :tag "show everything, even drawers" showeverything)))
  816. (defcustom org-startup-truncated t
  817. "Non-nil means entering Org-mode will set `truncate-lines'.
  818. This is useful since some lines containing links can be very long and
  819. uninteresting. Also tables look terrible when wrapped."
  820. :group 'org-startup
  821. :type 'boolean)
  822. (defcustom org-startup-indented nil
  823. "Non-nil means turn on `org-indent-mode' on startup.
  824. This can also be configured on a per-file basis by adding one of
  825. the following lines anywhere in the buffer:
  826. #+STARTUP: indent
  827. #+STARTUP: noindent"
  828. :group 'org-structure
  829. :type '(choice
  830. (const :tag "Not" nil)
  831. (const :tag "Globally (slow on startup in large files)" t)))
  832. (defcustom org-use-sub-superscripts t
  833. "Non-nil means interpret \"_\" and \"^\" for display.
  834. If you want to control how Org exports those characters, see
  835. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  836. used to be an alias for `org-export-with-sub-superscripts' in
  837. Org <8.0, it is not anymore.
  838. When this option is turned on, you can use TeX-like syntax for
  839. sub- and superscripts within the buffer. Several characters after
  840. \"_\" or \"^\" will be considered as a single item - so grouping
  841. with {} is normally not needed. For example, the following things
  842. will be parsed as single sub- or superscripts:
  843. 10^24 or 10^tau several digits will be considered 1 item.
  844. 10^-12 or 10^-tau a leading sign with digits or a word
  845. x^2-y^3 will be read as x^2 - y^3, because items are
  846. terminated by almost any nonword/nondigit char.
  847. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  848. Still, ambiguity is possible. So when in doubt, use {} to enclose
  849. the sub/superscript. If you set this variable to the symbol `{}',
  850. the braces are *required* in order to trigger interpretations as
  851. sub/superscript. This can be helpful in documents that need \"_\"
  852. frequently in plain text."
  853. :group 'org-startup
  854. :version "24.4"
  855. :package-version '(Org . "8.0")
  856. :type '(choice
  857. (const :tag "Always interpret" t)
  858. (const :tag "Only with braces" {})
  859. (const :tag "Never interpret" nil)))
  860. (defcustom org-startup-with-beamer-mode nil
  861. "Non-nil means turn on `org-beamer-mode' on startup.
  862. This can also be configured on a per-file basis by adding one of
  863. the following lines anywhere in the buffer:
  864. #+STARTUP: beamer"
  865. :group 'org-startup
  866. :version "24.1"
  867. :type 'boolean)
  868. (defcustom org-startup-align-all-tables nil
  869. "Non-nil means align all tables when visiting a file.
  870. This is useful when the column width in tables is forced with <N> cookies
  871. in table fields. Such tables will look correct only after the first re-align.
  872. This can also be configured on a per-file basis by adding one of
  873. the following lines anywhere in the buffer:
  874. #+STARTUP: align
  875. #+STARTUP: noalign"
  876. :group 'org-startup
  877. :type 'boolean)
  878. (defcustom org-startup-with-inline-images nil
  879. "Non-nil means show inline images when loading a new Org file.
  880. This can also be configured on a per-file basis by adding one of
  881. the following lines anywhere in the buffer:
  882. #+STARTUP: inlineimages
  883. #+STARTUP: noinlineimages"
  884. :group 'org-startup
  885. :version "24.1"
  886. :type 'boolean)
  887. (defcustom org-startup-with-latex-preview nil
  888. "Non-nil means preview LaTeX fragments when loading a new Org file.
  889. This can also be configured on a per-file basis by adding one of
  890. the following lines anywhere in the buffer:
  891. #+STARTUP: latexpreview
  892. #+STARTUP: nolatexpreview"
  893. :group 'org-startup
  894. :version "24.4"
  895. :package-version '(Org . "8.0")
  896. :type 'boolean)
  897. (defcustom org-insert-mode-line-in-empty-file nil
  898. "Non-nil means insert the first line setting Org-mode in empty files.
  899. When the function `org-mode' is called interactively in an empty file, this
  900. normally means that the file name does not automatically trigger Org-mode.
  901. To ensure that the file will always be in Org-mode in the future, a
  902. line enforcing Org-mode will be inserted into the buffer, if this option
  903. has been set."
  904. :group 'org-startup
  905. :type 'boolean)
  906. (defcustom org-replace-disputed-keys nil
  907. "Non-nil means use alternative key bindings for some keys.
  908. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  909. These keys are also used by other packages like shift-selection-mode'
  910. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  911. If you want to use Org-mode together with one of these other modes,
  912. or more generally if you would like to move some Org-mode commands to
  913. other keys, set this variable and configure the keys with the variable
  914. `org-disputed-keys'.
  915. This option is only relevant at load-time of Org-mode, and must be set
  916. *before* org.el is loaded. Changing it requires a restart of Emacs to
  917. become effective."
  918. :group 'org-startup
  919. :type 'boolean)
  920. (defcustom org-use-extra-keys nil
  921. "Non-nil means use extra key sequence definitions for certain commands.
  922. This happens automatically if you run XEmacs or if `window-system'
  923. is nil. This variable lets you do the same manually. You must
  924. set it before loading org.
  925. Example: on Carbon Emacs 22 running graphically, with an external
  926. keyboard on a Powerbook, the default way of setting M-left might
  927. not work for either Alt or ESC. Setting this variable will make
  928. it work for ESC."
  929. :group 'org-startup
  930. :type 'boolean)
  931. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  932. (defcustom org-disputed-keys
  933. '(([(shift up)] . [(meta p)])
  934. ([(shift down)] . [(meta n)])
  935. ([(shift left)] . [(meta -)])
  936. ([(shift right)] . [(meta +)])
  937. ([(control shift right)] . [(meta shift +)])
  938. ([(control shift left)] . [(meta shift -)]))
  939. "Keys for which Org-mode and other modes compete.
  940. This is an alist, cars are the default keys, second element specifies
  941. the alternative to use when `org-replace-disputed-keys' is t.
  942. Keys can be specified in any syntax supported by `define-key'.
  943. The value of this option takes effect only at Org-mode's startup,
  944. therefore you'll have to restart Emacs to apply it after changing."
  945. :group 'org-startup
  946. :type 'alist)
  947. (defun org-key (key)
  948. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  949. Or return the original if not disputed.
  950. Also apply the translations defined in `org-xemacs-key-equivalents'."
  951. (when org-replace-disputed-keys
  952. (let* ((nkey (key-description key))
  953. (x (org-find-if (lambda (x)
  954. (equal (key-description (car x)) nkey))
  955. org-disputed-keys)))
  956. (setq key (if x (cdr x) key))))
  957. (when (featurep 'xemacs)
  958. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  959. key)
  960. (defun org-find-if (predicate seq)
  961. (catch 'exit
  962. (while seq
  963. (if (funcall predicate (car seq))
  964. (throw 'exit (car seq))
  965. (pop seq)))))
  966. (defun org-defkey (keymap key def)
  967. "Define a key, possibly translated, as returned by `org-key'."
  968. (define-key keymap (org-key key) def))
  969. (defcustom org-ellipsis nil
  970. "The ellipsis to use in the Org-mode outline.
  971. When nil, just use the standard three dots.
  972. When a string, use that string instead.
  973. When a face, use the standard 3 dots, but with the specified face.
  974. The change affects only Org-mode (which will then use its own display table).
  975. Changing this requires executing \\[org-mode] in a buffer to become
  976. effective."
  977. :group 'org-startup
  978. :type '(choice (const :tag "Default" nil)
  979. (face :tag "Face" :value org-warning)
  980. (string :tag "String" :value "...#")))
  981. (defvar org-display-table nil
  982. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  983. (defgroup org-keywords nil
  984. "Keywords in Org-mode."
  985. :tag "Org Keywords"
  986. :group 'org)
  987. (defcustom org-closed-keep-when-no-todo nil
  988. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  989. :group 'org-todo
  990. :group 'org-keywords
  991. :version "24.4"
  992. :package-version '(Org . "8.0")
  993. :type 'boolean)
  994. (defgroup org-structure nil
  995. "Options concerning the general structure of Org-mode files."
  996. :tag "Org Structure"
  997. :group 'org)
  998. (defgroup org-reveal-location nil
  999. "Options about how to make context of a location visible."
  1000. :tag "Org Reveal Location"
  1001. :group 'org-structure)
  1002. (defconst org-context-choice
  1003. '(choice
  1004. (const :tag "Always" t)
  1005. (const :tag "Never" nil)
  1006. (repeat :greedy t :tag "Individual contexts"
  1007. (cons
  1008. (choice :tag "Context"
  1009. (const agenda)
  1010. (const org-goto)
  1011. (const occur-tree)
  1012. (const tags-tree)
  1013. (const link-search)
  1014. (const mark-goto)
  1015. (const bookmark-jump)
  1016. (const isearch)
  1017. (const default))
  1018. (boolean))))
  1019. "Contexts for the reveal options.")
  1020. (defcustom org-show-hierarchy-above '((default . t))
  1021. "Non-nil means show full hierarchy when revealing a location.
  1022. Org-mode often shows locations in an org-mode file which might have
  1023. been invisible before. When this is set, the hierarchy of headings
  1024. above the exposed location is shown.
  1025. Turning this off for example for sparse trees makes them very compact.
  1026. Instead of t, this can also be an alist specifying this option for different
  1027. contexts. Valid contexts are
  1028. agenda when exposing an entry from the agenda
  1029. org-goto when using the command `org-goto' on key C-c C-j
  1030. occur-tree when using the command `org-occur' on key C-c /
  1031. tags-tree when constructing a sparse tree based on tags matches
  1032. link-search when exposing search matches associated with a link
  1033. mark-goto when exposing the jump goal of a mark
  1034. bookmark-jump when exposing a bookmark location
  1035. isearch when exiting from an incremental search
  1036. default default for all contexts not set explicitly"
  1037. :group 'org-reveal-location
  1038. :type org-context-choice)
  1039. (defcustom org-show-following-heading '((default . nil))
  1040. "Non-nil means show following heading when revealing a location.
  1041. Org-mode often shows locations in an org-mode file which might have
  1042. been invisible before. When this is set, the heading following the
  1043. match is shown.
  1044. Turning this off for example for sparse trees makes them very compact,
  1045. but makes it harder to edit the location of the match. In such a case,
  1046. use the command \\[org-reveal] to show more context.
  1047. Instead of t, this can also be an alist specifying this option for different
  1048. contexts. See `org-show-hierarchy-above' for valid contexts."
  1049. :group 'org-reveal-location
  1050. :type org-context-choice)
  1051. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  1052. "Non-nil means show all sibling heading when revealing a location.
  1053. Org-mode often shows locations in an org-mode file which might have
  1054. been invisible before. When this is set, the sibling of the current entry
  1055. heading are all made visible. If `org-show-hierarchy-above' is t,
  1056. the same happens on each level of the hierarchy above the current entry.
  1057. By default this is on for the isearch context, off for all other contexts.
  1058. Turning this off for example for sparse trees makes them very compact,
  1059. but makes it harder to edit the location of the match. In such a case,
  1060. use the command \\[org-reveal] to show more context.
  1061. Instead of t, this can also be an alist specifying this option for different
  1062. contexts. See `org-show-hierarchy-above' for valid contexts."
  1063. :group 'org-reveal-location
  1064. :type org-context-choice
  1065. :version "24.4"
  1066. :package-version '(Org . "8.0"))
  1067. (defcustom org-show-entry-below '((default . nil))
  1068. "Non-nil means show the entry below a headline when revealing a location.
  1069. Org-mode often shows locations in an org-mode file which might have
  1070. been invisible before. When this is set, the text below the headline that is
  1071. exposed is also shown.
  1072. By default this is off for all contexts.
  1073. Instead of t, this can also be an alist specifying this option for different
  1074. contexts. See `org-show-hierarchy-above' for valid contexts."
  1075. :group 'org-reveal-location
  1076. :type org-context-choice)
  1077. (defcustom org-indirect-buffer-display 'other-window
  1078. "How should indirect tree buffers be displayed?
  1079. This applies to indirect buffers created with the commands
  1080. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1081. Valid values are:
  1082. current-window Display in the current window
  1083. other-window Just display in another window.
  1084. dedicated-frame Create one new frame, and re-use it each time.
  1085. new-frame Make a new frame each time. Note that in this case
  1086. previously-made indirect buffers are kept, and you need to
  1087. kill these buffers yourself."
  1088. :group 'org-structure
  1089. :group 'org-agenda-windows
  1090. :type '(choice
  1091. (const :tag "In current window" current-window)
  1092. (const :tag "In current frame, other window" other-window)
  1093. (const :tag "Each time a new frame" new-frame)
  1094. (const :tag "One dedicated frame" dedicated-frame)))
  1095. (defcustom org-use-speed-commands nil
  1096. "Non-nil means activate single letter commands at beginning of a headline.
  1097. This may also be a function to test for appropriate locations where speed
  1098. commands should be active.
  1099. For example, to activate speed commands when the point is on any
  1100. star at the beginning of the headline, you can do this:
  1101. (setq org-use-speed-commands
  1102. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1103. :group 'org-structure
  1104. :type '(choice
  1105. (const :tag "Never" nil)
  1106. (const :tag "At beginning of headline stars" t)
  1107. (function)))
  1108. (defcustom org-speed-commands-user nil
  1109. "Alist of additional speed commands.
  1110. This list will be checked before `org-speed-commands-default'
  1111. when the variable `org-use-speed-commands' is non-nil
  1112. and when the cursor is at the beginning of a headline.
  1113. The car if each entry is a string with a single letter, which must
  1114. be assigned to `self-insert-command' in the global map.
  1115. The cdr is either a command to be called interactively, a function
  1116. to be called, or a form to be evaluated.
  1117. An entry that is just a list with a single string will be interpreted
  1118. as a descriptive headline that will be added when listing the speed
  1119. commands in the Help buffer using the `?' speed command."
  1120. :group 'org-structure
  1121. :type '(repeat :value ("k" . ignore)
  1122. (choice :value ("k" . ignore)
  1123. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1124. (cons :tag "Letter and Command"
  1125. (string :tag "Command letter")
  1126. (choice
  1127. (function)
  1128. (sexp))))))
  1129. (defcustom org-bookmark-names-plist
  1130. '(:last-capture "org-capture-last-stored"
  1131. :last-refile "org-refile-last-stored"
  1132. :last-capture-marker "org-capture-last-stored-marker")
  1133. "Names for bookmarks automatically set by some Org commands.
  1134. This can provide strings as names for a number of bookmarks Org sets
  1135. automatically. The following keys are currently implemented:
  1136. :last-capture
  1137. :last-capture-marker
  1138. :last-refile
  1139. When a key does not show up in the property list, the corresponding bookmark
  1140. is not set."
  1141. :group 'org-structure
  1142. :type 'plist)
  1143. (defgroup org-cycle nil
  1144. "Options concerning visibility cycling in Org-mode."
  1145. :tag "Org Cycle"
  1146. :group 'org-structure)
  1147. (defcustom org-cycle-skip-children-state-if-no-children t
  1148. "Non-nil means skip CHILDREN state in entries that don't have any."
  1149. :group 'org-cycle
  1150. :type 'boolean)
  1151. (defcustom org-cycle-max-level nil
  1152. "Maximum level which should still be subject to visibility cycling.
  1153. Levels higher than this will, for cycling, be treated as text, not a headline.
  1154. When `org-odd-levels-only' is set, a value of N in this variable actually
  1155. means 2N-1 stars as the limiting headline.
  1156. When nil, cycle all levels.
  1157. Note that the limiting level of cycling is also influenced by
  1158. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1159. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1160. than its value."
  1161. :group 'org-cycle
  1162. :type '(choice
  1163. (const :tag "No limit" nil)
  1164. (integer :tag "Maximum level")))
  1165. (defcustom org-hide-block-startup nil
  1166. "Non-nil means entering Org-mode will fold all blocks.
  1167. This can also be set in on a per-file basis with
  1168. #+STARTUP: hideblocks
  1169. #+STARTUP: showblocks"
  1170. :group 'org-startup
  1171. :group 'org-cycle
  1172. :type 'boolean)
  1173. (defcustom org-cycle-global-at-bob nil
  1174. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1175. This makes it possible to do global cycling without having to use S-TAB or
  1176. \\[universal-argument] TAB. For this special case to work, the first line
  1177. of the buffer must not be a headline -- it may be empty or some other text.
  1178. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1179. sure the cursor stays at the beginning of the buffer. When this option is
  1180. nil, don't do anything special at the beginning of the buffer."
  1181. :group 'org-cycle
  1182. :type 'boolean)
  1183. (defcustom org-cycle-level-after-item/entry-creation t
  1184. "Non-nil means cycle entry level or item indentation in new empty entries.
  1185. When the cursor is at the end of an empty headline, i.e., with only stars
  1186. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1187. and then all possible ancestor states, before returning to the original state.
  1188. This makes data entry extremely fast: M-RET to create a new headline,
  1189. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1190. When the cursor is at the end of an empty plain list item, one TAB will
  1191. make it a subitem, two or more tabs will back up to make this an item
  1192. higher up in the item hierarchy."
  1193. :group 'org-cycle
  1194. :type 'boolean)
  1195. (defcustom org-cycle-emulate-tab t
  1196. "Where should `org-cycle' emulate TAB.
  1197. nil Never
  1198. white Only in completely white lines
  1199. whitestart Only at the beginning of lines, before the first non-white char
  1200. t Everywhere except in headlines
  1201. exc-hl-bol Everywhere except at the start of a headline
  1202. If TAB is used in a place where it does not emulate TAB, the current subtree
  1203. visibility is cycled."
  1204. :group 'org-cycle
  1205. :type '(choice (const :tag "Never" nil)
  1206. (const :tag "Only in completely white lines" white)
  1207. (const :tag "Before first char in a line" whitestart)
  1208. (const :tag "Everywhere except in headlines" t)
  1209. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1210. (defcustom org-cycle-separator-lines 2
  1211. "Number of empty lines needed to keep an empty line between collapsed trees.
  1212. If you leave an empty line between the end of a subtree and the following
  1213. headline, this empty line is hidden when the subtree is folded.
  1214. Org-mode will leave (exactly) one empty line visible if the number of
  1215. empty lines is equal or larger to the number given in this variable.
  1216. So the default 2 means at least 2 empty lines after the end of a subtree
  1217. are needed to produce free space between a collapsed subtree and the
  1218. following headline.
  1219. If the number is negative, and the number of empty lines is at least -N,
  1220. all empty lines are shown.
  1221. Special case: when 0, never leave empty lines in collapsed view."
  1222. :group 'org-cycle
  1223. :type 'integer)
  1224. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1225. (defcustom org-pre-cycle-hook nil
  1226. "Hook that is run before visibility cycling is happening.
  1227. The function(s) in this hook must accept a single argument which indicates
  1228. the new state that will be set right after running this hook. The
  1229. argument is a symbol. Before a global state change, it can have the values
  1230. `overview', `content', or `all'. Before a local state change, it can have
  1231. the values `folded', `children', or `subtree'."
  1232. :group 'org-cycle
  1233. :type 'hook)
  1234. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1235. org-cycle-hide-drawers
  1236. org-cycle-hide-inline-tasks
  1237. org-cycle-show-empty-lines
  1238. org-optimize-window-after-visibility-change)
  1239. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1240. The function(s) in this hook must accept a single argument which indicates
  1241. the new state that was set by the most recent `org-cycle' command. The
  1242. argument is a symbol. After a global state change, it can have the values
  1243. `overview', `contents', or `all'. After a local state change, it can have
  1244. the values `folded', `children', or `subtree'."
  1245. :group 'org-cycle
  1246. :type 'hook)
  1247. (defgroup org-edit-structure nil
  1248. "Options concerning structure editing in Org-mode."
  1249. :tag "Org Edit Structure"
  1250. :group 'org-structure)
  1251. (defcustom org-odd-levels-only nil
  1252. "Non-nil means skip even levels and only use odd levels for the outline.
  1253. This has the effect that two stars are being added/taken away in
  1254. promotion/demotion commands. It also influences how levels are
  1255. handled by the exporters.
  1256. Changing it requires restart of `font-lock-mode' to become effective
  1257. for fontification also in regions already fontified.
  1258. You may also set this on a per-file basis by adding one of the following
  1259. lines to the buffer:
  1260. #+STARTUP: odd
  1261. #+STARTUP: oddeven"
  1262. :group 'org-edit-structure
  1263. :group 'org-appearance
  1264. :type 'boolean)
  1265. (defcustom org-adapt-indentation t
  1266. "Non-nil means adapt indentation to outline node level.
  1267. When this variable is set, Org assumes that you write outlines by
  1268. indenting text in each node to align with the headline (after the stars).
  1269. The following issues are influenced by this variable:
  1270. - When this is set and the *entire* text in an entry is indented, the
  1271. indentation is increased by one space in a demotion command, and
  1272. decreased by one in a promotion command. If any line in the entry
  1273. body starts with text at column 0, indentation is not changed at all.
  1274. - Property drawers and planning information is inserted indented when
  1275. this variable s set. When nil, they will not be indented.
  1276. - TAB indents a line relative to context. The lines below a headline
  1277. will be indented when this variable is set.
  1278. Note that this is all about true indentation, by adding and removing
  1279. space characters. See also `org-indent.el' which does level-dependent
  1280. indentation in a virtual way, i.e. at display time in Emacs."
  1281. :group 'org-edit-structure
  1282. :type 'boolean)
  1283. (defcustom org-special-ctrl-a/e nil
  1284. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1285. When t, `C-a' will bring back the cursor to the beginning of the
  1286. headline text, i.e. after the stars and after a possible TODO
  1287. keyword. In an item, this will be the position after bullet and
  1288. check-box, if any. When the cursor is already at that position,
  1289. another `C-a' will bring it to the beginning of the line.
  1290. `C-e' will jump to the end of the headline, ignoring the presence
  1291. of tags in the headline. A second `C-e' will then jump to the
  1292. true end of the line, after any tags. This also means that, when
  1293. this variable is non-nil, `C-e' also will never jump beyond the
  1294. end of the heading of a folded section, i.e. not after the
  1295. ellipses.
  1296. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1297. normally, going to the true line boundary first. Only a directly
  1298. following, identical keypress will bring the cursor to the
  1299. special positions.
  1300. This may also be a cons cell where the behavior for `C-a' and
  1301. `C-e' is set separately."
  1302. :group 'org-edit-structure
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on: after stars/bullet and before tags first" t)
  1306. (const :tag "reversed: true line boundary first" reversed)
  1307. (cons :tag "Set C-a and C-e separately"
  1308. (choice :tag "Special C-a"
  1309. (const :tag "off" nil)
  1310. (const :tag "on: after stars/bullet first" t)
  1311. (const :tag "reversed: before stars/bullet first" reversed))
  1312. (choice :tag "Special C-e"
  1313. (const :tag "off" nil)
  1314. (const :tag "on: before tags first" t)
  1315. (const :tag "reversed: after tags first" reversed)))))
  1316. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1317. (defcustom org-special-ctrl-k nil
  1318. "Non-nil means `C-k' will behave specially in headlines.
  1319. When nil, `C-k' will call the default `kill-line' command.
  1320. When t, the following will happen while the cursor is in the headline:
  1321. - When the cursor is at the beginning of a headline, kill the entire
  1322. line and possible the folded subtree below the line.
  1323. - When in the middle of the headline text, kill the headline up to the tags.
  1324. - When after the headline text, kill the tags."
  1325. :group 'org-edit-structure
  1326. :type 'boolean)
  1327. (defcustom org-ctrl-k-protect-subtree nil
  1328. "Non-nil means, do not delete a hidden subtree with C-k.
  1329. When set to the symbol `error', simply throw an error when C-k is
  1330. used to kill (part-of) a headline that has hidden text behind it.
  1331. Any other non-nil value will result in a query to the user, if it is
  1332. OK to kill that hidden subtree. When nil, kill without remorse."
  1333. :group 'org-edit-structure
  1334. :version "24.1"
  1335. :type '(choice
  1336. (const :tag "Do not protect hidden subtrees" nil)
  1337. (const :tag "Protect hidden subtrees with a security query" t)
  1338. (const :tag "Never kill a hidden subtree with C-k" error)))
  1339. (defcustom org-special-ctrl-o t
  1340. "Non-nil means, make `C-o' insert a row in tables."
  1341. :group 'org-edit-structure
  1342. :type 'boolean)
  1343. (defcustom org-catch-invisible-edits nil
  1344. "Check if in invisible region before inserting or deleting a character.
  1345. Valid values are:
  1346. nil Do not check, so just do invisible edits.
  1347. error Throw an error and do nothing.
  1348. show Make point visible, and do the requested edit.
  1349. show-and-error Make point visible, then throw an error and abort the edit.
  1350. smart Make point visible, and do insertion/deletion if it is
  1351. adjacent to visible text and the change feels predictable.
  1352. Never delete a previously invisible character or add in the
  1353. middle or right after an invisible region. Basically, this
  1354. allows insertion and backward-delete right before ellipses.
  1355. FIXME: maybe in this case we should not even show?"
  1356. :group 'org-edit-structure
  1357. :version "24.1"
  1358. :type '(choice
  1359. (const :tag "Do not check" nil)
  1360. (const :tag "Throw error when trying to edit" error)
  1361. (const :tag "Unhide, but do not do the edit" show-and-error)
  1362. (const :tag "Show invisible part and do the edit" show)
  1363. (const :tag "Be smart and do the right thing" smart)))
  1364. (defcustom org-yank-folded-subtrees t
  1365. "Non-nil means when yanking subtrees, fold them.
  1366. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1367. it starts with a heading and all other headings in it are either children
  1368. or siblings, then fold all the subtrees. However, do this only if no
  1369. text after the yank would be swallowed into a folded tree by this action."
  1370. :group 'org-edit-structure
  1371. :type 'boolean)
  1372. (defcustom org-yank-adjusted-subtrees nil
  1373. "Non-nil means when yanking subtrees, adjust the level.
  1374. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1375. this function for details."
  1376. :group 'org-edit-structure
  1377. :type 'boolean)
  1378. (defcustom org-M-RET-may-split-line '((default . t))
  1379. "Non-nil means M-RET will split the line at the cursor position.
  1380. When nil, it will go to the end of the line before making a
  1381. new line.
  1382. You may also set this option in a different way for different
  1383. contexts. Valid contexts are:
  1384. headline when creating a new headline
  1385. item when creating a new item
  1386. table in a table field
  1387. default the value to be used for all contexts not explicitly
  1388. customized"
  1389. :group 'org-structure
  1390. :group 'org-table
  1391. :type '(choice
  1392. (const :tag "Always" t)
  1393. (const :tag "Never" nil)
  1394. (repeat :greedy t :tag "Individual contexts"
  1395. (cons
  1396. (choice :tag "Context"
  1397. (const headline)
  1398. (const item)
  1399. (const table)
  1400. (const default))
  1401. (boolean)))))
  1402. (defcustom org-insert-heading-respect-content nil
  1403. "Non-nil means insert new headings after the current subtree.
  1404. When nil, the new heading is created directly after the current line.
  1405. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1406. this variable on for the duration of the command."
  1407. :group 'org-structure
  1408. :type 'boolean)
  1409. (defcustom org-blank-before-new-entry '((heading . auto)
  1410. (plain-list-item . auto))
  1411. "Should `org-insert-heading' leave a blank line before new heading/item?
  1412. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1413. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1414. which case Org will look at the surrounding headings/items and try to
  1415. make an intelligent decision whether to insert a blank line or not.
  1416. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1417. the setting here is ignored and no empty line is inserted to avoid breaking
  1418. the list structure."
  1419. :group 'org-edit-structure
  1420. :type '(list
  1421. (cons (const heading)
  1422. (choice (const :tag "Never" nil)
  1423. (const :tag "Always" t)
  1424. (const :tag "Auto" auto)))
  1425. (cons (const plain-list-item)
  1426. (choice (const :tag "Never" nil)
  1427. (const :tag "Always" t)
  1428. (const :tag "Auto" auto)))))
  1429. (defcustom org-insert-heading-hook nil
  1430. "Hook being run after inserting a new heading."
  1431. :group 'org-edit-structure
  1432. :type 'hook)
  1433. (defcustom org-enable-fixed-width-editor t
  1434. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1435. This currently only means they are never auto-wrapped.
  1436. When nil, such lines will be treated like ordinary lines."
  1437. :group 'org-edit-structure
  1438. :type 'boolean)
  1439. (defcustom org-goto-auto-isearch t
  1440. "Non-nil means typing characters in `org-goto' starts incremental search.
  1441. When nil, you can use these keybindings to navigate the buffer:
  1442. q Quit the org-goto interface
  1443. n Go to the next visible heading
  1444. p Go to the previous visible heading
  1445. f Go one heading forward on same level
  1446. b Go one heading backward on same level
  1447. u Go one heading up"
  1448. :group 'org-edit-structure
  1449. :type 'boolean)
  1450. (defgroup org-sparse-trees nil
  1451. "Options concerning sparse trees in Org-mode."
  1452. :tag "Org Sparse Trees"
  1453. :group 'org-structure)
  1454. (defcustom org-highlight-sparse-tree-matches t
  1455. "Non-nil means highlight all matches that define a sparse tree.
  1456. The highlights will automatically disappear the next time the buffer is
  1457. changed by an edit command."
  1458. :group 'org-sparse-trees
  1459. :type 'boolean)
  1460. (defcustom org-remove-highlights-with-change t
  1461. "Non-nil means any change to the buffer will remove temporary highlights.
  1462. Such highlights are created by `org-occur' and `org-clock-display'.
  1463. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1464. The highlights created by `org-toggle-latex-fragment' always need
  1465. `C-c C-x C-l' to be removed."
  1466. :group 'org-sparse-trees
  1467. :group 'org-time
  1468. :type 'boolean)
  1469. (defcustom org-occur-hook '(org-first-headline-recenter)
  1470. "Hook that is run after `org-occur' has constructed a sparse tree.
  1471. This can be used to recenter the window to show as much of the structure
  1472. as possible."
  1473. :group 'org-sparse-trees
  1474. :type 'hook)
  1475. (defgroup org-imenu-and-speedbar nil
  1476. "Options concerning imenu and speedbar in Org-mode."
  1477. :tag "Org Imenu and Speedbar"
  1478. :group 'org-structure)
  1479. (defcustom org-imenu-depth 2
  1480. "The maximum level for Imenu access to Org-mode headlines.
  1481. This also applied for speedbar access."
  1482. :group 'org-imenu-and-speedbar
  1483. :type 'integer)
  1484. (defgroup org-table nil
  1485. "Options concerning tables in Org-mode."
  1486. :tag "Org Table"
  1487. :group 'org)
  1488. (defcustom org-enable-table-editor 'optimized
  1489. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1490. When nil, such lines will be treated like ordinary lines.
  1491. When equal to the symbol `optimized', the table editor will be optimized to
  1492. do the following:
  1493. - Automatic overwrite mode in front of whitespace in table fields.
  1494. This makes the structure of the table stay in tact as long as the edited
  1495. field does not exceed the column width.
  1496. - Minimize the number of realigns. Normally, the table is aligned each time
  1497. TAB or RET are pressed to move to another field. With optimization this
  1498. happens only if changes to a field might have changed the column width.
  1499. Optimization requires replacing the functions `self-insert-command',
  1500. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1501. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1502. very good at guessing when a re-align will be necessary, but you can always
  1503. force one with \\[org-ctrl-c-ctrl-c].
  1504. If you would like to use the optimized version in Org-mode, but the
  1505. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1506. This variable can be used to turn on and off the table editor during a session,
  1507. but in order to toggle optimization, a restart is required.
  1508. See also the variable `org-table-auto-blank-field'."
  1509. :group 'org-table
  1510. :type '(choice
  1511. (const :tag "off" nil)
  1512. (const :tag "on" t)
  1513. (const :tag "on, optimized" optimized)))
  1514. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1515. (version<= emacs-version "24.1"))
  1516. "Non-nil means cluster self-insert commands for undo when possible.
  1517. If this is set, then, like in the Emacs command loop, 20 consecutive
  1518. characters will be undone together.
  1519. This is configurable, because there is some impact on typing performance."
  1520. :group 'org-table
  1521. :type 'boolean)
  1522. (defcustom org-table-tab-recognizes-table.el t
  1523. "Non-nil means TAB will automatically notice a table.el table.
  1524. When it sees such a table, it moves point into it and - if necessary -
  1525. calls `table-recognize-table'."
  1526. :group 'org-table-editing
  1527. :type 'boolean)
  1528. (defgroup org-link nil
  1529. "Options concerning links in Org-mode."
  1530. :tag "Org Link"
  1531. :group 'org)
  1532. (defvar org-link-abbrev-alist-local nil
  1533. "Buffer-local version of `org-link-abbrev-alist', which see.
  1534. The value of this is taken from the #+LINK lines.")
  1535. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1536. (defcustom org-link-abbrev-alist nil
  1537. "Alist of link abbreviations.
  1538. The car of each element is a string, to be replaced at the start of a link.
  1539. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1540. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1541. [[linkkey:tag][description]]
  1542. The 'linkkey' must be a word word, starting with a letter, followed
  1543. by letters, numbers, '-' or '_'.
  1544. If REPLACE is a string, the tag will simply be appended to create the link.
  1545. If the string contains \"%s\", the tag will be inserted there. If the string
  1546. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1547. at that point (see the function `url-hexify-string'). If the string contains
  1548. the specifier \"%(my-function)\", then the custom function `my-function' will
  1549. be invoked: this function takes the tag as its only argument and must return
  1550. a string.
  1551. REPLACE may also be a function that will be called with the tag as the
  1552. only argument to create the link, which should be returned as a string.
  1553. See the manual for examples."
  1554. :group 'org-link
  1555. :type '(repeat
  1556. (cons
  1557. (string :tag "Protocol")
  1558. (choice
  1559. (string :tag "Format")
  1560. (function)))))
  1561. (defcustom org-descriptive-links t
  1562. "Non-nil means Org will display descriptive links.
  1563. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1564. \"Org Website\", hiding the link itself and just displaying its
  1565. description. When set to `nil', Org will display the full links
  1566. literally.
  1567. You can interactively set the value of this variable by calling
  1568. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1569. :group 'org-link
  1570. :type 'boolean)
  1571. (defcustom org-link-file-path-type 'adaptive
  1572. "How the path name in file links should be stored.
  1573. Valid values are:
  1574. relative Relative to the current directory, i.e. the directory of the file
  1575. into which the link is being inserted.
  1576. absolute Absolute path, if possible with ~ for home directory.
  1577. noabbrev Absolute path, no abbreviation of home directory.
  1578. adaptive Use relative path for files in the current directory and sub-
  1579. directories of it. For other files, use an absolute path."
  1580. :group 'org-link
  1581. :type '(choice
  1582. (const relative)
  1583. (const absolute)
  1584. (const noabbrev)
  1585. (const adaptive)))
  1586. (defvaralias 'org-activate-links 'org-highlight-links)
  1587. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1588. "Types of links that should be highlighted in Org-mode files.
  1589. This is a list of symbols, each one of them leading to the
  1590. highlighting of a certain link type.
  1591. You can still open links that are not highlighted.
  1592. In principle, it does not hurt to turn on highlighting for all
  1593. link types. There may be a small gain when turning off unused
  1594. link types. The types are:
  1595. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1596. angle Links in angular brackets that may contain whitespace like
  1597. <bbdb:Carsten Dominik>.
  1598. plain Plain links in normal text, no whitespace, like http://google.com.
  1599. radio Text that is matched by a radio target, see manual for details.
  1600. tag Tag settings in a headline (link to tag search).
  1601. date Time stamps (link to calendar).
  1602. footnote Footnote labels.
  1603. If you set this variable during an Emacs session, use `org-mode-restart'
  1604. in the Org buffer so that the change takes effect."
  1605. :group 'org-link
  1606. :group 'org-appearance
  1607. :type '(set :greedy t
  1608. (const :tag "Double bracket links" bracket)
  1609. (const :tag "Angular bracket links" angle)
  1610. (const :tag "Plain text links" plain)
  1611. (const :tag "Radio target matches" radio)
  1612. (const :tag "Tags" tag)
  1613. (const :tag "Timestamps" date)
  1614. (const :tag "Footnotes" footnote)))
  1615. (defcustom org-make-link-description-function nil
  1616. "Function to use for generating link descriptions from links.
  1617. When nil, the link location will be used. This function must take
  1618. two parameters: the first one is the link, the second one is the
  1619. description generated by `org-insert-link'. The function should
  1620. return the description to use."
  1621. :group 'org-link
  1622. :type '(choice (const nil) (function)))
  1623. (defgroup org-link-store nil
  1624. "Options concerning storing links in Org-mode."
  1625. :tag "Org Store Link"
  1626. :group 'org-link)
  1627. (defcustom org-url-hexify-p t
  1628. "When non-nil, hexify URL when creating a link."
  1629. :type 'boolean
  1630. :version "24.3"
  1631. :group 'org-link-store)
  1632. (defcustom org-email-link-description-format "Email %c: %.30s"
  1633. "Format of the description part of a link to an email or usenet message.
  1634. The following %-escapes will be replaced by corresponding information:
  1635. %F full \"From\" field
  1636. %f name, taken from \"From\" field, address if no name
  1637. %T full \"To\" field
  1638. %t first name in \"To\" field, address if no name
  1639. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1640. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1641. %s subject
  1642. %d date
  1643. %m message-id.
  1644. You may use normal field width specification between the % and the letter.
  1645. This is for example useful to limit the length of the subject.
  1646. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1647. :group 'org-link-store
  1648. :type 'string)
  1649. (defcustom org-from-is-user-regexp
  1650. (let (r1 r2)
  1651. (when (and user-mail-address (not (string= user-mail-address "")))
  1652. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1653. (when (and user-full-name (not (string= user-full-name "")))
  1654. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1655. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1656. "Regexp matched against the \"From:\" header of an email or usenet message.
  1657. It should match if the message is from the user him/herself."
  1658. :group 'org-link-store
  1659. :type 'regexp)
  1660. (defcustom org-context-in-file-links t
  1661. "Non-nil means file links from `org-store-link' contain context.
  1662. A search string will be added to the file name with :: as separator and
  1663. used to find the context when the link is activated by the command
  1664. `org-open-at-point'. When this option is t, the entire active region
  1665. will be placed in the search string of the file link. If set to a
  1666. positive integer, only the first n lines of context will be stored.
  1667. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1668. negates this setting for the duration of the command."
  1669. :group 'org-link-store
  1670. :type '(choice boolean integer))
  1671. (defcustom org-keep-stored-link-after-insertion nil
  1672. "Non-nil means keep link in list for entire session.
  1673. The command `org-store-link' adds a link pointing to the current
  1674. location to an internal list. These links accumulate during a session.
  1675. The command `org-insert-link' can be used to insert links into any
  1676. Org-mode file (offering completion for all stored links). When this
  1677. option is nil, every link which has been inserted once using \\[org-insert-link]
  1678. will be removed from the list, to make completing the unused links
  1679. more efficient."
  1680. :group 'org-link-store
  1681. :type 'boolean)
  1682. (defgroup org-link-follow nil
  1683. "Options concerning following links in Org-mode."
  1684. :tag "Org Follow Link"
  1685. :group 'org-link)
  1686. (defcustom org-link-translation-function nil
  1687. "Function to translate links with different syntax to Org syntax.
  1688. This can be used to translate links created for example by the Planner
  1689. or emacs-wiki packages to Org syntax.
  1690. The function must accept two parameters, a TYPE containing the link
  1691. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1692. which is everything after the link protocol. It should return a cons
  1693. with possibly modified values of type and path.
  1694. Org contains a function for this, so if you set this variable to
  1695. `org-translate-link-from-planner', you should be able follow many
  1696. links created by planner."
  1697. :group 'org-link-follow
  1698. :type '(choice (const nil) (function)))
  1699. (defcustom org-follow-link-hook nil
  1700. "Hook that is run after a link has been followed."
  1701. :group 'org-link-follow
  1702. :type 'hook)
  1703. (defcustom org-tab-follows-link nil
  1704. "Non-nil means on links TAB will follow the link.
  1705. Needs to be set before org.el is loaded.
  1706. This really should not be used, it does not make sense, and the
  1707. implementation is bad."
  1708. :group 'org-link-follow
  1709. :type 'boolean)
  1710. (defcustom org-return-follows-link nil
  1711. "Non-nil means on links RET will follow the link.
  1712. In tables, the special behavior of RET has precedence."
  1713. :group 'org-link-follow
  1714. :type 'boolean)
  1715. (defcustom org-mouse-1-follows-link
  1716. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1717. "Non-nil means mouse-1 on a link will follow the link.
  1718. A longer mouse click will still set point. Does not work on XEmacs.
  1719. Needs to be set before org.el is loaded."
  1720. :group 'org-link-follow
  1721. :version "24.4"
  1722. :package-version '(Org . "8.3")
  1723. :type '(choice
  1724. (const :tag "A double click follows the link" double)
  1725. (const :tag "Unconditionally follow the link with mouse-1" t)
  1726. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1727. (defcustom org-mark-ring-length 4
  1728. "Number of different positions to be recorded in the ring.
  1729. Changing this requires a restart of Emacs to work correctly."
  1730. :group 'org-link-follow
  1731. :type 'integer)
  1732. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1733. "Non-nil means internal links in Org files must exactly match a headline.
  1734. When nil, the link search tries to match a phrase with all words
  1735. in the search text."
  1736. :group 'org-link-follow
  1737. :version "24.1"
  1738. :type '(choice
  1739. (const :tag "Use fuzzy text search" nil)
  1740. (const :tag "Match only exact headline" t)
  1741. (const :tag "Match exact headline or query to create it"
  1742. query-to-create)))
  1743. (defcustom org-link-frame-setup
  1744. '((vm . vm-visit-folder-other-frame)
  1745. (vm-imap . vm-visit-imap-folder-other-frame)
  1746. (gnus . org-gnus-no-new-news)
  1747. (file . find-file-other-window)
  1748. (wl . wl-other-frame))
  1749. "Setup the frame configuration for following links.
  1750. When following a link with Emacs, it may often be useful to display
  1751. this link in another window or frame. This variable can be used to
  1752. set this up for the different types of links.
  1753. For VM, use any of
  1754. `vm-visit-folder'
  1755. `vm-visit-folder-other-window'
  1756. `vm-visit-folder-other-frame'
  1757. For Gnus, use any of
  1758. `gnus'
  1759. `gnus-other-frame'
  1760. `org-gnus-no-new-news'
  1761. For FILE, use any of
  1762. `find-file'
  1763. `find-file-other-window'
  1764. `find-file-other-frame'
  1765. For Wanderlust use any of
  1766. `wl'
  1767. `wl-other-frame'
  1768. For the calendar, use the variable `calendar-setup'.
  1769. For BBDB, it is currently only possible to display the matches in
  1770. another window."
  1771. :group 'org-link-follow
  1772. :type '(list
  1773. (cons (const vm)
  1774. (choice
  1775. (const vm-visit-folder)
  1776. (const vm-visit-folder-other-window)
  1777. (const vm-visit-folder-other-frame)))
  1778. (cons (const vm-imap)
  1779. (choice
  1780. (const vm-visit-imap-folder)
  1781. (const vm-visit-imap-folder-other-window)
  1782. (const vm-visit-imap-folder-other-frame)))
  1783. (cons (const gnus)
  1784. (choice
  1785. (const gnus)
  1786. (const gnus-other-frame)
  1787. (const org-gnus-no-new-news)))
  1788. (cons (const file)
  1789. (choice
  1790. (const find-file)
  1791. (const find-file-other-window)
  1792. (const find-file-other-frame)))
  1793. (cons (const wl)
  1794. (choice
  1795. (const wl)
  1796. (const wl-other-frame)))))
  1797. (defcustom org-display-internal-link-with-indirect-buffer nil
  1798. "Non-nil means use indirect buffer to display infile links.
  1799. Activating internal links (from one location in a file to another location
  1800. in the same file) normally just jumps to the location. When the link is
  1801. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1802. is displayed in
  1803. another window. When this option is set, the other window actually displays
  1804. an indirect buffer clone of the current buffer, to avoid any visibility
  1805. changes to the current buffer."
  1806. :group 'org-link-follow
  1807. :type 'boolean)
  1808. (defcustom org-open-non-existing-files nil
  1809. "Non-nil means `org-open-file' will open non-existing files.
  1810. When nil, an error will be generated.
  1811. This variable applies only to external applications because they
  1812. might choke on non-existing files. If the link is to a file that
  1813. will be opened in Emacs, the variable is ignored."
  1814. :group 'org-link-follow
  1815. :type 'boolean)
  1816. (defcustom org-open-directory-means-index-dot-org nil
  1817. "Non-nil means a link to a directory really means to index.org.
  1818. When nil, following a directory link will run dired or open a finder/explorer
  1819. window on that directory."
  1820. :group 'org-link-follow
  1821. :type 'boolean)
  1822. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1823. "Non-nil means ask for confirmation before executing shell links.
  1824. Shell links can be dangerous: just think about a link
  1825. [[shell:rm -rf ~/*][Google Search]]
  1826. This link would show up in your Org-mode document as \"Google Search\",
  1827. but really it would remove your entire home directory.
  1828. Therefore we advise against setting this variable to nil.
  1829. Just change it to `y-or-n-p' if you want to confirm with a
  1830. single keystroke rather than having to type \"yes\"."
  1831. :group 'org-link-follow
  1832. :type '(choice
  1833. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1834. (const :tag "with y-or-n (faster)" y-or-n-p)
  1835. (const :tag "no confirmation (dangerous)" nil)))
  1836. (put 'org-confirm-shell-link-function
  1837. 'safe-local-variable
  1838. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1839. (defcustom org-confirm-shell-link-not-regexp ""
  1840. "A regexp to skip confirmation for shell links."
  1841. :group 'org-link-follow
  1842. :version "24.1"
  1843. :type 'regexp)
  1844. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1845. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1846. Elisp links can be dangerous: just think about a link
  1847. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1848. This link would show up in your Org-mode document as \"Google Search\",
  1849. but really it would remove your entire home directory.
  1850. Therefore we advise against setting this variable to nil.
  1851. Just change it to `y-or-n-p' if you want to confirm with a
  1852. single keystroke rather than having to type \"yes\"."
  1853. :group 'org-link-follow
  1854. :type '(choice
  1855. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1856. (const :tag "with y-or-n (faster)" y-or-n-p)
  1857. (const :tag "no confirmation (dangerous)" nil)))
  1858. (put 'org-confirm-shell-link-function
  1859. 'safe-local-variable
  1860. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1861. (defcustom org-confirm-elisp-link-not-regexp ""
  1862. "A regexp to skip confirmation for Elisp links."
  1863. :group 'org-link-follow
  1864. :version "24.1"
  1865. :type 'regexp)
  1866. (defconst org-file-apps-defaults-gnu
  1867. '((remote . emacs)
  1868. (system . mailcap)
  1869. (t . mailcap))
  1870. "Default file applications on a UNIX or GNU/Linux system.
  1871. See `org-file-apps'.")
  1872. (defconst org-file-apps-defaults-macosx
  1873. '((remote . emacs)
  1874. (t . "open %s")
  1875. (system . "open %s")
  1876. ("ps.gz" . "gv %s")
  1877. ("eps.gz" . "gv %s")
  1878. ("dvi" . "xdvi %s")
  1879. ("fig" . "xfig %s"))
  1880. "Default file applications on a MacOS X system.
  1881. The system \"open\" is known as a default, but we use X11 applications
  1882. for some files for which the OS does not have a good default.
  1883. See `org-file-apps'.")
  1884. (defconst org-file-apps-defaults-windowsnt
  1885. (list
  1886. '(remote . emacs)
  1887. (cons t
  1888. (list (if (featurep 'xemacs)
  1889. 'mswindows-shell-execute
  1890. 'w32-shell-execute)
  1891. "open" 'file))
  1892. (cons 'system
  1893. (list (if (featurep 'xemacs)
  1894. 'mswindows-shell-execute
  1895. 'w32-shell-execute)
  1896. "open" 'file)))
  1897. "Default file applications on a Windows NT system.
  1898. The system \"open\" is used for most files.
  1899. See `org-file-apps'.")
  1900. (defcustom org-file-apps
  1901. '((auto-mode . emacs)
  1902. ("\\.mm\\'" . default)
  1903. ("\\.x?html?\\'" . default)
  1904. ("\\.pdf\\'" . default))
  1905. "External applications for opening `file:path' items in a document.
  1906. Org-mode uses system defaults for different file types, but
  1907. you can use this variable to set the application for a given file
  1908. extension. The entries in this list are cons cells where the car identifies
  1909. files and the cdr the corresponding command. Possible values for the
  1910. file identifier are
  1911. \"string\" A string as a file identifier can be interpreted in different
  1912. ways, depending on its contents:
  1913. - Alphanumeric characters only:
  1914. Match links with this file extension.
  1915. Example: (\"pdf\" . \"evince %s\")
  1916. to open PDFs with evince.
  1917. - Regular expression: Match links where the
  1918. filename matches the regexp. If you want to
  1919. use groups here, use shy groups.
  1920. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1921. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1922. to open *.html and *.xhtml with firefox.
  1923. - Regular expression which contains (non-shy) groups:
  1924. Match links where the whole link, including \"::\", and
  1925. anything after that, matches the regexp.
  1926. In a custom command string, %1, %2, etc. are replaced with
  1927. the parts of the link that were matched by the groups.
  1928. For backwards compatibility, if a command string is given
  1929. that does not use any of the group matches, this case is
  1930. handled identically to the second one (i.e. match against
  1931. file name only).
  1932. In a custom lisp form, you can access the group matches with
  1933. (match-string n link).
  1934. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1935. to open [[file:document.pdf::5]] with evince at page 5.
  1936. `directory' Matches a directory
  1937. `remote' Matches a remote file, accessible through tramp or efs.
  1938. Remote files most likely should be visited through Emacs
  1939. because external applications cannot handle such paths.
  1940. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1941. so all files Emacs knows how to handle. Using this with
  1942. command `emacs' will open most files in Emacs. Beware that this
  1943. will also open html files inside Emacs, unless you add
  1944. (\"html\" . default) to the list as well.
  1945. t Default for files not matched by any of the other options.
  1946. `system' The system command to open files, like `open' on Windows
  1947. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1948. that will be selected if you call `C-c C-o' with a double
  1949. \\[universal-argument] \\[universal-argument] prefix.
  1950. Possible values for the command are:
  1951. `emacs' The file will be visited by the current Emacs process.
  1952. `default' Use the default application for this file type, which is the
  1953. association for t in the list, most likely in the system-specific
  1954. part.
  1955. This can be used to overrule an unwanted setting in the
  1956. system-specific variable.
  1957. `system' Use the system command for opening files, like \"open\".
  1958. This command is specified by the entry whose car is `system'.
  1959. Most likely, the system-specific version of this variable
  1960. does define this command, but you can overrule/replace it
  1961. here.
  1962. string A command to be executed by a shell; %s will be replaced
  1963. by the path to the file.
  1964. sexp A Lisp form which will be evaluated. The file path will
  1965. be available in the Lisp variable `file'.
  1966. For more examples, see the system specific constants
  1967. `org-file-apps-defaults-macosx'
  1968. `org-file-apps-defaults-windowsnt'
  1969. `org-file-apps-defaults-gnu'."
  1970. :group 'org-link-follow
  1971. :type '(repeat
  1972. (cons (choice :value ""
  1973. (string :tag "Extension")
  1974. (const :tag "System command to open files" system)
  1975. (const :tag "Default for unrecognized files" t)
  1976. (const :tag "Remote file" remote)
  1977. (const :tag "Links to a directory" directory)
  1978. (const :tag "Any files that have Emacs modes"
  1979. auto-mode))
  1980. (choice :value ""
  1981. (const :tag "Visit with Emacs" emacs)
  1982. (const :tag "Use default" default)
  1983. (const :tag "Use the system command" system)
  1984. (string :tag "Command")
  1985. (sexp :tag "Lisp form")))))
  1986. (defcustom org-doi-server-url "http://dx.doi.org/"
  1987. "The URL of the DOI server."
  1988. :type 'string
  1989. :version "24.3"
  1990. :group 'org-link-follow)
  1991. (defgroup org-refile nil
  1992. "Options concerning refiling entries in Org-mode."
  1993. :tag "Org Refile"
  1994. :group 'org)
  1995. (defcustom org-directory "~/org"
  1996. "Directory with org files.
  1997. This is just a default location to look for Org files. There is no need
  1998. at all to put your files into this directory. It is only used in the
  1999. following situations:
  2000. 1. When a capture template specifies a target file that is not an
  2001. absolute path. The path will then be interpreted relative to
  2002. `org-directory'
  2003. 2. When a capture note is filed away in an interactive way (when exiting the
  2004. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  2005. with `org-directory' as the default path."
  2006. :group 'org-refile
  2007. :group 'org-capture
  2008. :type 'directory)
  2009. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2010. "Default target for storing notes.
  2011. Used as a fall back file for org-capture.el, for templates that
  2012. do not specify a target file."
  2013. :group 'org-refile
  2014. :group 'org-capture
  2015. :type '(choice
  2016. (const :tag "Default from remember-data-file" nil)
  2017. file))
  2018. (defcustom org-goto-interface 'outline
  2019. "The default interface to be used for `org-goto'.
  2020. Allowed values are:
  2021. outline The interface shows an outline of the relevant file
  2022. and the correct heading is found by moving through
  2023. the outline or by searching with incremental search.
  2024. outline-path-completion Headlines in the current buffer are offered via
  2025. completion. This is the interface also used by
  2026. the refile command."
  2027. :group 'org-refile
  2028. :type '(choice
  2029. (const :tag "Outline" outline)
  2030. (const :tag "Outline-path-completion" outline-path-completion)))
  2031. (defcustom org-goto-max-level 5
  2032. "Maximum target level when running `org-goto' with refile interface."
  2033. :group 'org-refile
  2034. :type 'integer)
  2035. (defcustom org-reverse-note-order nil
  2036. "Non-nil means store new notes at the beginning of a file or entry.
  2037. When nil, new notes will be filed to the end of a file or entry.
  2038. This can also be a list with cons cells of regular expressions that
  2039. are matched against file names, and values."
  2040. :group 'org-capture
  2041. :group 'org-refile
  2042. :type '(choice
  2043. (const :tag "Reverse always" t)
  2044. (const :tag "Reverse never" nil)
  2045. (repeat :tag "By file name regexp"
  2046. (cons regexp boolean))))
  2047. (defcustom org-log-refile nil
  2048. "Information to record when a task is refiled.
  2049. Possible values are:
  2050. nil Don't add anything
  2051. time Add a time stamp to the task
  2052. note Prompt for a note and add it with template `org-log-note-headings'
  2053. This option can also be set with on a per-file-basis with
  2054. #+STARTUP: nologrefile
  2055. #+STARTUP: logrefile
  2056. #+STARTUP: lognoterefile
  2057. You can have local logging settings for a subtree by setting the LOGGING
  2058. property to one or more of these keywords.
  2059. When bulk-refiling from the agenda, the value `note' is forbidden and
  2060. will temporarily be changed to `time'."
  2061. :group 'org-refile
  2062. :group 'org-progress
  2063. :version "24.1"
  2064. :type '(choice
  2065. (const :tag "No logging" nil)
  2066. (const :tag "Record timestamp" time)
  2067. (const :tag "Record timestamp with note." note)))
  2068. (defcustom org-refile-targets nil
  2069. "Targets for refiling entries with \\[org-refile].
  2070. This is a list of cons cells. Each cell contains:
  2071. - a specification of the files to be considered, either a list of files,
  2072. or a symbol whose function or variable value will be used to retrieve
  2073. a file name or a list of file names. If you use `org-agenda-files' for
  2074. that, all agenda files will be scanned for targets. Nil means consider
  2075. headings in the current buffer.
  2076. - A specification of how to find candidate refile targets. This may be
  2077. any of:
  2078. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2079. This tag has to be present in all target headlines, inheritance will
  2080. not be considered.
  2081. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2082. todo keyword.
  2083. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2084. headlines that are refiling targets.
  2085. - a cons cell (:level . N). Any headline of level N is considered a target.
  2086. Note that, when `org-odd-levels-only' is set, level corresponds to
  2087. order in hierarchy, not to the number of stars.
  2088. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2089. Note that, when `org-odd-levels-only' is set, level corresponds to
  2090. order in hierarchy, not to the number of stars.
  2091. Each element of this list generates a set of possible targets.
  2092. The union of these sets is presented (with completion) to
  2093. the user by `org-refile'.
  2094. You can set the variable `org-refile-target-verify-function' to a function
  2095. to verify each headline found by the simple criteria above.
  2096. When this variable is nil, all top-level headlines in the current buffer
  2097. are used, equivalent to the value `((nil . (:level . 1))'."
  2098. :group 'org-refile
  2099. :type '(repeat
  2100. (cons
  2101. (choice :value org-agenda-files
  2102. (const :tag "All agenda files" org-agenda-files)
  2103. (const :tag "Current buffer" nil)
  2104. (function) (variable) (file))
  2105. (choice :tag "Identify target headline by"
  2106. (cons :tag "Specific tag" (const :value :tag) (string))
  2107. (cons :tag "TODO keyword" (const :value :todo) (string))
  2108. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2109. (cons :tag "Level number" (const :value :level) (integer))
  2110. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2111. (defcustom org-refile-target-verify-function nil
  2112. "Function to verify if the headline at point should be a refile target.
  2113. The function will be called without arguments, with point at the
  2114. beginning of the headline. It should return t and leave point
  2115. where it is if the headline is a valid target for refiling.
  2116. If the target should not be selected, the function must return nil.
  2117. In addition to this, it may move point to a place from where the search
  2118. should be continued. For example, the function may decide that the entire
  2119. subtree of the current entry should be excluded and move point to the end
  2120. of the subtree."
  2121. :group 'org-refile
  2122. :type '(choice
  2123. (const nil)
  2124. (function)))
  2125. (defcustom org-refile-use-cache nil
  2126. "Non-nil means cache refile targets to speed up the process.
  2127. The cache for a particular file will be updated automatically when
  2128. the buffer has been killed, or when any of the marker used for flagging
  2129. refile targets no longer points at a live buffer.
  2130. If you have added new entries to a buffer that might themselves be targets,
  2131. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2132. find that easier, `C-u C-u C-u C-c C-w'."
  2133. :group 'org-refile
  2134. :version "24.1"
  2135. :type 'boolean)
  2136. (defcustom org-refile-use-outline-path nil
  2137. "Non-nil means provide refile targets as paths.
  2138. So a level 3 headline will be available as level1/level2/level3.
  2139. When the value is `file', also include the file name (without directory)
  2140. into the path. In this case, you can also stop the completion after
  2141. the file name, to get entries inserted as top level in the file.
  2142. When `full-file-path', include the full file path."
  2143. :group 'org-refile
  2144. :type '(choice
  2145. (const :tag "Not" nil)
  2146. (const :tag "Yes" t)
  2147. (const :tag "Start with file name" file)
  2148. (const :tag "Start with full file path" full-file-path)))
  2149. (defcustom org-outline-path-complete-in-steps t
  2150. "Non-nil means complete the outline path in hierarchical steps.
  2151. When Org-mode uses the refile interface to select an outline path
  2152. \(see variable `org-refile-use-outline-path'), the completion of
  2153. the path can be done is a single go, or if can be done in steps down
  2154. the headline hierarchy. Going in steps is probably the best if you
  2155. do not use a special completion package like `ido' or `icicles'.
  2156. However, when using these packages, going in one step can be very
  2157. fast, while still showing the whole path to the entry."
  2158. :group 'org-refile
  2159. :type 'boolean)
  2160. (defcustom org-refile-allow-creating-parent-nodes nil
  2161. "Non-nil means allow to create new nodes as refile targets.
  2162. New nodes are then created by adding \"/new node name\" to the completion
  2163. of an existing node. When the value of this variable is `confirm',
  2164. new node creation must be confirmed by the user (recommended).
  2165. When nil, the completion must match an existing entry.
  2166. Note that, if the new heading is not seen by the criteria
  2167. listed in `org-refile-targets', multiple instances of the same
  2168. heading would be created by trying again to file under the new
  2169. heading."
  2170. :group 'org-refile
  2171. :type '(choice
  2172. (const :tag "Never" nil)
  2173. (const :tag "Always" t)
  2174. (const :tag "Prompt for confirmation" confirm)))
  2175. (defcustom org-refile-active-region-within-subtree nil
  2176. "Non-nil means also refile active region within a subtree.
  2177. By default `org-refile' doesn't allow refiling regions if they
  2178. don't contain a set of subtrees, but it might be convenient to
  2179. do so sometimes: in that case, the first line of the region is
  2180. converted to a headline before refiling."
  2181. :group 'org-refile
  2182. :version "24.1"
  2183. :type 'boolean)
  2184. (defgroup org-todo nil
  2185. "Options concerning TODO items in Org-mode."
  2186. :tag "Org TODO"
  2187. :group 'org)
  2188. (defgroup org-progress nil
  2189. "Options concerning Progress logging in Org-mode."
  2190. :tag "Org Progress"
  2191. :group 'org-time)
  2192. (defvar org-todo-interpretation-widgets
  2193. '((:tag "Sequence (cycling hits every state)" sequence)
  2194. (:tag "Type (cycling directly to DONE)" type))
  2195. "The available interpretation symbols for customizing `org-todo-keywords'.
  2196. Interested libraries should add to this list.")
  2197. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2198. "List of TODO entry keyword sequences and their interpretation.
  2199. \\<org-mode-map>This is a list of sequences.
  2200. Each sequence starts with a symbol, either `sequence' or `type',
  2201. indicating if the keywords should be interpreted as a sequence of
  2202. action steps, or as different types of TODO items. The first
  2203. keywords are states requiring action - these states will select a headline
  2204. for inclusion into the global TODO list Org-mode produces. If one of
  2205. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2206. signify that no further action is necessary. If \"|\" is not found,
  2207. the last keyword is treated as the only DONE state of the sequence.
  2208. The command \\[org-todo] cycles an entry through these states, and one
  2209. additional state where no keyword is present. For details about this
  2210. cycling, see the manual.
  2211. TODO keywords and interpretation can also be set on a per-file basis with
  2212. the special #+SEQ_TODO and #+TYP_TODO lines.
  2213. Each keyword can optionally specify a character for fast state selection
  2214. \(in combination with the variable `org-use-fast-todo-selection')
  2215. and specifiers for state change logging, using the same syntax that
  2216. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2217. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2218. indicates to record a time stamp each time this state is selected.
  2219. Each keyword may also specify if a timestamp or a note should be
  2220. recorded when entering or leaving the state, by adding additional
  2221. characters in the parenthesis after the keyword. This looks like this:
  2222. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2223. record only the time of the state change. With X and Y being either
  2224. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2225. Y when leaving the state if and only if the *target* state does not
  2226. define X. You may omit any of the fast-selection key or X or /Y,
  2227. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2228. For backward compatibility, this variable may also be just a list
  2229. of keywords. In this case the interpretation (sequence or type) will be
  2230. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2231. :group 'org-todo
  2232. :group 'org-keywords
  2233. :type '(choice
  2234. (repeat :tag "Old syntax, just keywords"
  2235. (string :tag "Keyword"))
  2236. (repeat :tag "New syntax"
  2237. (cons
  2238. (choice
  2239. :tag "Interpretation"
  2240. ;;Quick and dirty way to see
  2241. ;;`org-todo-interpretations'. This takes the
  2242. ;;place of item arguments
  2243. :convert-widget
  2244. (lambda (widget)
  2245. (widget-put widget
  2246. :args (mapcar
  2247. (lambda (x)
  2248. (widget-convert
  2249. (cons 'const x)))
  2250. org-todo-interpretation-widgets))
  2251. widget))
  2252. (repeat
  2253. (string :tag "Keyword"))))))
  2254. (defvar org-todo-keywords-1 nil
  2255. "All TODO and DONE keywords active in a buffer.")
  2256. (make-variable-buffer-local 'org-todo-keywords-1)
  2257. (defvar org-todo-keywords-for-agenda nil)
  2258. (defvar org-done-keywords-for-agenda nil)
  2259. (defvar org-todo-keyword-alist-for-agenda nil)
  2260. (defvar org-tag-alist-for-agenda nil
  2261. "Alist of all tags from all agenda files.")
  2262. (defvar org-tag-groups-alist-for-agenda nil
  2263. "Alist of all groups tags from all current agenda files.")
  2264. (defvar org-tag-groups-alist nil)
  2265. (make-variable-buffer-local 'org-tag-groups-alist)
  2266. (defvar org-agenda-contributing-files nil)
  2267. (defvar org-not-done-keywords nil)
  2268. (make-variable-buffer-local 'org-not-done-keywords)
  2269. (defvar org-done-keywords nil)
  2270. (make-variable-buffer-local 'org-done-keywords)
  2271. (defvar org-todo-heads nil)
  2272. (make-variable-buffer-local 'org-todo-heads)
  2273. (defvar org-todo-sets nil)
  2274. (make-variable-buffer-local 'org-todo-sets)
  2275. (defvar org-todo-log-states nil)
  2276. (make-variable-buffer-local 'org-todo-log-states)
  2277. (defvar org-todo-kwd-alist nil)
  2278. (make-variable-buffer-local 'org-todo-kwd-alist)
  2279. (defvar org-todo-key-alist nil)
  2280. (make-variable-buffer-local 'org-todo-key-alist)
  2281. (defvar org-todo-key-trigger nil)
  2282. (make-variable-buffer-local 'org-todo-key-trigger)
  2283. (defcustom org-todo-interpretation 'sequence
  2284. "Controls how TODO keywords are interpreted.
  2285. This variable is in principle obsolete and is only used for
  2286. backward compatibility, if the interpretation of todo keywords is
  2287. not given already in `org-todo-keywords'. See that variable for
  2288. more information."
  2289. :group 'org-todo
  2290. :group 'org-keywords
  2291. :type '(choice (const sequence)
  2292. (const type)))
  2293. (defcustom org-use-fast-todo-selection t
  2294. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2295. This variable describes if and under what circumstances the cycling
  2296. mechanism for TODO keywords will be replaced by a single-key, direct
  2297. selection scheme.
  2298. When nil, fast selection is never used.
  2299. When the symbol `prefix', it will be used when `org-todo' is called
  2300. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2301. `C-u t' in an agenda buffer.
  2302. When t, fast selection is used by default. In this case, the prefix
  2303. argument forces cycling instead.
  2304. In all cases, the special interface is only used if access keys have
  2305. actually been assigned by the user, i.e. if keywords in the configuration
  2306. are followed by a letter in parenthesis, like TODO(t)."
  2307. :group 'org-todo
  2308. :type '(choice
  2309. (const :tag "Never" nil)
  2310. (const :tag "By default" t)
  2311. (const :tag "Only with C-u C-c C-t" prefix)))
  2312. (defcustom org-provide-todo-statistics t
  2313. "Non-nil means update todo statistics after insert and toggle.
  2314. ALL-HEADLINES means update todo statistics by including headlines
  2315. with no TODO keyword as well, counting them as not done.
  2316. A list of TODO keywords means the same, but skip keywords that are
  2317. not in this list.
  2318. When set to a list of two lists, the first list contains keywords
  2319. to consider as TODO keywords, the second list contains keywords
  2320. to consider as DONE keywords.
  2321. When this is set, todo statistics is updated in the parent of the
  2322. current entry each time a todo state is changed."
  2323. :group 'org-todo
  2324. :type '(choice
  2325. (const :tag "Yes, only for TODO entries" t)
  2326. (const :tag "Yes, including all entries" all-headlines)
  2327. (repeat :tag "Yes, for TODOs in this list"
  2328. (string :tag "TODO keyword"))
  2329. (list :tag "Yes, for TODOs and DONEs in these lists"
  2330. (repeat (string :tag "TODO keyword"))
  2331. (repeat (string :tag "DONE keyword")))
  2332. (other :tag "No TODO statistics" nil)))
  2333. (defcustom org-hierarchical-todo-statistics t
  2334. "Non-nil means TODO statistics covers just direct children.
  2335. When nil, all entries in the subtree are considered.
  2336. This has only an effect if `org-provide-todo-statistics' is set.
  2337. To set this to nil for only a single subtree, use a COOKIE_DATA
  2338. property and include the word \"recursive\" into the value."
  2339. :group 'org-todo
  2340. :type 'boolean)
  2341. (defcustom org-after-todo-state-change-hook nil
  2342. "Hook which is run after the state of a TODO item was changed.
  2343. The new state (a string with a TODO keyword, or nil) is available in the
  2344. Lisp variable `org-state'."
  2345. :group 'org-todo
  2346. :type 'hook)
  2347. (defvar org-blocker-hook nil
  2348. "Hook for functions that are allowed to block a state change.
  2349. Functions in this hook should not modify the buffer.
  2350. Each function gets as its single argument a property list,
  2351. see `org-trigger-hook' for more information about this list.
  2352. If any of the functions in this hook returns nil, the state change
  2353. is blocked.")
  2354. (defvar org-trigger-hook nil
  2355. "Hook for functions that are triggered by a state change.
  2356. Each function gets as its single argument a property list with at
  2357. least the following elements:
  2358. (:type type-of-change :position pos-at-entry-start
  2359. :from old-state :to new-state)
  2360. Depending on the type, more properties may be present.
  2361. This mechanism is currently implemented for:
  2362. TODO state changes
  2363. ------------------
  2364. :type todo-state-change
  2365. :from previous state (keyword as a string), or nil, or a symbol
  2366. 'todo' or 'done', to indicate the general type of state.
  2367. :to new state, like in :from")
  2368. (defcustom org-enforce-todo-dependencies nil
  2369. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2370. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2371. be blocked if any prior sibling is not yet done.
  2372. Finally, if the parent is blocked because of ordered siblings of its own,
  2373. the child will also be blocked."
  2374. :set (lambda (var val)
  2375. (set var val)
  2376. (if val
  2377. (add-hook 'org-blocker-hook
  2378. 'org-block-todo-from-children-or-siblings-or-parent)
  2379. (remove-hook 'org-blocker-hook
  2380. 'org-block-todo-from-children-or-siblings-or-parent)))
  2381. :group 'org-todo
  2382. :type 'boolean)
  2383. (defcustom org-enforce-todo-checkbox-dependencies nil
  2384. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2385. When this is nil, checkboxes have no influence on switching TODO states.
  2386. When non-nil, you first need to check off all check boxes before the TODO
  2387. entry can be switched to DONE.
  2388. This variable needs to be set before org.el is loaded, and you need to
  2389. restart Emacs after a change to make the change effective. The only way
  2390. to change is while Emacs is running is through the customize interface."
  2391. :set (lambda (var val)
  2392. (set var val)
  2393. (if val
  2394. (add-hook 'org-blocker-hook
  2395. 'org-block-todo-from-checkboxes)
  2396. (remove-hook 'org-blocker-hook
  2397. 'org-block-todo-from-checkboxes)))
  2398. :group 'org-todo
  2399. :type 'boolean)
  2400. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2401. "Non-nil means inserting a TODO heading is treated as state change.
  2402. So when the command \\[org-insert-todo-heading] is used, state change
  2403. logging will apply if appropriate. When nil, the new TODO item will
  2404. be inserted directly, and no logging will take place."
  2405. :group 'org-todo
  2406. :type 'boolean)
  2407. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2408. "Non-nil means switching TODO states with S-cursor counts as state change.
  2409. This is the default behavior. However, setting this to nil allows a
  2410. convenient way to select a TODO state and bypass any logging associated
  2411. with that."
  2412. :group 'org-todo
  2413. :type 'boolean)
  2414. (defcustom org-todo-state-tags-triggers nil
  2415. "Tag changes that should be triggered by TODO state changes.
  2416. This is a list. Each entry is
  2417. (state-change (tag . flag) .......)
  2418. State-change can be a string with a state, and empty string to indicate the
  2419. state that has no TODO keyword, or it can be one of the symbols `todo'
  2420. or `done', meaning any not-done or done state, respectively."
  2421. :group 'org-todo
  2422. :group 'org-tags
  2423. :type '(repeat
  2424. (cons (choice :tag "When changing to"
  2425. (const :tag "Not-done state" todo)
  2426. (const :tag "Done state" done)
  2427. (string :tag "State"))
  2428. (repeat
  2429. (cons :tag "Tag action"
  2430. (string :tag "Tag")
  2431. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2432. (defcustom org-log-done nil
  2433. "Information to record when a task moves to the DONE state.
  2434. Possible values are:
  2435. nil Don't add anything, just change the keyword
  2436. time Add a time stamp to the task
  2437. note Prompt for a note and add it with template `org-log-note-headings'
  2438. This option can also be set with on a per-file-basis with
  2439. #+STARTUP: nologdone
  2440. #+STARTUP: logdone
  2441. #+STARTUP: lognotedone
  2442. You can have local logging settings for a subtree by setting the LOGGING
  2443. property to one or more of these keywords."
  2444. :group 'org-todo
  2445. :group 'org-progress
  2446. :type '(choice
  2447. (const :tag "No logging" nil)
  2448. (const :tag "Record CLOSED timestamp" time)
  2449. (const :tag "Record CLOSED timestamp with note." note)))
  2450. ;; Normalize old uses of org-log-done.
  2451. (cond
  2452. ((eq org-log-done t) (setq org-log-done 'time))
  2453. ((and (listp org-log-done) (memq 'done org-log-done))
  2454. (setq org-log-done 'note)))
  2455. (defcustom org-log-reschedule nil
  2456. "Information to record when the scheduling date of a tasks is modified.
  2457. Possible values are:
  2458. nil Don't add anything, just change the date
  2459. time Add a time stamp to the task
  2460. note Prompt for a note and add it with template `org-log-note-headings'
  2461. This option can also be set with on a per-file-basis with
  2462. #+STARTUP: nologreschedule
  2463. #+STARTUP: logreschedule
  2464. #+STARTUP: lognotereschedule"
  2465. :group 'org-todo
  2466. :group 'org-progress
  2467. :type '(choice
  2468. (const :tag "No logging" nil)
  2469. (const :tag "Record timestamp" time)
  2470. (const :tag "Record timestamp with note." note)))
  2471. (defcustom org-log-redeadline nil
  2472. "Information to record when the deadline date of a tasks is modified.
  2473. Possible values are:
  2474. nil Don't add anything, just change the date
  2475. time Add a time stamp to the task
  2476. note Prompt for a note and add it with template `org-log-note-headings'
  2477. This option can also be set with on a per-file-basis with
  2478. #+STARTUP: nologredeadline
  2479. #+STARTUP: logredeadline
  2480. #+STARTUP: lognoteredeadline
  2481. You can have local logging settings for a subtree by setting the LOGGING
  2482. property to one or more of these keywords."
  2483. :group 'org-todo
  2484. :group 'org-progress
  2485. :type '(choice
  2486. (const :tag "No logging" nil)
  2487. (const :tag "Record timestamp" time)
  2488. (const :tag "Record timestamp with note." note)))
  2489. (defcustom org-log-note-clock-out nil
  2490. "Non-nil means record a note when clocking out of an item.
  2491. This can also be configured on a per-file basis by adding one of
  2492. the following lines anywhere in the buffer:
  2493. #+STARTUP: lognoteclock-out
  2494. #+STARTUP: nolognoteclock-out"
  2495. :group 'org-todo
  2496. :group 'org-progress
  2497. :type 'boolean)
  2498. (defcustom org-log-done-with-time t
  2499. "Non-nil means the CLOSED time stamp will contain date and time.
  2500. When nil, only the date will be recorded."
  2501. :group 'org-progress
  2502. :type 'boolean)
  2503. (defcustom org-log-note-headings
  2504. '((done . "CLOSING NOTE %t")
  2505. (state . "State %-12s from %-12S %t")
  2506. (note . "Note taken on %t")
  2507. (reschedule . "Rescheduled from %S on %t")
  2508. (delschedule . "Not scheduled, was %S on %t")
  2509. (redeadline . "New deadline from %S on %t")
  2510. (deldeadline . "Removed deadline, was %S on %t")
  2511. (refile . "Refiled on %t")
  2512. (clock-out . ""))
  2513. "Headings for notes added to entries.
  2514. The value is an alist, with the car being a symbol indicating the note
  2515. context, and the cdr is the heading to be used. The heading may also be the
  2516. empty string.
  2517. %t in the heading will be replaced by a time stamp.
  2518. %T will be an active time stamp instead the default inactive one
  2519. %d will be replaced by a short-format time stamp.
  2520. %D will be replaced by an active short-format time stamp.
  2521. %s will be replaced by the new TODO state, in double quotes.
  2522. %S will be replaced by the old TODO state, in double quotes.
  2523. %u will be replaced by the user name.
  2524. %U will be replaced by the full user name.
  2525. In fact, it is not a good idea to change the `state' entry, because
  2526. agenda log mode depends on the format of these entries."
  2527. :group 'org-todo
  2528. :group 'org-progress
  2529. :type '(list :greedy t
  2530. (cons (const :tag "Heading when closing an item" done) string)
  2531. (cons (const :tag
  2532. "Heading when changing todo state (todo sequence only)"
  2533. state) string)
  2534. (cons (const :tag "Heading when just taking a note" note) string)
  2535. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2536. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2537. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2538. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2539. (cons (const :tag "Heading when refiling" refile) string)
  2540. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2541. (unless (assq 'note org-log-note-headings)
  2542. (push '(note . "%t") org-log-note-headings))
  2543. (defcustom org-log-into-drawer nil
  2544. "Non-nil means insert state change notes and time stamps into a drawer.
  2545. When nil, state changes notes will be inserted after the headline and
  2546. any scheduling and clock lines, but not inside a drawer.
  2547. The value of this variable should be the name of the drawer to use.
  2548. LOGBOOK is proposed as the default drawer for this purpose, you can
  2549. also set this to a string to define the drawer of your choice.
  2550. A value of t is also allowed, representing \"LOGBOOK\".
  2551. A value of t or nil can also be set with on a per-file-basis with
  2552. #+STARTUP: logdrawer
  2553. #+STARTUP: nologdrawer
  2554. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2555. will be ignored.
  2556. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2557. a subtree."
  2558. :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. "Return the value of `org-log-into-drawer', but let properties overrule.
  2567. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2568. used instead of the default value."
  2569. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2570. (cond
  2571. ((not p) org-log-into-drawer)
  2572. ((equal p "nil") nil)
  2573. ((equal p "t") "LOGBOOK")
  2574. (t p))))
  2575. (defcustom org-log-state-notes-insert-after-drawers nil
  2576. "Non-nil means insert state change notes after any drawers in entry.
  2577. Only the drawers that *immediately* follow the headline and the
  2578. deadline/scheduled line are skipped.
  2579. When nil, insert notes right after the heading and perhaps the line
  2580. with deadline/scheduling if present.
  2581. This variable will have no effect if `org-log-into-drawer' is
  2582. set."
  2583. :group 'org-todo
  2584. :group 'org-progress
  2585. :type 'boolean)
  2586. (defcustom org-log-states-order-reversed t
  2587. "Non-nil means the latest state note will be directly after heading.
  2588. When nil, the state change notes will be ordered according to time.
  2589. This option can also be set with on a per-file-basis with
  2590. #+STARTUP: logstatesreversed
  2591. #+STARTUP: nologstatesreversed"
  2592. :group 'org-todo
  2593. :group 'org-progress
  2594. :type 'boolean)
  2595. (defcustom org-todo-repeat-to-state nil
  2596. "The TODO state to which a repeater should return the repeating task.
  2597. By default this is the first task in a TODO sequence, or the previous state
  2598. in a TODO_TYP set. But you can specify another task here.
  2599. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2600. :group 'org-todo
  2601. :version "24.1"
  2602. :type '(choice (const :tag "Head of sequence" nil)
  2603. (string :tag "Specific state")))
  2604. (defcustom org-log-repeat 'time
  2605. "Non-nil means record moving through the DONE state when triggering repeat.
  2606. An auto-repeating task is immediately switched back to TODO when
  2607. marked DONE. If you are not logging state changes (by adding \"@\"
  2608. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2609. record a closing note, there will be no record of the task moving
  2610. through DONE. This variable forces taking a note anyway.
  2611. nil Don't force a record
  2612. time Record a time stamp
  2613. note Prompt for a note and add it with template `org-log-note-headings'
  2614. This option can also be set with on a per-file-basis with
  2615. #+STARTUP: nologrepeat
  2616. #+STARTUP: logrepeat
  2617. #+STARTUP: lognoterepeat
  2618. You can have local logging settings for a subtree by setting the LOGGING
  2619. property to one or more of these keywords."
  2620. :group 'org-todo
  2621. :group 'org-progress
  2622. :type '(choice
  2623. (const :tag "Don't force a record" nil)
  2624. (const :tag "Force recording the DONE state" time)
  2625. (const :tag "Force recording a note with the DONE state" note)))
  2626. (defgroup org-priorities nil
  2627. "Priorities in Org-mode."
  2628. :tag "Org Priorities"
  2629. :group 'org-todo)
  2630. (defcustom org-enable-priority-commands t
  2631. "Non-nil means priority commands are active.
  2632. When nil, these commands will be disabled, so that you never accidentally
  2633. set a priority."
  2634. :group 'org-priorities
  2635. :type 'boolean)
  2636. (defcustom org-highest-priority ?A
  2637. "The highest priority of TODO items. A character like ?A, ?B etc.
  2638. Must have a smaller ASCII number than `org-lowest-priority'."
  2639. :group 'org-priorities
  2640. :type 'character)
  2641. (defcustom org-lowest-priority ?C
  2642. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2643. Must have a larger ASCII number than `org-highest-priority'."
  2644. :group 'org-priorities
  2645. :type 'character)
  2646. (defcustom org-default-priority ?B
  2647. "The default priority of TODO items.
  2648. This is the priority an item gets if no explicit priority is given.
  2649. When starting to cycle on an empty priority the first step in the cycle
  2650. depends on `org-priority-start-cycle-with-default'. The resulting first
  2651. step priority must not exceed the range from `org-highest-priority' to
  2652. `org-lowest-priority' which means that `org-default-priority' has to be
  2653. in this range exclusive or inclusive the range boundaries. Else the
  2654. first step refuses to set the default and the second will fall back
  2655. to (depending on the command used) the highest or lowest priority."
  2656. :group 'org-priorities
  2657. :type 'character)
  2658. (defcustom org-priority-start-cycle-with-default t
  2659. "Non-nil means start with default priority when starting to cycle.
  2660. When this is nil, the first step in the cycle will be (depending on the
  2661. command used) one higher or lower than the default priority.
  2662. See also `org-default-priority'."
  2663. :group 'org-priorities
  2664. :type 'boolean)
  2665. (defcustom org-get-priority-function nil
  2666. "Function to extract the priority from a string.
  2667. The string is normally the headline. If this is nil Org computes the
  2668. priority from the priority cookie like [#A] in the headline. It returns
  2669. an integer, increasing by 1000 for each priority level.
  2670. The user can set a different function here, which should take a string
  2671. as an argument and return the numeric priority."
  2672. :group 'org-priorities
  2673. :version "24.1"
  2674. :type '(choice
  2675. (const nil)
  2676. (function)))
  2677. (defgroup org-time nil
  2678. "Options concerning time stamps and deadlines in Org-mode."
  2679. :tag "Org Time"
  2680. :group 'org)
  2681. (defcustom org-insert-labeled-timestamps-at-point nil
  2682. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2683. When nil, these labeled time stamps are forces into the second line of an
  2684. entry, just after the headline. When scheduling from the global TODO list,
  2685. the time stamp will always be forced into the second line."
  2686. :group 'org-time
  2687. :type 'boolean)
  2688. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2689. "Number of minutes to round time stamps to.
  2690. These are two values, the first applies when first creating a time stamp.
  2691. The second applies when changing it with the commands `S-up' and `S-down'.
  2692. When changing the time stamp, this means that it will change in steps
  2693. of N minutes, as given by the second value.
  2694. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2695. numbers should be factors of 60, so for example 5, 10, 15.
  2696. When this is larger than 1, you can still force an exact time stamp by using
  2697. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2698. and by using a prefix arg to `S-up/down' to specify the exact number
  2699. of minutes to shift."
  2700. :group 'org-time
  2701. :get (lambda (var) ; Make sure both elements are there
  2702. (if (integerp (default-value var))
  2703. (list (default-value var) 5)
  2704. (default-value var)))
  2705. :type '(list
  2706. (integer :tag "when inserting times")
  2707. (integer :tag "when modifying times")))
  2708. ;; Normalize old customizations of this variable.
  2709. (when (integerp org-time-stamp-rounding-minutes)
  2710. (setq org-time-stamp-rounding-minutes
  2711. (list org-time-stamp-rounding-minutes
  2712. org-time-stamp-rounding-minutes)))
  2713. (defcustom org-display-custom-times nil
  2714. "Non-nil means overlay custom formats over all time stamps.
  2715. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2716. To turn this on on a per-file basis, insert anywhere in the file:
  2717. #+STARTUP: customtime"
  2718. :group 'org-time
  2719. :set 'set-default
  2720. :type 'sexp)
  2721. (make-variable-buffer-local 'org-display-custom-times)
  2722. (defcustom org-time-stamp-custom-formats
  2723. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2724. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2725. These are overlaid over the default ISO format if the variable
  2726. `org-display-custom-times' is set. Time like %H:%M should be at the
  2727. end of the second format. The custom formats are also honored by export
  2728. commands, if custom time display is turned on at the time of export."
  2729. :group 'org-time
  2730. :type 'sexp)
  2731. (defun org-time-stamp-format (&optional long inactive)
  2732. "Get the right format for a time string."
  2733. (let ((f (if long (cdr org-time-stamp-formats)
  2734. (car org-time-stamp-formats))))
  2735. (if inactive
  2736. (concat "[" (substring f 1 -1) "]")
  2737. f)))
  2738. (defcustom org-time-clocksum-format
  2739. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2740. "The format string used when creating CLOCKSUM lines.
  2741. This is also used when Org mode generates a time duration.
  2742. The value can be a single format string containing two
  2743. %-sequences, which will be filled with the number of hours and
  2744. minutes in that order.
  2745. Alternatively, the value can be a plist associating any of the
  2746. keys :years, :months, :weeks, :days, :hours or :minutes with
  2747. format strings. The time duration is formatted using only the
  2748. time components that are needed and concatenating the results.
  2749. If a time unit in absent, it falls back to the next smallest
  2750. unit.
  2751. The keys :require-years, :require-months, :require-days,
  2752. :require-weeks, :require-hours, :require-minutes are also
  2753. meaningful. A non-nil value for these keys indicates that the
  2754. corresponding time component should always be included, even if
  2755. its value is 0.
  2756. For example,
  2757. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2758. :require-minutes t)
  2759. means durations longer than a day will be expressed in days,
  2760. hours and minutes, and durations less than a day will always be
  2761. expressed in hours and minutes (even for durations less than an
  2762. hour).
  2763. The value
  2764. \(:days \"%dd\" :minutes \"%dm\")
  2765. means durations longer than a day will be expressed in days and
  2766. minutes, and durations less than a day will be expressed entirely
  2767. in minutes (even for durations longer than an hour)."
  2768. :group 'org-time
  2769. :group 'org-clock
  2770. :version "24.4"
  2771. :package-version '(Org . "8.0")
  2772. :type '(choice (string :tag "Format string")
  2773. (set :tag "Plist"
  2774. (group :inline t (const :tag "Years" :years)
  2775. (string :tag "Format string"))
  2776. (group :inline t
  2777. (const :tag "Always show years" :require-years)
  2778. (const t))
  2779. (group :inline t (const :tag "Months" :months)
  2780. (string :tag "Format string"))
  2781. (group :inline t
  2782. (const :tag "Always show months" :require-months)
  2783. (const t))
  2784. (group :inline t (const :tag "Weeks" :weeks)
  2785. (string :tag "Format string"))
  2786. (group :inline t
  2787. (const :tag "Always show weeks" :require-weeks)
  2788. (const t))
  2789. (group :inline t (const :tag "Days" :days)
  2790. (string :tag "Format string"))
  2791. (group :inline t
  2792. (const :tag "Always show days" :require-days)
  2793. (const t))
  2794. (group :inline t (const :tag "Hours" :hours)
  2795. (string :tag "Format string"))
  2796. (group :inline t
  2797. (const :tag "Always show hours" :require-hours)
  2798. (const t))
  2799. (group :inline t (const :tag "Minutes" :minutes)
  2800. (string :tag "Format string"))
  2801. (group :inline t
  2802. (const :tag "Always show minutes" :require-minutes)
  2803. (const t)))))
  2804. (defcustom org-time-clocksum-use-fractional nil
  2805. "When non-nil, \\[org-clock-display] uses fractional times.
  2806. See `org-time-clocksum-format' for more on time clock formats."
  2807. :group 'org-time
  2808. :group 'org-clock
  2809. :version "24.3"
  2810. :type 'boolean)
  2811. (defcustom org-time-clocksum-use-effort-durations nil
  2812. "When non-nil, \\[org-clock-display] uses effort durations.
  2813. E.g. by default, one day is considered to be a 8 hours effort,
  2814. so a task that has been clocked for 16 hours will be displayed
  2815. as during 2 days in the clock display or in the clocktable.
  2816. See `org-effort-durations' on how to set effort durations
  2817. and `org-time-clocksum-format' for more on time clock formats."
  2818. :group 'org-time
  2819. :group 'org-clock
  2820. :version "24.4"
  2821. :package-version '(Org . "8.0")
  2822. :type 'boolean)
  2823. (defcustom org-time-clocksum-fractional-format "%.2f"
  2824. "The format string used when creating CLOCKSUM lines,
  2825. or when Org mode generates a time duration, if
  2826. `org-time-clocksum-use-fractional' is enabled.
  2827. The value can be a single format string containing one
  2828. %-sequence, which will be filled with the number of hours as
  2829. a float.
  2830. Alternatively, the value can be a plist associating any of the
  2831. keys :years, :months, :weeks, :days, :hours or :minutes with
  2832. a format string. The time duration is formatted using the
  2833. largest time unit which gives a non-zero integer part. If all
  2834. specified formats have zero integer part, the smallest time unit
  2835. is used."
  2836. :group 'org-time
  2837. :type '(choice (string :tag "Format string")
  2838. (set (group :inline t (const :tag "Years" :years)
  2839. (string :tag "Format string"))
  2840. (group :inline t (const :tag "Months" :months)
  2841. (string :tag "Format string"))
  2842. (group :inline t (const :tag "Weeks" :weeks)
  2843. (string :tag "Format string"))
  2844. (group :inline t (const :tag "Days" :days)
  2845. (string :tag "Format string"))
  2846. (group :inline t (const :tag "Hours" :hours)
  2847. (string :tag "Format string"))
  2848. (group :inline t (const :tag "Minutes" :minutes)
  2849. (string :tag "Format string")))))
  2850. (defcustom org-deadline-warning-days 14
  2851. "Number of days before expiration during which a deadline becomes active.
  2852. This variable governs the display in sparse trees and in the agenda.
  2853. When 0 or negative, it means use this number (the absolute value of it)
  2854. even if a deadline has a different individual lead time specified.
  2855. Custom commands can set this variable in the options section."
  2856. :group 'org-time
  2857. :group 'org-agenda-daily/weekly
  2858. :type 'integer)
  2859. (defcustom org-scheduled-delay-days 0
  2860. "Number of days before a scheduled item becomes active.
  2861. This variable governs the display in sparse trees and in the agenda.
  2862. The default value (i.e. 0) means: don't delay scheduled item.
  2863. When negative, it means use this number (the absolute value of it)
  2864. even if a scheduled item has a different individual delay time
  2865. specified.
  2866. Custom commands can set this variable in the options section."
  2867. :group 'org-time
  2868. :group 'org-agenda-daily/weekly
  2869. :version "24.4"
  2870. :package-version '(Org . "8.0")
  2871. :type 'integer)
  2872. (defcustom org-read-date-prefer-future t
  2873. "Non-nil means assume future for incomplete date input from user.
  2874. This affects the following situations:
  2875. 1. The user gives a month but not a year.
  2876. For example, if it is April and you enter \"feb 2\", this will be read
  2877. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2878. 2. The user gives a day, but no month.
  2879. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2880. read this as the third of *next* month. However, if you enter \"17\",
  2881. it will be considered as *this* month.
  2882. If you set this variable to the symbol `time', then also the following
  2883. will work:
  2884. 3. If the user gives a time.
  2885. If the time is before now, it will be interpreted as tomorrow.
  2886. Currently none of this works for ISO week specifications.
  2887. When this option is nil, the current day, month and year will always be
  2888. used as defaults.
  2889. See also `org-agenda-jump-prefer-future'."
  2890. :group 'org-time
  2891. :type '(choice
  2892. (const :tag "Never" nil)
  2893. (const :tag "Check month and day" t)
  2894. (const :tag "Check month, day, and time" time)))
  2895. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2896. "Should the agenda jump command prefer the future for incomplete dates?
  2897. The default is to do the same as configured in `org-read-date-prefer-future'.
  2898. But you can also set a deviating value here.
  2899. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2900. :group 'org-agenda
  2901. :group 'org-time
  2902. :version "24.1"
  2903. :type '(choice
  2904. (const :tag "Use org-read-date-prefer-future"
  2905. org-read-date-prefer-future)
  2906. (const :tag "Never" nil)
  2907. (const :tag "Always" t)))
  2908. (defcustom org-read-date-force-compatible-dates t
  2909. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2910. Depending on the system Emacs is running on, certain dates cannot
  2911. be represented with the type used internally to represent time.
  2912. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2913. correctly. Some systems allow for earlier dates, some for later,
  2914. some for both. One way to find out it to insert any date into an
  2915. Org buffer, putting the cursor on the year and hitting S-up and
  2916. S-down to test the range.
  2917. When this variable is set to t, the date/time prompt will not let
  2918. you specify dates outside the 1970-2037 range, so it is certain that
  2919. these dates will work in whatever version of Emacs you are
  2920. running, and also that you can move a file from one Emacs implementation
  2921. to another. WHenever Org is forcing the year for you, it will display
  2922. a message and beep.
  2923. When this variable is nil, Org will check if the date is
  2924. representable in the specific Emacs implementation you are using.
  2925. If not, it will force a year, usually the current year, and beep
  2926. to remind you. Currently this setting is not recommended because
  2927. the likelihood that you will open your Org files in an Emacs that
  2928. has limited date range is not negligible.
  2929. A workaround for this problem is to use diary sexp dates for time
  2930. stamps outside of this range."
  2931. :group 'org-time
  2932. :version "24.1"
  2933. :type 'boolean)
  2934. (defcustom org-read-date-display-live t
  2935. "Non-nil means display current interpretation of date prompt live.
  2936. This display will be in an overlay, in the minibuffer."
  2937. :group 'org-time
  2938. :type 'boolean)
  2939. (defcustom org-read-date-popup-calendar t
  2940. "Non-nil means pop up a calendar when prompting for a date.
  2941. In the calendar, the date can be selected with mouse-1. However, the
  2942. minibuffer will also be active, and you can simply enter the date as well.
  2943. When nil, only the minibuffer will be available."
  2944. :group 'org-time
  2945. :type 'boolean)
  2946. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2947. 'org-read-date-popup-calendar)
  2948. (make-obsolete-variable
  2949. 'org-read-date-minibuffer-setup-hook
  2950. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2951. (defcustom org-read-date-minibuffer-setup-hook nil
  2952. "Hook to be used to set up keys for the date/time interface.
  2953. Add key definitions to `minibuffer-local-map', which will be a
  2954. temporary copy.
  2955. WARNING: This option is obsolete, you should use
  2956. `org-read-date-minibuffer-local-map' to set up keys."
  2957. :group 'org-time
  2958. :type 'hook)
  2959. (defcustom org-extend-today-until 0
  2960. "The hour when your day really ends. Must be an integer.
  2961. This has influence for the following applications:
  2962. - When switching the agenda to \"today\". It it is still earlier than
  2963. the time given here, the day recognized as TODAY is actually yesterday.
  2964. - When a date is read from the user and it is still before the time given
  2965. here, the current date and time will be assumed to be yesterday, 23:59.
  2966. Also, timestamps inserted in capture templates follow this rule.
  2967. IMPORTANT: This is a feature whose implementation is and likely will
  2968. remain incomplete. Really, it is only here because past midnight seems to
  2969. be the favorite working time of John Wiegley :-)"
  2970. :group 'org-time
  2971. :type 'integer)
  2972. (defcustom org-use-effective-time nil
  2973. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2974. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2975. \"effective time\" of any timestamps between midnight and 8am will be
  2976. 23:59 of the previous day."
  2977. :group 'org-time
  2978. :version "24.1"
  2979. :type 'boolean)
  2980. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2981. "When non-nil, use the last clock out time for `org-todo'.
  2982. Note that this option has precedence over the combined use of
  2983. `org-use-effective-time' and `org-extend-today-until'."
  2984. :group 'org-time
  2985. :version "24.4"
  2986. :package-version '(Org . "8.0")
  2987. :type 'boolean)
  2988. (defcustom org-edit-timestamp-down-means-later nil
  2989. "Non-nil means S-down will increase the time in a time stamp.
  2990. When nil, S-up will increase."
  2991. :group 'org-time
  2992. :type 'boolean)
  2993. (defcustom org-calendar-follow-timestamp-change t
  2994. "Non-nil means make the calendar window follow timestamp changes.
  2995. When a timestamp is modified and the calendar window is visible, it will be
  2996. moved to the new date."
  2997. :group 'org-time
  2998. :type 'boolean)
  2999. (defgroup org-tags nil
  3000. "Options concerning tags in Org-mode."
  3001. :tag "Org Tags"
  3002. :group 'org)
  3003. (defcustom org-tag-alist nil
  3004. "List of tags allowed in Org-mode files.
  3005. When this list is nil, Org-mode will base TAG input on what is already in the
  3006. buffer.
  3007. The value of this variable is an alist, the car of each entry must be a
  3008. keyword as a string, the cdr may be a character that is used to select
  3009. that tag through the fast-tag-selection interface.
  3010. See the manual for details."
  3011. :group 'org-tags
  3012. :type '(repeat
  3013. (choice
  3014. (cons (string :tag "Tag name")
  3015. (character :tag "Access char"))
  3016. (list :tag "Start radio group"
  3017. (const :startgroup)
  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. (const :tag "New line" (:newline)))))
  3025. (defcustom org-tag-persistent-alist nil
  3026. "List of tags that will always appear in all Org-mode files.
  3027. This is in addition to any in buffer settings or customizations
  3028. of `org-tag-alist'.
  3029. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3030. The value of this variable is an alist, the car of each entry must be a
  3031. keyword as a string, the cdr may be a character that is used to select
  3032. that tag through the fast-tag-selection interface.
  3033. See the manual for details.
  3034. To disable these tags on a per-file basis, insert anywhere in the file:
  3035. #+STARTUP: noptag"
  3036. :group 'org-tags
  3037. :type '(repeat
  3038. (choice
  3039. (cons (string :tag "Tag name")
  3040. (character :tag "Access char"))
  3041. (const :tag "Start radio group" (:startgroup))
  3042. (const :tag "Group tags delimiter" (:grouptags))
  3043. (const :tag "End radio group" (:endgroup))
  3044. (const :tag "New line" (:newline)))))
  3045. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3046. "If non-nil, always offer completion for all tags of all agenda files.
  3047. Instead of customizing this variable directly, you might want to
  3048. set it locally for capture buffers, because there no list of
  3049. tags in that file can be created dynamically (there are none).
  3050. (add-hook 'org-capture-mode-hook
  3051. (lambda ()
  3052. (set (make-local-variable
  3053. 'org-complete-tags-always-offer-all-agenda-tags)
  3054. t)))"
  3055. :group 'org-tags
  3056. :version "24.1"
  3057. :type 'boolean)
  3058. (defvar org-file-tags nil
  3059. "List of tags that can be inherited by all entries in the file.
  3060. The tags will be inherited if the variable `org-use-tag-inheritance'
  3061. says they should be.
  3062. This variable is populated from #+FILETAGS lines.")
  3063. (defcustom org-use-fast-tag-selection 'auto
  3064. "Non-nil means use fast tag selection scheme.
  3065. This is a special interface to select and deselect tags with single keys.
  3066. When nil, fast selection is never used.
  3067. When the symbol `auto', fast selection is used if and only if selection
  3068. characters for tags have been configured, either through the variable
  3069. `org-tag-alist' or through a #+TAGS line in the buffer.
  3070. When t, fast selection is always used and selection keys are assigned
  3071. automatically if necessary."
  3072. :group 'org-tags
  3073. :type '(choice
  3074. (const :tag "Always" t)
  3075. (const :tag "Never" nil)
  3076. (const :tag "When selection characters are configured" auto)))
  3077. (defcustom org-fast-tag-selection-single-key nil
  3078. "Non-nil means fast tag selection exits after first change.
  3079. When nil, you have to press RET to exit it.
  3080. During fast tag selection, you can toggle this flag with `C-c'.
  3081. This variable can also have the value `expert'. In this case, the window
  3082. displaying the tags menu is not even shown, until you press C-c again."
  3083. :group 'org-tags
  3084. :type '(choice
  3085. (const :tag "No" nil)
  3086. (const :tag "Yes" t)
  3087. (const :tag "Expert" expert)))
  3088. (defvar org-fast-tag-selection-include-todo nil
  3089. "Non-nil means fast tags selection interface will also offer TODO states.
  3090. This is an undocumented feature, you should not rely on it.")
  3091. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3092. "The column to which tags should be indented in a headline.
  3093. If this number is positive, it specifies the column. If it is negative,
  3094. it means that the tags should be flushright to that column. For example,
  3095. -80 works well for a normal 80 character screen.
  3096. When 0, place tags directly after headline text, with only one space in
  3097. between."
  3098. :group 'org-tags
  3099. :type 'integer)
  3100. (defcustom org-auto-align-tags t
  3101. "Non-nil keeps tags aligned when modifying headlines.
  3102. Some operations (i.e. demoting) change the length of a headline and
  3103. therefore shift the tags around. With this option turned on, after
  3104. each such operation the tags are again aligned to `org-tags-column'."
  3105. :group 'org-tags
  3106. :type 'boolean)
  3107. (defcustom org-use-tag-inheritance t
  3108. "Non-nil means tags in levels apply also for sublevels.
  3109. When nil, only the tags directly given in a specific line apply there.
  3110. This may also be a list of tags that should be inherited, or a regexp that
  3111. matches tags that should be inherited. Additional control is possible
  3112. with the variable `org-tags-exclude-from-inheritance' which gives an
  3113. explicit list of tags to be excluded from inheritance, even if the value of
  3114. `org-use-tag-inheritance' would select it for inheritance.
  3115. If this option is t, a match early-on in a tree can lead to a large
  3116. number of matches in the subtree when constructing the agenda or creating
  3117. a sparse tree. If you only want to see the first match in a tree during
  3118. a search, check out the variable `org-tags-match-list-sublevels'."
  3119. :group 'org-tags
  3120. :type '(choice
  3121. (const :tag "Not" nil)
  3122. (const :tag "Always" t)
  3123. (repeat :tag "Specific tags" (string :tag "Tag"))
  3124. (regexp :tag "Tags matched by regexp")))
  3125. (defcustom org-tags-exclude-from-inheritance nil
  3126. "List of tags that should never be inherited.
  3127. This is a way to exclude a few tags from inheritance. For way to do
  3128. the opposite, to actively allow inheritance for selected tags,
  3129. see the variable `org-use-tag-inheritance'."
  3130. :group 'org-tags
  3131. :type '(repeat (string :tag "Tag")))
  3132. (defun org-tag-inherit-p (tag)
  3133. "Check if TAG is one that should be inherited."
  3134. (cond
  3135. ((member tag org-tags-exclude-from-inheritance) nil)
  3136. ((eq org-use-tag-inheritance t) t)
  3137. ((not org-use-tag-inheritance) nil)
  3138. ((stringp org-use-tag-inheritance)
  3139. (string-match org-use-tag-inheritance tag))
  3140. ((listp org-use-tag-inheritance)
  3141. (member tag org-use-tag-inheritance))
  3142. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3143. (defcustom org-tags-match-list-sublevels t
  3144. "Non-nil means list also sublevels of headlines matching a search.
  3145. This variable applies to tags/property searches, and also to stuck
  3146. projects because this search is based on a tags match as well.
  3147. When set to the symbol `indented', sublevels are indented with
  3148. leading dots.
  3149. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3150. the sublevels of a headline matching a tag search often also match
  3151. the same search. Listing all of them can create very long lists.
  3152. Setting this variable to nil causes subtrees of a match to be skipped.
  3153. This variable is semi-obsolete and probably should always be true. It
  3154. is better to limit inheritance to certain tags using the variables
  3155. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3156. :group 'org-tags
  3157. :type '(choice
  3158. (const :tag "No, don't list them" nil)
  3159. (const :tag "Yes, do list them" t)
  3160. (const :tag "List them, indented with leading dots" indented)))
  3161. (defcustom org-tags-sort-function nil
  3162. "When set, tags are sorted using this function as a comparator."
  3163. :group 'org-tags
  3164. :type '(choice
  3165. (const :tag "No sorting" nil)
  3166. (const :tag "Alphabetical" string<)
  3167. (const :tag "Reverse alphabetical" string>)
  3168. (function :tag "Custom function" nil)))
  3169. (defvar org-tags-history nil
  3170. "History of minibuffer reads for tags.")
  3171. (defvar org-last-tags-completion-table nil
  3172. "The last used completion table for tags.")
  3173. (defvar org-after-tags-change-hook nil
  3174. "Hook that is run after the tags in a line have changed.")
  3175. (defgroup org-properties nil
  3176. "Options concerning properties in Org-mode."
  3177. :tag "Org Properties"
  3178. :group 'org)
  3179. (defcustom org-property-format "%-10s %s"
  3180. "How property key/value pairs should be formatted by `indent-line'.
  3181. When `indent-line' hits a property definition, it will format the line
  3182. according to this format, mainly to make sure that the values are
  3183. lined-up with respect to each other."
  3184. :group 'org-properties
  3185. :type 'string)
  3186. (defcustom org-properties-postprocess-alist nil
  3187. "Alist of properties and functions to adjust inserted values.
  3188. Elements of this alist must be of the form
  3189. ([string] [function])
  3190. where [string] must be a property name and [function] must be a
  3191. lambda expression: this lambda expression must take one argument,
  3192. the value to adjust, and return the new value as a string.
  3193. For example, this element will allow the property \"Remaining\"
  3194. to be updated wrt the relation between the \"Effort\" property
  3195. and the clock summary:
  3196. ((\"Remaining\" (lambda(value)
  3197. (let ((clocksum (org-clock-sum-current-item))
  3198. (effort (org-duration-string-to-minutes
  3199. (org-entry-get (point) \"Effort\"))))
  3200. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3201. :group 'org-properties
  3202. :version "24.1"
  3203. :type '(alist :key-type (string :tag "Property")
  3204. :value-type (function :tag "Function")))
  3205. (defcustom org-use-property-inheritance nil
  3206. "Non-nil means properties apply also for sublevels.
  3207. This setting is chiefly used during property searches. Turning it on can
  3208. cause significant overhead when doing a search, which is why it is not
  3209. on by default.
  3210. When nil, only the properties directly given in the current entry count.
  3211. When t, every property is inherited. The value may also be a list of
  3212. properties that should have inheritance, or a regular expression matching
  3213. properties that should be inherited.
  3214. However, note that some special properties use inheritance under special
  3215. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3216. and the properties ending in \"_ALL\" when they are used as descriptor
  3217. for valid values of a property.
  3218. Note for programmers:
  3219. When querying an entry with `org-entry-get', you can control if inheritance
  3220. should be used. By default, `org-entry-get' looks only at the local
  3221. properties. You can request inheritance by setting the inherit argument
  3222. to t (to force inheritance) or to `selective' (to respect the setting
  3223. in this variable)."
  3224. :group 'org-properties
  3225. :type '(choice
  3226. (const :tag "Not" nil)
  3227. (const :tag "Always" t)
  3228. (repeat :tag "Specific properties" (string :tag "Property"))
  3229. (regexp :tag "Properties matched by regexp")))
  3230. (defun org-property-inherit-p (property)
  3231. "Check if PROPERTY is one that should be inherited."
  3232. (cond
  3233. ((eq org-use-property-inheritance t) t)
  3234. ((not org-use-property-inheritance) nil)
  3235. ((stringp org-use-property-inheritance)
  3236. (string-match org-use-property-inheritance property))
  3237. ((listp org-use-property-inheritance)
  3238. (member property org-use-property-inheritance))
  3239. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3240. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3241. "The default column format, if no other format has been defined.
  3242. This variable can be set on the per-file basis by inserting a line
  3243. #+COLUMNS: %25ITEM ....."
  3244. :group 'org-properties
  3245. :type 'string)
  3246. (defcustom org-columns-ellipses ".."
  3247. "The ellipses to be used when a field in column view is truncated.
  3248. When this is the empty string, as many characters as possible are shown,
  3249. but then there will be no visual indication that the field has been truncated.
  3250. When this is a string of length N, the last N characters of a truncated
  3251. field are replaced by this string. If the column is narrower than the
  3252. ellipses string, only part of the ellipses string will be shown."
  3253. :group 'org-properties
  3254. :type 'string)
  3255. (defcustom org-columns-modify-value-for-display-function nil
  3256. "Function that modifies values for display in column view.
  3257. For example, it can be used to cut out a certain part from a time stamp.
  3258. The function must take 2 arguments:
  3259. column-title The title of the column (*not* the property name)
  3260. value The value that should be modified.
  3261. The function should return the value that should be displayed,
  3262. or nil if the normal value should be used."
  3263. :group 'org-properties
  3264. :type '(choice (const nil) (function)))
  3265. (defconst org-global-properties-fixed
  3266. '(("VISIBILITY_ALL" . "folded children content all")
  3267. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3268. "List of property/value pairs that can be inherited by any entry.
  3269. These are fixed values, for the preset properties. The user variable
  3270. that can be used to add to this list is `org-global-properties'.
  3271. The entries in this list are cons cells where the car is a property
  3272. name and cdr is a string with the value. If the value represents
  3273. multiple items like an \"_ALL\" property, separate the items by
  3274. spaces.")
  3275. (defcustom org-global-properties nil
  3276. "List of property/value pairs that can be inherited by any entry.
  3277. This list will be combined with the constant `org-global-properties-fixed'.
  3278. The entries in this list are cons cells where the car is a property
  3279. name and cdr is a string with the value.
  3280. You can set buffer-local values for the same purpose in the variable
  3281. `org-file-properties' this by adding lines like
  3282. #+PROPERTY: NAME VALUE"
  3283. :group 'org-properties
  3284. :type '(repeat
  3285. (cons (string :tag "Property")
  3286. (string :tag "Value"))))
  3287. (defvar org-file-properties nil
  3288. "List of property/value pairs that can be inherited by any entry.
  3289. Valid for the current buffer.
  3290. This variable is populated from #+PROPERTY lines.")
  3291. (make-variable-buffer-local 'org-file-properties)
  3292. (defgroup org-agenda nil
  3293. "Options concerning agenda views in Org-mode."
  3294. :tag "Org Agenda"
  3295. :group 'org)
  3296. (defvar org-category nil
  3297. "Variable used by org files to set a category for agenda display.
  3298. Such files should use a file variable to set it, for example
  3299. # -*- mode: org; org-category: \"ELisp\"
  3300. or contain a special line
  3301. #+CATEGORY: ELisp
  3302. If the file does not specify a category, then file's base name
  3303. is used instead.")
  3304. (make-variable-buffer-local 'org-category)
  3305. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3306. (defcustom org-agenda-files nil
  3307. "The files to be used for agenda display.
  3308. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3309. \\[org-remove-file]. You can also use customize to edit the list.
  3310. If an entry is a directory, all files in that directory that are matched by
  3311. `org-agenda-file-regexp' will be part of the file list.
  3312. If the value of the variable is not a list but a single file name, then
  3313. the list of agenda files is actually stored and maintained in that file, one
  3314. agenda file per line. In this file paths can be given relative to
  3315. `org-directory'. Tilde expansion and environment variable substitution
  3316. are also made."
  3317. :group 'org-agenda
  3318. :type '(choice
  3319. (repeat :tag "List of files and directories" file)
  3320. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3321. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3322. "Regular expression to match files for `org-agenda-files'.
  3323. If any element in the list in that variable contains a directory instead
  3324. of a normal file, all files in that directory that are matched by this
  3325. regular expression will be included."
  3326. :group 'org-agenda
  3327. :type 'regexp)
  3328. (defcustom org-agenda-text-search-extra-files nil
  3329. "List of extra files to be searched by text search commands.
  3330. These files will be searched in addition to the agenda files by the
  3331. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3332. Note that these files will only be searched for text search commands,
  3333. not for the other agenda views like todo lists, tag searches or the weekly
  3334. agenda. This variable is intended to list notes and possibly archive files
  3335. that should also be searched by these two commands.
  3336. In fact, if the first element in the list is the symbol `agenda-archives',
  3337. then all archive files of all agenda files will be added to the search
  3338. scope."
  3339. :group 'org-agenda
  3340. :type '(set :greedy t
  3341. (const :tag "Agenda Archives" agenda-archives)
  3342. (repeat :inline t (file))))
  3343. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3344. 'org-agenda-text-search-extra-files)
  3345. (defcustom org-agenda-skip-unavailable-files nil
  3346. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3347. A nil value means to remove them, after a query, from the list."
  3348. :group 'org-agenda
  3349. :type 'boolean)
  3350. (defcustom org-calendar-to-agenda-key [?c]
  3351. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3352. The command `org-calendar-goto-agenda' will be bound to this key. The
  3353. default is the character `c' because then `c' can be used to switch back and
  3354. forth between agenda and calendar."
  3355. :group 'org-agenda
  3356. :type 'sexp)
  3357. (defcustom org-calendar-insert-diary-entry-key [?i]
  3358. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3359. This option is irrelevant until `org-agenda-diary-file' has been configured
  3360. to point to an Org-mode file. When that is the case, the command
  3361. `org-agenda-diary-entry' will be bound to the key given here, by default
  3362. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3363. if you want to continue doing this, you need to change this to a different
  3364. key."
  3365. :group 'org-agenda
  3366. :type 'sexp)
  3367. (defcustom org-agenda-diary-file 'diary-file
  3368. "File to which to add new entries with the `i' key in agenda and calendar.
  3369. When this is the symbol `diary-file', the functionality in the Emacs
  3370. calendar will be used to add entries to the `diary-file'. But when this
  3371. points to a file, `org-agenda-diary-entry' will be used instead."
  3372. :group 'org-agenda
  3373. :type '(choice
  3374. (const :tag "The standard Emacs diary file" diary-file)
  3375. (file :tag "Special Org file diary entries")))
  3376. (eval-after-load "calendar"
  3377. '(progn
  3378. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3379. 'org-calendar-goto-agenda)
  3380. (add-hook 'calendar-mode-hook
  3381. (lambda ()
  3382. (unless (eq org-agenda-diary-file 'diary-file)
  3383. (define-key calendar-mode-map
  3384. org-calendar-insert-diary-entry-key
  3385. 'org-agenda-diary-entry))))))
  3386. (defgroup org-latex nil
  3387. "Options for embedding LaTeX code into Org-mode."
  3388. :tag "Org LaTeX"
  3389. :group 'org)
  3390. (defcustom org-format-latex-options
  3391. '(:foreground default :background default :scale 1.0
  3392. :html-foreground "Black" :html-background "Transparent"
  3393. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3394. "Options for creating images from LaTeX fragments.
  3395. This is a property list with the following properties:
  3396. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3397. `default' means use the foreground of the default face.
  3398. `auto' means use the foreground from the text face.
  3399. :background the background color, or \"Transparent\".
  3400. `default' means use the background of the default face.
  3401. `auto' means use the background from the text face.
  3402. :scale a scaling factor for the size of the images, to get more pixels
  3403. :html-foreground, :html-background, :html-scale
  3404. the same numbers for HTML export.
  3405. :matchers a list indicating which matchers should be used to
  3406. find LaTeX fragments. Valid members of this list are:
  3407. \"begin\" find environments
  3408. \"$1\" find single characters surrounded by $.$
  3409. \"$\" find math expressions surrounded by $...$
  3410. \"$$\" find math expressions surrounded by $$....$$
  3411. \"\\(\" find math expressions surrounded by \\(...\\)
  3412. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3413. :group 'org-latex
  3414. :type 'plist)
  3415. (defcustom org-format-latex-signal-error t
  3416. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3417. When nil, just push out a message."
  3418. :group 'org-latex
  3419. :version "24.1"
  3420. :type 'boolean)
  3421. (defcustom org-latex-to-mathml-jar-file nil
  3422. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3423. Use this to specify additional executable file say a jar file.
  3424. When using MathToWeb as the converter, specify the full-path to
  3425. your mathtoweb.jar file."
  3426. :group 'org-latex
  3427. :version "24.1"
  3428. :type '(choice
  3429. (const :tag "None" nil)
  3430. (file :tag "JAR file" :must-match t)))
  3431. (defcustom org-latex-to-mathml-convert-command nil
  3432. "Command to convert LaTeX fragments to MathML.
  3433. Replace format-specifiers in the command as noted below and use
  3434. `shell-command' to convert LaTeX to MathML.
  3435. %j: Executable file in fully expanded form as specified by
  3436. `org-latex-to-mathml-jar-file'.
  3437. %I: Input LaTeX file in fully expanded form
  3438. %o: Output MathML file
  3439. This command is used by `org-create-math-formula'.
  3440. When using MathToWeb as the converter, set this to
  3441. \"java -jar %j -unicode -force -df %o %I\"."
  3442. :group 'org-latex
  3443. :version "24.1"
  3444. :type '(choice
  3445. (const :tag "None" nil)
  3446. (string :tag "\nShell command")))
  3447. (defcustom org-latex-create-formula-image-program 'dvipng
  3448. "Program to convert LaTeX fragments with.
  3449. dvipng Process the LaTeX fragments to dvi file, then convert
  3450. dvi files to png files using dvipng.
  3451. This will also include processing of non-math environments.
  3452. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3453. to convert pdf files to png files"
  3454. :group 'org-latex
  3455. :version "24.1"
  3456. :type '(choice
  3457. (const :tag "dvipng" dvipng)
  3458. (const :tag "imagemagick" imagemagick)))
  3459. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3460. "Path to store latex preview images.
  3461. A relative path here creates many directories relative to the
  3462. processed org files paths. An absolute path puts all preview
  3463. images at the same place."
  3464. :group 'org-latex
  3465. :version "24.3"
  3466. :type 'string)
  3467. (defun org-format-latex-mathml-available-p ()
  3468. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3469. (save-match-data
  3470. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3471. org-latex-to-mathml-convert-command)
  3472. (let ((executable (car (split-string
  3473. org-latex-to-mathml-convert-command))))
  3474. (when (executable-find executable)
  3475. (if (string-match
  3476. "%j" org-latex-to-mathml-convert-command)
  3477. (file-readable-p org-latex-to-mathml-jar-file)
  3478. t))))))
  3479. (defcustom org-format-latex-header "\\documentclass{article}
  3480. \\usepackage[usenames]{color}
  3481. \[PACKAGES]
  3482. \[DEFAULT-PACKAGES]
  3483. \\pagestyle{empty} % do not remove
  3484. % The settings below are copied from fullpage.sty
  3485. \\setlength{\\textwidth}{\\paperwidth}
  3486. \\addtolength{\\textwidth}{-3cm}
  3487. \\setlength{\\oddsidemargin}{1.5cm}
  3488. \\addtolength{\\oddsidemargin}{-2.54cm}
  3489. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3490. \\setlength{\\textheight}{\\paperheight}
  3491. \\addtolength{\\textheight}{-\\headheight}
  3492. \\addtolength{\\textheight}{-\\headsep}
  3493. \\addtolength{\\textheight}{-\\footskip}
  3494. \\addtolength{\\textheight}{-3cm}
  3495. \\setlength{\\topmargin}{1.5cm}
  3496. \\addtolength{\\topmargin}{-2.54cm}"
  3497. "The document header used for processing LaTeX fragments.
  3498. It is imperative that this header make sure that no page number
  3499. appears on the page. The package defined in the variables
  3500. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3501. will either replace the placeholder \"[PACKAGES]\" in this
  3502. header, or they will be appended."
  3503. :group 'org-latex
  3504. :type 'string)
  3505. (defun org-set-packages-alist (var val)
  3506. "Set the packages alist and make sure it has 3 elements per entry."
  3507. (set var (mapcar (lambda (x)
  3508. (if (and (consp x) (= (length x) 2))
  3509. (list (car x) (nth 1 x) t)
  3510. x))
  3511. val)))
  3512. (defun org-get-packages-alist (var)
  3513. "Get the packages alist and make sure it has 3 elements per entry."
  3514. (mapcar (lambda (x)
  3515. (if (and (consp x) (= (length x) 2))
  3516. (list (car x) (nth 1 x) t)
  3517. x))
  3518. (default-value var)))
  3519. (defcustom org-latex-default-packages-alist
  3520. '(("AUTO" "inputenc" t)
  3521. ("T1" "fontenc" t)
  3522. ("" "fixltx2e" nil)
  3523. ("" "graphicx" t)
  3524. ("" "longtable" nil)
  3525. ("" "float" nil)
  3526. ("" "wrapfig" nil)
  3527. ("" "rotating" nil)
  3528. ("normalem" "ulem" t)
  3529. ("" "amsmath" t)
  3530. ("" "textcomp" t)
  3531. ("" "marvosym" t)
  3532. ("" "wasysym" t)
  3533. ("" "amssymb" t)
  3534. ("" "capt-of" nil)
  3535. ("" "hyperref" nil)
  3536. "\\tolerance=1000")
  3537. "Alist of default packages to be inserted in the header.
  3538. Change this only if one of the packages here causes an
  3539. incompatibility with another package you are using.
  3540. The packages in this list are needed by one part or another of
  3541. Org mode to function properly:
  3542. - inputenc, fontenc: for basic font and character selection
  3543. - fixltx2e: Important patches of LaTeX itself
  3544. - graphicx: for including images
  3545. - longtable: For multipage tables
  3546. - float, wrapfig: for figure placement
  3547. - rotating: for sideways figures and tables
  3548. - ulem: for underline and strike-through
  3549. - amsmath: for subscript and superscript and math environments
  3550. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3551. for interpreting the entities in `org-entities'. You can skip
  3552. some of these packages if you don't use any of their symbols.
  3553. - capt-of: for captions outside of floats
  3554. - hyperref: for cross references
  3555. Therefore you should not modify this variable unless you know
  3556. what you are doing. The one reason to change it anyway is that
  3557. you might be loading some other package that conflicts with one
  3558. of the default packages. Each element is either a cell or
  3559. a string.
  3560. A cell is of the format:
  3561. \( \"options\" \"package\" SNIPPET-FLAG).
  3562. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3563. when compiling LaTeX snippets into images for inclusion into
  3564. non-LaTeX output.
  3565. A string will be inserted as-is in the header of the document."
  3566. :group 'org-latex
  3567. :group 'org-export-latex
  3568. :set 'org-set-packages-alist
  3569. :get 'org-get-packages-alist
  3570. :version "24.1"
  3571. :type '(repeat
  3572. (choice
  3573. (list :tag "options/package pair"
  3574. (string :tag "options")
  3575. (string :tag "package")
  3576. (boolean :tag "Snippet"))
  3577. (string :tag "A line of LaTeX"))))
  3578. (defcustom org-latex-packages-alist nil
  3579. "Alist of packages to be inserted in every LaTeX header.
  3580. These will be inserted after `org-latex-default-packages-alist'.
  3581. Each element is either a cell or a string.
  3582. A cell is of the format:
  3583. \(\"options\" \"package\" SNIPPET-FLAG)
  3584. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3585. needed when turning LaTeX snippets into images for inclusion into
  3586. non-LaTeX output.
  3587. A string will be inserted as-is in the header of the document.
  3588. Make sure that you only list packages here which:
  3589. - you want in every file;
  3590. - do not conflict with the setup in `org-format-latex-header';
  3591. - do not conflict with the default packages in
  3592. `org-latex-default-packages-alist'."
  3593. :group 'org-latex
  3594. :group 'org-export-latex
  3595. :set 'org-set-packages-alist
  3596. :get 'org-get-packages-alist
  3597. :type '(repeat
  3598. (choice
  3599. (list :tag "options/package pair"
  3600. (string :tag "options")
  3601. (string :tag "package")
  3602. (boolean :tag "Snippet"))
  3603. (string :tag "A line of LaTeX"))))
  3604. (defgroup org-appearance nil
  3605. "Settings for Org-mode appearance."
  3606. :tag "Org Appearance"
  3607. :group 'org)
  3608. (defcustom org-level-color-stars-only nil
  3609. "Non-nil means fontify only the stars in each headline.
  3610. When nil, the entire headline is fontified.
  3611. Changing it requires restart of `font-lock-mode' to become effective
  3612. also in regions already fontified."
  3613. :group 'org-appearance
  3614. :type 'boolean)
  3615. (defcustom org-hide-leading-stars nil
  3616. "Non-nil means hide the first N-1 stars in a headline.
  3617. This works by using the face `org-hide' for these stars. This
  3618. face is white for a light background, and black for a dark
  3619. background. You may have to customize the face `org-hide' to
  3620. make this work.
  3621. Changing it requires restart of `font-lock-mode' to become effective
  3622. also in regions already fontified.
  3623. You may also set this on a per-file basis by adding one of the following
  3624. lines to the buffer:
  3625. #+STARTUP: hidestars
  3626. #+STARTUP: showstars"
  3627. :group 'org-appearance
  3628. :type 'boolean)
  3629. (defcustom org-hidden-keywords nil
  3630. "List of symbols corresponding to keywords to be hidden the org buffer.
  3631. For example, a value '(title) for this list will make the document's title
  3632. appear in the buffer without the initial #+TITLE: keyword."
  3633. :group 'org-appearance
  3634. :version "24.1"
  3635. :type '(set (const :tag "#+AUTHOR" author)
  3636. (const :tag "#+DATE" date)
  3637. (const :tag "#+EMAIL" email)
  3638. (const :tag "#+TITLE" title)))
  3639. (defcustom org-custom-properties nil
  3640. "List of properties (as strings) with a special meaning.
  3641. The default use of these custom properties is to let the user
  3642. hide them with `org-toggle-custom-properties-visibility'."
  3643. :group 'org-properties
  3644. :group 'org-appearance
  3645. :version "24.3"
  3646. :type '(repeat (string :tag "Property Name")))
  3647. (defcustom org-fontify-done-headline nil
  3648. "Non-nil means change the face of a headline if it is marked DONE.
  3649. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3650. When this is non-nil, the headline after the keyword is set to the
  3651. `org-headline-done' as an additional indication."
  3652. :group 'org-appearance
  3653. :type 'boolean)
  3654. (defcustom org-fontify-emphasized-text t
  3655. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3656. Changing this variable requires a restart of Emacs to take effect."
  3657. :group 'org-appearance
  3658. :type 'boolean)
  3659. (defcustom org-fontify-whole-heading-line nil
  3660. "Non-nil means fontify the whole line for headings.
  3661. This is useful when setting a background color for the
  3662. org-level-* faces."
  3663. :group 'org-appearance
  3664. :type 'boolean)
  3665. (defcustom org-highlight-latex-and-related nil
  3666. "Non-nil means highlight LaTeX related syntax in the buffer.
  3667. When non nil, the value should be a list containing any of the
  3668. following symbols:
  3669. `latex' Highlight LaTeX snippets and environments.
  3670. `script' Highlight subscript and superscript.
  3671. `entities' Highlight entities."
  3672. :group 'org-appearance
  3673. :version "24.4"
  3674. :package-version '(Org . "8.0")
  3675. :type '(choice
  3676. (const :tag "No highlighting" nil)
  3677. (set :greedy t :tag "Highlight"
  3678. (const :tag "LaTeX snippets and environments" latex)
  3679. (const :tag "Subscript and superscript" script)
  3680. (const :tag "Entities" entities))))
  3681. (defcustom org-hide-emphasis-markers nil
  3682. "Non-nil mean font-lock should hide the emphasis marker characters."
  3683. :group 'org-appearance
  3684. :type 'boolean)
  3685. (defcustom org-hide-macro-markers nil
  3686. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3687. :group 'org-appearance
  3688. :type 'boolean)
  3689. (defcustom org-pretty-entities nil
  3690. "Non-nil means show entities as UTF8 characters.
  3691. When nil, the \\name form remains in the buffer."
  3692. :group 'org-appearance
  3693. :version "24.1"
  3694. :type 'boolean)
  3695. (defcustom org-pretty-entities-include-sub-superscripts t
  3696. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3697. :group 'org-appearance
  3698. :version "24.1"
  3699. :type 'boolean)
  3700. (defvar org-emph-re nil
  3701. "Regular expression for matching emphasis.
  3702. After a match, the match groups contain these elements:
  3703. 0 The match of the full regular expression, including the characters
  3704. before and after the proper match
  3705. 1 The character before the proper match, or empty at beginning of line
  3706. 2 The proper match, including the leading and trailing markers
  3707. 3 The leading marker like * or /, indicating the type of highlighting
  3708. 4 The text between the emphasis markers, not including the markers
  3709. 5 The character after the match, empty at the end of a line")
  3710. (defvar org-verbatim-re nil
  3711. "Regular expression for matching verbatim text.")
  3712. (defvar org-emphasis-regexp-components) ; defined just below
  3713. (defvar org-emphasis-alist) ; defined just below
  3714. (defun org-set-emph-re (var val)
  3715. "Set variable and compute the emphasis regular expression."
  3716. (set var val)
  3717. (when (and (boundp 'org-emphasis-alist)
  3718. (boundp 'org-emphasis-regexp-components)
  3719. org-emphasis-alist org-emphasis-regexp-components)
  3720. (let* ((e org-emphasis-regexp-components)
  3721. (pre (car e))
  3722. (post (nth 1 e))
  3723. (border (nth 2 e))
  3724. (body (nth 3 e))
  3725. (nl (nth 4 e))
  3726. (body1 (concat body "*?"))
  3727. (markers (mapconcat 'car org-emphasis-alist ""))
  3728. (vmarkers (mapconcat
  3729. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3730. org-emphasis-alist "")))
  3731. ;; make sure special characters appear at the right position in the class
  3732. (if (string-match "\\^" markers)
  3733. (setq markers (concat (replace-match "" t t markers) "^")))
  3734. (if (string-match "-" markers)
  3735. (setq markers (concat (replace-match "" t t markers) "-")))
  3736. (if (string-match "\\^" vmarkers)
  3737. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3738. (if (string-match "-" vmarkers)
  3739. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3740. (if (> nl 0)
  3741. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3742. (int-to-string nl) "\\}")))
  3743. ;; Make the regexp
  3744. (setq org-emph-re
  3745. (concat "\\([" pre "]\\|^\\)"
  3746. "\\("
  3747. "\\([" markers "]\\)"
  3748. "\\("
  3749. "[^" border "]\\|"
  3750. "[^" border "]"
  3751. body1
  3752. "[^" border "]"
  3753. "\\)"
  3754. "\\3\\)"
  3755. "\\([" post "]\\|$\\)"))
  3756. (setq org-verbatim-re
  3757. (concat "\\([" pre "]\\|^\\)"
  3758. "\\("
  3759. "\\([" vmarkers "]\\)"
  3760. "\\("
  3761. "[^" border "]\\|"
  3762. "[^" border "]"
  3763. body1
  3764. "[^" border "]"
  3765. "\\)"
  3766. "\\3\\)"
  3767. "\\([" post "]\\|$\\)")))))
  3768. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3769. ;; set this option proved cumbersome. See this message/thread:
  3770. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3771. (defvar org-emphasis-regexp-components
  3772. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3773. "Components used to build the regular expression for emphasis.
  3774. This is a list with five entries. Terminology: In an emphasis string
  3775. like \" *strong word* \", we call the initial space PREMATCH, the final
  3776. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3777. and \"trong wor\" is the body. The different components in this variable
  3778. specify what is allowed/forbidden in each part:
  3779. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3780. post Chars allowed as postmatch. End of line will be allowed too.
  3781. border The chars *forbidden* as border characters.
  3782. body-regexp A regexp like \".\" to match a body character. Don't use
  3783. non-shy groups here, and don't allow newline here.
  3784. newline The maximum number of newlines allowed in an emphasis exp.
  3785. You need to reload Org or to restart Emacs after customizing this.")
  3786. (defcustom org-emphasis-alist
  3787. `(("*" bold)
  3788. ("/" italic)
  3789. ("_" underline)
  3790. ("=" org-verbatim verbatim)
  3791. ("~" org-code verbatim)
  3792. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3793. "Alist of characters and faces to emphasize text.
  3794. Text starting and ending with a special character will be emphasized,
  3795. for example *bold*, _underlined_ and /italic/. This variable sets the
  3796. marker characters and the face to be used by font-lock for highlighting
  3797. in Org-mode Emacs buffers.
  3798. You need to reload Org or to restart Emacs after customizing this."
  3799. :group 'org-appearance
  3800. :set 'org-set-emph-re
  3801. :version "24.4"
  3802. :package-version '(Org . "8.0")
  3803. :type '(repeat
  3804. (list
  3805. (string :tag "Marker character")
  3806. (choice
  3807. (face :tag "Font-lock-face")
  3808. (plist :tag "Face property list"))
  3809. (option (const verbatim)))))
  3810. (defvar org-protecting-blocks
  3811. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3812. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3813. This is needed for font-lock setup.")
  3814. ;;; Miscellaneous options
  3815. (defgroup org-completion nil
  3816. "Completion in Org-mode."
  3817. :tag "Org Completion"
  3818. :group 'org)
  3819. (defcustom org-completion-use-ido nil
  3820. "Non-nil means use ido completion wherever possible.
  3821. Note that `ido-mode' must be active for this variable to be relevant.
  3822. If you decide to turn this variable on, you might well want to turn off
  3823. `org-outline-path-complete-in-steps'.
  3824. See also `org-completion-use-iswitchb'."
  3825. :group 'org-completion
  3826. :type 'boolean)
  3827. (defcustom org-completion-use-iswitchb nil
  3828. "Non-nil means use iswitchb completion wherever possible.
  3829. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3830. If you decide to turn this variable on, you might well want to turn off
  3831. `org-outline-path-complete-in-steps'.
  3832. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3833. :group 'org-completion
  3834. :type 'boolean)
  3835. (defcustom org-completion-fallback-command 'hippie-expand
  3836. "The expansion command called by \\[pcomplete] in normal context.
  3837. Normal means, no org-mode-specific context."
  3838. :group 'org-completion
  3839. :type 'function)
  3840. ;;; Functions and variables from their packages
  3841. ;; Declared here to avoid compiler warnings
  3842. ;; XEmacs only
  3843. (defvar outline-mode-menu-heading)
  3844. (defvar outline-mode-menu-show)
  3845. (defvar outline-mode-menu-hide)
  3846. (defvar zmacs-regions) ; XEmacs regions
  3847. ;; Emacs only
  3848. (defvar mark-active)
  3849. ;; Various packages
  3850. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3851. (declare-function calendar-forward-day "cal-move" (arg))
  3852. (declare-function calendar-goto-date "cal-move" (date))
  3853. (declare-function calendar-goto-today "cal-move" ())
  3854. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3855. (defvar calc-embedded-close-formula)
  3856. (defvar calc-embedded-open-formula)
  3857. (declare-function cdlatex-tab "ext:cdlatex" ())
  3858. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3859. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3860. (defvar font-lock-unfontify-region-function)
  3861. (declare-function iswitchb-read-buffer "iswitchb"
  3862. (prompt &optional default require-match start matches-set))
  3863. (defvar iswitchb-temp-buflist)
  3864. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3865. (defvar org-agenda-tags-todo-honor-ignore-options)
  3866. (declare-function org-agenda-skip "org-agenda" ())
  3867. (declare-function
  3868. org-agenda-format-item "org-agenda"
  3869. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3870. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3871. (declare-function org-agenda-change-all-lines "org-agenda"
  3872. (newhead hdmarker &optional fixface just-this))
  3873. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3874. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3875. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3876. (beg end))
  3877. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3878. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3879. "org-agenda" (&optional end))
  3880. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3881. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3882. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3883. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3884. (declare-function org-indent-mode "org-indent" (&optional arg))
  3885. (declare-function parse-time-string "parse-time" (string))
  3886. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3887. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3888. (defvar remember-data-file)
  3889. (defvar texmathp-why)
  3890. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3891. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3892. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3893. ;;;###autoload
  3894. (defun turn-on-orgtbl ()
  3895. "Unconditionally turn on `orgtbl-mode'."
  3896. (require 'org-table)
  3897. (orgtbl-mode 1))
  3898. (defun org-at-table-p (&optional table-type)
  3899. "Return t if the cursor is inside an org-type table.
  3900. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3901. (if org-enable-table-editor
  3902. (save-excursion
  3903. (beginning-of-line 1)
  3904. (looking-at (if table-type org-table-any-line-regexp
  3905. org-table-line-regexp)))
  3906. nil))
  3907. (defsubst org-table-p () (org-at-table-p))
  3908. (defun org-at-table.el-p ()
  3909. "Return t if and only if we are at a table.el table."
  3910. (and (org-at-table-p 'any)
  3911. (save-excursion
  3912. (goto-char (org-table-begin 'any))
  3913. (looking-at org-table1-hline-regexp))))
  3914. (defun org-table-recognize-table.el ()
  3915. "If there is a table.el table nearby, recognize it and move into it."
  3916. (if org-table-tab-recognizes-table.el
  3917. (if (org-at-table.el-p)
  3918. (progn
  3919. (beginning-of-line 1)
  3920. (if (looking-at org-table-dataline-regexp)
  3921. nil
  3922. (if (looking-at org-table1-hline-regexp)
  3923. (progn
  3924. (beginning-of-line 2)
  3925. (if (looking-at org-table-any-border-regexp)
  3926. (beginning-of-line -1)))))
  3927. (if (re-search-forward "|" (org-table-end t) t)
  3928. (progn
  3929. (require 'table)
  3930. (if (table--at-cell-p (point))
  3931. t
  3932. (message "recognizing table.el table...")
  3933. (table-recognize-table)
  3934. (message "recognizing table.el table...done")))
  3935. (error "This should not happen"))
  3936. t)
  3937. nil)
  3938. nil))
  3939. (defun org-at-table-hline-p ()
  3940. "Return t if the cursor is inside a hline in a table."
  3941. (if org-enable-table-editor
  3942. (save-excursion
  3943. (beginning-of-line 1)
  3944. (looking-at org-table-hline-regexp))
  3945. nil))
  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%%" (/ (* 100.0 (point)) (buffer-size))))
  3955. (beginning-of-line 1)
  3956. (when (and (looking-at org-table-line-regexp)
  3957. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3958. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3959. (save-excursion (funcall function))
  3960. (or (looking-at org-table-line-regexp)
  3961. (forward-char 1)))
  3962. (re-search-forward org-table-any-border-regexp nil 1))))
  3963. (unless quietly (message "Mapping tables: done")))
  3964. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3965. (declare-function org-clock-update-mode-line "org-clock" ())
  3966. (declare-function org-resolve-clocks "org-clock"
  3967. (&optional also-non-dangling-p prompt last-valid))
  3968. (defun org-at-TBLFM-p (&optional pos)
  3969. "Return t when point (or POS) is in #+TBLFM line."
  3970. (save-excursion
  3971. (let ((pos pos)))
  3972. (goto-char (or pos (point)))
  3973. (beginning-of-line 1)
  3974. (looking-at org-TBLFM-regexp)))
  3975. (defvar org-clock-start-time)
  3976. (defvar org-clock-marker (make-marker)
  3977. "Marker recording the last clock-in.")
  3978. (defvar org-clock-hd-marker (make-marker)
  3979. "Marker recording the last clock-in, but the headline position.")
  3980. (defvar org-clock-heading ""
  3981. "The heading of the current clock entry.")
  3982. (defun org-clock-is-active ()
  3983. "Return the buffer where the clock is currently running.
  3984. Return nil if no clock is running."
  3985. (marker-buffer org-clock-marker))
  3986. (defun org-check-running-clock ()
  3987. "Check if the current buffer contains the running clock.
  3988. If yes, offer to stop it and to save the buffer with the changes."
  3989. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3990. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3991. (buffer-name))))
  3992. (org-clock-out)
  3993. (when (y-or-n-p "Save changed buffer?")
  3994. (save-buffer))))
  3995. (defun org-clocktable-try-shift (dir n)
  3996. "Check if this line starts a clock table, if yes, shift the time block."
  3997. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3998. (org-clocktable-shift dir n)))
  3999. ;;;###autoload
  4000. (defun org-clock-persistence-insinuate ()
  4001. "Set up hooks for clock persistence."
  4002. (require 'org-clock)
  4003. (add-hook 'org-mode-hook 'org-clock-load)
  4004. (add-hook 'kill-emacs-hook 'org-clock-save))
  4005. (defgroup org-archive nil
  4006. "Options concerning archiving in Org-mode."
  4007. :tag "Org Archive"
  4008. :group 'org-structure)
  4009. (defcustom org-archive-location "%s_archive::"
  4010. "The location where subtrees should be archived.
  4011. The value of this variable is a string, consisting of two parts,
  4012. separated by a double-colon. The first part is a filename and
  4013. the second part is a headline.
  4014. When the filename is omitted, archiving happens in the same file.
  4015. %s in the filename will be replaced by the current file
  4016. name (without the directory part). Archiving to a different file
  4017. is useful to keep archived entries from contributing to the
  4018. Org-mode Agenda.
  4019. The archived entries will be filed as subtrees of the specified
  4020. headline. When the headline is omitted, the subtrees are simply
  4021. filed away at the end of the file, as top-level entries. Also in
  4022. the heading you can use %s to represent the file name, this can be
  4023. useful when using the same archive for a number of different files.
  4024. Here are a few examples:
  4025. \"%s_archive::\"
  4026. If the current file is Projects.org, archive in file
  4027. Projects.org_archive, as top-level trees. This is the default.
  4028. \"::* Archived Tasks\"
  4029. Archive in the current file, under the top-level headline
  4030. \"* Archived Tasks\".
  4031. \"~/org/archive.org::\"
  4032. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4033. \"~/org/archive.org::* From %s\"
  4034. Archive in file ~/org/archive.org (absolute path), under headlines
  4035. \"From FILENAME\" where file name is the current file name.
  4036. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4037. The \"datetree/\" string is special, signifying to archive
  4038. items to the datetree. Items are placed in either the CLOSED
  4039. date of the item, or the current date if there is no CLOSED date.
  4040. The heading will be a subentry to the current date. There doesn't
  4041. need to be a heading, but there always needs to be a slash after
  4042. datetree. For example, to store archived items directly in the
  4043. datetree, use \"~/org/datetree.org::datetree/\".
  4044. \"basement::** Finished Tasks\"
  4045. Archive in file ./basement (relative path), as level 3 trees
  4046. below the level 2 heading \"** Finished Tasks\".
  4047. You may set this option on a per-file basis by adding to the buffer a
  4048. line like
  4049. #+ARCHIVE: basement::** Finished Tasks
  4050. You may also define it locally for a subtree by setting an ARCHIVE property
  4051. in the entry. If such a property is found in an entry, or anywhere up
  4052. the hierarchy, it will be used."
  4053. :group 'org-archive
  4054. :type 'string)
  4055. (defcustom org-agenda-skip-archived-trees t
  4056. "Non-nil means the agenda will skip any items located in archived trees.
  4057. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4058. variable is no longer recommended, you should leave it at the value t.
  4059. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4060. :group 'org-archive
  4061. :group 'org-agenda-skip
  4062. :type 'boolean)
  4063. (defcustom org-columns-skip-archived-trees t
  4064. "Non-nil means ignore archived trees when creating column view."
  4065. :group 'org-archive
  4066. :group 'org-properties
  4067. :type 'boolean)
  4068. (defcustom org-cycle-open-archived-trees nil
  4069. "Non-nil means `org-cycle' will open archived trees.
  4070. An archived tree is a tree marked with the tag ARCHIVE.
  4071. When nil, archived trees will stay folded. You can still open them with
  4072. normal outline commands like `show-all', but not with the cycling commands."
  4073. :group 'org-archive
  4074. :group 'org-cycle
  4075. :type 'boolean)
  4076. (defcustom org-sparse-tree-open-archived-trees nil
  4077. "Non-nil means sparse tree construction shows matches in archived trees.
  4078. When nil, matches in these trees are highlighted, but the trees are kept in
  4079. collapsed state."
  4080. :group 'org-archive
  4081. :group 'org-sparse-trees
  4082. :type 'boolean)
  4083. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  4084. "The default date type when building a sparse tree.
  4085. When this is nil, a date is a scheduled or a deadline timestamp.
  4086. Otherwise, these types are allowed:
  4087. all: all timestamps
  4088. active: only active timestamps (<...>)
  4089. inactive: only inactive timestamps (<...)
  4090. scheduled: only scheduled timestamps
  4091. deadline: only deadline timestamps"
  4092. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4093. (const :tag "All timestamps" all)
  4094. (const :tag "Only active timestamps" active)
  4095. (const :tag "Only inactive timestamps" inactive)
  4096. (const :tag "Only scheduled timestamps" scheduled)
  4097. (const :tag "Only deadline timestamps" deadline)
  4098. (const :tag "Only closed timestamps" closed))
  4099. :version "24.3"
  4100. :group 'org-sparse-trees)
  4101. (defun org-cycle-hide-archived-subtrees (state)
  4102. "Re-hide all archived subtrees after a visibility state change."
  4103. (when (and (not org-cycle-open-archived-trees)
  4104. (not (memq state '(overview folded))))
  4105. (save-excursion
  4106. (let* ((globalp (memq state '(contents all)))
  4107. (beg (if globalp (point-min) (point)))
  4108. (end (if globalp (point-max) (org-end-of-subtree t))))
  4109. (org-hide-archived-subtrees beg end)
  4110. (goto-char beg)
  4111. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4112. (message "%s" (substitute-command-keys
  4113. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4114. (defun org-force-cycle-archived ()
  4115. "Cycle subtree even if it is archived."
  4116. (interactive)
  4117. (setq this-command 'org-cycle)
  4118. (let ((org-cycle-open-archived-trees t))
  4119. (call-interactively 'org-cycle)))
  4120. (defun org-hide-archived-subtrees (beg end)
  4121. "Re-hide all archived subtrees after a visibility state change."
  4122. (save-excursion
  4123. (let* ((re (concat ":" org-archive-tag ":")))
  4124. (goto-char beg)
  4125. (while (re-search-forward re end t)
  4126. (when (org-at-heading-p)
  4127. (org-flag-subtree t)
  4128. (org-end-of-subtree t))))))
  4129. (declare-function outline-end-of-heading "outline" ())
  4130. (declare-function outline-flag-region "outline" (from to flag))
  4131. (defun org-flag-subtree (flag)
  4132. (save-excursion
  4133. (org-back-to-heading t)
  4134. (outline-end-of-heading)
  4135. (outline-flag-region (point)
  4136. (progn (org-end-of-subtree t) (point))
  4137. flag)))
  4138. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4139. ;; Declare Column View Code
  4140. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4141. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4142. (declare-function org-columns-compute "org-colview" (property))
  4143. ;; Declare ID code
  4144. (declare-function org-id-store-link "org-id")
  4145. (declare-function org-id-locations-load "org-id")
  4146. (declare-function org-id-locations-save "org-id")
  4147. (defvar org-id-track-globally)
  4148. ;;; Variables for pre-computed regular expressions, all buffer local
  4149. (defvar org-todo-regexp nil
  4150. "Matches any of the TODO state keywords.")
  4151. (make-variable-buffer-local 'org-todo-regexp)
  4152. (defvar org-not-done-regexp nil
  4153. "Matches any of the TODO state keywords except the last one.")
  4154. (make-variable-buffer-local 'org-not-done-regexp)
  4155. (defvar org-not-done-heading-regexp nil
  4156. "Matches a TODO headline that is not done.")
  4157. (make-variable-buffer-local 'org-not-done-regexp)
  4158. (defvar org-todo-line-regexp nil
  4159. "Matches a headline and puts TODO state into group 2 if present.")
  4160. (make-variable-buffer-local 'org-todo-line-regexp)
  4161. (defvar org-complex-heading-regexp nil
  4162. "Matches a headline and puts everything into groups:
  4163. group 1: the stars
  4164. group 2: The todo keyword, maybe
  4165. group 3: Priority cookie
  4166. group 4: True headline
  4167. group 5: Tags")
  4168. (make-variable-buffer-local 'org-complex-heading-regexp)
  4169. (defvar org-complex-heading-regexp-format nil
  4170. "Printf format to make regexp to match an exact headline.
  4171. This regexp will match the headline of any node which has the
  4172. exact headline text that is put into the format, but may have any
  4173. TODO state, priority and tags.")
  4174. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4175. (defvar org-todo-line-tags-regexp nil
  4176. "Matches a headline and puts TODO state into group 2 if present.
  4177. Also put tags into group 4 if tags are present.")
  4178. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4179. (defconst org-plain-time-of-day-regexp
  4180. (concat
  4181. "\\(\\<[012]?[0-9]"
  4182. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4183. "\\(--?"
  4184. "\\(\\<[012]?[0-9]"
  4185. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4186. "\\)?")
  4187. "Regular expression to match a plain time or time range.
  4188. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4189. groups carry important information:
  4190. 0 the full match
  4191. 1 the first time, range or not
  4192. 8 the second time, if it is a range.")
  4193. (defconst org-plain-time-extension-regexp
  4194. (concat
  4195. "\\(\\<[012]?[0-9]"
  4196. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4197. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4198. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4199. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4200. groups carry important information:
  4201. 0 the full match
  4202. 7 hours of duration
  4203. 9 minutes of duration")
  4204. (defconst org-stamp-time-of-day-regexp
  4205. (concat
  4206. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4207. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4208. "\\(--?"
  4209. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4210. "Regular expression to match a timestamp time or time range.
  4211. After a match, the following groups carry important information:
  4212. 0 the full match
  4213. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4214. 2 the first time, range or not
  4215. 4 the second time, if it is a range.")
  4216. (defconst org-startup-options
  4217. '(("fold" org-startup-folded t)
  4218. ("overview" org-startup-folded t)
  4219. ("nofold" org-startup-folded nil)
  4220. ("showall" org-startup-folded nil)
  4221. ("showeverything" org-startup-folded showeverything)
  4222. ("content" org-startup-folded content)
  4223. ("indent" org-startup-indented t)
  4224. ("noindent" org-startup-indented nil)
  4225. ("hidestars" org-hide-leading-stars t)
  4226. ("showstars" org-hide-leading-stars nil)
  4227. ("odd" org-odd-levels-only t)
  4228. ("oddeven" org-odd-levels-only nil)
  4229. ("align" org-startup-align-all-tables t)
  4230. ("noalign" org-startup-align-all-tables nil)
  4231. ("inlineimages" org-startup-with-inline-images t)
  4232. ("noinlineimages" org-startup-with-inline-images nil)
  4233. ("latexpreview" org-startup-with-latex-preview t)
  4234. ("nolatexpreview" org-startup-with-latex-preview nil)
  4235. ("customtime" org-display-custom-times t)
  4236. ("logdone" org-log-done time)
  4237. ("lognotedone" org-log-done note)
  4238. ("nologdone" org-log-done nil)
  4239. ("lognoteclock-out" org-log-note-clock-out t)
  4240. ("nolognoteclock-out" org-log-note-clock-out nil)
  4241. ("logrepeat" org-log-repeat state)
  4242. ("lognoterepeat" org-log-repeat note)
  4243. ("logdrawer" org-log-into-drawer t)
  4244. ("nologdrawer" org-log-into-drawer nil)
  4245. ("logstatesreversed" org-log-states-order-reversed t)
  4246. ("nologstatesreversed" org-log-states-order-reversed nil)
  4247. ("nologrepeat" org-log-repeat nil)
  4248. ("logreschedule" org-log-reschedule time)
  4249. ("lognotereschedule" org-log-reschedule note)
  4250. ("nologreschedule" org-log-reschedule nil)
  4251. ("logredeadline" org-log-redeadline time)
  4252. ("lognoteredeadline" org-log-redeadline note)
  4253. ("nologredeadline" org-log-redeadline nil)
  4254. ("logrefile" org-log-refile time)
  4255. ("lognoterefile" org-log-refile note)
  4256. ("nologrefile" org-log-refile nil)
  4257. ("fninline" org-footnote-define-inline t)
  4258. ("nofninline" org-footnote-define-inline nil)
  4259. ("fnlocal" org-footnote-section nil)
  4260. ("fnauto" org-footnote-auto-label t)
  4261. ("fnprompt" org-footnote-auto-label nil)
  4262. ("fnconfirm" org-footnote-auto-label confirm)
  4263. ("fnplain" org-footnote-auto-label plain)
  4264. ("fnadjust" org-footnote-auto-adjust t)
  4265. ("nofnadjust" org-footnote-auto-adjust nil)
  4266. ("constcgs" constants-unit-system cgs)
  4267. ("constSI" constants-unit-system SI)
  4268. ("noptag" org-tag-persistent-alist nil)
  4269. ("hideblocks" org-hide-block-startup t)
  4270. ("nohideblocks" org-hide-block-startup nil)
  4271. ("beamer" org-startup-with-beamer-mode t)
  4272. ("entitiespretty" org-pretty-entities t)
  4273. ("entitiesplain" org-pretty-entities nil))
  4274. "Variable associated with STARTUP options for org-mode.
  4275. Each element is a list of three items: the startup options (as written
  4276. in the #+STARTUP line), the corresponding variable, and the value to set
  4277. this variable to if the option is found. An optional forth element PUSH
  4278. means to push this value onto the list in the variable.")
  4279. (defcustom org-group-tags t
  4280. "When non-nil (the default), use group tags.
  4281. This can be turned on/off through `org-toggle-tags-groups'."
  4282. :group 'org-tags
  4283. :group 'org-startup
  4284. :type 'boolean)
  4285. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4286. (defun org-toggle-tags-groups ()
  4287. "Toggle support for group tags.
  4288. Support for group tags is controlled by the option
  4289. `org-group-tags', which is non-nil by default."
  4290. (interactive)
  4291. (setq org-group-tags (not org-group-tags))
  4292. (cond ((and (derived-mode-p 'org-agenda-mode)
  4293. org-group-tags)
  4294. (org-agenda-redo))
  4295. ((derived-mode-p 'org-mode)
  4296. (let ((org-inhibit-startup t)) (org-mode))))
  4297. (message "Groups tags support has been turned %s"
  4298. (if org-group-tags "on" "off")))
  4299. (defun org-set-regexps-and-options (&optional tags-only)
  4300. "Precompute regular expressions used in the current buffer.
  4301. When optional argument TAGS-ONLY is non-nil, only compute tags
  4302. related expressions."
  4303. (when (derived-mode-p 'org-mode)
  4304. (let ((alist (org--setup-collect-keywords
  4305. (org-make-options-regexp
  4306. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4307. (and (not tags-only)
  4308. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4309. "LINK" "PRIORITIES" "PROPERTY" "SEQ_TODO"
  4310. "STARTUP" "TODO" "TYP_TODO")))))))
  4311. (org--setup-process-tags
  4312. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4313. (unless tags-only
  4314. ;; File properties.
  4315. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4316. ;; Archive location.
  4317. (let ((archive (cdr (assq 'archive alist))))
  4318. (when archive (org-set-local 'org-archive-location archive)))
  4319. ;; Category.
  4320. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4321. (when cat
  4322. (org-set-local 'org-category (intern cat))
  4323. (org-set-local 'org-file-properties
  4324. (org--update-property-plist
  4325. "CATEGORY" cat org-file-properties))))
  4326. ;; Columns.
  4327. (let ((column (cdr (assq 'columns alist))))
  4328. (when column (org-set-local 'org-columns-default-format column)))
  4329. ;; Constants.
  4330. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4331. ;; Link abbreviations.
  4332. (let ((links (cdr (assq 'link alist))))
  4333. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4334. ;; Priorities.
  4335. (let ((priorities (cdr (assq 'priorities alist))))
  4336. (when priorities
  4337. (org-set-local 'org-highest-priority (nth 0 priorities))
  4338. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4339. (org-set-local 'org-default-priority (nth 2 priorities))))
  4340. ;; Startup options.
  4341. (let ((startup (cdr (assq 'startup alist))))
  4342. (dolist (option startup)
  4343. (let ((entry (assoc-string option org-startup-options t)))
  4344. (let ((var (nth 1 entry))
  4345. (val (nth 2 entry)))
  4346. (if (not (nth 3 entry)) (org-set-local var val)
  4347. (unless (listp (symbol-value var))
  4348. (org-set-local var nil))
  4349. (add-to-list var val))))))
  4350. ;; TODO keywords.
  4351. (org-set-local 'org-todo-kwd-alist nil)
  4352. (org-set-local 'org-todo-key-alist nil)
  4353. (org-set-local 'org-todo-key-trigger nil)
  4354. (org-set-local 'org-todo-keywords-1 nil)
  4355. (org-set-local 'org-done-keywords nil)
  4356. (org-set-local 'org-todo-heads nil)
  4357. (org-set-local 'org-todo-sets nil)
  4358. (org-set-local 'org-todo-log-states nil)
  4359. (let ((todo-sequences
  4360. (reverse
  4361. (or (cdr (assq 'todo alist))
  4362. (let ((d (default-value 'org-todo-keywords)))
  4363. (if (not (stringp (car d))) d
  4364. ;; XXX: Backward compatibility code.
  4365. (list (cons org-todo-interpretation d))))))))
  4366. (dolist (sequence todo-sequences)
  4367. (let* ((sequence (or (run-hook-with-args-until-success
  4368. 'org-todo-setup-filter-hook sequence)
  4369. sequence))
  4370. (sequence-type (car sequence))
  4371. (keywords (cdr sequence))
  4372. (sep (member "|" keywords))
  4373. names alist)
  4374. (dolist (k (remove "|" keywords))
  4375. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4376. k)
  4377. (error "Invalid TODO keyword %s" k))
  4378. (let ((name (match-string 1 k))
  4379. (key (match-string 2 k))
  4380. (log (org-extract-log-state-settings k)))
  4381. (push name names)
  4382. (push (cons name (and key (string-to-char key))) alist)
  4383. (when log (push log org-todo-log-states))))
  4384. (let* ((names (nreverse names))
  4385. (done (if sep (org-remove-keyword-keys (cdr sep))
  4386. (last names)))
  4387. (head (car names))
  4388. (tail (list sequence-type head (car done) (org-last done))))
  4389. (add-to-list 'org-todo-heads head 'append)
  4390. (push names org-todo-sets)
  4391. (setq org-done-keywords (append org-done-keywords done nil))
  4392. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4393. (setq org-todo-key-alist
  4394. (append org-todo-key-alist
  4395. (and alist
  4396. (append '((:startgroup))
  4397. (nreverse alist)
  4398. '((:endgroup))))))
  4399. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4400. (setq org-todo-sets (nreverse org-todo-sets)
  4401. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4402. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4403. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4404. ;; Compute the regular expressions and other local variables.
  4405. ;; Using `org-outline-regexp-bol' would complicate them much,
  4406. ;; because of the fixed white space at the end of that string.
  4407. (if (not org-done-keywords)
  4408. (setq org-done-keywords
  4409. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4410. (setq org-not-done-keywords
  4411. (org-delete-all org-done-keywords
  4412. (copy-sequence org-todo-keywords-1))
  4413. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4414. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4415. org-not-done-heading-regexp
  4416. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4417. org-todo-line-regexp
  4418. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4419. org-complex-heading-regexp
  4420. (concat "^\\(\\*+\\)"
  4421. "\\(?: +" org-todo-regexp "\\)?"
  4422. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4423. "\\(?: +\\(.*?\\)\\)??"
  4424. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4425. "[ \t]*$")
  4426. org-complex-heading-regexp-format
  4427. (concat "^\\(\\*+\\)"
  4428. "\\(?: +" org-todo-regexp "\\)?"
  4429. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4430. "\\(?: +"
  4431. ;; Stats cookies can be stuck to body.
  4432. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4433. "\\(%s\\)"
  4434. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4435. "\\)"
  4436. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4437. "[ \t]*$")
  4438. org-todo-line-tags-regexp
  4439. (concat "^\\(\\*+\\)"
  4440. "\\(?: +" org-todo-regexp "\\)?"
  4441. "\\(?: +\\(.*?\\)\\)??"
  4442. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4443. "[ \t]*$"))
  4444. (org-compute-latex-and-related-regexp)))))
  4445. (defun org--setup-collect-keywords (regexp &optional files alist)
  4446. "Return setup keywords values as an alist.
  4447. REGEXP matches a subset of setup keywords. FILES is a list of
  4448. file names already visited. It is used to avoid circular setup
  4449. files. ALIST, when non-nil, is the alist computed so far.
  4450. Return value contains the following keys: `archive', `category',
  4451. `columns', `constants', `filetags', `link', `priorities',
  4452. `property', `startup', `tags' and `todo'."
  4453. (org-with-wide-buffer
  4454. (goto-char (point-min))
  4455. (let ((case-fold-search t))
  4456. (while (re-search-forward regexp nil t)
  4457. (let ((element (org-element-at-point)))
  4458. (when (eq (org-element-type element) 'keyword)
  4459. (let ((key (org-element-property :key element))
  4460. (value (org-element-property :value element)))
  4461. (cond
  4462. ((equal key "ARCHIVE")
  4463. (when (org-string-nw-p value)
  4464. (push (cons 'archive value) alist)))
  4465. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4466. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4467. ((equal key "CONSTANTS")
  4468. (let* ((constants (assq 'constants alist))
  4469. (store (cdr constants)))
  4470. (dolist (pair (org-split-string value))
  4471. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4472. pair)
  4473. (let* ((name (match-string 1 pair))
  4474. (value (match-string 2 pair))
  4475. (old (assoc name store)))
  4476. (if old (setcdr old value)
  4477. (push (cons name value) store)))))
  4478. (if constants (setcdr constants store)
  4479. (push (cons 'constants store) alist))))
  4480. ((equal key "FILETAGS")
  4481. (when (org-string-nw-p value)
  4482. (let ((old (assq 'filetags alist))
  4483. (new (apply #'nconc
  4484. (mapcar (lambda (x) (org-split-string x ":"))
  4485. (org-split-string value)))))
  4486. (if old (setcdr old (nconc new (cdr old)))
  4487. (push (cons 'filetags new) alist)))))
  4488. ((equal key "LINK")
  4489. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4490. (let ((links (assq 'link alist))
  4491. (pair (cons (org-match-string-no-properties 1 value)
  4492. (org-match-string-no-properties 2 value))))
  4493. (if links (push pair (cdr links))
  4494. (push (list 'link pair) alist)))))
  4495. ((equal key "PRIORITIES")
  4496. (push (cons 'priorities
  4497. (let ((prio (org-split-string value)))
  4498. (if (< (length prio) 3) '(?A ?C ?B)
  4499. (mapcar #'string-to-char prio))))
  4500. alist))
  4501. ((equal key "PROPERTY")
  4502. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4503. (let* ((property (assq 'property alist))
  4504. (value (org--update-property-plist
  4505. (org-match-string-no-properties 1 value)
  4506. (org-match-string-no-properties 2 value)
  4507. (cdr property))))
  4508. (if property (setcdr property value)
  4509. (push (cons 'property value) alist)))))
  4510. ((equal key "STARTUP")
  4511. (let ((startup (assq 'startup alist)))
  4512. (if startup
  4513. (setcdr startup
  4514. (nconc (cdr startup) (org-split-string value)))
  4515. (push (cons 'startup (org-split-string value)) alist))))
  4516. ((equal key "TAGS")
  4517. (let ((tag-cell (assq 'tags alist)))
  4518. (if tag-cell
  4519. (setcdr tag-cell
  4520. (nconc (cdr tag-cell)
  4521. '("\\n")
  4522. (org-split-string value)))
  4523. (push (cons 'tags (org-split-string value)) alist))))
  4524. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4525. (let ((todo (assq 'todo alist))
  4526. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4527. (org-split-string value))))
  4528. (if todo (push value (cdr todo))
  4529. (push (list 'todo value) alist))))
  4530. ((equal key "SETUPFILE")
  4531. (unless buffer-read-only ; Do not check in Gnus messages.
  4532. (let ((f (expand-file-name (org-remove-double-quotes value))))
  4533. (when (and (org-string-nw-p f)
  4534. (file-readable-p f)
  4535. (not (member f files)))
  4536. (with-temp-buffer
  4537. (let ((org-inhibit-startup t)) (org-mode))
  4538. (insert-file-contents f)
  4539. (setq alist
  4540. (org--setup-collect-keywords
  4541. regexp alist (cons f files)))))))))))))))
  4542. alist)
  4543. (defun org--setup-process-tags (tags filetags)
  4544. "Precompute variables used for tags.
  4545. TAGS is a list of tags and tag group symbols, as strings.
  4546. FILETAGS is a list of tags, as strings."
  4547. ;; Process the file tags.
  4548. (org-set-local 'org-file-tags
  4549. (mapcar #'org-add-prop-inherited filetags))
  4550. ;; Provide default tags if no local tags are found.
  4551. (when (and (not tags) org-tag-alist)
  4552. (setq tags
  4553. (mapcar (lambda (tag)
  4554. (case (car tag)
  4555. (:startgroup "{")
  4556. (:endgroup "}")
  4557. (:grouptags ":")
  4558. (:newline "\\n")
  4559. (otherwise (concat (car tag)
  4560. (and (characterp (cdr tag))
  4561. (format "(%c)" (cdr tag)))))))
  4562. org-tag-alist)))
  4563. ;; Process the tags.
  4564. (org-set-local 'org-tag-groups-alist nil)
  4565. (org-set-local 'org-tag-alist nil)
  4566. (let (group-flag)
  4567. (dolist (e tags)
  4568. (cond
  4569. ((equal e "{")
  4570. (push '(:startgroup) org-tag-alist)
  4571. (setq group-flag t))
  4572. ((equal e "}")
  4573. (push '(:endgroup) org-tag-alist)
  4574. (setq group-flag nil))
  4575. ((equal e ":")
  4576. (push '(:grouptags) org-tag-alist)
  4577. (setq group-flag 'append))
  4578. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4579. ((string-match (org-re "\\`\\([[:alnum:]_@#%]+\\)\\(?:(\\(.\\))\\)?\\'")
  4580. e)
  4581. (let ((tag (match-string 1 e))
  4582. (key (and (match-beginning 2)
  4583. (string-to-char (match-string 2 e)))))
  4584. (cond ((eq group-flag 'append)
  4585. (setcar org-tag-groups-alist
  4586. (append (car org-tag-groups-alist) (list tag))))
  4587. (group-flag (push (list tag) org-tag-groups-alist)))
  4588. (unless (assoc tag org-tag-alist)
  4589. (push (cons tag key) org-tag-alist)))))))
  4590. (setq org-tag-alist (nreverse org-tag-alist)))
  4591. (defun org-file-contents (file &optional noerror)
  4592. "Return the contents of FILE, as a string."
  4593. (if (and file (file-readable-p file))
  4594. (with-temp-buffer
  4595. (insert-file-contents file)
  4596. (buffer-string))
  4597. (funcall (if noerror 'message 'error)
  4598. "Cannot read file \"%s\"%s"
  4599. file
  4600. (let ((from (buffer-file-name (buffer-base-buffer))))
  4601. (if from (concat " (referenced in file \"" from "\")") "")))))
  4602. (defun org-extract-log-state-settings (x)
  4603. "Extract the log state setting from a TODO keyword string.
  4604. This will extract info from a string like \"WAIT(w@/!)\"."
  4605. (let (kw key log1 log2)
  4606. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4607. (setq kw (match-string 1 x)
  4608. key (and (match-end 2) (match-string 2 x))
  4609. log1 (and (match-end 3) (match-string 3 x))
  4610. log2 (and (match-end 4) (match-string 4 x)))
  4611. (and (or log1 log2)
  4612. (list kw
  4613. (and log1 (if (equal log1 "!") 'time 'note))
  4614. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4615. (defun org-remove-keyword-keys (list)
  4616. "Remove a pair of parenthesis at the end of each string in LIST."
  4617. (mapcar (lambda (x)
  4618. (if (string-match "(.*)$" x)
  4619. (substring x 0 (match-beginning 0))
  4620. x))
  4621. list))
  4622. (defun org-assign-fast-keys (alist)
  4623. "Assign fast keys to a keyword-key alist.
  4624. Respect keys that are already there."
  4625. (let (new e (alt ?0))
  4626. (while (setq e (pop alist))
  4627. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4628. (cdr e)) ;; Key already assigned.
  4629. (push e new)
  4630. (let ((clist (string-to-list (downcase (car e))))
  4631. (used (append new alist)))
  4632. (when (= (car clist) ?@)
  4633. (pop clist))
  4634. (while (and clist (rassoc (car clist) used))
  4635. (pop clist))
  4636. (unless clist
  4637. (while (rassoc alt used)
  4638. (incf alt)))
  4639. (push (cons (car e) (or (car clist) alt)) new))))
  4640. (nreverse new)))
  4641. ;;; Some variables used in various places
  4642. (defvar org-window-configuration nil
  4643. "Used in various places to store a window configuration.")
  4644. (defvar org-selected-window nil
  4645. "Used in various places to store a window configuration.")
  4646. (defvar org-finish-function nil
  4647. "Function to be called when `C-c C-c' is used.
  4648. This is for getting out of special buffers like capture.")
  4649. ;; FIXME: Occasionally check by commenting these, to make sure
  4650. ;; no other functions uses these, forgetting to let-bind them.
  4651. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4652. (defvar org-last-state)
  4653. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4654. ;; Defined somewhere in this file, but used before definition.
  4655. (defvar org-entities) ;; defined in org-entities.el
  4656. (defvar org-struct-menu)
  4657. (defvar org-org-menu)
  4658. (defvar org-tbl-menu)
  4659. ;;;; Define the Org-mode
  4660. ;; We use a before-change function to check if a table might need
  4661. ;; an update.
  4662. (defvar org-table-may-need-update t
  4663. "Indicates that a table might need an update.
  4664. This variable is set by `org-before-change-function'.
  4665. `org-table-align' sets it back to nil.")
  4666. (defun org-before-change-function (beg end)
  4667. "Every change indicates that a table might need an update."
  4668. (setq org-table-may-need-update t))
  4669. (defvar org-mode-map)
  4670. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4671. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4672. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4673. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4674. (defvar org-table-buffer-is-an nil)
  4675. (defvar bidi-paragraph-direction)
  4676. (defvar buffer-face-mode-face)
  4677. (require 'outline)
  4678. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4679. (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"))
  4680. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4681. ;; Other stuff we need.
  4682. (require 'time-date)
  4683. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4684. (require 'easymenu)
  4685. (require 'overlay)
  4686. ;; (require 'org-macs) moved higher up in the file before it is first used
  4687. (require 'org-entities)
  4688. ;; (require 'org-compat) moved higher up in the file before it is first used
  4689. (require 'org-faces)
  4690. (require 'org-list)
  4691. (require 'org-pcomplete)
  4692. (require 'org-src)
  4693. (require 'org-footnote)
  4694. (require 'org-macro)
  4695. ;; babel
  4696. (require 'ob)
  4697. ;;;###autoload
  4698. (define-derived-mode org-mode outline-mode "Org"
  4699. "Outline-based notes management and organizer, alias
  4700. \"Carsten's outline-mode for keeping track of everything.\"
  4701. Org-mode develops organizational tasks around a NOTES file which
  4702. contains information about projects as plain text. Org-mode is
  4703. implemented on top of outline-mode, which is ideal to keep the content
  4704. of large files well structured. It supports ToDo items, deadlines and
  4705. time stamps, which magically appear in the diary listing of the Emacs
  4706. calendar. Tables are easily created with a built-in table editor.
  4707. Plain text URL-like links connect to websites, emails (VM), Usenet
  4708. messages (Gnus), BBDB entries, and any files related to the project.
  4709. For printing and sharing of notes, an Org-mode file (or a part of it)
  4710. can be exported as a structured ASCII or HTML file.
  4711. The following commands are available:
  4712. \\{org-mode-map}"
  4713. ;; Get rid of Outline menus, they are not needed
  4714. ;; Need to do this here because define-derived-mode sets up
  4715. ;; the keymap so late. Still, it is a waste to call this each time
  4716. ;; we switch another buffer into org-mode.
  4717. (if (featurep 'xemacs)
  4718. (when (boundp 'outline-mode-menu-heading)
  4719. ;; Assume this is Greg's port, it uses easymenu
  4720. (easy-menu-remove outline-mode-menu-heading)
  4721. (easy-menu-remove outline-mode-menu-show)
  4722. (easy-menu-remove outline-mode-menu-hide))
  4723. (define-key org-mode-map [menu-bar headings] 'undefined)
  4724. (define-key org-mode-map [menu-bar hide] 'undefined)
  4725. (define-key org-mode-map [menu-bar show] 'undefined))
  4726. (org-load-modules-maybe)
  4727. (easy-menu-add org-org-menu)
  4728. (easy-menu-add org-tbl-menu)
  4729. (org-install-agenda-files-menu)
  4730. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4731. (add-to-invisibility-spec '(org-cwidth))
  4732. (add-to-invisibility-spec '(org-hide-block . t))
  4733. (when (featurep 'xemacs)
  4734. (org-set-local 'line-move-ignore-invisible t))
  4735. (org-set-local 'outline-regexp org-outline-regexp)
  4736. (org-set-local 'outline-level 'org-outline-level)
  4737. (setq bidi-paragraph-direction 'left-to-right)
  4738. (when (and org-ellipsis
  4739. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4740. (fboundp 'make-glyph-code))
  4741. (unless org-display-table
  4742. (setq org-display-table (make-display-table)))
  4743. (set-display-table-slot
  4744. org-display-table 4
  4745. (vconcat (mapcar
  4746. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4747. org-ellipsis)))
  4748. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4749. (setq buffer-display-table org-display-table))
  4750. (org-set-regexps-and-options)
  4751. (org-set-font-lock-defaults)
  4752. (when (and org-tag-faces (not org-tags-special-faces-re))
  4753. ;; tag faces set outside customize.... force initialization.
  4754. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4755. ;; Calc embedded
  4756. (org-set-local 'calc-embedded-open-mode "# ")
  4757. ;; Modify a few syntax entries
  4758. (modify-syntax-entry ?@ "w")
  4759. (modify-syntax-entry ?\" "\"")
  4760. (modify-syntax-entry ?\\ "_")
  4761. (modify-syntax-entry ?~ "_")
  4762. (if org-startup-truncated (setq truncate-lines t))
  4763. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4764. (org-set-local 'font-lock-unfontify-region-function
  4765. 'org-unfontify-region)
  4766. ;; Activate before-change-function
  4767. (org-set-local 'org-table-may-need-update t)
  4768. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4769. 'local)
  4770. ;; Check for running clock before killing a buffer
  4771. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4772. ;; Initialize macros templates.
  4773. (org-macro-initialize-templates)
  4774. ;; Initialize radio targets.
  4775. (org-update-radio-target-regexp)
  4776. ;; Indentation.
  4777. (org-set-local 'indent-line-function 'org-indent-line)
  4778. (org-set-local 'indent-region-function 'org-indent-region)
  4779. ;; Filling and auto-filling.
  4780. (org-setup-filling)
  4781. ;; Comments.
  4782. (org-setup-comments-handling)
  4783. ;; Initialize cache.
  4784. (org-element-cache-reset)
  4785. ;; Beginning/end of defun
  4786. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4787. (org-set-local 'end-of-defun-function
  4788. (lambda ()
  4789. (if (not (org-at-heading-p))
  4790. (org-forward-element)
  4791. (org-forward-element)
  4792. (forward-char -1))))
  4793. ;; Next error for sparse trees
  4794. (org-set-local 'next-error-function 'org-occur-next-match)
  4795. ;; Make sure dependence stuff works reliably, even for users who set it
  4796. ;; too late :-(
  4797. (if org-enforce-todo-dependencies
  4798. (add-hook 'org-blocker-hook
  4799. 'org-block-todo-from-children-or-siblings-or-parent)
  4800. (remove-hook 'org-blocker-hook
  4801. 'org-block-todo-from-children-or-siblings-or-parent))
  4802. (if org-enforce-todo-checkbox-dependencies
  4803. (add-hook 'org-blocker-hook
  4804. 'org-block-todo-from-checkboxes)
  4805. (remove-hook 'org-blocker-hook
  4806. 'org-block-todo-from-checkboxes))
  4807. ;; Align options lines
  4808. (org-set-local
  4809. 'align-mode-rules-list
  4810. '((org-in-buffer-settings
  4811. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4812. (modes . '(org-mode)))))
  4813. ;; Imenu
  4814. (org-set-local 'imenu-create-index-function
  4815. 'org-imenu-get-tree)
  4816. ;; Make isearch reveal context
  4817. (if (or (featurep 'xemacs)
  4818. (not (boundp 'outline-isearch-open-invisible-function)))
  4819. ;; Emacs 21 and XEmacs make use of the hook
  4820. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4821. ;; Emacs 22 deals with this through a special variable
  4822. (org-set-local 'outline-isearch-open-invisible-function
  4823. (lambda (&rest ignore) (org-show-context 'isearch))))
  4824. ;; Setup the pcomplete hooks
  4825. (set (make-local-variable 'pcomplete-command-completion-function)
  4826. 'org-pcomplete-initial)
  4827. (set (make-local-variable 'pcomplete-command-name-function)
  4828. 'org-command-at-point)
  4829. (set (make-local-variable 'pcomplete-default-completion-function)
  4830. 'ignore)
  4831. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4832. 'org-parse-arguments)
  4833. (set (make-local-variable 'pcomplete-termination-string) "")
  4834. (when (>= emacs-major-version 23)
  4835. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4836. ;; If empty file that did not turn on org-mode automatically, make it to.
  4837. (if (and org-insert-mode-line-in-empty-file
  4838. (org-called-interactively-p 'any)
  4839. (= (point-min) (point-max)))
  4840. (insert "# -*- mode: org -*-\n\n"))
  4841. (unless org-inhibit-startup
  4842. (org-unmodified
  4843. (and org-startup-with-beamer-mode (org-beamer-mode))
  4844. (when org-startup-align-all-tables
  4845. (org-table-map-tables 'org-table-align 'quietly))
  4846. (when org-startup-with-inline-images
  4847. (org-display-inline-images))
  4848. (when org-startup-with-latex-preview
  4849. (org-toggle-latex-fragment))
  4850. (unless org-inhibit-startup-visibility-stuff
  4851. (org-set-startup-visibility))
  4852. (org-refresh-effort-properties)))
  4853. ;; Try to set org-hide correctly
  4854. (let ((foreground (org-find-invisible-foreground)))
  4855. (if foreground
  4856. (set-face-foreground 'org-hide foreground))))
  4857. ;; Update `customize-package-emacs-version-alist'
  4858. (add-to-list 'customize-package-emacs-version-alist
  4859. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4860. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4861. ("8.2.6" . "24.4")))
  4862. (defvar org-mode-transpose-word-syntax-table
  4863. (let ((st (make-syntax-table)))
  4864. (mapc (lambda(c) (modify-syntax-entry
  4865. (string-to-char (car c)) "w p" st))
  4866. org-emphasis-alist)
  4867. st))
  4868. (when (fboundp 'abbrev-table-put)
  4869. (abbrev-table-put org-mode-abbrev-table
  4870. :parents (list text-mode-abbrev-table)))
  4871. (defun org-find-invisible-foreground ()
  4872. (let ((candidates (remove
  4873. "unspecified-bg"
  4874. (nconc
  4875. (list (face-background 'default)
  4876. (face-background 'org-default))
  4877. (mapcar
  4878. (lambda (alist)
  4879. (when (boundp alist)
  4880. (cdr (assoc 'background-color (symbol-value alist)))))
  4881. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4882. (list (face-foreground 'org-hide))))))
  4883. (car (remove nil candidates))))
  4884. (defun org-current-time (&optional rounding-minutes past)
  4885. "Current time, possibly rounded to ROUNDING-MINUTES.
  4886. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4887. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4888. the rounding returns a past time."
  4889. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4890. (car org-time-stamp-rounding-minutes)))
  4891. (time (decode-time)) res)
  4892. (if (< r 1)
  4893. (current-time)
  4894. (setq res
  4895. (apply 'encode-time
  4896. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4897. (nthcdr 2 time))))
  4898. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4899. (seconds-to-time (- (org-float-time res) (* r 60)))
  4900. res))))
  4901. (defun org-today ()
  4902. "Return today date, considering `org-extend-today-until'."
  4903. (time-to-days
  4904. (time-subtract (current-time)
  4905. (list 0 (* 3600 org-extend-today-until) 0))))
  4906. ;;;; Font-Lock stuff, including the activators
  4907. (defvar org-mouse-map (make-sparse-keymap))
  4908. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4909. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4910. (when org-mouse-1-follows-link
  4911. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4912. (when org-tab-follows-link
  4913. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4914. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4915. (require 'font-lock)
  4916. (defconst org-non-link-chars "]\t\n\r<>")
  4917. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4918. "file+sys" "news" "shell" "elisp" "doi" "message"
  4919. "help"))
  4920. (defvar org-link-types-re nil
  4921. "Matches a link that has a url-like prefix like \"http:\"")
  4922. (defvar org-link-re-with-space nil
  4923. "Matches a link with spaces, optional angular brackets around it.")
  4924. (defvar org-link-re-with-space2 nil
  4925. "Matches a link with spaces, optional angular brackets around it.")
  4926. (defvar org-link-re-with-space3 nil
  4927. "Matches a link with spaces, only for internal part in bracket links.")
  4928. (defvar org-angle-link-re nil
  4929. "Matches link with angular brackets, spaces are allowed.")
  4930. (defvar org-plain-link-re nil
  4931. "Matches plain link, without spaces.")
  4932. (defvar org-bracket-link-regexp nil
  4933. "Matches a link in double brackets.")
  4934. (defvar org-bracket-link-analytic-regexp nil
  4935. "Regular expression used to analyze links.
  4936. Here is what the match groups contain after a match:
  4937. 1: http:
  4938. 2: http
  4939. 3: path
  4940. 4: [desc]
  4941. 5: desc")
  4942. (defvar org-bracket-link-analytic-regexp++ nil
  4943. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4944. (defvar org-any-link-re nil
  4945. "Regular expression matching any link.")
  4946. (defconst org-match-sexp-depth 3
  4947. "Number of stacked braces for sub/superscript matching.")
  4948. (defun org-create-multibrace-regexp (left right n)
  4949. "Create a regular expression which will match a balanced sexp.
  4950. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4951. as single character strings.
  4952. The regexp returned will match the entire expression including the
  4953. delimiters. It will also define a single group which contains the
  4954. match except for the outermost delimiters. The maximum depth of
  4955. stacked delimiters is N. Escaping delimiters is not possible."
  4956. (let* ((nothing (concat "[^" left right "]*?"))
  4957. (or "\\|")
  4958. (re nothing)
  4959. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4960. (while (> n 1)
  4961. (setq n (1- n)
  4962. re (concat re or next)
  4963. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4964. (concat left "\\(" re "\\)" right)))
  4965. (defconst org-match-substring-regexp
  4966. (concat
  4967. "\\(\\S-\\)\\([_^]\\)\\("
  4968. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4969. "\\|"
  4970. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4971. "\\|"
  4972. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4973. "The regular expression matching a sub- or superscript.")
  4974. (defconst org-match-substring-with-braces-regexp
  4975. (concat
  4976. "\\(\\S-\\)\\([_^]\\)"
  4977. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4978. "The regular expression matching a sub- or superscript, forcing braces.")
  4979. (defun org-make-link-regexps ()
  4980. "Update the link regular expressions.
  4981. This should be called after the variable `org-link-types' has changed."
  4982. (let ((types-re (regexp-opt org-link-types t)))
  4983. (setq org-link-types-re
  4984. (concat "\\`" types-re ":")
  4985. org-link-re-with-space
  4986. (concat "<?" types-re ":"
  4987. "\\([^" org-non-link-chars " ]"
  4988. "[^" org-non-link-chars "]*"
  4989. "[^" org-non-link-chars " ]\\)>?")
  4990. org-link-re-with-space2
  4991. (concat "<?" types-re ":"
  4992. "\\([^" org-non-link-chars " ]"
  4993. "[^\t\n\r]*"
  4994. "[^" org-non-link-chars " ]\\)>?")
  4995. org-link-re-with-space3
  4996. (concat "<?" types-re ":"
  4997. "\\([^" org-non-link-chars " ]"
  4998. "[^\t\n\r]*\\)")
  4999. org-angle-link-re
  5000. (concat "<" types-re ":"
  5001. "\\([^" org-non-link-chars " ]"
  5002. "[^" org-non-link-chars "]*"
  5003. "\\)>")
  5004. org-plain-link-re
  5005. (concat
  5006. "\\<" types-re ":"
  5007. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5008. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5009. org-bracket-link-regexp
  5010. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5011. org-bracket-link-analytic-regexp
  5012. (concat
  5013. "\\[\\["
  5014. "\\(" types-re ":\\)?"
  5015. "\\([^]]+\\)"
  5016. "\\]"
  5017. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5018. "\\]")
  5019. org-bracket-link-analytic-regexp++
  5020. (concat
  5021. "\\[\\["
  5022. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5023. "\\([^]]+\\)"
  5024. "\\]"
  5025. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5026. "\\]")
  5027. org-any-link-re
  5028. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5029. org-angle-link-re "\\)\\|\\("
  5030. org-plain-link-re "\\)"))))
  5031. (org-make-link-regexps)
  5032. (defvar org-emph-face nil)
  5033. (defun org-do-emphasis-faces (limit)
  5034. "Run through the buffer and emphasize strings."
  5035. (let (rtn a)
  5036. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5037. (let* ((border (char-after (match-beginning 3)))
  5038. (bre (regexp-quote (char-to-string border))))
  5039. (if (and (not (= border (char-after (match-beginning 4))))
  5040. (not (save-match-data
  5041. (string-match (concat bre ".*" bre)
  5042. (replace-regexp-in-string
  5043. "\n" " "
  5044. (substring (match-string 2) 1 -1))))))
  5045. (progn
  5046. (setq rtn t)
  5047. (setq a (assoc (match-string 3) org-emphasis-alist))
  5048. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5049. 'face
  5050. (nth 1 a))
  5051. (and (nth 2 a)
  5052. (org-remove-flyspell-overlays-in
  5053. (match-beginning 0) (match-end 0)))
  5054. (add-text-properties (match-beginning 2) (match-end 2)
  5055. '(font-lock-multiline t org-emphasis t))
  5056. (when org-hide-emphasis-markers
  5057. (add-text-properties (match-end 4) (match-beginning 5)
  5058. '(invisible org-link))
  5059. (add-text-properties (match-beginning 3) (match-end 3)
  5060. '(invisible org-link))))))
  5061. (goto-char (1+ (match-beginning 0))))
  5062. rtn))
  5063. (defun org-emphasize (&optional char)
  5064. "Insert or change an emphasis, i.e. a font like bold or italic.
  5065. If there is an active region, change that region to a new emphasis.
  5066. If there is no region, just insert the marker characters and position
  5067. the cursor between them.
  5068. CHAR should be the marker character. If it is a space, it means to
  5069. remove the emphasis of the selected region.
  5070. If CHAR is not given (for example in an interactive call) it will be
  5071. prompted for."
  5072. (interactive)
  5073. (let ((erc org-emphasis-regexp-components)
  5074. (prompt "")
  5075. (string "") beg end move c s)
  5076. (if (org-region-active-p)
  5077. (setq beg (region-beginning) end (region-end)
  5078. string (buffer-substring beg end))
  5079. (setq move t))
  5080. (unless char
  5081. (message "Emphasis marker or tag: [%s]"
  5082. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5083. (setq char (read-char-exclusive)))
  5084. (if (equal char ?\ )
  5085. (setq s "" move nil)
  5086. (unless (assoc (char-to-string char) org-emphasis-alist)
  5087. (user-error "No such emphasis marker: \"%c\"" char))
  5088. (setq s (char-to-string char)))
  5089. (while (and (> (length string) 1)
  5090. (equal (substring string 0 1) (substring string -1))
  5091. (assoc (substring string 0 1) org-emphasis-alist))
  5092. (setq string (substring string 1 -1)))
  5093. (setq string (concat s string s))
  5094. (if beg (delete-region beg end))
  5095. (unless (or (bolp)
  5096. (string-match (concat "[" (nth 0 erc) "\n]")
  5097. (char-to-string (char-before (point)))))
  5098. (insert " "))
  5099. (unless (or (eobp)
  5100. (string-match (concat "[" (nth 1 erc) "\n]")
  5101. (char-to-string (char-after (point)))))
  5102. (insert " ") (backward-char 1))
  5103. (insert string)
  5104. (and move (backward-char 1))))
  5105. (defconst org-nonsticky-props
  5106. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5107. (defsubst org-rear-nonsticky-at (pos)
  5108. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5109. (defun org-activate-plain-links (limit)
  5110. "Add link properties for plain links."
  5111. (let (f hl)
  5112. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5113. (not (member 'org-tag
  5114. (get-text-property (1- (match-beginning 0)) 'face)))
  5115. (not (org-in-src-block-p)))
  5116. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5117. (setq f (get-text-property (match-beginning 0) 'face))
  5118. (setq hl (org-match-string-no-properties 0))
  5119. (if (or (eq f 'org-tag)
  5120. (and (listp f) (memq 'org-tag f)))
  5121. nil
  5122. (add-text-properties (match-beginning 0) (match-end 0)
  5123. (list 'mouse-face 'highlight
  5124. 'face 'org-link
  5125. 'htmlize-link `(:uri ,hl)
  5126. 'keymap org-mouse-map))
  5127. (org-rear-nonsticky-at (match-end 0)))
  5128. t)))
  5129. (defun org-activate-code (limit)
  5130. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5131. (progn
  5132. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5133. (remove-text-properties (match-beginning 0) (match-end 0)
  5134. '(display t invisible t intangible t))
  5135. t)))
  5136. (defcustom org-src-fontify-natively t
  5137. "When non-nil, fontify code in code blocks."
  5138. :type 'boolean
  5139. :version "24.4"
  5140. :package-version '(Org . "8.3")
  5141. :group 'org-appearance
  5142. :group 'org-babel)
  5143. (defcustom org-allow-promoting-top-level-subtree nil
  5144. "When non-nil, allow promoting a top level subtree.
  5145. The leading star of the top level headline will be replaced
  5146. by a #."
  5147. :type 'boolean
  5148. :version "24.1"
  5149. :group 'org-appearance)
  5150. (defun org-fontify-meta-lines-and-blocks (limit)
  5151. (condition-case nil
  5152. (org-fontify-meta-lines-and-blocks-1 limit)
  5153. (error (message "org-mode fontification error"))))
  5154. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5155. "Fontify #+ lines and blocks."
  5156. (let ((case-fold-search t))
  5157. (if (re-search-forward
  5158. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5159. limit t)
  5160. (let ((beg (match-beginning 0))
  5161. (block-start (match-end 0))
  5162. (block-end nil)
  5163. (lang (match-string 7))
  5164. (beg1 (line-beginning-position 2))
  5165. (dc1 (downcase (match-string 2)))
  5166. (dc3 (downcase (match-string 3)))
  5167. end end1 quoting block-type ovl)
  5168. (cond
  5169. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5170. ;; a single line of backend-specific content
  5171. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5172. (remove-text-properties (match-beginning 0) (match-end 0)
  5173. '(display t invisible t intangible t))
  5174. (add-text-properties (match-beginning 1) (match-end 3)
  5175. '(font-lock-fontified t face org-meta-line))
  5176. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5177. '(font-lock-fontified t face org-block))
  5178. ; for backend-specific code
  5179. t)
  5180. ((and (match-end 4) (equal dc3 "+begin"))
  5181. ;; Truly a block
  5182. (setq block-type (downcase (match-string 5))
  5183. quoting (member block-type org-protecting-blocks))
  5184. (when (re-search-forward
  5185. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5186. nil t) ;; on purpose, we look further than LIMIT
  5187. (setq end (min (point-max) (match-end 0))
  5188. end1 (min (point-max) (1- (match-beginning 0))))
  5189. (setq block-end (match-beginning 0))
  5190. (when quoting
  5191. (org-remove-flyspell-overlays-in beg1 end1)
  5192. (remove-text-properties beg end
  5193. '(display t invisible t intangible t)))
  5194. (add-text-properties
  5195. beg end '(font-lock-fontified t font-lock-multiline t))
  5196. (add-text-properties beg beg1 '(face org-meta-line))
  5197. (org-remove-flyspell-overlays-in beg beg1)
  5198. (add-text-properties ; For end_src
  5199. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5200. (org-remove-flyspell-overlays-in end1 end)
  5201. (cond
  5202. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5203. (org-src-font-lock-fontify-block lang block-start block-end)
  5204. (add-text-properties beg1 block-end '(src-block t)))
  5205. (quoting
  5206. (add-text-properties beg1 (min (point-max) (1+ end1))
  5207. '(face org-block))) ; end of source block
  5208. ((not org-fontify-quote-and-verse-blocks))
  5209. ((string= block-type "quote")
  5210. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5211. ((string= block-type "verse")
  5212. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5213. (add-text-properties beg beg1 '(face org-block-begin-line))
  5214. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5215. '(face org-block-end-line))
  5216. t))
  5217. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5218. (org-remove-flyspell-overlays-in
  5219. (match-beginning 0)
  5220. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5221. (add-text-properties
  5222. beg (match-end 3)
  5223. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5224. '(font-lock-fontified t invisible t)
  5225. '(font-lock-fontified t face org-document-info-keyword)))
  5226. (add-text-properties
  5227. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5228. (if (string-equal dc1 "+title:")
  5229. '(font-lock-fontified t face org-document-title)
  5230. '(font-lock-fontified t face org-document-info))))
  5231. ((or (equal dc1 "+results")
  5232. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5233. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5234. "+call:" "+header:" "+headers:" "+name:"))
  5235. (and (match-end 4) (equal dc3 "+attr")))
  5236. (org-remove-flyspell-overlays-in
  5237. (match-beginning 0)
  5238. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5239. (add-text-properties
  5240. beg (match-end 0)
  5241. '(font-lock-fontified t face org-meta-line))
  5242. t)
  5243. ((member dc3 '(" " ""))
  5244. (org-remove-flyspell-overlays-in beg (match-end 0))
  5245. (add-text-properties
  5246. beg (match-end 0)
  5247. '(font-lock-fontified t face font-lock-comment-face)))
  5248. (t ;; just any other in-buffer setting, but not indented
  5249. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5250. (add-text-properties
  5251. beg (match-end 0)
  5252. '(font-lock-fontified t face org-meta-line))
  5253. t))))))
  5254. (defun org-fontify-drawers (limit)
  5255. "Fontify drawers."
  5256. (when (re-search-forward org-drawer-regexp limit t)
  5257. (add-text-properties
  5258. (match-beginning 0) (match-end 0)
  5259. '(font-lock-fontified t face org-special-keyword))
  5260. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5261. t))
  5262. (defun org-fontify-macros (limit)
  5263. "Fontify macros."
  5264. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5265. (add-text-properties
  5266. (match-beginning 0) (match-end 0)
  5267. '(font-lock-fontified t face org-macro))
  5268. (when org-hide-macro-markers
  5269. (add-text-properties (match-end 2) (match-beginning 2)
  5270. '(invisible t))
  5271. (add-text-properties (match-beginning 1) (match-end 1)
  5272. '(invisible t)))
  5273. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5274. t))
  5275. (defun org-activate-angle-links (limit)
  5276. "Add text properties for angle links."
  5277. (if (and (re-search-forward org-angle-link-re limit t)
  5278. (not (org-in-src-block-p)))
  5279. (progn
  5280. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5281. (add-text-properties (match-beginning 0) (match-end 0)
  5282. (list 'mouse-face 'highlight
  5283. 'keymap org-mouse-map))
  5284. (org-rear-nonsticky-at (match-end 0))
  5285. t)))
  5286. (defun org-activate-footnote-links (limit)
  5287. "Add text properties for footnotes."
  5288. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5289. (when fn
  5290. (let* ((beg (nth 1 fn))
  5291. (end (nth 2 fn))
  5292. (label (car fn))
  5293. (referencep (/= (line-beginning-position) beg)))
  5294. (when (and referencep (nth 3 fn))
  5295. (save-excursion
  5296. (goto-char beg)
  5297. (search-forward (or label "fn:"))
  5298. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5299. (add-text-properties beg end
  5300. (list 'mouse-face 'highlight
  5301. 'keymap org-mouse-map
  5302. 'help-echo
  5303. (if referencep "Footnote reference"
  5304. "Footnote definition")
  5305. 'font-lock-fontified t
  5306. 'font-lock-multiline t
  5307. 'face 'org-footnote))))))
  5308. (defun org-activate-bracket-links (limit)
  5309. "Add text properties for bracketed links."
  5310. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5311. (not (org-in-src-block-p)))
  5312. (let* ((hl (org-match-string-no-properties 1))
  5313. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5314. (ip (org-maybe-intangible
  5315. (list 'invisible 'org-link
  5316. 'keymap org-mouse-map 'mouse-face 'highlight
  5317. 'font-lock-multiline t 'help-echo help
  5318. 'htmlize-link `(:uri ,hl))))
  5319. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5320. 'font-lock-multiline t 'help-echo help
  5321. 'htmlize-link `(:uri ,hl))))
  5322. ;; We need to remove the invisible property here. Table narrowing
  5323. ;; may have made some of this invisible.
  5324. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5325. (remove-text-properties (match-beginning 0) (match-end 0)
  5326. '(invisible nil))
  5327. (if (match-end 3)
  5328. (progn
  5329. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5330. (org-rear-nonsticky-at (match-beginning 3))
  5331. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5332. (org-rear-nonsticky-at (match-end 3))
  5333. (add-text-properties (match-end 3) (match-end 0) ip)
  5334. (org-rear-nonsticky-at (match-end 0)))
  5335. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5336. (org-rear-nonsticky-at (match-beginning 1))
  5337. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5338. (org-rear-nonsticky-at (match-end 1))
  5339. (add-text-properties (match-end 1) (match-end 0) ip)
  5340. (org-rear-nonsticky-at (match-end 0)))
  5341. t)))
  5342. (defun org-activate-dates (limit)
  5343. "Add text properties for dates."
  5344. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5345. (not (equal (char-before (match-beginning 0)) 91)))
  5346. (progn
  5347. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5348. (add-text-properties (match-beginning 0) (match-end 0)
  5349. (list 'mouse-face 'highlight
  5350. 'keymap org-mouse-map))
  5351. (org-rear-nonsticky-at (match-end 0))
  5352. (when org-display-custom-times
  5353. (if (match-end 3)
  5354. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5355. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5356. t)))
  5357. (defvar org-target-link-regexp nil
  5358. "Regular expression matching radio targets in plain text.")
  5359. (make-variable-buffer-local 'org-target-link-regexp)
  5360. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5361. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5362. border border border))
  5363. "Regular expression matching a link target.")
  5364. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5365. "Regular expression matching a radio target.")
  5366. (defconst org-any-target-regexp
  5367. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5368. "Regular expression matching any target.")
  5369. (defun org-activate-target-links (limit)
  5370. "Add text properties for target matches."
  5371. (when org-target-link-regexp
  5372. (let ((case-fold-search t))
  5373. (if (re-search-forward org-target-link-regexp limit t)
  5374. (progn
  5375. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5376. (add-text-properties (match-beginning 1) (match-end 1)
  5377. (list 'mouse-face 'highlight
  5378. 'keymap org-mouse-map
  5379. 'help-echo "Radio target link"
  5380. 'org-linked-text t))
  5381. (org-rear-nonsticky-at (match-end 1))
  5382. t)))))
  5383. (defun org-update-radio-target-regexp ()
  5384. "Find all radio targets in this file and update the regular expression.
  5385. Also refresh fontification if needed."
  5386. (interactive)
  5387. (let ((old-regexp org-target-link-regexp)
  5388. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5389. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5390. (targets
  5391. (org-with-wide-buffer
  5392. (goto-char (point-min))
  5393. (let (rtn)
  5394. (while (re-search-forward org-radio-target-regexp nil t)
  5395. ;; Make sure point is really within the object.
  5396. (backward-char)
  5397. (let ((obj (org-element-context)))
  5398. (when (eq (org-element-type obj) 'radio-target)
  5399. (add-to-list 'rtn (org-element-property :value obj)))))
  5400. rtn))))
  5401. (setq org-target-link-regexp
  5402. (and targets
  5403. (concat before-re
  5404. (mapconcat
  5405. (lambda (x)
  5406. (replace-regexp-in-string
  5407. " +" "\\s-+" (regexp-quote x) t t))
  5408. targets
  5409. "\\|")
  5410. after-re)))
  5411. (unless (equal old-regexp org-target-link-regexp)
  5412. ;; Clean-up cache.
  5413. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5414. ((not org-target-link-regexp) old-regexp)
  5415. (t
  5416. (concat before-re
  5417. (mapconcat
  5418. (lambda (re)
  5419. (substring re (length before-re)
  5420. (- (length after-re))))
  5421. (list old-regexp org-target-link-regexp)
  5422. "\\|")
  5423. after-re)))))
  5424. (org-with-wide-buffer
  5425. (goto-char (point-min))
  5426. (while (re-search-forward regexp nil t)
  5427. (org-element-cache-refresh (match-beginning 1)))))
  5428. ;; Re fontify buffer.
  5429. (when (memq 'radio org-highlight-links)
  5430. (org-restart-font-lock)))))
  5431. (defun org-hide-wide-columns (limit)
  5432. (let (s e)
  5433. (setq s (text-property-any (point) (or limit (point-max))
  5434. 'org-cwidth t))
  5435. (when s
  5436. (setq e (next-single-property-change s 'org-cwidth))
  5437. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5438. (goto-char e)
  5439. t)))
  5440. (defvar org-latex-and-related-regexp nil
  5441. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5442. (defun org-compute-latex-and-related-regexp ()
  5443. "Compute regular expression for LaTeX, entities and sub/superscript.
  5444. Result depends on variable `org-highlight-latex-and-related'."
  5445. (org-set-local
  5446. 'org-latex-and-related-regexp
  5447. (let* ((re-sub
  5448. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5449. ((eq org-use-sub-superscripts '{})
  5450. (list org-match-substring-with-braces-regexp))
  5451. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5452. (re-latex
  5453. (when (memq 'latex org-highlight-latex-and-related)
  5454. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5455. (delq nil
  5456. (mapcar (lambda (x)
  5457. (and (member (car x) matchers) (nth 1 x)))
  5458. org-latex-regexps)))))
  5459. (re-entities
  5460. (when (memq 'entities org-highlight-latex-and-related)
  5461. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5462. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5463. (defun org-do-latex-and-related (limit)
  5464. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5465. LIMIT bounds the search for syntax to highlight. Stop at first
  5466. highlighted object, if any. Return t if some highlighting was
  5467. done, nil otherwise."
  5468. (when (org-string-nw-p org-latex-and-related-regexp)
  5469. (catch 'found
  5470. (while (re-search-forward org-latex-and-related-regexp limit t)
  5471. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5472. 'face))
  5473. '(org-code org-verbatim underline))
  5474. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5475. '(?_ ?^))
  5476. 1
  5477. 0)))
  5478. (font-lock-prepend-text-property
  5479. (+ offset (match-beginning 0)) (match-end 0)
  5480. 'face 'org-latex-and-related)
  5481. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5482. '(font-lock-multiline t)))
  5483. (throw 'found t)))
  5484. nil)))
  5485. (defun org-restart-font-lock ()
  5486. "Restart `font-lock-mode', to force refontification."
  5487. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5488. (font-lock-mode -1)
  5489. (font-lock-mode 1)))
  5490. (defun org-activate-tags (limit)
  5491. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5492. (progn
  5493. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5494. (add-text-properties (match-beginning 1) (match-end 1)
  5495. (list 'mouse-face 'highlight
  5496. 'keymap org-mouse-map))
  5497. (org-rear-nonsticky-at (match-end 1))
  5498. t)))
  5499. (defun org-outline-level ()
  5500. "Compute the outline level of the heading at point.
  5501. If this is called at a normal headline, the level is the number of stars.
  5502. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5503. (save-excursion
  5504. (if (not (ignore-errors (org-back-to-heading t)))
  5505. 0
  5506. (looking-at org-outline-regexp)
  5507. (1- (- (match-end 0) (match-beginning 0))))))
  5508. (defvar org-font-lock-keywords nil)
  5509. (defsubst org-re-property (property &optional literal allow-null)
  5510. "Return a regexp matching a PROPERTY line.
  5511. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5512. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5513. non-nil, match properties even without a value.
  5514. Match group 3 is set to the value when it exists. If there is no
  5515. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5516. (concat
  5517. "^\\(?4:[ \t]*\\)"
  5518. (format "\\(?1::\\(?2:%s\\):\\)"
  5519. (if literal property (regexp-quote property)))
  5520. (if allow-null
  5521. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5522. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5523. (defconst org-property-re
  5524. (org-re-property "\\S-+" 'literal t)
  5525. "Regular expression matching a property line.
  5526. There are four matching groups:
  5527. 1: :PROPKEY: including the leading and trailing colon,
  5528. 2: PROPKEY without the leading and trailing colon,
  5529. 3: PROPVAL without leading or trailing spaces,
  5530. 4: the indentation of the current line,
  5531. 5: trailing whitespace.")
  5532. (defvar org-font-lock-hook nil
  5533. "Functions to be called for special font lock stuff.")
  5534. (defvar org-font-lock-set-keywords-hook nil
  5535. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5536. This is called after `org-font-lock-extra-keywords' is defined, but before
  5537. it is installed to be used by font lock. This can be useful if something
  5538. needs to be inserted at a specific position in the font-lock sequence.")
  5539. (defun org-font-lock-hook (limit)
  5540. "Run `org-font-lock-hook' within LIMIT."
  5541. (run-hook-with-args 'org-font-lock-hook limit))
  5542. (defun org-set-font-lock-defaults ()
  5543. "Set font lock defaults for the current buffer."
  5544. (let* ((em org-fontify-emphasized-text)
  5545. (lk org-highlight-links)
  5546. (org-font-lock-extra-keywords
  5547. (list
  5548. ;; Call the hook
  5549. '(org-font-lock-hook)
  5550. ;; Headlines
  5551. `(,(if org-fontify-whole-heading-line
  5552. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5553. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5554. (1 (org-get-level-face 1))
  5555. (2 (org-get-level-face 2))
  5556. (3 (org-get-level-face 3)))
  5557. ;; Table lines
  5558. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5559. (1 'org-table t))
  5560. ;; Table internals
  5561. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5562. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5563. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5564. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5565. ;; Drawers
  5566. '(org-fontify-drawers)
  5567. ;; Properties
  5568. (list org-property-re
  5569. '(1 'org-special-keyword t)
  5570. '(3 'org-property-value t))
  5571. ;; Links
  5572. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5573. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5574. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5575. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5576. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5577. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5578. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5579. ;; Targets.
  5580. (list org-any-target-regexp '(0 'org-target t))
  5581. ;; Diary sexps.
  5582. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5583. ;; Macro
  5584. '(org-fontify-macros)
  5585. '(org-hide-wide-columns (0 nil append))
  5586. ;; TODO keyword
  5587. (list (format org-heading-keyword-regexp-format
  5588. org-todo-regexp)
  5589. '(2 (org-get-todo-face 2) t))
  5590. ;; DONE
  5591. (if org-fontify-done-headline
  5592. (list (format org-heading-keyword-regexp-format
  5593. (concat
  5594. "\\(?:"
  5595. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5596. "\\)"))
  5597. '(2 'org-headline-done t))
  5598. nil)
  5599. ;; Priorities
  5600. '(org-font-lock-add-priority-faces)
  5601. ;; Tags
  5602. '(org-font-lock-add-tag-faces)
  5603. ;; Tags groups
  5604. (if (and org-group-tags org-tag-groups-alist)
  5605. (list (concat org-outline-regexp-bol ".+\\(:"
  5606. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5607. ":\\).*$")
  5608. '(1 'org-tag-group prepend)))
  5609. ;; Special keywords
  5610. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5611. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5612. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5613. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5614. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5615. ;; Emphasis
  5616. (if em
  5617. (if (featurep 'xemacs)
  5618. '(org-do-emphasis-faces (0 nil append))
  5619. '(org-do-emphasis-faces)))
  5620. ;; Checkboxes
  5621. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5622. 1 'org-checkbox prepend)
  5623. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5624. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5625. (0 (org-get-checkbox-statistics-face) t)))
  5626. ;; Description list items
  5627. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5628. 1 'org-list-dt prepend)
  5629. ;; ARCHIVEd headings
  5630. (list (concat
  5631. org-outline-regexp-bol
  5632. "\\(.*:" org-archive-tag ":.*\\)")
  5633. '(1 'org-archived prepend))
  5634. ;; Specials
  5635. '(org-do-latex-and-related)
  5636. '(org-fontify-entities)
  5637. '(org-raise-scripts)
  5638. ;; Code
  5639. '(org-activate-code (1 'org-code t))
  5640. ;; COMMENT
  5641. (list (format
  5642. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5643. org-todo-regexp
  5644. org-comment-string)
  5645. '(9 'org-special-keyword t))
  5646. ;; Blocks and meta lines
  5647. '(org-fontify-meta-lines-and-blocks))))
  5648. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5649. (run-hooks 'org-font-lock-set-keywords-hook)
  5650. ;; Now set the full font-lock-keywords
  5651. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5652. (org-set-local 'font-lock-defaults
  5653. '(org-font-lock-keywords t nil nil backward-paragraph))
  5654. (kill-local-variable 'font-lock-keywords) nil))
  5655. (defun org-toggle-pretty-entities ()
  5656. "Toggle the composition display of entities as UTF8 characters."
  5657. (interactive)
  5658. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5659. (org-restart-font-lock)
  5660. (if org-pretty-entities
  5661. (message "Entities are now displayed as UTF8 characters")
  5662. (save-restriction
  5663. (widen)
  5664. (org-decompose-region (point-min) (point-max))
  5665. (message "Entities are now displayed as plain text"))))
  5666. (defvar org-custom-properties-overlays nil
  5667. "List of overlays used for custom properties.")
  5668. (make-variable-buffer-local 'org-custom-properties-overlays)
  5669. (defun org-toggle-custom-properties-visibility ()
  5670. "Display or hide properties in `org-custom-properties'."
  5671. (interactive)
  5672. (if org-custom-properties-overlays
  5673. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5674. (setq org-custom-properties-overlays nil))
  5675. (when org-custom-properties
  5676. (org-with-wide-buffer
  5677. (goto-char (point-min))
  5678. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5679. (while (re-search-forward regexp nil t)
  5680. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5681. (when (and end (< (point) end))
  5682. ;; Hide first custom property in current drawer.
  5683. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5684. (overlay-put o 'invisible t)
  5685. (overlay-put o 'org-custom-property t)
  5686. (push o org-custom-properties-overlays))
  5687. ;; Hide additional custom properties in the same drawer.
  5688. (while (re-search-forward regexp end t)
  5689. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5690. (overlay-put o 'invisible t)
  5691. (overlay-put o 'org-custom-property t)
  5692. (push o org-custom-properties-overlays)))))
  5693. ;; Each entry is limited to a single property drawer.
  5694. (outline-next-heading)))))))
  5695. (defun org-fontify-entities (limit)
  5696. "Find an entity to fontify."
  5697. (let (ee)
  5698. (when org-pretty-entities
  5699. (catch 'match
  5700. (while (re-search-forward
  5701. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5702. limit t)
  5703. (if (and (not (org-at-comment-p))
  5704. (setq ee (org-entity-get (match-string 1)))
  5705. (= (length (nth 6 ee)) 1))
  5706. (let*
  5707. ((end (if (equal (match-string 2) "{}")
  5708. (match-end 2)
  5709. (match-end 1))))
  5710. (add-text-properties
  5711. (match-beginning 0) end
  5712. (list 'font-lock-fontified t))
  5713. (compose-region (match-beginning 0) end
  5714. (nth 6 ee) nil)
  5715. (backward-char 1)
  5716. (throw 'match t))))
  5717. nil))))
  5718. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5719. "Fontify string S like in Org-mode."
  5720. (with-temp-buffer
  5721. (insert s)
  5722. (let ((org-odd-levels-only odd-levels))
  5723. (org-mode)
  5724. (font-lock-ensure)
  5725. (buffer-string))))
  5726. (defvar org-m nil)
  5727. (defvar org-l nil)
  5728. (defvar org-f nil)
  5729. (defun org-get-level-face (n)
  5730. "Get the right face for match N in font-lock matching of headlines."
  5731. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5732. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5733. (if org-cycle-level-faces
  5734. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5735. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5736. (cond
  5737. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5738. ((eq n 2) org-f)
  5739. (t (if org-level-color-stars-only nil org-f))))
  5740. (defun org-get-todo-face (kwd)
  5741. "Get the right face for a TODO keyword KWD.
  5742. If KWD is a number, get the corresponding match group."
  5743. (if (numberp kwd) (setq kwd (match-string kwd)))
  5744. (or (org-face-from-face-or-color
  5745. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5746. (and (member kwd org-done-keywords) 'org-done)
  5747. 'org-todo))
  5748. (defun org-face-from-face-or-color (context inherit face-or-color)
  5749. "Create a face list that inherits INHERIT, but sets the foreground color.
  5750. When FACE-OR-COLOR is not a string, just return it."
  5751. (if (stringp face-or-color)
  5752. (list :inherit inherit
  5753. (cdr (assoc context org-faces-easy-properties))
  5754. face-or-color)
  5755. face-or-color))
  5756. (defun org-font-lock-add-tag-faces (limit)
  5757. "Add the special tag faces."
  5758. (when (and org-tag-faces org-tags-special-faces-re)
  5759. (while (re-search-forward org-tags-special-faces-re limit t)
  5760. (add-text-properties (match-beginning 1) (match-end 1)
  5761. (list 'face (org-get-tag-face 1)
  5762. 'font-lock-fontified t))
  5763. (backward-char 1))))
  5764. (defun org-font-lock-add-priority-faces (limit)
  5765. "Add the special priority faces."
  5766. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5767. (when (save-match-data (org-at-heading-p))
  5768. (add-text-properties
  5769. (match-beginning 0) (match-end 0)
  5770. (list 'face (or (org-face-from-face-or-color
  5771. 'priority 'org-priority
  5772. (cdr (assoc (char-after (match-beginning 1))
  5773. org-priority-faces)))
  5774. 'org-priority)
  5775. 'font-lock-fontified t)))))
  5776. (defun org-get-tag-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. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5782. 'org-tag))
  5783. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5784. "Remove fontification and activation overlays from links."
  5785. (font-lock-default-unfontify-region beg end)
  5786. (let* ((buffer-undo-list t)
  5787. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5788. (inhibit-modification-hooks t)
  5789. deactivate-mark buffer-file-name buffer-file-truename)
  5790. (org-decompose-region beg end)
  5791. (remove-text-properties beg end
  5792. '(mouse-face t keymap t org-linked-text t
  5793. invisible t intangible t
  5794. org-emphasis t))
  5795. (org-remove-font-lock-display-properties beg end)))
  5796. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5797. ((raise 0.3) (height 0.7))
  5798. ((raise -0.5))
  5799. ((raise 0.5)))
  5800. "Display properties for showing superscripts and subscripts.")
  5801. (defun org-remove-font-lock-display-properties (beg end)
  5802. "Remove specific display properties that have been added by font lock.
  5803. The will remove the raise properties that are used to show superscripts
  5804. and subscripts."
  5805. (let (next prop)
  5806. (while (< beg end)
  5807. (setq next (next-single-property-change beg 'display nil end)
  5808. prop (get-text-property beg 'display))
  5809. (if (member prop org-script-display)
  5810. (put-text-property beg next 'display nil))
  5811. (setq beg next))))
  5812. (defun org-raise-scripts (limit)
  5813. "Add raise properties to sub/superscripts."
  5814. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5815. (if (re-search-forward
  5816. (if (eq org-use-sub-superscripts t)
  5817. org-match-substring-regexp
  5818. org-match-substring-with-braces-regexp)
  5819. limit t)
  5820. (let* ((pos (point)) table-p comment-p
  5821. (mpos (match-beginning 3))
  5822. (emph-p (get-text-property mpos 'org-emphasis))
  5823. (link-p (get-text-property mpos 'mouse-face))
  5824. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5825. (goto-char (point-at-bol))
  5826. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5827. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5828. (goto-char pos)
  5829. ;; Handle a_b^c
  5830. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5831. (if (or comment-p emph-p link-p keyw-p)
  5832. t
  5833. (put-text-property (match-beginning 3) (match-end 0)
  5834. 'display
  5835. (if (equal (char-after (match-beginning 2)) ?^)
  5836. (nth (if table-p 3 1) org-script-display)
  5837. (nth (if table-p 2 0) org-script-display)))
  5838. (add-text-properties (match-beginning 2) (match-end 2)
  5839. (list 'invisible t
  5840. 'org-dwidth t 'org-dwidth-n 1))
  5841. (if (and (eq (char-after (match-beginning 3)) ?{)
  5842. (eq (char-before (match-end 3)) ?}))
  5843. (progn
  5844. (add-text-properties
  5845. (match-beginning 3) (1+ (match-beginning 3))
  5846. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5847. (add-text-properties
  5848. (1- (match-end 3)) (match-end 3)
  5849. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5850. t)))))
  5851. ;;;; Visibility cycling, including org-goto and indirect buffer
  5852. ;;; Cycling
  5853. (defvar org-cycle-global-status nil)
  5854. (make-variable-buffer-local 'org-cycle-global-status)
  5855. (put 'org-cycle-global-status 'org-state t)
  5856. (defvar org-cycle-subtree-status nil)
  5857. (make-variable-buffer-local 'org-cycle-subtree-status)
  5858. (put 'org-cycle-subtree-status 'org-state t)
  5859. (defvar org-inlinetask-min-level)
  5860. (defun org-unlogged-message (&rest args)
  5861. "Display a message, but avoid logging it in the *Messages* buffer."
  5862. (let ((message-log-max nil))
  5863. (apply 'message args)))
  5864. ;;;###autoload
  5865. (defun org-cycle (&optional arg)
  5866. "TAB-action and visibility cycling for Org-mode.
  5867. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5868. is outline visibility cycling, but it also invokes other actions
  5869. in special contexts.
  5870. - When this function is called with a prefix argument, rotate the entire
  5871. buffer through 3 states (global cycling)
  5872. 1. OVERVIEW: Show only top-level headlines.
  5873. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5874. 3. SHOW ALL: Show everything.
  5875. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5876. determined by the variable `org-startup-folded', and by any VISIBILITY
  5877. properties in the buffer.
  5878. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5879. including any drawers.
  5880. - When inside a table, re-align the table and move to the next field.
  5881. - When point is at the beginning of a headline, rotate the subtree started
  5882. by this line through 3 different states (local cycling)
  5883. 1. FOLDED: Only the main headline is shown.
  5884. 2. CHILDREN: The main headline and the direct children are shown.
  5885. From this state, you can move to one of the children
  5886. and zoom in further.
  5887. 3. SUBTREE: Show the entire subtree, including body text.
  5888. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5889. - When point is at the beginning of an empty headline and the variable
  5890. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5891. of the headline by demoting and promoting it to likely levels. This
  5892. speeds up creation document structure by pressing TAB once or several
  5893. times right after creating a new headline.
  5894. - When there is a numeric prefix, go up to a heading with level ARG, do
  5895. a `show-subtree' and return to the previous cursor position. If ARG
  5896. is negative, go up that many levels.
  5897. - When point is not at the beginning of a headline, execute the global
  5898. binding for TAB, which is re-indenting the line. See the option
  5899. `org-cycle-emulate-tab' for details.
  5900. - Special case: if point is at the beginning of the buffer and there is
  5901. no headline in line 1, this function will act as if called with prefix arg
  5902. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5903. But only if also the variable `org-cycle-global-at-bob' is t."
  5904. (interactive "P")
  5905. (org-load-modules-maybe)
  5906. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5907. (and org-cycle-level-after-item/entry-creation
  5908. (or (org-cycle-level)
  5909. (org-cycle-item-indentation))))
  5910. (let* ((limit-level
  5911. (or org-cycle-max-level
  5912. (and (boundp 'org-inlinetask-min-level)
  5913. org-inlinetask-min-level
  5914. (1- org-inlinetask-min-level))))
  5915. (nstars (and limit-level
  5916. (if org-odd-levels-only
  5917. (and limit-level (1- (* limit-level 2)))
  5918. limit-level)))
  5919. (org-outline-regexp
  5920. (if (not (derived-mode-p 'org-mode))
  5921. outline-regexp
  5922. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5923. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5924. (not (looking-at org-outline-regexp))))
  5925. (org-cycle-hook
  5926. (if bob-special
  5927. (delq 'org-optimize-window-after-visibility-change
  5928. (copy-sequence org-cycle-hook))
  5929. org-cycle-hook))
  5930. (pos (point)))
  5931. (if (or bob-special (equal arg '(4)))
  5932. ;; special case: use global cycling
  5933. (setq arg t))
  5934. (cond
  5935. ((equal arg '(16))
  5936. (setq last-command 'dummy)
  5937. (org-set-startup-visibility)
  5938. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5939. ((equal arg '(64))
  5940. (show-all)
  5941. (org-unlogged-message "Entire buffer visible, including drawers"))
  5942. ;; Try cdlatex TAB completion
  5943. ((org-try-cdlatex-tab))
  5944. ;; Table: enter it or move to the next field.
  5945. ((org-at-table-p 'any)
  5946. (if (org-at-table.el-p)
  5947. (message "Use C-c ' to edit table.el tables")
  5948. (if arg (org-table-edit-field t)
  5949. (org-table-justify-field-maybe)
  5950. (call-interactively 'org-table-next-field))))
  5951. ((run-hook-with-args-until-success
  5952. 'org-tab-after-check-for-table-hook))
  5953. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5954. ((eq arg t) (org-cycle-internal-global))
  5955. ;; Drawers: delegate to `org-flag-drawer'.
  5956. ((save-excursion
  5957. (beginning-of-line 1)
  5958. (looking-at org-drawer-regexp))
  5959. (org-flag-drawer ; toggle block visibility
  5960. (not (get-char-property (match-end 0) 'invisible))))
  5961. ;; Show-subtree, ARG levels up from here.
  5962. ((integerp arg)
  5963. (save-excursion
  5964. (org-back-to-heading)
  5965. (outline-up-heading (if (< arg 0) (- arg)
  5966. (- (funcall outline-level) arg)))
  5967. (org-show-subtree)))
  5968. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5969. ((and (featurep 'org-inlinetask)
  5970. (org-inlinetask-at-task-p)
  5971. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5972. (org-inlinetask-toggle-visibility))
  5973. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5974. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5975. (save-excursion (beginning-of-line 1)
  5976. (looking-at org-outline-regexp)))
  5977. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5978. (org-cycle-internal-local))
  5979. ;; From there: TAB emulation and template completion.
  5980. (buffer-read-only (org-back-to-heading))
  5981. ((run-hook-with-args-until-success
  5982. 'org-tab-after-check-for-cycling-hook))
  5983. ((org-try-structure-completion))
  5984. ((run-hook-with-args-until-success
  5985. 'org-tab-before-tab-emulation-hook))
  5986. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5987. (or (not (bolp))
  5988. (not (looking-at org-outline-regexp))))
  5989. (call-interactively (global-key-binding "\t")))
  5990. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5991. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5992. (or (and (eq org-cycle-emulate-tab 'white)
  5993. (= (match-end 0) (point-at-eol)))
  5994. (and (eq org-cycle-emulate-tab 'whitestart)
  5995. (>= (match-end 0) pos))))
  5996. t
  5997. (eq org-cycle-emulate-tab t))
  5998. (call-interactively (global-key-binding "\t")))
  5999. (t (save-excursion
  6000. (org-back-to-heading)
  6001. (org-cycle)))))))
  6002. (defun org-cycle-internal-global ()
  6003. "Do the global cycling action."
  6004. ;; Hack to avoid display of messages for .org attachments in Gnus
  6005. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6006. (cond
  6007. ((and (eq last-command this-command)
  6008. (eq org-cycle-global-status 'overview))
  6009. ;; We just created the overview - now do table of contents
  6010. ;; This can be slow in very large buffers, so indicate action
  6011. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6012. (unless ga (org-unlogged-message "CONTENTS..."))
  6013. (org-content)
  6014. (unless ga (org-unlogged-message "CONTENTS...done"))
  6015. (setq org-cycle-global-status 'contents)
  6016. (run-hook-with-args 'org-cycle-hook 'contents))
  6017. ((and (eq last-command this-command)
  6018. (eq org-cycle-global-status 'contents))
  6019. ;; We just showed the table of contents - now show everything
  6020. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6021. (show-all)
  6022. (unless ga (org-unlogged-message "SHOW ALL"))
  6023. (setq org-cycle-global-status 'all)
  6024. (run-hook-with-args 'org-cycle-hook 'all))
  6025. (t
  6026. ;; Default action: go to overview
  6027. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6028. (org-overview)
  6029. (unless ga (org-unlogged-message "OVERVIEW"))
  6030. (setq org-cycle-global-status 'overview)
  6031. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6032. (defvar org-called-with-limited-levels nil
  6033. "Non-nil when `org-with-limited-levels' is currently active.")
  6034. (defun org-cycle-internal-local ()
  6035. "Do the local cycling action."
  6036. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6037. ;; First, determine end of headline (EOH), end of subtree or item
  6038. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6039. (save-excursion
  6040. (if (org-at-item-p)
  6041. (progn
  6042. (beginning-of-line)
  6043. (setq struct (org-list-struct))
  6044. (setq eoh (point-at-eol))
  6045. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6046. (setq has-children (org-list-has-child-p (point) struct)))
  6047. (org-back-to-heading)
  6048. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6049. (setq eos (save-excursion (org-end-of-subtree t t)
  6050. (when (bolp) (backward-char)) (point)))
  6051. (setq has-children
  6052. (or (save-excursion
  6053. (let ((level (funcall outline-level)))
  6054. (outline-next-heading)
  6055. (and (org-at-heading-p t)
  6056. (> (funcall outline-level) level))))
  6057. (save-excursion
  6058. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6059. ;; Determine end invisible part of buffer (EOL)
  6060. (beginning-of-line 2)
  6061. ;; XEmacs doesn't have `next-single-char-property-change'
  6062. (if (featurep 'xemacs)
  6063. (while (and (not (eobp)) ;; this is like `next-line'
  6064. (get-char-property (1- (point)) 'invisible))
  6065. (beginning-of-line 2))
  6066. (while (and (not (eobp)) ;; this is like `next-line'
  6067. (get-char-property (1- (point)) 'invisible))
  6068. (goto-char (next-single-char-property-change (point) 'invisible))
  6069. (and (eolp) (beginning-of-line 2))))
  6070. (setq eol (point)))
  6071. ;; Find out what to do next and set `this-command'
  6072. (cond
  6073. ((= eos eoh)
  6074. ;; Nothing is hidden behind this heading
  6075. (unless (org-before-first-heading-p)
  6076. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6077. (org-unlogged-message "EMPTY ENTRY")
  6078. (setq org-cycle-subtree-status nil)
  6079. (save-excursion
  6080. (goto-char eos)
  6081. (outline-next-heading)
  6082. (if (outline-invisible-p) (org-flag-heading nil))))
  6083. ((and (or (>= eol eos)
  6084. (not (string-match "\\S-" (buffer-substring eol eos))))
  6085. (or has-children
  6086. (not (setq children-skipped
  6087. org-cycle-skip-children-state-if-no-children))))
  6088. ;; Entire subtree is hidden in one line: children view
  6089. (unless (org-before-first-heading-p)
  6090. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6091. (if (org-at-item-p)
  6092. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6093. (org-show-entry)
  6094. (org-with-limited-levels (show-children))
  6095. ;; FIXME: This slows down the func way too much.
  6096. ;; How keep drawers hidden in subtree anyway?
  6097. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6098. ;; (org-cycle-hide-drawers 'subtree))
  6099. ;; Fold every list in subtree to top-level items.
  6100. (when (eq org-cycle-include-plain-lists 'integrate)
  6101. (save-excursion
  6102. (org-back-to-heading)
  6103. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6104. (beginning-of-line 1)
  6105. (let* ((struct (org-list-struct))
  6106. (prevs (org-list-prevs-alist struct))
  6107. (end (org-list-get-bottom-point struct)))
  6108. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6109. (org-list-get-all-items (point) struct prevs))
  6110. (goto-char (if (< end eos) end eos)))))))
  6111. (org-unlogged-message "CHILDREN")
  6112. (save-excursion
  6113. (goto-char eos)
  6114. (outline-next-heading)
  6115. (if (outline-invisible-p) (org-flag-heading nil)))
  6116. (setq org-cycle-subtree-status 'children)
  6117. (unless (org-before-first-heading-p)
  6118. (run-hook-with-args 'org-cycle-hook 'children)))
  6119. ((or children-skipped
  6120. (and (eq last-command this-command)
  6121. (eq org-cycle-subtree-status 'children)))
  6122. ;; We just showed the children, or no children are there,
  6123. ;; now show everything.
  6124. (unless (org-before-first-heading-p)
  6125. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6126. (outline-flag-region eoh eos nil)
  6127. (org-unlogged-message
  6128. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6129. (setq org-cycle-subtree-status 'subtree)
  6130. (unless (org-before-first-heading-p)
  6131. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6132. (t
  6133. ;; Default action: hide the subtree.
  6134. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6135. (outline-flag-region eoh eos t)
  6136. (org-unlogged-message "FOLDED")
  6137. (setq org-cycle-subtree-status 'folded)
  6138. (unless (org-before-first-heading-p)
  6139. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6140. ;;;###autoload
  6141. (defun org-global-cycle (&optional arg)
  6142. "Cycle the global visibility. For details see `org-cycle'.
  6143. With \\[universal-argument] prefix arg, switch to startup visibility.
  6144. With a numeric prefix, show all headlines up to that level."
  6145. (interactive "P")
  6146. (let ((org-cycle-include-plain-lists
  6147. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6148. (cond
  6149. ((integerp arg)
  6150. (show-all)
  6151. (hide-sublevels arg)
  6152. (setq org-cycle-global-status 'contents))
  6153. ((equal arg '(4))
  6154. (org-set-startup-visibility)
  6155. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6156. (t
  6157. (org-cycle '(4))))))
  6158. (defun org-set-startup-visibility ()
  6159. "Set the visibility required by startup options and properties."
  6160. (cond
  6161. ((eq org-startup-folded t)
  6162. (org-overview))
  6163. ((eq org-startup-folded 'content)
  6164. (org-content))
  6165. ((or (eq org-startup-folded 'showeverything)
  6166. (eq org-startup-folded nil))
  6167. (show-all)))
  6168. (unless (eq org-startup-folded 'showeverything)
  6169. (if org-hide-block-startup (org-hide-block-all))
  6170. (org-set-visibility-according-to-property 'no-cleanup)
  6171. (org-cycle-hide-archived-subtrees 'all)
  6172. (org-cycle-hide-drawers 'all)
  6173. (org-cycle-show-empty-lines t)))
  6174. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6175. "Switch subtree visibilities according to :VISIBILITY: property."
  6176. (interactive)
  6177. (let (org-show-entry-below state)
  6178. (save-excursion
  6179. (goto-char (point-min))
  6180. (while (re-search-forward
  6181. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6182. nil t)
  6183. (setq state (match-string 1))
  6184. (save-excursion
  6185. (org-back-to-heading t)
  6186. (hide-subtree)
  6187. (org-reveal)
  6188. (cond
  6189. ((equal state '("fold" "folded"))
  6190. (hide-subtree))
  6191. ((equal state "children")
  6192. (org-show-hidden-entry)
  6193. (show-children))
  6194. ((equal state "content")
  6195. (save-excursion
  6196. (save-restriction
  6197. (org-narrow-to-subtree)
  6198. (org-content))))
  6199. ((member state '("all" "showall"))
  6200. (show-subtree)))))
  6201. (unless no-cleanup
  6202. (org-cycle-hide-archived-subtrees 'all)
  6203. (org-cycle-hide-drawers 'all)
  6204. (org-cycle-show-empty-lines 'all)))))
  6205. ;; This function uses outline-regexp instead of the more fundamental
  6206. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6207. ;; buffers, where outline-regexp is needed.
  6208. (defun org-overview ()
  6209. "Switch to overview mode, showing only top-level headlines.
  6210. This shows all headlines with a level equal or greater than the level
  6211. of the first headline in the buffer. This is important, because if the
  6212. first headline is not level one, then (hide-sublevels 1) gives confusing
  6213. results."
  6214. (interactive)
  6215. (save-excursion
  6216. (let ((level
  6217. (save-excursion
  6218. (goto-char (point-min))
  6219. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6220. (progn
  6221. (goto-char (match-beginning 0))
  6222. (funcall outline-level))))))
  6223. (and level (hide-sublevels level)))))
  6224. (defun org-content (&optional arg)
  6225. "Show all headlines in the buffer, like a table of contents.
  6226. With numerical argument N, show content up to level N."
  6227. (interactive "P")
  6228. (org-overview)
  6229. (save-excursion
  6230. ;; Visit all headings and show their offspring
  6231. (and (integerp arg) (org-overview))
  6232. (goto-char (point-max))
  6233. (catch 'exit
  6234. (while (and (progn (condition-case nil
  6235. (outline-previous-visible-heading 1)
  6236. (error (goto-char (point-min))))
  6237. t)
  6238. (looking-at org-outline-regexp))
  6239. (if (integerp arg)
  6240. (show-children (1- arg))
  6241. (show-branches))
  6242. (if (bobp) (throw 'exit nil))))))
  6243. (defun org-optimize-window-after-visibility-change (state)
  6244. "Adjust the window after a change in outline visibility.
  6245. This function is the default value of the hook `org-cycle-hook'."
  6246. (when (get-buffer-window (current-buffer))
  6247. (cond
  6248. ((eq state 'content) nil)
  6249. ((eq state 'all) nil)
  6250. ((eq state 'folded) nil)
  6251. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6252. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6253. (defun org-remove-empty-overlays-at (pos)
  6254. "Remove outline overlays that do not contain non-white stuff."
  6255. (mapc
  6256. (lambda (o)
  6257. (and (eq 'outline (overlay-get o 'invisible))
  6258. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6259. (overlay-end o))))
  6260. (delete-overlay o)))
  6261. (overlays-at pos)))
  6262. (defun org-clean-visibility-after-subtree-move ()
  6263. "Fix visibility issues after moving a subtree."
  6264. ;; First, find a reasonable region to look at:
  6265. ;; Start two siblings above, end three below
  6266. (let* ((beg (save-excursion
  6267. (and (org-get-last-sibling)
  6268. (org-get-last-sibling))
  6269. (point)))
  6270. (end (save-excursion
  6271. (and (org-get-next-sibling)
  6272. (org-get-next-sibling)
  6273. (org-get-next-sibling))
  6274. (if (org-at-heading-p)
  6275. (point-at-eol)
  6276. (point))))
  6277. (level (looking-at "\\*+"))
  6278. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6279. (save-excursion
  6280. (save-restriction
  6281. (narrow-to-region beg end)
  6282. (when re
  6283. ;; Properly fold already folded siblings
  6284. (goto-char (point-min))
  6285. (while (re-search-forward re nil t)
  6286. (if (and (not (outline-invisible-p))
  6287. (save-excursion
  6288. (goto-char (point-at-eol)) (outline-invisible-p)))
  6289. (hide-entry))))
  6290. (org-cycle-show-empty-lines 'overview)
  6291. (org-cycle-hide-drawers 'overview)))))
  6292. (defun org-cycle-show-empty-lines (state)
  6293. "Show empty lines above all visible headlines.
  6294. The region to be covered depends on STATE when called through
  6295. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6296. entire buffer covered. Note that an empty line is only shown if there
  6297. are at least `org-cycle-separator-lines' empty lines before the headline."
  6298. (when (not (= org-cycle-separator-lines 0))
  6299. (save-excursion
  6300. (let* ((n (abs org-cycle-separator-lines))
  6301. (re (cond
  6302. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6303. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6304. (t (let ((ns (number-to-string (- n 2))))
  6305. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6306. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6307. beg end b e)
  6308. (cond
  6309. ((memq state '(overview contents t))
  6310. (setq beg (point-min) end (point-max)))
  6311. ((memq state '(children folded))
  6312. (setq beg (point) end (progn (org-end-of-subtree t t)
  6313. (beginning-of-line 2)
  6314. (point)))))
  6315. (when beg
  6316. (goto-char beg)
  6317. (while (re-search-forward re end t)
  6318. (unless (get-char-property (match-end 1) 'invisible)
  6319. (setq e (match-end 1))
  6320. (if (< org-cycle-separator-lines 0)
  6321. (setq b (save-excursion
  6322. (goto-char (match-beginning 0))
  6323. (org-back-over-empty-lines)
  6324. (if (save-excursion
  6325. (goto-char (max (point-min) (1- (point))))
  6326. (org-at-heading-p))
  6327. (1- (point))
  6328. (point))))
  6329. (setq b (match-beginning 1)))
  6330. (outline-flag-region b e nil)))))))
  6331. ;; Never hide empty lines at the end of the file.
  6332. (save-excursion
  6333. (goto-char (point-max))
  6334. (outline-previous-heading)
  6335. (outline-end-of-heading)
  6336. (if (and (looking-at "[ \t\n]+")
  6337. (= (match-end 0) (point-max)))
  6338. (outline-flag-region (point) (match-end 0) nil))))
  6339. (defun org-show-empty-lines-in-parent ()
  6340. "Move to the parent and re-show empty lines before visible headlines."
  6341. (save-excursion
  6342. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6343. (org-cycle-show-empty-lines context))))
  6344. (defun org-files-list ()
  6345. "Return `org-agenda-files' list, plus all open org-mode files.
  6346. This is useful for operations that need to scan all of a user's
  6347. open and agenda-wise Org files."
  6348. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6349. (dolist (buf (buffer-list))
  6350. (with-current-buffer buf
  6351. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6352. (let ((file (expand-file-name (buffer-file-name))))
  6353. (unless (member file files)
  6354. (push file files))))))
  6355. files))
  6356. (defsubst org-entry-beginning-position ()
  6357. "Return the beginning position of the current entry."
  6358. (save-excursion (outline-back-to-heading t) (point)))
  6359. (defsubst org-entry-end-position ()
  6360. "Return the end position of the current entry."
  6361. (save-excursion (outline-next-heading) (point)))
  6362. (defun org-cycle-hide-drawers (state &optional exceptions)
  6363. "Re-hide all drawers after a visibility state change.
  6364. When non-nil, optional argument EXCEPTIONS is a list of strings
  6365. specifying which drawers should not be hidden."
  6366. (when (and (derived-mode-p 'org-mode)
  6367. (not (memq state '(overview folded contents))))
  6368. (save-excursion
  6369. (let* ((globalp (memq state '(contents all)))
  6370. (beg (if globalp (point-min) (point)))
  6371. (end (if globalp (point-max)
  6372. (if (eq state 'children)
  6373. (save-excursion (outline-next-heading) (point))
  6374. (org-end-of-subtree t)))))
  6375. (goto-char beg)
  6376. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6377. (unless (member-ignore-case (match-string 1) exceptions)
  6378. (let ((drawer (org-element-at-point)))
  6379. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6380. (org-flag-drawer t drawer)
  6381. ;; Make sure to skip drawer entirely or we might flag
  6382. ;; it another time when matching its ending line with
  6383. ;; `org-drawer-regexp'.
  6384. (goto-char (org-element-property :end drawer))))))))))
  6385. (defun org-cycle-hide-inline-tasks (state)
  6386. "Re-hide inline tasks when switching to 'contents or 'children
  6387. visibility state."
  6388. (case state
  6389. (contents
  6390. (when (org-bound-and-true-p org-inlinetask-min-level)
  6391. (hide-sublevels (1- org-inlinetask-min-level))))
  6392. (children
  6393. (when (featurep 'org-inlinetask)
  6394. (save-excursion
  6395. (while (and (outline-next-heading)
  6396. (org-inlinetask-at-task-p))
  6397. (org-inlinetask-toggle-visibility)
  6398. (org-inlinetask-goto-end)))))))
  6399. (defun org-flag-drawer (flag &optional element)
  6400. "When FLAG is non-nil, hide the drawer we are at.
  6401. Otherwise make it visible. When optional argument ELEMENT is
  6402. a parsed drawer, as returned by `org-element-at-point', hide or
  6403. show that drawer instead."
  6404. (when (save-excursion
  6405. (beginning-of-line)
  6406. (org-looking-at-p org-drawer-regexp))
  6407. (let ((drawer (or element (org-element-at-point))))
  6408. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6409. (let ((post (org-element-property :post-affiliated drawer)))
  6410. (save-excursion
  6411. (outline-flag-region
  6412. (progn (goto-char post) (line-end-position))
  6413. (progn (goto-char (org-element-property :end drawer))
  6414. (skip-chars-backward " \r\t\n")
  6415. (line-end-position))
  6416. flag))
  6417. ;; When the drawer is hidden away, make sure point lies in
  6418. ;; a visible part of the buffer.
  6419. (when (and flag (> (line-beginning-position) post))
  6420. (goto-char post)))))))
  6421. (defun org-subtree-end-visible-p ()
  6422. "Is the end of the current subtree visible?"
  6423. (pos-visible-in-window-p
  6424. (save-excursion (org-end-of-subtree t) (point))))
  6425. (defun org-first-headline-recenter ()
  6426. "Move cursor to the first headline and recenter the headline."
  6427. (goto-char (point-min))
  6428. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6429. (set-window-start (selected-window) (point-at-bol))))
  6430. ;;; Saving and restoring visibility
  6431. (defun org-outline-overlay-data (&optional use-markers)
  6432. "Return a list of the locations of all outline overlays.
  6433. These are overlays with the `invisible' property value `outline'.
  6434. The return value is a list of cons cells, with start and stop
  6435. positions for each overlay.
  6436. If USE-MARKERS is set, return the positions as markers."
  6437. (let (beg end)
  6438. (save-excursion
  6439. (save-restriction
  6440. (widen)
  6441. (delq nil
  6442. (mapcar (lambda (o)
  6443. (when (eq (overlay-get o 'invisible) 'outline)
  6444. (setq beg (overlay-start o)
  6445. end (overlay-end o))
  6446. (and beg end (> end beg)
  6447. (if use-markers
  6448. (cons (copy-marker beg)
  6449. (copy-marker end t))
  6450. (cons beg end)))))
  6451. (overlays-in (point-min) (point-max))))))))
  6452. (defun org-set-outline-overlay-data (data)
  6453. "Create visibility overlays for all positions in DATA.
  6454. DATA should have been made by `org-outline-overlay-data'."
  6455. (let (o)
  6456. (save-excursion
  6457. (save-restriction
  6458. (widen)
  6459. (show-all)
  6460. (mapc (lambda (c)
  6461. (outline-flag-region (car c) (cdr c) t))
  6462. data)))))
  6463. ;;; Folding of blocks
  6464. (defvar org-hide-block-overlays nil
  6465. "Overlays hiding blocks.")
  6466. (make-variable-buffer-local 'org-hide-block-overlays)
  6467. (defun org-block-map (function &optional start end)
  6468. "Call FUNCTION at the head of all source blocks in the current buffer.
  6469. Optional arguments START and END can be used to limit the range."
  6470. (let ((start (or start (point-min)))
  6471. (end (or end (point-max))))
  6472. (save-excursion
  6473. (goto-char start)
  6474. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6475. (save-excursion
  6476. (save-match-data
  6477. (goto-char (match-beginning 0))
  6478. (funcall function)))))))
  6479. (defun org-hide-block-toggle-all ()
  6480. "Toggle the visibility of all blocks in the current buffer."
  6481. (org-block-map 'org-hide-block-toggle))
  6482. (defun org-hide-block-all ()
  6483. "Fold all blocks in the current buffer."
  6484. (interactive)
  6485. (org-show-block-all)
  6486. (org-block-map 'org-hide-block-toggle-maybe))
  6487. (defun org-show-block-all ()
  6488. "Unfold all blocks in the current buffer."
  6489. (interactive)
  6490. (mapc 'delete-overlay org-hide-block-overlays)
  6491. (setq org-hide-block-overlays nil))
  6492. (defun org-hide-block-toggle-maybe ()
  6493. "Toggle visibility of block at point.
  6494. Unlike to `org-hide-block-toggle', this function does not throw
  6495. an error. Return a non-nil value when toggling is successful."
  6496. (interactive)
  6497. (ignore-errors (org-hide-block-toggle)))
  6498. (defun org-hide-block-toggle (&optional force)
  6499. "Toggle the visibility of the current block.
  6500. When optional argument FORCE is `off', make block visible. If it
  6501. is non-nil, hide it unconditionally. Throw an error when not at
  6502. a block. Return a non-nil value when toggling is successful."
  6503. (interactive)
  6504. (let ((element (org-element-at-point)))
  6505. (unless (memq (org-element-type element)
  6506. '(center-block comment-block dynamic-block example-block
  6507. export-block quote-block special-block
  6508. src-block verse-block))
  6509. (user-error "Not at a block"))
  6510. (let* ((start (save-excursion
  6511. (goto-char (org-element-property :post-affiliated element))
  6512. (line-end-position)))
  6513. (end (save-excursion
  6514. (goto-char (org-element-property :end element))
  6515. (skip-chars-backward " \r\t\n")
  6516. (line-end-position)))
  6517. (overlays (overlays-at start)))
  6518. (cond
  6519. ;; Do nothing when not before or at the block opening line or
  6520. ;; at the block closing line.
  6521. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6522. ((and (not (eq force 'off))
  6523. (not (memq t (mapcar
  6524. (lambda (o)
  6525. (eq (overlay-get o 'invisible) 'org-hide-block))
  6526. overlays))))
  6527. (let ((ov (make-overlay start end)))
  6528. (overlay-put ov 'invisible 'org-hide-block)
  6529. ;; Make the block accessible to `isearch'.
  6530. (overlay-put
  6531. ov 'isearch-open-invisible
  6532. (lambda (ov)
  6533. (when (memq ov org-hide-block-overlays)
  6534. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6535. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6536. (delete-overlay ov))))
  6537. (push ov org-hide-block-overlays)
  6538. ;; When the block is hidden away, make sure point is left in
  6539. ;; a visible part of the buffer.
  6540. (when (> (line-beginning-position) start)
  6541. (goto-char start)
  6542. (beginning-of-line))
  6543. ;; Signal successful toggling.
  6544. t))
  6545. ((or (not force) (eq force 'off))
  6546. (dolist (ov overlays t)
  6547. (when (memq ov org-hide-block-overlays)
  6548. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6549. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6550. (delete-overlay ov))))))))
  6551. ;; org-tab-after-check-for-cycling-hook
  6552. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6553. ;; Remove overlays when changing major mode
  6554. (add-hook 'org-mode-hook
  6555. (lambda () (org-add-hook 'change-major-mode-hook
  6556. 'org-show-block-all 'append 'local)))
  6557. ;;; Org-goto
  6558. (defvar org-goto-window-configuration nil)
  6559. (defvar org-goto-marker nil)
  6560. (defvar org-goto-map)
  6561. (defun org-goto-map ()
  6562. "Set the keymap `org-goto'."
  6563. (setq org-goto-map
  6564. (let ((map (make-sparse-keymap)))
  6565. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6566. mouse-drag-region universal-argument org-occur))
  6567. cmd)
  6568. (while (setq cmd (pop cmds))
  6569. (substitute-key-definition cmd cmd map global-map)))
  6570. (suppress-keymap map)
  6571. (org-defkey map "\C-m" 'org-goto-ret)
  6572. (org-defkey map [(return)] 'org-goto-ret)
  6573. (org-defkey map [(left)] 'org-goto-left)
  6574. (org-defkey map [(right)] 'org-goto-right)
  6575. (org-defkey map [(control ?g)] 'org-goto-quit)
  6576. (org-defkey map "\C-i" 'org-cycle)
  6577. (org-defkey map [(tab)] 'org-cycle)
  6578. (org-defkey map [(down)] 'outline-next-visible-heading)
  6579. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6580. (if org-goto-auto-isearch
  6581. (if (fboundp 'define-key-after)
  6582. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6583. nil)
  6584. (org-defkey map "q" 'org-goto-quit)
  6585. (org-defkey map "n" 'outline-next-visible-heading)
  6586. (org-defkey map "p" 'outline-previous-visible-heading)
  6587. (org-defkey map "f" 'outline-forward-same-level)
  6588. (org-defkey map "b" 'outline-backward-same-level)
  6589. (org-defkey map "u" 'outline-up-heading))
  6590. (org-defkey map "/" 'org-occur)
  6591. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6592. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6593. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6594. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6595. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6596. map)))
  6597. (defconst org-goto-help
  6598. "Browse buffer copy, to find location or copy text.%s
  6599. RET=jump to location C-g=quit and return to previous location
  6600. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6601. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6602. (defun org-goto (&optional alternative-interface)
  6603. "Look up a different location in the current file, keeping current visibility.
  6604. When you want look-up or go to a different location in a
  6605. document, the fastest way is often to fold the entire buffer and
  6606. then dive into the tree. This method has the disadvantage, that
  6607. the previous location will be folded, which may not be what you
  6608. want.
  6609. This command works around this by showing a copy of the current
  6610. buffer in an indirect buffer, in overview mode. You can dive
  6611. into the tree in that copy, use org-occur and incremental search
  6612. to find a location. When pressing RET or `Q', the command
  6613. returns to the original buffer in which the visibility is still
  6614. unchanged. After RET it will also jump to the location selected
  6615. in the indirect buffer and expose the headline hierarchy above.
  6616. With a prefix argument, use the alternative interface: e.g. if
  6617. `org-goto-interface' is 'outline use 'outline-path-completion."
  6618. (interactive "P")
  6619. (org-goto-map)
  6620. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6621. (org-refile-use-outline-path t)
  6622. (org-refile-target-verify-function nil)
  6623. (interface
  6624. (if (not alternative-interface)
  6625. org-goto-interface
  6626. (if (eq org-goto-interface 'outline)
  6627. 'outline-path-completion
  6628. 'outline)))
  6629. (org-goto-start-pos (point))
  6630. (selected-point
  6631. (if (eq interface 'outline)
  6632. (car (org-get-location (current-buffer) org-goto-help))
  6633. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6634. (org-refile-check-position pa)
  6635. (nth 3 pa)))))
  6636. (if selected-point
  6637. (progn
  6638. (org-mark-ring-push org-goto-start-pos)
  6639. (goto-char selected-point)
  6640. (if (or (outline-invisible-p) (org-invisible-p2))
  6641. (org-show-context 'org-goto)))
  6642. (message "Quit"))))
  6643. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6644. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6645. (defvar org-goto-local-auto-isearch-map) ; defined below
  6646. (defun org-get-location (buf help)
  6647. "Let the user select a location in the Org-mode buffer BUF.
  6648. This function uses a recursive edit. It returns the selected position
  6649. or nil."
  6650. (org-no-popups
  6651. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6652. (isearch-hide-immediately nil)
  6653. (isearch-search-fun-function
  6654. (lambda () 'org-goto-local-search-headings))
  6655. (org-goto-selected-point org-goto-exit-command))
  6656. (save-excursion
  6657. (save-window-excursion
  6658. (delete-other-windows)
  6659. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6660. (org-pop-to-buffer-same-window
  6661. (condition-case nil
  6662. (make-indirect-buffer (current-buffer) "*org-goto*")
  6663. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6664. (with-output-to-temp-buffer "*Org Help*"
  6665. (princ (format help (if org-goto-auto-isearch
  6666. " Just type for auto-isearch."
  6667. " n/p/f/b/u to navigate, q to quit."))))
  6668. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6669. (setq buffer-read-only nil)
  6670. (let ((org-startup-truncated t)
  6671. (org-startup-folded nil)
  6672. (org-startup-align-all-tables nil))
  6673. (org-mode)
  6674. (org-overview))
  6675. (setq buffer-read-only t)
  6676. (if (and (boundp 'org-goto-start-pos)
  6677. (integer-or-marker-p org-goto-start-pos))
  6678. (let ((org-show-hierarchy-above t)
  6679. (org-show-siblings t)
  6680. (org-show-following-heading t))
  6681. (goto-char org-goto-start-pos)
  6682. (and (outline-invisible-p) (org-show-context)))
  6683. (goto-char (point-min)))
  6684. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6685. (message "Select location and press RET")
  6686. (use-local-map org-goto-map)
  6687. (recursive-edit)))
  6688. (kill-buffer "*org-goto*")
  6689. (cons org-goto-selected-point org-goto-exit-command))))
  6690. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6691. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6692. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6693. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6694. (defun org-goto-local-search-headings (string bound noerror)
  6695. "Search and make sure that any matches are in headlines."
  6696. (catch 'return
  6697. (while (if isearch-forward
  6698. (search-forward string bound noerror)
  6699. (search-backward string bound noerror))
  6700. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6701. (and (member :headline context)
  6702. (not (member :tags context))))
  6703. (throw 'return (point))))))
  6704. (defun org-goto-local-auto-isearch ()
  6705. "Start isearch."
  6706. (interactive)
  6707. (goto-char (point-min))
  6708. (let ((keys (this-command-keys)))
  6709. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6710. (isearch-mode t)
  6711. (isearch-process-search-char (string-to-char keys)))))
  6712. (defun org-goto-ret (&optional arg)
  6713. "Finish `org-goto' by going to the new location."
  6714. (interactive "P")
  6715. (setq org-goto-selected-point (point)
  6716. org-goto-exit-command 'return)
  6717. (throw 'exit nil))
  6718. (defun org-goto-left ()
  6719. "Finish `org-goto' by going to the new location."
  6720. (interactive)
  6721. (if (org-at-heading-p)
  6722. (progn
  6723. (beginning-of-line 1)
  6724. (setq org-goto-selected-point (point)
  6725. org-goto-exit-command 'left)
  6726. (throw 'exit nil))
  6727. (user-error "Not on a heading")))
  6728. (defun org-goto-right ()
  6729. "Finish `org-goto' by going to the new location."
  6730. (interactive)
  6731. (if (org-at-heading-p)
  6732. (progn
  6733. (setq org-goto-selected-point (point)
  6734. org-goto-exit-command 'right)
  6735. (throw 'exit nil))
  6736. (user-error "Not on a heading")))
  6737. (defun org-goto-quit ()
  6738. "Finish `org-goto' without cursor motion."
  6739. (interactive)
  6740. (setq org-goto-selected-point nil)
  6741. (setq org-goto-exit-command 'quit)
  6742. (throw 'exit nil))
  6743. ;;; Indirect buffer display of subtrees
  6744. (defvar org-indirect-dedicated-frame nil
  6745. "This is the frame being used for indirect tree display.")
  6746. (defvar org-last-indirect-buffer nil)
  6747. (defun org-tree-to-indirect-buffer (&optional arg)
  6748. "Create indirect buffer and narrow it to current subtree.
  6749. With a numerical prefix ARG, go up to this level and then take that tree.
  6750. If ARG is negative, go up that many levels.
  6751. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6752. indirect buffer previously made with this command, to avoid proliferation of
  6753. indirect buffers. However, when you call the command with a \
  6754. \\[universal-argument] prefix, or
  6755. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6756. is kept so that you can work with several indirect buffers at the same time.
  6757. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6758. \\[universal-argument] prefix also
  6759. requests that a new frame be made for the new buffer, so that the dedicated
  6760. frame is not changed."
  6761. (interactive "P")
  6762. (let ((cbuf (current-buffer))
  6763. (cwin (selected-window))
  6764. (pos (point))
  6765. beg end level heading ibuf)
  6766. (save-excursion
  6767. (org-back-to-heading t)
  6768. (when (numberp arg)
  6769. (setq level (org-outline-level))
  6770. (if (< arg 0) (setq arg (+ level arg)))
  6771. (while (> (setq level (org-outline-level)) arg)
  6772. (org-up-heading-safe)))
  6773. (setq beg (point)
  6774. heading (org-get-heading))
  6775. (org-end-of-subtree t t)
  6776. (if (org-at-heading-p) (backward-char 1))
  6777. (setq end (point)))
  6778. (if (and (buffer-live-p org-last-indirect-buffer)
  6779. (not (eq org-indirect-buffer-display 'new-frame))
  6780. (not arg))
  6781. (kill-buffer org-last-indirect-buffer))
  6782. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6783. org-last-indirect-buffer ibuf)
  6784. (cond
  6785. ((or (eq org-indirect-buffer-display 'new-frame)
  6786. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6787. (select-frame (make-frame))
  6788. (delete-other-windows)
  6789. (org-pop-to-buffer-same-window ibuf)
  6790. (org-set-frame-title heading))
  6791. ((eq org-indirect-buffer-display 'dedicated-frame)
  6792. (raise-frame
  6793. (select-frame (or (and org-indirect-dedicated-frame
  6794. (frame-live-p org-indirect-dedicated-frame)
  6795. org-indirect-dedicated-frame)
  6796. (setq org-indirect-dedicated-frame (make-frame)))))
  6797. (delete-other-windows)
  6798. (org-pop-to-buffer-same-window ibuf)
  6799. (org-set-frame-title (concat "Indirect: " heading)))
  6800. ((eq org-indirect-buffer-display 'current-window)
  6801. (org-pop-to-buffer-same-window ibuf))
  6802. ((eq org-indirect-buffer-display 'other-window)
  6803. (pop-to-buffer ibuf))
  6804. (t (error "Invalid value")))
  6805. (if (featurep 'xemacs)
  6806. (save-excursion (org-mode) (turn-on-font-lock)))
  6807. (narrow-to-region beg end)
  6808. (show-all)
  6809. (goto-char pos)
  6810. (run-hook-with-args 'org-cycle-hook 'all)
  6811. (and (window-live-p cwin) (select-window cwin))))
  6812. (defun org-get-indirect-buffer (&optional buffer heading)
  6813. (setq buffer (or buffer (current-buffer)))
  6814. (let ((n 1) (base (buffer-name buffer)) bname)
  6815. (while (buffer-live-p
  6816. (get-buffer
  6817. (setq bname
  6818. (concat base "-"
  6819. (if heading (concat heading "-" (number-to-string n))
  6820. (number-to-string n))))))
  6821. (setq n (1+ n)))
  6822. (condition-case nil
  6823. (make-indirect-buffer buffer bname 'clone)
  6824. (error (make-indirect-buffer buffer bname)))))
  6825. (defun org-set-frame-title (title)
  6826. "Set the title of the current frame to the string TITLE."
  6827. ;; FIXME: how to name a single frame in XEmacs???
  6828. (unless (featurep 'xemacs)
  6829. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6830. ;;;; Structure editing
  6831. ;;; Inserting headlines
  6832. (defun org-previous-line-empty-p (&optional next)
  6833. "Is the previous line a blank line?
  6834. When NEXT is non-nil, check the next line instead."
  6835. (save-excursion
  6836. (and (not (bobp))
  6837. (or (beginning-of-line (if next 2 0)) t)
  6838. (save-match-data
  6839. (looking-at "[ \t]*$")))))
  6840. (defun org-insert-heading (&optional arg invisible-ok)
  6841. "Insert a new heading or an item with the same depth at point.
  6842. If point is at the beginning of a heading or a list item, insert
  6843. a new heading or a new item above the current one. If point is
  6844. at the beginning of a normal line, turn the line into a heading.
  6845. If point is in the middle of a headline or a list item, split the
  6846. headline or the item and create a new headline/item with the text
  6847. in the current line after point \(see `org-M-RET-may-split-line'
  6848. on how to modify this behavior).
  6849. With one universal prefix argument, set the user option
  6850. `org-insert-heading-respect-content' to t for the duration of
  6851. the command. This modifies the behavior described above in this
  6852. ways: on list items and at the beginning of normal lines, force
  6853. the insertion of a heading after the current subtree.
  6854. With two universal prefix arguments, insert the heading at the
  6855. end of the grandparent subtree. For example, if point is within
  6856. a 2nd-level heading, then it will insert a 2nd-level heading at
  6857. the end of the 1st-level parent heading.
  6858. If point is at the beginning of a headline, insert a sibling
  6859. before the current headline. If point is not at the beginning,
  6860. split the line and create a new headline with the text in the
  6861. current line after point \(see `org-M-RET-may-split-line' on how
  6862. to modify this behavior).
  6863. If point is at the beginning of a normal line, turn this line
  6864. into a heading.
  6865. When INVISIBLE-OK is set, stop at invisible headlines when going
  6866. back. This is important for non-interactive uses of the
  6867. command."
  6868. (interactive "P")
  6869. (if (org-called-interactively-p 'any) (org-reveal))
  6870. (let ((itemp (org-in-item-p))
  6871. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6872. (respect-content (or org-insert-heading-respect-content
  6873. (equal arg '(4))))
  6874. (initial-content "")
  6875. (adjust-empty-lines t))
  6876. (cond
  6877. ((or (= (buffer-size) 0)
  6878. (and (not (save-excursion
  6879. (and (ignore-errors (org-back-to-heading invisible-ok))
  6880. (org-at-heading-p))))
  6881. (or arg (not itemp))))
  6882. ;; At beginning of buffer or so high up that only a heading
  6883. ;; makes sense.
  6884. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6885. ((not respect-content)
  6886. (unless may-split (end-of-line))
  6887. (insert "\n* "))
  6888. ((re-search-forward org-outline-regexp-bol nil t)
  6889. (beginning-of-line)
  6890. (insert "* \n")
  6891. (backward-char))
  6892. (t (goto-char (point-max))
  6893. (insert "\n* ")))
  6894. (run-hooks 'org-insert-heading-hook))
  6895. ((and itemp (not (member arg '((4) (16)))))
  6896. ;; Insert an item
  6897. (org-insert-item))
  6898. (t
  6899. ;; Maybe move at the end of the subtree
  6900. (when (equal arg '(16))
  6901. (org-up-heading-safe)
  6902. (org-end-of-subtree t))
  6903. ;; Insert a heading
  6904. (save-restriction
  6905. (widen)
  6906. (let* ((level nil)
  6907. (on-heading (org-at-heading-p))
  6908. (empty-line-p (if on-heading
  6909. (org-previous-line-empty-p)
  6910. ;; We will decide later
  6911. nil))
  6912. ;; Get a level string to fall back on
  6913. (fix-level
  6914. (if (org-before-first-heading-p) "*"
  6915. (save-excursion
  6916. (org-back-to-heading t)
  6917. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6918. (looking-at org-outline-regexp)
  6919. (make-string (1- (length (match-string 0))) ?*))))
  6920. (stars
  6921. (save-excursion
  6922. (condition-case nil
  6923. (progn
  6924. (org-back-to-heading invisible-ok)
  6925. (when (and (not on-heading)
  6926. (featurep 'org-inlinetask)
  6927. (integerp org-inlinetask-min-level)
  6928. (>= (length (match-string 0))
  6929. org-inlinetask-min-level))
  6930. ;; Find a heading level before the inline task
  6931. (while (and (setq level (org-up-heading-safe))
  6932. (>= level org-inlinetask-min-level)))
  6933. (if (org-at-heading-p)
  6934. (org-back-to-heading invisible-ok)
  6935. (error "This should not happen")))
  6936. (unless (and (save-excursion
  6937. (save-match-data
  6938. (org-backward-heading-same-level
  6939. 1 invisible-ok))
  6940. (= (point) (match-beginning 0)))
  6941. (not (org-previous-line-empty-p t)))
  6942. (setq empty-line-p (or empty-line-p
  6943. (org-previous-line-empty-p))))
  6944. (match-string 0))
  6945. (error (or fix-level "* ")))))
  6946. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6947. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6948. pos hide-previous previous-pos)
  6949. ;; If we insert after content, move there and clean up whitespace
  6950. (when (and respect-content
  6951. (not (org-looking-at-p org-outline-regexp-bol)))
  6952. (if (not (org-before-first-heading-p))
  6953. (org-end-of-subtree nil t)
  6954. (re-search-forward org-outline-regexp-bol)
  6955. (beginning-of-line 0))
  6956. (skip-chars-backward " \r\n")
  6957. (and (not (looking-back "^\\*+"))
  6958. (looking-at "[ \t]+") (replace-match ""))
  6959. (unless (eobp) (forward-char 1))
  6960. (when (looking-at "^\\*")
  6961. (unless (bobp) (backward-char 1))
  6962. (insert "\n")))
  6963. ;; If we are splitting, grab the text that should be moved to the new headline
  6964. (when may-split
  6965. (if (org-on-heading-p)
  6966. ;; This is a heading, we split intelligently (keeping tags)
  6967. (let ((pos (point)))
  6968. (goto-char (point-at-bol))
  6969. (unless (looking-at org-complex-heading-regexp)
  6970. (error "This should not happen"))
  6971. (when (and (match-beginning 4)
  6972. (> pos (match-beginning 4))
  6973. (< pos (match-end 4)))
  6974. (setq initial-content (buffer-substring pos (match-end 4)))
  6975. (goto-char pos)
  6976. (delete-region (point) (match-end 4))
  6977. (if (looking-at "[ \t]*$")
  6978. (replace-match "")
  6979. (insert (make-string (length initial-content) ?\ )))
  6980. (setq initial-content (org-trim initial-content)))
  6981. (goto-char pos))
  6982. ;; a normal line
  6983. (setq initial-content
  6984. (org-trim (buffer-substring (point) (point-at-eol))))
  6985. (delete-region (point) (point-at-eol))))
  6986. ;; If we are at the beginning of the line, insert before it. Else after
  6987. (cond
  6988. ((and (bolp) (looking-at "[ \t]*$")))
  6989. ((and (bolp) (not (looking-at "[ \t]*$")))
  6990. (open-line 1))
  6991. (t
  6992. (goto-char (point-at-eol))
  6993. (insert "\n")))
  6994. ;; Insert the new heading
  6995. (insert stars)
  6996. (just-one-space)
  6997. (insert initial-content)
  6998. (when adjust-empty-lines
  6999. (if (or (not blank)
  7000. (and blank (not (org-previous-line-empty-p))))
  7001. (org-N-empty-lines-before-current (if blank 1 0))))
  7002. (run-hooks 'org-insert-heading-hook)))))))
  7003. (defun org-N-empty-lines-before-current (N)
  7004. "Make the number of empty lines before current exactly N.
  7005. So this will delete or add empty lines."
  7006. (save-excursion
  7007. (beginning-of-line)
  7008. (let ((p (point)))
  7009. (skip-chars-backward " \r\t\n")
  7010. (unless (bolp) (forward-line))
  7011. (delete-region (point) p))
  7012. (when (> N 0) (insert (make-string N ?\n)))))
  7013. (defun org-get-heading (&optional no-tags no-todo)
  7014. "Return the heading of the current entry, without the stars.
  7015. When NO-TAGS is non-nil, don't include tags.
  7016. When NO-TODO is non-nil, don't include TODO keywords."
  7017. (save-excursion
  7018. (org-back-to-heading t)
  7019. (cond
  7020. ((and no-tags no-todo)
  7021. (looking-at org-complex-heading-regexp)
  7022. (match-string 4))
  7023. (no-tags
  7024. (looking-at (concat org-outline-regexp
  7025. "\\(.*?\\)"
  7026. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7027. (match-string 1))
  7028. (no-todo
  7029. (looking-at org-todo-line-regexp)
  7030. (match-string 3))
  7031. (t (looking-at org-heading-regexp)
  7032. (match-string 2)))))
  7033. (defvar orgstruct-mode) ; defined below
  7034. (defun org-heading-components ()
  7035. "Return the components of the current heading.
  7036. This is a list with the following elements:
  7037. - the level as an integer
  7038. - the reduced level, different if `org-odd-levels-only' is set.
  7039. - the TODO keyword, or nil
  7040. - the priority character, like ?A, or nil if no priority is given
  7041. - the headline text itself, or the tags string if no headline text
  7042. - the tags string, or nil."
  7043. (save-excursion
  7044. (org-back-to-heading t)
  7045. (if (let (case-fold-search)
  7046. (looking-at
  7047. (if orgstruct-mode
  7048. org-heading-regexp
  7049. org-complex-heading-regexp)))
  7050. (if orgstruct-mode
  7051. (list (length (match-string 1))
  7052. (org-reduced-level (length (match-string 1)))
  7053. nil
  7054. nil
  7055. (match-string 2)
  7056. nil)
  7057. (list (length (match-string 1))
  7058. (org-reduced-level (length (match-string 1)))
  7059. (org-match-string-no-properties 2)
  7060. (and (match-end 3) (aref (match-string 3) 2))
  7061. (org-match-string-no-properties 4)
  7062. (org-match-string-no-properties 5))))))
  7063. (defun org-get-entry ()
  7064. "Get the entry text, after heading, entire subtree."
  7065. (save-excursion
  7066. (org-back-to-heading t)
  7067. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7068. (defun org-insert-heading-after-current ()
  7069. "Insert a new heading with same level as current, after current subtree."
  7070. (interactive)
  7071. (org-back-to-heading)
  7072. (org-insert-heading)
  7073. (org-move-subtree-down)
  7074. (end-of-line 1))
  7075. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7076. "Insert heading with `org-insert-heading-respect-content' set to t."
  7077. (interactive)
  7078. (org-insert-heading '(4) invisible-ok))
  7079. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7080. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7081. (interactive)
  7082. (org-insert-todo-heading force-state '(4)))
  7083. (defun org-insert-todo-heading (arg &optional force-heading)
  7084. "Insert a new heading with the same level and TODO state as current heading.
  7085. If the heading has no TODO state, or if the state is DONE, use the first
  7086. state (TODO by default). Also with one prefix arg, force first state. With
  7087. two prefix args, force inserting at the end of the parent subtree."
  7088. (interactive "P")
  7089. (when (or force-heading (not (org-insert-item 'checkbox)))
  7090. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7091. force-heading))
  7092. (save-excursion
  7093. (org-back-to-heading)
  7094. (outline-previous-heading)
  7095. (looking-at org-todo-line-regexp))
  7096. (let*
  7097. ((new-mark-x
  7098. (if (or (equal arg '(4))
  7099. (not (match-beginning 2))
  7100. (member (match-string 2) org-done-keywords))
  7101. (car org-todo-keywords-1)
  7102. (match-string 2)))
  7103. (new-mark
  7104. (or
  7105. (run-hook-with-args-until-success
  7106. 'org-todo-get-default-hook new-mark-x nil)
  7107. new-mark-x)))
  7108. (beginning-of-line 1)
  7109. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7110. (if org-treat-insert-todo-heading-as-state-change
  7111. (org-todo new-mark)
  7112. (insert new-mark " "))))
  7113. (when org-provide-todo-statistics
  7114. (org-update-parent-todo-statistics))))
  7115. (defun org-insert-subheading (arg)
  7116. "Insert a new subheading and demote it.
  7117. Works for outline headings and for plain lists alike."
  7118. (interactive "P")
  7119. (org-insert-heading arg)
  7120. (cond
  7121. ((org-at-heading-p) (org-do-demote))
  7122. ((org-at-item-p) (org-indent-item))))
  7123. (defun org-insert-todo-subheading (arg)
  7124. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7125. Works for outline headings and for plain lists alike."
  7126. (interactive "P")
  7127. (org-insert-todo-heading arg)
  7128. (cond
  7129. ((org-at-heading-p) (org-do-demote))
  7130. ((org-at-item-p) (org-indent-item))))
  7131. ;;; Promotion and Demotion
  7132. (defvar org-after-demote-entry-hook nil
  7133. "Hook run after an entry has been demoted.
  7134. The cursor will be at the beginning of the entry.
  7135. When a subtree is being demoted, the hook will be called for each node.")
  7136. (defvar org-after-promote-entry-hook nil
  7137. "Hook run after an entry has been promoted.
  7138. The cursor will be at the beginning of the entry.
  7139. When a subtree is being promoted, the hook will be called for each node.")
  7140. (defun org-promote-subtree ()
  7141. "Promote the entire subtree.
  7142. See also `org-promote'."
  7143. (interactive)
  7144. (save-excursion
  7145. (org-with-limited-levels (org-map-tree 'org-promote)))
  7146. (org-fix-position-after-promote))
  7147. (defun org-demote-subtree ()
  7148. "Demote the entire subtree. See `org-demote'.
  7149. See also `org-promote'."
  7150. (interactive)
  7151. (save-excursion
  7152. (org-with-limited-levels (org-map-tree 'org-demote)))
  7153. (org-fix-position-after-promote))
  7154. (defun org-do-promote ()
  7155. "Promote the current heading higher up the tree.
  7156. If the region is active in `transient-mark-mode', promote all headings
  7157. in the region."
  7158. (interactive)
  7159. (save-excursion
  7160. (if (org-region-active-p)
  7161. (org-map-region 'org-promote (region-beginning) (region-end))
  7162. (org-promote)))
  7163. (org-fix-position-after-promote))
  7164. (defun org-do-demote ()
  7165. "Demote the current heading lower down the tree.
  7166. If the region is active in `transient-mark-mode', demote all headings
  7167. in the region."
  7168. (interactive)
  7169. (save-excursion
  7170. (if (org-region-active-p)
  7171. (org-map-region 'org-demote (region-beginning) (region-end))
  7172. (org-demote)))
  7173. (org-fix-position-after-promote))
  7174. (defun org-fix-position-after-promote ()
  7175. "Make sure that after pro/demotion cursor position is right."
  7176. (let ((pos (point)))
  7177. (when (save-excursion
  7178. (beginning-of-line 1)
  7179. (looking-at org-todo-line-regexp)
  7180. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7181. (cond ((eobp) (insert " "))
  7182. ((eolp) (insert " "))
  7183. ((equal (char-after) ?\ ) (forward-char 1))))))
  7184. (defun org-current-level ()
  7185. "Return the level of the current entry, or nil if before the first headline.
  7186. The level is the number of stars at the beginning of the headline."
  7187. (save-excursion
  7188. (org-with-limited-levels
  7189. (if (ignore-errors (org-back-to-heading t))
  7190. (funcall outline-level)))))
  7191. (defun org-get-previous-line-level ()
  7192. "Return the outline depth of the last headline before the current line.
  7193. Returns 0 for the first headline in the buffer, and nil if before the
  7194. first headline."
  7195. (and (org-current-level)
  7196. (or (and (/= (line-beginning-position) (point-min))
  7197. (save-excursion (beginning-of-line 0) (org-current-level)))
  7198. 0)))
  7199. (defun org-reduced-level (l)
  7200. "Compute the effective level of a heading.
  7201. This takes into account the setting of `org-odd-levels-only'."
  7202. (cond
  7203. ((zerop l) 0)
  7204. (org-odd-levels-only (1+ (floor (/ l 2))))
  7205. (t l)))
  7206. (defun org-level-increment ()
  7207. "Return the number of stars that will be added or removed at a
  7208. time to headlines when structure editing, based on the value of
  7209. `org-odd-levels-only'."
  7210. (if org-odd-levels-only 2 1))
  7211. (defun org-get-valid-level (level &optional change)
  7212. "Rectify a level change under the influence of `org-odd-levels-only'
  7213. LEVEL is a current level, CHANGE is by how much the level should be
  7214. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7215. even level numbers will become the next higher odd number."
  7216. (if org-odd-levels-only
  7217. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7218. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7219. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7220. (max 1 (+ level (or change 0)))))
  7221. (if (boundp 'define-obsolete-function-alias)
  7222. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7223. (define-obsolete-function-alias 'org-get-legal-level
  7224. 'org-get-valid-level)
  7225. (define-obsolete-function-alias 'org-get-legal-level
  7226. 'org-get-valid-level "23.1")))
  7227. (defun org-promote ()
  7228. "Promote the current heading higher up the tree.
  7229. If the region is active in `transient-mark-mode', promote all headings
  7230. in the region."
  7231. (org-back-to-heading t)
  7232. (let* ((level (save-match-data (funcall outline-level)))
  7233. (after-change-functions (remove 'flyspell-after-change-function
  7234. after-change-functions))
  7235. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7236. (diff (abs (- level (length up-head) -1))))
  7237. (cond ((and (= level 1) org-called-with-limited-levels
  7238. org-allow-promoting-top-level-subtree)
  7239. (replace-match "# " nil t))
  7240. ((= level 1)
  7241. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7242. (t (replace-match up-head nil t)))
  7243. ;; Fixup tag positioning
  7244. (unless (= level 1)
  7245. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7246. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7247. (run-hooks 'org-after-promote-entry-hook)))
  7248. (defun org-demote ()
  7249. "Demote the current heading lower down the tree.
  7250. If the region is active in `transient-mark-mode', demote all headings
  7251. in the region."
  7252. (org-back-to-heading t)
  7253. (let* ((level (save-match-data (funcall outline-level)))
  7254. (after-change-functions (remove 'flyspell-after-change-function
  7255. after-change-functions))
  7256. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7257. (diff (abs (- level (length down-head) -1))))
  7258. (replace-match down-head nil t)
  7259. ;; Fixup tag positioning
  7260. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7261. (if org-adapt-indentation (org-fixup-indentation diff))
  7262. (run-hooks 'org-after-demote-entry-hook)))
  7263. (defun org-cycle-level ()
  7264. "Cycle the level of an empty headline through possible states.
  7265. This goes first to child, then to parent, level, then up the hierarchy.
  7266. After top level, it switches back to sibling level."
  7267. (interactive)
  7268. (let ((org-adapt-indentation nil))
  7269. (when (org-point-at-end-of-empty-headline)
  7270. (setq this-command 'org-cycle-level) ; Only needed for caching
  7271. (let ((cur-level (org-current-level))
  7272. (prev-level (org-get-previous-line-level)))
  7273. (cond
  7274. ;; If first headline in file, promote to top-level.
  7275. ((= prev-level 0)
  7276. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7277. do (org-do-promote)))
  7278. ;; If same level as prev, demote one.
  7279. ((= prev-level cur-level)
  7280. (org-do-demote))
  7281. ;; If parent is top-level, promote to top level if not already.
  7282. ((= prev-level 1)
  7283. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7284. do (org-do-promote)))
  7285. ;; If top-level, return to prev-level.
  7286. ((= cur-level 1)
  7287. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7288. do (org-do-demote)))
  7289. ;; If less than prev-level, promote one.
  7290. ((< cur-level prev-level)
  7291. (org-do-promote))
  7292. ;; If deeper than prev-level, promote until higher than
  7293. ;; prev-level.
  7294. ((> cur-level prev-level)
  7295. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7296. do (org-do-promote))))
  7297. t))))
  7298. (defun org-map-tree (fun)
  7299. "Call FUN for every heading underneath the current one."
  7300. (org-back-to-heading)
  7301. (let ((level (funcall outline-level)))
  7302. (save-excursion
  7303. (funcall fun)
  7304. (while (and (progn
  7305. (outline-next-heading)
  7306. (> (funcall outline-level) level))
  7307. (not (eobp)))
  7308. (funcall fun)))))
  7309. (defun org-map-region (fun beg end)
  7310. "Call FUN for every heading between BEG and END."
  7311. (let ((org-ignore-region t))
  7312. (save-excursion
  7313. (setq end (copy-marker end))
  7314. (goto-char beg)
  7315. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7316. (< (point) end))
  7317. (funcall fun))
  7318. (while (and (progn
  7319. (outline-next-heading)
  7320. (< (point) end))
  7321. (not (eobp)))
  7322. (funcall fun)))))
  7323. (defvar org-property-end-re) ; silence byte-compiler
  7324. (defun org-fixup-indentation (diff)
  7325. "Change the indentation in the current entry by DIFF.
  7326. However, if any line in the current entry has no indentation, or if it
  7327. would end up with no indentation after the change, nothing at all is done."
  7328. (save-excursion
  7329. (let ((end (save-excursion (outline-next-heading)
  7330. (point-marker)))
  7331. (prohibit (if (> diff 0)
  7332. "^\\S-"
  7333. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7334. col)
  7335. (unless (save-excursion (end-of-line 1)
  7336. (re-search-forward prohibit end t))
  7337. (while (and (< (point) end)
  7338. (re-search-forward "^[ \t]+" end t))
  7339. (goto-char (match-end 0))
  7340. (setq col (current-column))
  7341. (if (< diff 0) (replace-match ""))
  7342. (org-indent-to-column (+ diff col))))
  7343. (move-marker end nil))))
  7344. (defun org-convert-to-odd-levels ()
  7345. "Convert an org-mode file with all levels allowed to one with odd levels.
  7346. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7347. level 5 etc."
  7348. (interactive)
  7349. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7350. (let ((outline-level 'org-outline-level)
  7351. (org-odd-levels-only nil) n)
  7352. (save-excursion
  7353. (goto-char (point-min))
  7354. (while (re-search-forward "^\\*\\*+ " nil t)
  7355. (setq n (- (length (match-string 0)) 2))
  7356. (while (>= (setq n (1- n)) 0)
  7357. (org-demote))
  7358. (end-of-line 1))))))
  7359. (defun org-convert-to-oddeven-levels ()
  7360. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7361. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7362. file contains a section with an even level, conversion would
  7363. destroy the structure of the file. An error is signaled in this
  7364. case."
  7365. (interactive)
  7366. (goto-char (point-min))
  7367. ;; First check if there are no even levels
  7368. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7369. (org-show-context t)
  7370. (error "Not all levels are odd in this file. Conversion not possible"))
  7371. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7372. (let ((outline-regexp org-outline-regexp)
  7373. (outline-level 'org-outline-level)
  7374. (org-odd-levels-only nil) n)
  7375. (save-excursion
  7376. (goto-char (point-min))
  7377. (while (re-search-forward "^\\*\\*+ " nil t)
  7378. (setq n (/ (1- (length (match-string 0))) 2))
  7379. (while (>= (setq n (1- n)) 0)
  7380. (org-promote))
  7381. (end-of-line 1))))))
  7382. (defun org-tr-level (n)
  7383. "Make N odd if required."
  7384. (if org-odd-levels-only (1+ (/ n 2)) n))
  7385. ;;; Vertical tree motion, cutting and pasting of subtrees
  7386. (defun org-move-subtree-up (&optional arg)
  7387. "Move the current subtree up past ARG headlines of the same level."
  7388. (interactive "p")
  7389. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7390. (defun org-move-subtree-down (&optional arg)
  7391. "Move the current subtree down past ARG headlines of the same level."
  7392. (interactive "p")
  7393. (setq arg (prefix-numeric-value arg))
  7394. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7395. 'org-get-last-sibling))
  7396. (ins-point (make-marker))
  7397. (cnt (abs arg))
  7398. (col (current-column))
  7399. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7400. ;; Select the tree
  7401. (org-back-to-heading)
  7402. (setq beg0 (point))
  7403. (save-excursion
  7404. (setq ne-beg (org-back-over-empty-lines))
  7405. (setq beg (point)))
  7406. (save-match-data
  7407. (save-excursion (outline-end-of-heading)
  7408. (setq folded (outline-invisible-p)))
  7409. (progn (org-end-of-subtree nil t)
  7410. (unless (eobp) (backward-char))))
  7411. (outline-next-heading)
  7412. (setq ne-end (org-back-over-empty-lines))
  7413. (setq end (point))
  7414. (goto-char beg0)
  7415. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7416. ;; include less whitespace
  7417. (save-excursion
  7418. (goto-char beg)
  7419. (forward-line (- ne-beg ne-end))
  7420. (setq beg (point))))
  7421. ;; Find insertion point, with error handling
  7422. (while (> cnt 0)
  7423. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7424. (progn (goto-char beg0)
  7425. (user-error "Cannot move past superior level or buffer limit")))
  7426. (setq cnt (1- cnt)))
  7427. (if (> arg 0)
  7428. ;; Moving forward - still need to move over subtree
  7429. (progn (org-end-of-subtree t t)
  7430. (save-excursion
  7431. (org-back-over-empty-lines)
  7432. (or (bolp) (newline)))))
  7433. (setq ne-ins (org-back-over-empty-lines))
  7434. (move-marker ins-point (point))
  7435. (setq txt (buffer-substring beg end))
  7436. (org-save-markers-in-region beg end)
  7437. (delete-region beg end)
  7438. (org-remove-empty-overlays-at beg)
  7439. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7440. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7441. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7442. (let ((bbb (point)))
  7443. (insert-before-markers txt)
  7444. (org-reinstall-markers-in-region bbb)
  7445. (move-marker ins-point bbb))
  7446. (or (bolp) (insert "\n"))
  7447. (setq ins-end (point))
  7448. (goto-char ins-point)
  7449. (org-skip-whitespace)
  7450. (when (and (< arg 0)
  7451. (org-first-sibling-p)
  7452. (> ne-ins ne-beg))
  7453. ;; Move whitespace back to beginning
  7454. (save-excursion
  7455. (goto-char ins-end)
  7456. (let ((kill-whole-line t))
  7457. (kill-line (- ne-ins ne-beg)) (point)))
  7458. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7459. (move-marker ins-point nil)
  7460. (if folded
  7461. (hide-subtree)
  7462. (org-show-entry)
  7463. (show-children)
  7464. (org-cycle-hide-drawers 'children))
  7465. (org-clean-visibility-after-subtree-move)
  7466. ;; move back to the initial column we were at
  7467. (move-to-column col)))
  7468. (defvar org-subtree-clip ""
  7469. "Clipboard for cut and paste of subtrees.
  7470. This is actually only a copy of the kill, because we use the normal kill
  7471. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7472. (defvar org-subtree-clip-folded nil
  7473. "Was the last copied subtree folded?
  7474. This is used to fold the tree back after pasting.")
  7475. (defun org-cut-subtree (&optional n)
  7476. "Cut the current subtree into the clipboard.
  7477. With prefix arg N, cut this many sequential subtrees.
  7478. This is a short-hand for marking the subtree and then cutting it."
  7479. (interactive "p")
  7480. (org-copy-subtree n 'cut))
  7481. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7482. "Copy the current subtree it in the clipboard.
  7483. With prefix arg N, copy this many sequential subtrees.
  7484. This is a short-hand for marking the subtree and then copying it.
  7485. If CUT is non-nil, actually cut the subtree.
  7486. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7487. of some markers in the region, even if CUT is non-nil. This is
  7488. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7489. (interactive "p")
  7490. (let (beg end folded (beg0 (point)))
  7491. (if (org-called-interactively-p 'any)
  7492. (org-back-to-heading nil) ; take what looks like a subtree
  7493. (org-back-to-heading t)) ; take what is really there
  7494. (setq beg (point))
  7495. (skip-chars-forward " \t\r\n")
  7496. (save-match-data
  7497. (if nosubtrees
  7498. (outline-next-heading)
  7499. (save-excursion (outline-end-of-heading)
  7500. (setq folded (outline-invisible-p)))
  7501. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7502. (org-end-of-subtree t t)))
  7503. (setq end (point))
  7504. (goto-char beg0)
  7505. (when (> end beg)
  7506. (setq org-subtree-clip-folded folded)
  7507. (when (or cut force-store-markers)
  7508. (org-save-markers-in-region beg end))
  7509. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7510. (setq org-subtree-clip (current-kill 0))
  7511. (message "%s: Subtree(s) with %d characters"
  7512. (if cut "Cut" "Copied")
  7513. (length org-subtree-clip)))))
  7514. (defun org-paste-subtree (&optional level tree for-yank remove)
  7515. "Paste the clipboard as a subtree, with modification of headline level.
  7516. The entire subtree is promoted or demoted in order to match a new headline
  7517. level.
  7518. If the cursor is at the beginning of a headline, the same level as
  7519. that headline is used to paste the tree.
  7520. If not, the new level is derived from the *visible* headings
  7521. before and after the insertion point, and taken to be the inferior headline
  7522. level of the two. So if the previous visible heading is level 3 and the
  7523. next is level 4 (or vice versa), level 4 will be used for insertion.
  7524. This makes sure that the subtree remains an independent subtree and does
  7525. not swallow low level entries.
  7526. You can also force a different level, either by using a numeric prefix
  7527. argument, or by inserting the heading marker by hand. For example, if the
  7528. cursor is after \"*****\", then the tree will be shifted to level 5.
  7529. If optional TREE is given, use this text instead of the kill ring.
  7530. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7531. move back over whitespace before inserting, and move point to the end of
  7532. the inserted text when done.
  7533. When REMOVE is non-nil, remove the subtree from the clipboard."
  7534. (interactive "P")
  7535. (setq tree (or tree (and kill-ring (current-kill 0))))
  7536. (unless (org-kill-is-subtree-p tree)
  7537. (user-error "%s"
  7538. (substitute-command-keys
  7539. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7540. (org-with-limited-levels
  7541. (let* ((visp (not (outline-invisible-p)))
  7542. (txt tree)
  7543. (^re_ "\\(\\*+\\)[ \t]*")
  7544. (old-level (if (string-match org-outline-regexp-bol txt)
  7545. (- (match-end 0) (match-beginning 0) 1)
  7546. -1))
  7547. (force-level (cond (level (prefix-numeric-value level))
  7548. ((and (looking-at "[ \t]*$")
  7549. (string-match
  7550. "^\\*+$" (buffer-substring
  7551. (point-at-bol) (point))))
  7552. (- (match-end 0) (match-beginning 0)))
  7553. ((and (bolp)
  7554. (looking-at org-outline-regexp))
  7555. (- (match-end 0) (point) 1))))
  7556. (previous-level (save-excursion
  7557. (condition-case nil
  7558. (progn
  7559. (outline-previous-visible-heading 1)
  7560. (if (looking-at ^re_)
  7561. (- (match-end 0) (match-beginning 0) 1)
  7562. 1))
  7563. (error 1))))
  7564. (next-level (save-excursion
  7565. (condition-case nil
  7566. (progn
  7567. (or (looking-at org-outline-regexp)
  7568. (outline-next-visible-heading 1))
  7569. (if (looking-at ^re_)
  7570. (- (match-end 0) (match-beginning 0) 1)
  7571. 1))
  7572. (error 1))))
  7573. (new-level (or force-level (max previous-level next-level)))
  7574. (shift (if (or (= old-level -1)
  7575. (= new-level -1)
  7576. (= old-level new-level))
  7577. 0
  7578. (- new-level old-level)))
  7579. (delta (if (> shift 0) -1 1))
  7580. (func (if (> shift 0) 'org-demote 'org-promote))
  7581. (org-odd-levels-only nil)
  7582. beg end newend)
  7583. ;; Remove the forced level indicator
  7584. (if force-level
  7585. (delete-region (point-at-bol) (point)))
  7586. ;; Paste
  7587. (beginning-of-line (if (bolp) 1 2))
  7588. (setq beg (point))
  7589. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7590. (insert-before-markers txt)
  7591. (unless (string-match "\n\\'" txt) (insert "\n"))
  7592. (setq newend (point))
  7593. (org-reinstall-markers-in-region beg)
  7594. (setq end (point))
  7595. (goto-char beg)
  7596. (skip-chars-forward " \t\n\r")
  7597. (setq beg (point))
  7598. (if (and (outline-invisible-p) visp)
  7599. (save-excursion (outline-show-heading)))
  7600. ;; Shift if necessary
  7601. (unless (= shift 0)
  7602. (save-restriction
  7603. (narrow-to-region beg end)
  7604. (while (not (= shift 0))
  7605. (org-map-region func (point-min) (point-max))
  7606. (setq shift (+ delta shift)))
  7607. (goto-char (point-min))
  7608. (setq newend (point-max))))
  7609. (when (or (org-called-interactively-p 'interactive) for-yank)
  7610. (message "Clipboard pasted as level %d subtree" new-level))
  7611. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7612. kill-ring
  7613. (eq org-subtree-clip (current-kill 0))
  7614. org-subtree-clip-folded)
  7615. ;; The tree was folded before it was killed/copied
  7616. (hide-subtree))
  7617. (and for-yank (goto-char newend))
  7618. (and remove (setq kill-ring (cdr kill-ring))))))
  7619. (defun org-kill-is-subtree-p (&optional txt)
  7620. "Check if the current kill is an outline subtree, or a set of trees.
  7621. Returns nil if kill does not start with a headline, or if the first
  7622. headline level is not the largest headline level in the tree.
  7623. So this will actually accept several entries of equal levels as well,
  7624. which is OK for `org-paste-subtree'.
  7625. If optional TXT is given, check this string instead of the current kill."
  7626. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7627. (re (org-get-limited-outline-regexp))
  7628. (^re (concat "^" re))
  7629. (start-level (and kill
  7630. (string-match
  7631. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7632. kill)
  7633. (- (match-end 2) (match-beginning 2) 1)))
  7634. (start (1+ (or (match-beginning 2) -1))))
  7635. (if (not start-level)
  7636. (progn
  7637. nil) ;; does not even start with a heading
  7638. (catch 'exit
  7639. (while (setq start (string-match ^re kill (1+ start)))
  7640. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7641. (throw 'exit nil)))
  7642. t))))
  7643. (defvar org-markers-to-move nil
  7644. "Markers that should be moved with a cut-and-paste operation.
  7645. Those markers are stored together with their positions relative to
  7646. the start of the region.")
  7647. (defun org-save-markers-in-region (beg end)
  7648. "Check markers in region.
  7649. If these markers are between BEG and END, record their position relative
  7650. to BEG, so that after moving the block of text, we can put the markers back
  7651. into place.
  7652. This function gets called just before an entry or tree gets cut from the
  7653. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7654. called immediately, to move the markers with the entries."
  7655. (setq org-markers-to-move nil)
  7656. (when (featurep 'org-clock)
  7657. (org-clock-save-markers-for-cut-and-paste beg end))
  7658. (when (featurep 'org-agenda)
  7659. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7660. (defun org-check-and-save-marker (marker beg end)
  7661. "Check if MARKER is between BEG and END.
  7662. If yes, remember the marker and the distance to BEG."
  7663. (when (and (marker-buffer marker)
  7664. (equal (marker-buffer marker) (current-buffer)))
  7665. (if (and (>= marker beg) (< marker end))
  7666. (push (cons marker (- marker beg)) org-markers-to-move))))
  7667. (defun org-reinstall-markers-in-region (beg)
  7668. "Move all remembered markers to their position relative to BEG."
  7669. (mapc (lambda (x)
  7670. (move-marker (car x) (+ beg (cdr x))))
  7671. org-markers-to-move)
  7672. (setq org-markers-to-move nil))
  7673. (defun org-narrow-to-subtree ()
  7674. "Narrow buffer to the current subtree."
  7675. (interactive)
  7676. (save-excursion
  7677. (save-match-data
  7678. (org-with-limited-levels
  7679. (narrow-to-region
  7680. (progn (org-back-to-heading t) (point))
  7681. (progn (org-end-of-subtree t t)
  7682. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7683. (point)))))))
  7684. (defun org-narrow-to-block ()
  7685. "Narrow buffer to the current block."
  7686. (interactive)
  7687. (let* ((case-fold-search t)
  7688. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7689. "^[ \t]*#\\+end_.*")))
  7690. (if blockp
  7691. (narrow-to-region (car blockp) (cdr blockp))
  7692. (user-error "Not in a block"))))
  7693. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7694. "Clone the task (subtree) at point N times.
  7695. The clones will be inserted as siblings.
  7696. In interactive use, the user will be prompted for the number of
  7697. clones to be produced. If the entry has a timestamp, the user
  7698. will also be prompted for a time shift, which may be a repeater
  7699. as used in time stamps, for example `+3d'. To disable this,
  7700. you can call the function with a universal prefix argument.
  7701. When a valid repeater is given and the entry contains any time
  7702. stamps, the clones will become a sequence in time, with time
  7703. stamps in the subtree shifted for each clone produced. If SHIFT
  7704. is nil or the empty string, time stamps will be left alone. The
  7705. ID property of the original subtree is removed.
  7706. If the original subtree did contain time stamps with a repeater,
  7707. the following will happen:
  7708. - the repeater will be removed in each clone
  7709. - an additional clone will be produced, with the current, unshifted
  7710. date(s) in the entry.
  7711. - the original entry will be placed *after* all the clones, with
  7712. repeater intact.
  7713. - the start days in the repeater in the original entry will be shifted
  7714. to past the last clone.
  7715. In this way you can spell out a number of instances of a repeating task,
  7716. and still retain the repeater to cover future instances of the task."
  7717. (interactive "nNumber of clones to produce: ")
  7718. (let ((shift
  7719. (or shift
  7720. (if (and (not (equal current-prefix-arg '(4)))
  7721. (save-excursion
  7722. (re-search-forward org-ts-regexp-both
  7723. (save-excursion
  7724. (org-end-of-subtree t)
  7725. (point)) t)))
  7726. (read-from-minibuffer
  7727. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7728. ""))) ;; No time shift
  7729. (n-no-remove -1)
  7730. (drawer-re org-drawer-regexp)
  7731. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7732. beg end template task idprop
  7733. shift-n shift-what doshift nmin nmax)
  7734. (if (not (and (integerp n) (> n 0)))
  7735. (user-error "Invalid number of replications %s" n))
  7736. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7737. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7738. shift)))
  7739. (user-error "Invalid shift specification %s" shift))
  7740. (when doshift
  7741. (setq shift-n (string-to-number (match-string 1 shift))
  7742. shift-what (cdr (assoc (match-string 2 shift)
  7743. '(("d" . day) ("w" . week)
  7744. ("m" . month) ("y" . year))))))
  7745. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7746. (setq nmin 1 nmax n)
  7747. (org-back-to-heading t)
  7748. (setq beg (point))
  7749. (setq idprop (org-entry-get nil "ID"))
  7750. (org-end-of-subtree t t)
  7751. (or (bolp) (insert "\n"))
  7752. (setq end (point))
  7753. (setq template (buffer-substring beg end))
  7754. (when (and doshift
  7755. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7756. (delete-region beg end)
  7757. (setq end beg)
  7758. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7759. (goto-char end)
  7760. (loop for n from nmin to nmax do
  7761. ;; prepare clone
  7762. (with-temp-buffer
  7763. (insert template)
  7764. (org-mode)
  7765. (goto-char (point-min))
  7766. (org-show-subtree)
  7767. (and idprop (if org-clone-delete-id
  7768. (org-entry-delete nil "ID")
  7769. (org-id-get-create t)))
  7770. (unless (= n 0)
  7771. (while (re-search-forward org-clock-re nil t)
  7772. (kill-whole-line))
  7773. (goto-char (point-min))
  7774. (while (re-search-forward drawer-re nil t)
  7775. (org-remove-empty-drawer-at (point))))
  7776. (goto-char (point-min))
  7777. (when doshift
  7778. (while (re-search-forward org-ts-regexp-both nil t)
  7779. (org-timestamp-change (* n shift-n) shift-what))
  7780. (unless (= n n-no-remove)
  7781. (goto-char (point-min))
  7782. (while (re-search-forward org-ts-regexp nil t)
  7783. (save-excursion
  7784. (goto-char (match-beginning 0))
  7785. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7786. (delete-region (match-beginning 1) (match-end 1)))))))
  7787. (setq task (buffer-string)))
  7788. (insert task))
  7789. (goto-char beg)))
  7790. ;;; Outline Sorting
  7791. (defun org-sort (with-case)
  7792. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7793. Optional argument WITH-CASE means sort case-sensitively."
  7794. (interactive "P")
  7795. (cond
  7796. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7797. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7798. (t
  7799. (org-call-with-arg 'org-sort-entries with-case))))
  7800. (defun org-sort-remove-invisible (s)
  7801. "Remove invisible links from string S."
  7802. (remove-text-properties 0 (length s) org-rm-props s)
  7803. (while (string-match org-bracket-link-regexp s)
  7804. (setq s (replace-match (if (match-end 2)
  7805. (match-string 3 s)
  7806. (match-string 1 s)) t t s)))
  7807. (let ((st (format " %s " s)))
  7808. (while (string-match org-emph-re st)
  7809. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7810. (setq s (substring st 1 -1)))
  7811. s)
  7812. (defvar org-priority-regexp) ; defined later in the file
  7813. (defvar org-after-sorting-entries-or-items-hook nil
  7814. "Hook that is run after a bunch of entries or items have been sorted.
  7815. When children are sorted, the cursor is in the parent line when this
  7816. hook gets called. When a region or a plain list is sorted, the cursor
  7817. will be in the first entry of the sorted region/list.")
  7818. (defun org-sort-entries
  7819. (&optional with-case sorting-type getkey-func compare-func property)
  7820. "Sort entries on a certain level of an outline tree.
  7821. If there is an active region, the entries in the region are sorted.
  7822. Else, if the cursor is before the first entry, sort the top-level items.
  7823. Else, the children of the entry at point are sorted.
  7824. Sorting can be alphabetically, numerically, by date/time as given by
  7825. a time stamp, by a property, by priority order, or by a custom function.
  7826. The command prompts for the sorting type unless it has been given to the
  7827. function through the SORTING-TYPE argument, which needs to be a character,
  7828. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7829. the precise meaning of each character:
  7830. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7831. c By creation time, which is assumed to be the first inactive time stamp
  7832. at the beginning of a line.
  7833. d By deadline date/time.
  7834. k By clocking time.
  7835. n Numerically, by converting the beginning of the entry/item to a number.
  7836. o By order of TODO keywords.
  7837. p By priority according to the cookie.
  7838. r By the value of a property.
  7839. s By scheduled date/time.
  7840. t By date/time, either the first active time stamp in the entry, or, if
  7841. none exist, by the first inactive one.
  7842. Capital letters will reverse the sort order.
  7843. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7844. called with point at the beginning of the record. It must return either
  7845. a string or a number that should serve as the sorting key for that record.
  7846. Comparing entries ignores case by default. However, with an optional argument
  7847. WITH-CASE, the sorting considers case as well.
  7848. Sorting is done against the visible part of the headlines, it ignores hidden
  7849. links.
  7850. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7851. (interactive "P")
  7852. (let ((case-func (if with-case 'identity 'downcase))
  7853. (cmstr
  7854. ;; The clock marker is lost when using `sort-subr', let's
  7855. ;; store the clocking string.
  7856. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7857. (save-excursion
  7858. (goto-char org-clock-marker)
  7859. (looking-back "^.*") (match-string-no-properties 0))))
  7860. start beg end stars re re2
  7861. txt what tmp)
  7862. ;; Find beginning and end of region to sort
  7863. (cond
  7864. ((org-region-active-p)
  7865. ;; we will sort the region
  7866. (setq end (region-end)
  7867. what "region")
  7868. (goto-char (region-beginning))
  7869. (if (not (org-at-heading-p)) (outline-next-heading))
  7870. (setq start (point)))
  7871. ((or (org-at-heading-p)
  7872. (ignore-errors (progn (org-back-to-heading) t)))
  7873. ;; we will sort the children of the current headline
  7874. (org-back-to-heading)
  7875. (setq start (point)
  7876. end (progn (org-end-of-subtree t t)
  7877. (or (bolp) (insert "\n"))
  7878. (when (>= (org-back-over-empty-lines) 1)
  7879. (forward-line 1))
  7880. (point))
  7881. what "children")
  7882. (goto-char start)
  7883. (show-subtree)
  7884. (outline-next-heading))
  7885. (t
  7886. ;; we will sort the top-level entries in this file
  7887. (goto-char (point-min))
  7888. (or (org-at-heading-p) (outline-next-heading))
  7889. (setq start (point))
  7890. (goto-char (point-max))
  7891. (beginning-of-line 1)
  7892. (when (looking-at ".*?\\S-")
  7893. ;; File ends in a non-white line
  7894. (end-of-line 1)
  7895. (insert "\n"))
  7896. (setq end (point-max))
  7897. (setq what "top-level")
  7898. (goto-char start)
  7899. (show-all)))
  7900. (setq beg (point))
  7901. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7902. (looking-at "\\(\\*+\\)")
  7903. (setq stars (match-string 1)
  7904. re (concat "^" (regexp-quote stars) " +")
  7905. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7906. txt (buffer-substring beg end))
  7907. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7908. (if (and (not (equal stars "*")) (string-match re2 txt))
  7909. (user-error "Region to sort contains a level above the first entry"))
  7910. (unless sorting-type
  7911. (message
  7912. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7913. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7914. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7915. what)
  7916. (setq sorting-type (read-char-exclusive))
  7917. (unless getkey-func
  7918. (and (= (downcase sorting-type) ?f)
  7919. (setq getkey-func
  7920. (org-icompleting-read "Sort using function: "
  7921. obarray 'fboundp t nil nil))
  7922. (setq getkey-func (intern getkey-func))))
  7923. (and (= (downcase sorting-type) ?r)
  7924. (not property)
  7925. (setq property
  7926. (org-icompleting-read "Property: "
  7927. (mapcar 'list (org-buffer-property-keys t))
  7928. nil t))))
  7929. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7930. (message "Sorting entries...")
  7931. (save-restriction
  7932. (narrow-to-region start end)
  7933. (let ((dcst (downcase sorting-type))
  7934. (case-fold-search nil)
  7935. (now (current-time)))
  7936. (sort-subr
  7937. (/= dcst sorting-type)
  7938. ;; This function moves to the beginning character of the "record" to
  7939. ;; be sorted.
  7940. (lambda nil
  7941. (if (re-search-forward re nil t)
  7942. (goto-char (match-beginning 0))
  7943. (goto-char (point-max))))
  7944. ;; This function moves to the last character of the "record" being
  7945. ;; sorted.
  7946. (lambda nil
  7947. (save-match-data
  7948. (condition-case nil
  7949. (outline-forward-same-level 1)
  7950. (error
  7951. (goto-char (point-max))))))
  7952. ;; This function returns the value that gets sorted against.
  7953. (lambda nil
  7954. (cond
  7955. ((= dcst ?n)
  7956. (if (looking-at org-complex-heading-regexp)
  7957. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7958. nil))
  7959. ((= dcst ?a)
  7960. (if (looking-at org-complex-heading-regexp)
  7961. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7962. nil))
  7963. ((= dcst ?k)
  7964. (or (get-text-property (point) :org-clock-minutes) 0))
  7965. ((= dcst ?t)
  7966. (let ((end (save-excursion (outline-next-heading) (point))))
  7967. (if (or (re-search-forward org-ts-regexp end t)
  7968. (re-search-forward org-ts-regexp-both end t))
  7969. (org-time-string-to-seconds (match-string 0))
  7970. (org-float-time now))))
  7971. ((= dcst ?c)
  7972. (let ((end (save-excursion (outline-next-heading) (point))))
  7973. (if (re-search-forward
  7974. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7975. end t)
  7976. (org-time-string-to-seconds (match-string 0))
  7977. (org-float-time now))))
  7978. ((= dcst ?s)
  7979. (let ((end (save-excursion (outline-next-heading) (point))))
  7980. (if (re-search-forward org-scheduled-time-regexp end t)
  7981. (org-time-string-to-seconds (match-string 1))
  7982. (org-float-time now))))
  7983. ((= dcst ?d)
  7984. (let ((end (save-excursion (outline-next-heading) (point))))
  7985. (if (re-search-forward org-deadline-time-regexp end t)
  7986. (org-time-string-to-seconds (match-string 1))
  7987. (org-float-time now))))
  7988. ((= dcst ?p)
  7989. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7990. (string-to-char (match-string 2))
  7991. org-default-priority))
  7992. ((= dcst ?r)
  7993. (or (org-entry-get nil property) ""))
  7994. ((= dcst ?o)
  7995. (if (looking-at org-complex-heading-regexp)
  7996. (let* ((m (match-string 2))
  7997. (s (if (member m org-done-keywords) '- '+)))
  7998. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  7999. ((= dcst ?f)
  8000. (if getkey-func
  8001. (progn
  8002. (setq tmp (funcall getkey-func))
  8003. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8004. tmp)
  8005. (error "Invalid key function `%s'" getkey-func)))
  8006. (t (error "Invalid sorting type `%c'" sorting-type))))
  8007. nil
  8008. (cond
  8009. ((= dcst ?a) 'string<)
  8010. ((= dcst ?f) compare-func)
  8011. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8012. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8013. ;; Reset the clock marker if needed
  8014. (when cmstr
  8015. (save-excursion
  8016. (goto-char start)
  8017. (search-forward cmstr nil t)
  8018. (move-marker org-clock-marker (point))))
  8019. (message "Sorting entries...done")))
  8020. (defun org-do-sort (table what &optional with-case sorting-type)
  8021. "Sort TABLE of WHAT according to SORTING-TYPE.
  8022. The user will be prompted for the SORTING-TYPE if the call to this
  8023. function does not specify it.
  8024. WHAT is only for the prompt, to indicate what is being sorted.
  8025. The sorting key will be extracted from the car of the elements of
  8026. the table.
  8027. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  8028. (unless sorting-type
  8029. (message
  8030. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  8031. what)
  8032. (setq sorting-type (read-char-exclusive)))
  8033. (let ((dcst (downcase sorting-type))
  8034. extractfun comparefun)
  8035. ;; Define the appropriate functions
  8036. (cond
  8037. ((= dcst ?n)
  8038. (setq extractfun 'string-to-number
  8039. comparefun (if (= dcst sorting-type) '< '>)))
  8040. ((= dcst ?a)
  8041. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  8042. (lambda(x) (downcase (org-sort-remove-invisible x))))
  8043. comparefun (if (= dcst sorting-type)
  8044. 'string<
  8045. (lambda (a b) (and (not (string< a b))
  8046. (not (string= a b)))))))
  8047. ((= dcst ?t)
  8048. (setq extractfun
  8049. (lambda (x)
  8050. (cond ((or (string-match org-ts-regexp x)
  8051. (string-match org-ts-regexp-both x))
  8052. (org-float-time
  8053. (org-time-string-to-time (match-string 0 x))))
  8054. ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
  8055. (org-hh:mm-string-to-minutes x))
  8056. (t 0)))
  8057. comparefun (if (= dcst sorting-type) '< '>)))
  8058. (t (error "Invalid sorting type `%c'" sorting-type)))
  8059. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  8060. table)
  8061. (lambda (a b) (funcall comparefun (car a) (car b))))))
  8062. ;;; The orgstruct minor mode
  8063. ;; Define a minor mode which can be used in other modes in order to
  8064. ;; integrate the org-mode structure editing commands.
  8065. ;; This is really a hack, because the org-mode structure commands use
  8066. ;; keys which normally belong to the major mode. Here is how it
  8067. ;; works: The minor mode defines all the keys necessary to operate the
  8068. ;; structure commands, but wraps the commands into a function which
  8069. ;; tests if the cursor is currently at a headline or a plain list
  8070. ;; item. If that is the case, the structure command is used,
  8071. ;; temporarily setting many Org-mode variables like regular
  8072. ;; expressions for filling etc. However, when any of those keys is
  8073. ;; used at a different location, function uses `key-binding' to look
  8074. ;; up if the key has an associated command in another currently active
  8075. ;; keymap (minor modes, major mode, global), and executes that
  8076. ;; command. There might be problems if any of the keys is otherwise
  8077. ;; used as a prefix key.
  8078. (defcustom orgstruct-heading-prefix-regexp ""
  8079. "Regexp that matches the custom prefix of Org headlines in
  8080. orgstruct(++)-mode."
  8081. :group 'org
  8082. :version "24.4"
  8083. :package-version '(Org . "8.3")
  8084. :type 'regexp)
  8085. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8086. (defcustom orgstruct-setup-hook nil
  8087. "Hook run after orgstruct-mode-map is filled."
  8088. :group 'org
  8089. :version "24.4"
  8090. :package-version '(Org . "8.0")
  8091. :type 'hook)
  8092. (defvar orgstruct-initialized nil)
  8093. (defvar org-local-vars nil
  8094. "List of local variables, for use by `orgstruct-mode'.")
  8095. ;;;###autoload
  8096. (define-minor-mode orgstruct-mode
  8097. "Toggle the minor mode `orgstruct-mode'.
  8098. This mode is for using Org-mode structure commands in other
  8099. modes. The following keys behave as if Org-mode were active, if
  8100. the cursor is on a headline, or on a plain list item (both as
  8101. defined by Org-mode)."
  8102. nil " OrgStruct" (make-sparse-keymap)
  8103. (funcall (if orgstruct-mode
  8104. 'add-to-invisibility-spec
  8105. 'remove-from-invisibility-spec)
  8106. '(outline . t))
  8107. (when orgstruct-mode
  8108. (org-load-modules-maybe)
  8109. (unless orgstruct-initialized
  8110. (orgstruct-setup)
  8111. (setq orgstruct-initialized t))))
  8112. ;;;###autoload
  8113. (defun turn-on-orgstruct ()
  8114. "Unconditionally turn on `orgstruct-mode'."
  8115. (orgstruct-mode 1))
  8116. (defvar org-fb-vars nil)
  8117. (make-variable-buffer-local 'org-fb-vars)
  8118. (defun orgstruct++-mode (&optional arg)
  8119. "Toggle `orgstruct-mode', the enhanced version of it.
  8120. In addition to setting orgstruct-mode, this also exports all
  8121. indentation and autofilling variables from org-mode into the
  8122. buffer. It will also recognize item context in multiline items."
  8123. (interactive "P")
  8124. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8125. (if (< arg 1)
  8126. (progn (orgstruct-mode -1)
  8127. (mapc (lambda(v)
  8128. (org-set-local (car v)
  8129. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8130. org-fb-vars))
  8131. (orgstruct-mode 1)
  8132. (setq org-fb-vars nil)
  8133. (unless org-local-vars
  8134. (setq org-local-vars (org-get-local-variables)))
  8135. (let (var val)
  8136. (mapc
  8137. (lambda (x)
  8138. (when (string-match
  8139. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8140. (symbol-name (car x)))
  8141. (setq var (car x) val (nth 1 x))
  8142. (push (list var `(quote ,(eval var))) org-fb-vars)
  8143. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8144. org-local-vars)
  8145. (org-set-local 'orgstruct-is-++ t))))
  8146. (defvar orgstruct-is-++ nil
  8147. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8148. (make-variable-buffer-local 'orgstruct-is-++)
  8149. ;;;###autoload
  8150. (defun turn-on-orgstruct++ ()
  8151. "Unconditionally turn on `orgstruct++-mode'."
  8152. (orgstruct++-mode 1))
  8153. (defun orgstruct-error ()
  8154. "Error when there is no default binding for a structure key."
  8155. (interactive)
  8156. (funcall (if (fboundp 'user-error)
  8157. 'user-error
  8158. 'error)
  8159. "This key has no function outside structure elements"))
  8160. (defun orgstruct-setup ()
  8161. "Setup orgstruct keymap."
  8162. (dolist (cell '((org-demote . t)
  8163. (org-metaleft . t)
  8164. (org-metaright . t)
  8165. (org-promote . t)
  8166. (org-shiftmetaleft . t)
  8167. (org-shiftmetaright . t)
  8168. org-backward-element
  8169. org-backward-heading-same-level
  8170. org-ctrl-c-ret
  8171. org-ctrl-c-minus
  8172. org-ctrl-c-star
  8173. org-cycle
  8174. org-forward-heading-same-level
  8175. org-insert-heading
  8176. org-insert-heading-respect-content
  8177. org-kill-note-or-show-branches
  8178. org-mark-subtree
  8179. org-meta-return
  8180. org-metadown
  8181. org-metaup
  8182. org-narrow-to-subtree
  8183. org-promote-subtree
  8184. org-reveal
  8185. org-shiftdown
  8186. org-shiftleft
  8187. org-shiftmetadown
  8188. org-shiftmetaup
  8189. org-shiftright
  8190. org-shifttab
  8191. org-shifttab
  8192. org-shiftup
  8193. org-show-subtree
  8194. org-sort
  8195. org-up-element
  8196. outline-demote
  8197. outline-next-visible-heading
  8198. outline-previous-visible-heading
  8199. outline-promote
  8200. outline-up-heading
  8201. show-children))
  8202. (let ((f (or (car-safe cell) cell))
  8203. (disable-when-heading-prefix (cdr-safe cell)))
  8204. (when (fboundp f)
  8205. (let ((new-bindings))
  8206. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8207. (where-is-internal f outline-mode-map)))
  8208. (push binding new-bindings)
  8209. ;; TODO use local-function-key-map
  8210. (dolist (rep '(("<tab>" . "TAB")
  8211. ("<return>" . "RET")
  8212. ("<escape>" . "ESC")
  8213. ("<delete>" . "DEL")))
  8214. (setq binding (read-kbd-macro
  8215. (let ((case-fold-search))
  8216. (replace-regexp-in-string
  8217. (regexp-quote (cdr rep))
  8218. (car rep)
  8219. (key-description binding)))))
  8220. (pushnew binding new-bindings :test 'equal)))
  8221. (dolist (binding new-bindings)
  8222. (let ((key (lookup-key orgstruct-mode-map binding)))
  8223. (when (or (not key) (numberp key))
  8224. (ignore-errors
  8225. (org-defkey orgstruct-mode-map
  8226. binding
  8227. (orgstruct-make-binding
  8228. f binding disable-when-heading-prefix))))))))))
  8229. (run-hooks 'orgstruct-setup-hook))
  8230. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8231. "Create a function for binding in the structure minor mode.
  8232. FUN is the command to call inside a table. KEY is the key that
  8233. should be checked in for a command to execute outside of tables.
  8234. Non-nil `disable-when-heading-prefix' means to disable the command
  8235. if `orgstruct-heading-prefix-regexp' is not empty."
  8236. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8237. (let ((nname name)
  8238. (i 0))
  8239. (while (fboundp (intern nname))
  8240. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8241. (setq name (intern nname)))
  8242. (eval
  8243. (let ((bindings '((org-heading-regexp
  8244. (concat "^"
  8245. orgstruct-heading-prefix-regexp
  8246. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8247. (org-outline-regexp
  8248. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8249. (org-outline-regexp-bol
  8250. (concat "^" org-outline-regexp))
  8251. (outline-regexp org-outline-regexp)
  8252. (outline-heading-end-regexp "\n")
  8253. (outline-level 'org-outline-level)
  8254. (outline-heading-alist))))
  8255. `(defun ,name (arg)
  8256. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8257. "Outside of structure, run the binding of `"
  8258. (key-description key) "'."
  8259. (when disable-when-heading-prefix
  8260. (concat
  8261. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8262. "back to the default binding due to limitations of Org's implementation of\n"
  8263. "`" (symbol-name fun) "'.")))
  8264. (interactive "p")
  8265. (let* ((disable
  8266. ,(and disable-when-heading-prefix
  8267. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8268. (fallback
  8269. (or disable
  8270. (not
  8271. (let* ,bindings
  8272. (org-context-p 'headline 'item
  8273. ,(when (memq fun
  8274. '(org-insert-heading
  8275. org-insert-heading-respect-content
  8276. org-meta-return))
  8277. '(when orgstruct-is-++
  8278. 'item-body))))))))
  8279. (if fallback
  8280. (let* ((orgstruct-mode)
  8281. (binding
  8282. (let ((key ,key))
  8283. (catch 'exit
  8284. (dolist
  8285. (rep
  8286. '(nil
  8287. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8288. ("<\\([^>]*\\)return>" . "\\1RET")
  8289. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8290. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8291. nil)
  8292. (when rep
  8293. (setq key (read-kbd-macro
  8294. (let ((case-fold-search))
  8295. (replace-regexp-in-string
  8296. (car rep)
  8297. (cdr rep)
  8298. (key-description key))))))
  8299. (when (key-binding key)
  8300. (throw 'exit (key-binding key))))))))
  8301. (if (keymapp binding)
  8302. (org-set-transient-map binding)
  8303. (let ((func (or binding
  8304. (unless disable
  8305. 'orgstruct-error))))
  8306. (when func
  8307. (call-interactively func)))))
  8308. (org-run-like-in-org-mode
  8309. (lambda ()
  8310. (interactive)
  8311. (let* ,bindings
  8312. (call-interactively ',fun)))))))))
  8313. name))
  8314. (defun org-contextualize-keys (alist contexts)
  8315. "Return valid elements in ALIST depending on CONTEXTS.
  8316. `org-agenda-custom-commands' or `org-capture-templates' are the
  8317. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8318. or `org-capture-templates-contexts' are the associated contexts
  8319. definitions."
  8320. (let ((contexts
  8321. ;; normalize contexts
  8322. (mapcar
  8323. (lambda(c) (cond ((listp (cadr c))
  8324. (list (car c) (car c) (cadr c)))
  8325. ((string= "" (cadr c))
  8326. (list (car c) (car c) (caddr c)))
  8327. (t c))) contexts))
  8328. (a alist) c r s)
  8329. ;; loop over all commands or templates
  8330. (while (setq c (pop a))
  8331. (let (vrules repl)
  8332. (cond
  8333. ((not (assoc (car c) contexts))
  8334. (push c r))
  8335. ((and (assoc (car c) contexts)
  8336. (setq vrules (org-contextualize-validate-key
  8337. (car c) contexts)))
  8338. (mapc (lambda (vr)
  8339. (when (not (equal (car vr) (cadr vr)))
  8340. (setq repl vr))) vrules)
  8341. (if (not repl) (push c r)
  8342. (push (cadr repl) s)
  8343. (push
  8344. (cons (car c)
  8345. (cdr (or (assoc (cadr repl) alist)
  8346. (error "Undefined key `%s' as contextual replacement for `%s'"
  8347. (cadr repl) (car c)))))
  8348. r))))))
  8349. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8350. (delq
  8351. nil
  8352. (delete-dups
  8353. (mapcar (lambda (x)
  8354. (let ((tpl (car x)))
  8355. (when (not (delq
  8356. nil
  8357. (mapcar (lambda(y)
  8358. (equal y tpl)) s))) x)))
  8359. (reverse r))))))
  8360. (defun org-contextualize-validate-key (key contexts)
  8361. "Check CONTEXTS for agenda or capture KEY."
  8362. (let (r rr res)
  8363. (while (setq r (pop contexts))
  8364. (mapc
  8365. (lambda (rr)
  8366. (when
  8367. (and (equal key (car r))
  8368. (if (functionp rr) (funcall rr)
  8369. (or (and (eq (car rr) 'in-file)
  8370. (buffer-file-name)
  8371. (string-match (cdr rr) (buffer-file-name)))
  8372. (and (eq (car rr) 'in-mode)
  8373. (string-match (cdr rr) (symbol-name major-mode)))
  8374. (and (eq (car rr) 'in-buffer)
  8375. (string-match (cdr rr) (buffer-name)))
  8376. (when (and (eq (car rr) 'not-in-file)
  8377. (buffer-file-name))
  8378. (not (string-match (cdr rr) (buffer-file-name))))
  8379. (when (eq (car rr) 'not-in-mode)
  8380. (not (string-match (cdr rr) (symbol-name major-mode))))
  8381. (when (eq (car rr) 'not-in-buffer)
  8382. (not (string-match (cdr rr) (buffer-name)))))))
  8383. (push r res)))
  8384. (car (last r))))
  8385. (delete-dups (delq nil res))))
  8386. (defun org-context-p (&rest contexts)
  8387. "Check if local context is any of CONTEXTS.
  8388. Possible values in the list of contexts are `table', `headline', and `item'."
  8389. (let ((pos (point)))
  8390. (goto-char (point-at-bol))
  8391. (prog1 (or (and (memq 'table contexts)
  8392. (looking-at "[ \t]*|"))
  8393. (and (memq 'headline contexts)
  8394. (looking-at org-outline-regexp))
  8395. (and (memq 'item contexts)
  8396. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8397. (and (memq 'item-body contexts)
  8398. (org-in-item-p)))
  8399. (goto-char pos))))
  8400. (defun org-get-local-variables ()
  8401. "Return a list of all local variables in an Org mode buffer."
  8402. (let (varlist)
  8403. (with-current-buffer (get-buffer-create "*Org tmp*")
  8404. (erase-buffer)
  8405. (org-mode)
  8406. (setq varlist (buffer-local-variables)))
  8407. (kill-buffer "*Org tmp*")
  8408. (delq nil
  8409. (mapcar
  8410. (lambda (x)
  8411. (setq x
  8412. (if (symbolp x)
  8413. (list x)
  8414. (list (car x) (cdr x))))
  8415. (if (and (not (get (car x) 'org-state))
  8416. (string-match
  8417. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8418. (symbol-name (car x))))
  8419. x nil))
  8420. varlist))))
  8421. (defun org-clone-local-variables (from-buffer &optional regexp)
  8422. "Clone local variables from FROM-BUFFER.
  8423. Optional argument REGEXP selects variables to clone."
  8424. (mapc
  8425. (lambda (pair)
  8426. (and (symbolp (car pair))
  8427. (or (null regexp)
  8428. (string-match regexp (symbol-name (car pair))))
  8429. (set (make-local-variable (car pair))
  8430. (cdr pair))))
  8431. (buffer-local-variables from-buffer)))
  8432. ;;;###autoload
  8433. (defun org-run-like-in-org-mode (cmd)
  8434. "Run a command, pretending that the current buffer is in Org-mode.
  8435. This will temporarily bind local variables that are typically bound in
  8436. Org-mode to the values they have in Org-mode, and then interactively
  8437. call CMD."
  8438. (org-load-modules-maybe)
  8439. (unless org-local-vars
  8440. (setq org-local-vars (org-get-local-variables)))
  8441. (let (binds)
  8442. (dolist (var org-local-vars)
  8443. (when (or (not (boundp (car var)))
  8444. (eq (symbol-value (car var))
  8445. (default-value (car var))))
  8446. (push (list (car var) `(quote ,(cadr var))) binds)))
  8447. (eval `(let ,binds
  8448. (call-interactively (quote ,cmd))))))
  8449. (defun org-get-category (&optional pos force-refresh)
  8450. "Get the category applying to position POS."
  8451. (save-match-data
  8452. (if force-refresh (org-refresh-category-properties))
  8453. (let ((pos (or pos (point))))
  8454. (or (get-text-property pos 'org-category)
  8455. (progn (org-refresh-category-properties)
  8456. (get-text-property pos 'org-category))))))
  8457. ;;; Refresh properties
  8458. (defun org-refresh-properties (dprop tprop)
  8459. "Refresh buffer text properties.
  8460. DPROP is the drawer property and TPROP is either the
  8461. corresponding text property to set, or an alist with each element
  8462. being a text property (as a symbol) and a function to apply to
  8463. the value of the drawer property."
  8464. (let ((case-fold-search t)
  8465. (inhibit-read-only t))
  8466. (org-with-silent-modifications
  8467. (save-excursion
  8468. (save-restriction
  8469. (widen)
  8470. (goto-char (point-min))
  8471. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8472. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8473. (defun org-refresh-property (tprop p)
  8474. "Refresh the buffer text property TPROP from the drawer property P.
  8475. The refresh happens only for the current tree (not subtree)."
  8476. (save-excursion
  8477. (org-back-to-heading t)
  8478. ;; tprop is a text property symbol
  8479. (if (symbolp tprop)
  8480. (put-text-property
  8481. (point) (or (outline-next-heading) (point-max)) tprop p)
  8482. ;; tprop is an alist with (properties . function) elements
  8483. (mapc (lambda(al)
  8484. (save-excursion
  8485. (put-text-property
  8486. (point-at-bol) (or (outline-next-heading) (point-max))
  8487. (car al)
  8488. (funcall (cdr al) p))))
  8489. tprop))))
  8490. (defun org-refresh-category-properties ()
  8491. "Refresh category text properties in the buffer."
  8492. (let ((case-fold-search t)
  8493. (inhibit-read-only t)
  8494. (def-cat (cond
  8495. ((null org-category)
  8496. (if buffer-file-name
  8497. (file-name-sans-extension
  8498. (file-name-nondirectory buffer-file-name))
  8499. "???"))
  8500. ((symbolp org-category) (symbol-name org-category))
  8501. (t org-category)))
  8502. beg end cat pos optionp)
  8503. (org-with-silent-modifications
  8504. (save-excursion
  8505. (save-restriction
  8506. (widen)
  8507. (goto-char (point-min))
  8508. (put-text-property (point) (point-max) 'org-category def-cat)
  8509. (while (re-search-forward
  8510. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8511. (setq pos (match-end 0)
  8512. optionp (equal (char-after (match-beginning 0)) ?#)
  8513. cat (org-trim (match-string 2)))
  8514. (if optionp
  8515. (setq beg (point-at-bol) end (point-max))
  8516. (org-back-to-heading t)
  8517. (setq beg (point) end (org-end-of-subtree t t)))
  8518. (put-text-property beg end 'org-category cat)
  8519. (goto-char pos)))))))
  8520. (defun org-refresh-stats-properties ()
  8521. "Refresh stats text properties in the buffer."
  8522. (let (stats)
  8523. (org-with-silent-modifications
  8524. (save-excursion
  8525. (save-restriction
  8526. (widen)
  8527. (goto-char (point-min))
  8528. (while (re-search-forward
  8529. (concat org-outline-regexp-bol ".*"
  8530. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8531. nil t)
  8532. (setq stats (cond ((equal (match-string 3) "0") 0)
  8533. ((match-string 2)
  8534. (/ (* (string-to-number (match-string 2)) 100)
  8535. (string-to-number (match-string 3))))
  8536. (t (string-to-number (match-string 1)))))
  8537. (org-back-to-heading t)
  8538. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8539. 'org-stats stats)))))))
  8540. (defun org-refresh-effort-properties ()
  8541. "Refresh effort properties"
  8542. (org-refresh-properties
  8543. org-effort-property
  8544. '((effort . identity)
  8545. (effort-minutes . org-duration-string-to-minutes))))
  8546. ;;;; Link Stuff
  8547. ;;; Link abbreviations
  8548. (defun org-link-expand-abbrev (link)
  8549. "Apply replacements as defined in `org-link-abbrev-alist'."
  8550. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8551. (let* ((key (match-string 1 link))
  8552. (as (or (assoc key org-link-abbrev-alist-local)
  8553. (assoc key org-link-abbrev-alist)))
  8554. (tag (and (match-end 2) (match-string 3 link)))
  8555. rpl)
  8556. (if (not as)
  8557. link
  8558. (setq rpl (cdr as))
  8559. (cond
  8560. ((symbolp rpl) (funcall rpl tag))
  8561. ((string-match "%(\\([^)]+\\))" rpl)
  8562. (replace-match
  8563. (save-match-data
  8564. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8565. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8566. ((string-match "%h" rpl)
  8567. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8568. (t (concat rpl tag)))))
  8569. link))
  8570. ;;; Storing and inserting links
  8571. (defvar org-insert-link-history nil
  8572. "Minibuffer history for links inserted with `org-insert-link'.")
  8573. (defvar org-stored-links nil
  8574. "Contains the links stored with `org-store-link'.")
  8575. (defvar org-store-link-plist nil
  8576. "Plist with info about the most recently link created with `org-store-link'.")
  8577. (defvar org-link-protocols nil
  8578. "Link protocols added to Org-mode using `org-add-link-type'.")
  8579. (defvar org-store-link-functions nil
  8580. "List of functions that are called to create and store a link.
  8581. Each function will be called in turn until one returns a non-nil
  8582. value. Each function should check if it is responsible for creating
  8583. this link (for example by looking at the major mode).
  8584. If not, it must exit and return nil.
  8585. If yes, it should return a non-nil value after a calling
  8586. `org-store-link-props' with a list of properties and values.
  8587. Special properties are:
  8588. :type The link prefix, like \"http\". This must be given.
  8589. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8590. This is obligatory as well.
  8591. :description Optional default description for the second pair
  8592. of brackets in an Org-mode link. The user can still change
  8593. this when inserting this link into an Org-mode buffer.
  8594. In addition to these, any additional properties can be specified
  8595. and then used in capture templates.")
  8596. (defun org-add-link-type (type &optional follow export)
  8597. "Add TYPE to the list of `org-link-types'.
  8598. Re-compute all regular expressions depending on `org-link-types'
  8599. FOLLOW and EXPORT are two functions.
  8600. FOLLOW should take the link path as the single argument and do whatever
  8601. is necessary to follow the link, for example find a file or display
  8602. a mail message.
  8603. EXPORT should format the link path for export to one of the export formats.
  8604. It should be a function accepting three arguments:
  8605. path the path of the link, the text after the prefix (like \"http:\")
  8606. desc the description of the link, if any, or a description added by
  8607. org-export-normalize-links if there is none
  8608. format the export format, a symbol like `html' or `latex' or `ascii'..
  8609. The function may use the FORMAT information to return different values
  8610. depending on the format. The return value will be put literally into
  8611. the exported file. If the return value is nil, this means Org should
  8612. do what it normally does with links which do not have EXPORT defined.
  8613. Org-mode has a built-in default for exporting links. If you are happy with
  8614. this default, there is no need to define an export function for the link
  8615. type. For a simple example of an export function, see `org-bbdb.el'."
  8616. (add-to-list 'org-link-types type t)
  8617. (org-make-link-regexps)
  8618. (if (assoc type org-link-protocols)
  8619. (setcdr (assoc type org-link-protocols) (list follow export))
  8620. (push (list type follow export) org-link-protocols)))
  8621. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8622. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8623. ;;;###autoload
  8624. (defun org-store-link (arg)
  8625. "\\<org-mode-map>Store an org-link to the current location.
  8626. This link is added to `org-stored-links' and can later be inserted
  8627. into an org-buffer with \\[org-insert-link].
  8628. For some link types, a prefix arg is interpreted.
  8629. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8630. For file links, arg negates `org-context-in-file-links'.
  8631. A double prefix arg force skipping storing functions that are not
  8632. part of Org's core.
  8633. A triple prefix arg force storing a link for each line in the
  8634. active region."
  8635. (interactive "P")
  8636. (org-load-modules-maybe)
  8637. (if (and (equal arg '(64)) (org-region-active-p))
  8638. (save-excursion
  8639. (let ((end (region-end)))
  8640. (goto-char (region-beginning))
  8641. (set-mark (point))
  8642. (while (< (point-at-eol) end)
  8643. (move-end-of-line 1) (activate-mark)
  8644. (let (current-prefix-arg)
  8645. (call-interactively 'org-store-link))
  8646. (move-beginning-of-line 2)
  8647. (set-mark (point)))))
  8648. (org-with-limited-levels
  8649. (setq org-store-link-plist nil)
  8650. (let (link cpltxt desc description search
  8651. txt custom-id agenda-link sfuns sfunsn)
  8652. (cond
  8653. ;; Store a link using an external link type
  8654. ((and (not (equal arg '(16)))
  8655. (setq sfuns
  8656. (delq
  8657. nil (mapcar (lambda (f)
  8658. (let (fs) (if (funcall f) (push f fs))))
  8659. org-store-link-functions))
  8660. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8661. (or (and (cdr sfuns)
  8662. (funcall (intern
  8663. (completing-read
  8664. "Which function for creating the link? "
  8665. sfunsn nil t (car sfunsn)))))
  8666. (funcall (caar sfuns)))
  8667. (setq link (plist-get org-store-link-plist :link)
  8668. desc (or (plist-get org-store-link-plist
  8669. :description) link))))
  8670. ;; Store a link from a source code buffer
  8671. ((org-src-edit-buffer-p)
  8672. (let (label gc)
  8673. (while (or (not label)
  8674. (save-excursion
  8675. (save-restriction
  8676. (widen)
  8677. (goto-char (point-min))
  8678. (re-search-forward
  8679. (regexp-quote (format org-coderef-label-format label))
  8680. nil t))))
  8681. (when label (message "Label exists already") (sit-for 2))
  8682. (setq label (read-string "Code line label: " label)))
  8683. (end-of-line 1)
  8684. (setq link (format org-coderef-label-format label))
  8685. (setq gc (- 79 (length link)))
  8686. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8687. (insert link)
  8688. (setq link (concat "(" label ")") desc nil)))
  8689. ;; We are in the agenda, link to referenced location
  8690. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8691. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8692. (get-text-property (point) 'org-marker))))
  8693. (when m
  8694. (org-with-point-at m
  8695. (setq agenda-link
  8696. (if (org-called-interactively-p 'any)
  8697. (call-interactively 'org-store-link)
  8698. (org-store-link nil)))))))
  8699. ((eq major-mode 'calendar-mode)
  8700. (let ((cd (calendar-cursor-to-date)))
  8701. (setq link
  8702. (format-time-string
  8703. (car org-time-stamp-formats)
  8704. (apply 'encode-time
  8705. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8706. nil nil nil))))
  8707. (org-store-link-props :type "calendar" :date cd)))
  8708. ((eq major-mode 'help-mode)
  8709. (setq link (concat "help:" (save-excursion
  8710. (goto-char (point-min))
  8711. (looking-at "^[^ ]+")
  8712. (match-string 0))))
  8713. (org-store-link-props :type "help"))
  8714. ((eq major-mode 'w3-mode)
  8715. (setq cpltxt (if (and (buffer-name)
  8716. (not (string-match "Untitled" (buffer-name))))
  8717. (buffer-name)
  8718. (url-view-url t))
  8719. link (url-view-url t))
  8720. (org-store-link-props :type "w3" :url (url-view-url t)))
  8721. ((eq major-mode 'image-mode)
  8722. (setq cpltxt (concat "file:"
  8723. (abbreviate-file-name buffer-file-name))
  8724. link cpltxt)
  8725. (org-store-link-props :type "image" :file buffer-file-name))
  8726. ;; In dired, store a link to the file of the current line
  8727. ((derived-mode-p 'dired-mode)
  8728. (let ((file (dired-get-filename nil t)))
  8729. (setq file (if file
  8730. (abbreviate-file-name
  8731. (expand-file-name (dired-get-filename nil t)))
  8732. ;; otherwise, no file so use current directory.
  8733. default-directory))
  8734. (setq cpltxt (concat "file:" file)
  8735. link cpltxt)))
  8736. ((setq search (run-hook-with-args-until-success
  8737. 'org-create-file-search-functions))
  8738. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8739. "::" search))
  8740. (setq cpltxt (or description link)))
  8741. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8742. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8743. (cond
  8744. ;; Store a link using the target at point
  8745. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8746. (setq cpltxt
  8747. (concat "file:"
  8748. (abbreviate-file-name
  8749. (buffer-file-name (buffer-base-buffer)))
  8750. "::" (match-string 1))
  8751. link cpltxt))
  8752. ((and (featurep 'org-id)
  8753. (or (eq org-id-link-to-org-use-id t)
  8754. (and (org-called-interactively-p 'any)
  8755. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8756. (and (eq org-id-link-to-org-use-id
  8757. 'create-if-interactive-and-no-custom-id)
  8758. (not custom-id))))
  8759. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8760. ;; Store a link using the ID at point
  8761. (setq link (condition-case nil
  8762. (prog1 (org-id-store-link)
  8763. (setq desc (or (plist-get org-store-link-plist
  8764. :description)
  8765. "")))
  8766. (error
  8767. ;; Probably before first headline, link only to file
  8768. (concat "file:"
  8769. (abbreviate-file-name
  8770. (buffer-file-name (buffer-base-buffer))))))))
  8771. (t
  8772. ;; Just link to current headline
  8773. (setq cpltxt (concat "file:"
  8774. (abbreviate-file-name
  8775. (buffer-file-name (buffer-base-buffer)))))
  8776. ;; Add a context search string
  8777. (when (org-xor org-context-in-file-links arg)
  8778. (let* ((ee (org-element-at-point))
  8779. (et (org-element-type ee))
  8780. (ev (plist-get (cadr ee) :value))
  8781. (ek (plist-get (cadr ee) :key))
  8782. (eok (and (stringp ek) (string-match "name" ek))))
  8783. (setq txt (cond
  8784. ((org-at-heading-p) nil)
  8785. ((and (eq et 'keyword) eok) ev)
  8786. ((org-region-active-p)
  8787. (buffer-substring (region-beginning) (region-end)))))
  8788. (when (or (null txt) (string-match "\\S-" txt))
  8789. (setq cpltxt
  8790. (concat cpltxt "::"
  8791. (condition-case nil
  8792. (org-make-org-heading-search-string txt)
  8793. (error "")))
  8794. desc (or (and (eq et 'keyword) eok ev)
  8795. (nth 4 (ignore-errors (org-heading-components)))
  8796. "NONE")))))
  8797. (if (string-match "::\\'" cpltxt)
  8798. (setq cpltxt (substring cpltxt 0 -2)))
  8799. (setq link cpltxt))))
  8800. ((buffer-file-name (buffer-base-buffer))
  8801. ;; Just link to this file here.
  8802. (setq cpltxt (concat "file:"
  8803. (abbreviate-file-name
  8804. (buffer-file-name (buffer-base-buffer)))))
  8805. ;; Add a context string.
  8806. (when (org-xor org-context-in-file-links arg)
  8807. (setq txt (if (org-region-active-p)
  8808. (buffer-substring (region-beginning) (region-end))
  8809. (buffer-substring (point-at-bol) (point-at-eol))))
  8810. ;; Only use search option if there is some text.
  8811. (when (string-match "\\S-" txt)
  8812. (setq cpltxt
  8813. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8814. desc "NONE")))
  8815. (setq link cpltxt))
  8816. ((org-called-interactively-p 'interactive)
  8817. (user-error "No method for storing a link from this buffer"))
  8818. (t (setq link nil)))
  8819. ;; We're done setting link and desc, clean up
  8820. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8821. (setq link (or link cpltxt)
  8822. desc (or desc cpltxt))
  8823. (cond ((equal desc "NONE") (setq desc nil))
  8824. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8825. (let ((d0 (match-string 3 desc))
  8826. (p0 (match-string 5 desc)))
  8827. (setq desc
  8828. (replace-regexp-in-string
  8829. org-bracket-link-regexp
  8830. (concat (or p0 d0)
  8831. (if (equal (length (match-string 0 desc))
  8832. (length desc)) "*" "")) desc)))))
  8833. ;; Return the link
  8834. (if (not (and (or (org-called-interactively-p 'any)
  8835. executing-kbd-macro) link))
  8836. (or agenda-link (and link (org-make-link-string link desc)))
  8837. (push (list link desc) org-stored-links)
  8838. (message "Stored: %s" (or desc link))
  8839. (when custom-id
  8840. (setq link (concat "file:" (abbreviate-file-name
  8841. (buffer-file-name)) "::#" custom-id))
  8842. (push (list link desc) org-stored-links))
  8843. (car org-stored-links))))))
  8844. (defun org-store-link-props (&rest plist)
  8845. "Store link properties, extract names and addresses."
  8846. (let (x adr)
  8847. (when (setq x (plist-get plist :from))
  8848. (setq adr (mail-extract-address-components x))
  8849. (setq plist (plist-put plist :fromname (car adr)))
  8850. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8851. (when (setq x (plist-get plist :to))
  8852. (setq adr (mail-extract-address-components x))
  8853. (setq plist (plist-put plist :toname (car adr)))
  8854. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8855. (let ((from (plist-get plist :from))
  8856. (to (plist-get plist :to)))
  8857. (when (and from to org-from-is-user-regexp)
  8858. (setq plist
  8859. (plist-put plist :fromto
  8860. (if (string-match org-from-is-user-regexp from)
  8861. (concat "to %t")
  8862. (concat "from %f"))))))
  8863. (setq org-store-link-plist plist))
  8864. (defun org-add-link-props (&rest plist)
  8865. "Add these properties to the link property list."
  8866. (let (key value)
  8867. (while plist
  8868. (setq key (pop plist) value (pop plist))
  8869. (setq org-store-link-plist
  8870. (plist-put org-store-link-plist key value)))))
  8871. (defun org-email-link-description (&optional fmt)
  8872. "Return the description part of an email link.
  8873. This takes information from `org-store-link-plist' and formats it
  8874. according to FMT (default from `org-email-link-description-format')."
  8875. (setq fmt (or fmt org-email-link-description-format))
  8876. (let* ((p org-store-link-plist)
  8877. (to (plist-get p :toaddress))
  8878. (from (plist-get p :fromaddress))
  8879. (table
  8880. (list
  8881. (cons "%c" (plist-get p :fromto))
  8882. (cons "%F" (plist-get p :from))
  8883. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8884. (cons "%T" (plist-get p :to))
  8885. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8886. (cons "%s" (plist-get p :subject))
  8887. (cons "%d" (plist-get p :date))
  8888. (cons "%m" (plist-get p :message-id)))))
  8889. (when (string-match "%c" fmt)
  8890. ;; Check if the user wrote this message
  8891. (if (and org-from-is-user-regexp from to
  8892. (save-match-data (string-match org-from-is-user-regexp from)))
  8893. (setq fmt (replace-match "to %t" t t fmt))
  8894. (setq fmt (replace-match "from %f" t t fmt))))
  8895. (org-replace-escapes fmt table)))
  8896. (defun org-make-org-heading-search-string (&optional string)
  8897. "Make search string for the current headline or STRING."
  8898. (let ((s (or string
  8899. (and (derived-mode-p 'org-mode)
  8900. (save-excursion
  8901. (org-back-to-heading t)
  8902. (org-element-property :raw-value (org-element-at-point))))))
  8903. (lines org-context-in-file-links))
  8904. (or string (setq s (concat "*" s))) ; Add * for headlines
  8905. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8906. (when (and string (integerp lines) (> lines 0))
  8907. (let ((slines (org-split-string s "\n")))
  8908. (when (< lines (length slines))
  8909. (setq s (mapconcat
  8910. 'identity
  8911. (reverse (nthcdr (- (length slines) lines)
  8912. (reverse slines))) "\n")))))
  8913. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8914. (defun org-make-link-string (link &optional description)
  8915. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8916. (unless (string-match "\\S-" link)
  8917. (error "Empty link"))
  8918. (when (and description
  8919. (stringp description)
  8920. (not (string-match "\\S-" description)))
  8921. (setq description nil))
  8922. (when (stringp description)
  8923. ;; Remove brackets from the description, they are fatal.
  8924. (while (string-match "\\[" description)
  8925. (setq description (replace-match "{" t t description)))
  8926. (while (string-match "\\]" description)
  8927. (setq description (replace-match "}" t t description))))
  8928. (when (equal link description)
  8929. ;; No description needed, it is identical
  8930. (setq description nil))
  8931. (when (and (not description)
  8932. (not (string-match (org-image-file-name-regexp) link))
  8933. (not (equal link (org-link-escape link))))
  8934. (setq description (org-extract-attributes link)))
  8935. (setq link
  8936. (cond ((string-match (org-image-file-name-regexp) link) link)
  8937. ((string-match org-link-types-re link)
  8938. (concat (match-string 1 link)
  8939. (org-link-escape (substring link (match-end 1)))))
  8940. (t (org-link-escape link))))
  8941. (concat "[[" link "]"
  8942. (if description (concat "[" description "]") "")
  8943. "]"))
  8944. (defconst org-link-escape-chars
  8945. ;;%20 %5B %5D
  8946. '(?\ ?\[ ?\])
  8947. "List of characters that should be escaped in a link when stored to Org.
  8948. This is the list that is used for internal purposes.")
  8949. (defconst org-link-escape-chars-browser
  8950. ;;%20 %22
  8951. '(?\ ?\")
  8952. "List of characters to be escaped before handing over to the browser.
  8953. If you consider using this constant then you probably want to use
  8954. the function `org-link-escape-browser' instead. See there why
  8955. this constant is a candidate to be removed once Org drops support
  8956. for Emacs 24.1 and 24.2.")
  8957. (defun org-link-escape (text &optional table merge)
  8958. "Return percent escaped representation of TEXT.
  8959. TEXT is a string with the text to escape.
  8960. Optional argument TABLE is a list with characters that should be
  8961. escaped. When nil, `org-link-escape-chars' is used.
  8962. If optional argument MERGE is set, merge TABLE into
  8963. `org-link-escape-chars'."
  8964. ;; Don't escape chars in internal links
  8965. (if (string-match "^\\*[[:alnum:]]+" text)
  8966. text
  8967. (cond
  8968. ((and table merge)
  8969. (mapc (lambda (defchr)
  8970. (unless (member defchr table)
  8971. (setq table (cons defchr table))))
  8972. org-link-escape-chars))
  8973. ((null table)
  8974. (setq table org-link-escape-chars)))
  8975. (mapconcat
  8976. (lambda (char)
  8977. (if (or (member char table)
  8978. (and (or (< char 32) (= char ?\%) (> char 126))
  8979. org-url-hexify-p))
  8980. (mapconcat (lambda (sequence-element)
  8981. (format "%%%.2X" sequence-element))
  8982. (or (encode-coding-char char 'utf-8)
  8983. (error "Unable to percent escape character: %s"
  8984. (char-to-string char))) "")
  8985. (char-to-string char))) text "")))
  8986. (defun org-link-escape-browser (text)
  8987. "Escape some characters before handing over to the browser.
  8988. This function is a candidate to be removed together with the
  8989. constant `org-link-escape-chars-browser' once Org drops support
  8990. for Emacs 24.1 and 24.2. All calls to this function will have to
  8991. be replaced with `url-encode-url' which is available since Emacs
  8992. 24.3.1."
  8993. ;; Example with the Org link
  8994. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  8995. ;; to open the browser with +subject:"Release 8.2" filled into the
  8996. ;; query field: In this case the variable TEXT contains the
  8997. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  8998. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  8999. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9000. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9001. (if (fboundp 'url-encode-url)
  9002. (url-encode-url text)
  9003. (if (org-string-match-p
  9004. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9005. text)
  9006. (org-link-escape text org-link-escape-chars-browser)
  9007. text)))
  9008. (defun org-link-unescape (str)
  9009. "Unhex hexified Unicode strings as returned from the JavaScript function
  9010. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9011. (unless (and (null str) (string= "" str))
  9012. (let ((pos 0) (case-fold-search t) unhexed)
  9013. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9014. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9015. (setq str (replace-match unhexed t t str))
  9016. (setq pos (+ pos (length unhexed))))))
  9017. str)
  9018. (defun org-link-unescape-compound (hex)
  9019. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9020. Note: this function also decodes single byte encodings like
  9021. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9022. (save-match-data
  9023. (let* ((bytes (cdr (split-string hex "%")))
  9024. (ret "")
  9025. (eat 0)
  9026. (sum 0))
  9027. (while bytes
  9028. (let* ((val (string-to-number (pop bytes) 16))
  9029. (shift-xor
  9030. (if (= 0 eat)
  9031. (cond
  9032. ((>= val 252) (cons 6 252))
  9033. ((>= val 248) (cons 5 248))
  9034. ((>= val 240) (cons 4 240))
  9035. ((>= val 224) (cons 3 224))
  9036. ((>= val 192) (cons 2 192))
  9037. (t (cons 0 0)))
  9038. (cons 6 128))))
  9039. (if (>= val 192) (setq eat (car shift-xor)))
  9040. (setq val (logxor val (cdr shift-xor)))
  9041. (setq sum (+ (lsh sum (car shift-xor)) val))
  9042. (if (> eat 0) (setq eat (- eat 1)))
  9043. (cond
  9044. ((= 0 eat) ;multi byte
  9045. (setq ret (concat ret (org-char-to-string sum)))
  9046. (setq sum 0))
  9047. ((not bytes) ; single byte(s)
  9048. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9049. ret)))
  9050. (defun org-link-unescape-single-byte-sequence (hex)
  9051. "Unhexify hex-encoded single byte character sequences."
  9052. (mapconcat (lambda (byte)
  9053. (char-to-string (string-to-number byte 16)))
  9054. (cdr (split-string hex "%")) ""))
  9055. (defun org-xor (a b)
  9056. "Exclusive or."
  9057. (if a (not b) b))
  9058. (defun org-fixup-message-id-for-http (s)
  9059. "Replace special characters in a message id, so it can be used in an http query."
  9060. (when (string-match "%" s)
  9061. (setq s (mapconcat (lambda (c)
  9062. (if (eq c ?%)
  9063. "%25"
  9064. (char-to-string c)))
  9065. s "")))
  9066. (while (string-match "<" s)
  9067. (setq s (replace-match "%3C" t t s)))
  9068. (while (string-match ">" s)
  9069. (setq s (replace-match "%3E" t t s)))
  9070. (while (string-match "@" s)
  9071. (setq s (replace-match "%40" t t s)))
  9072. s)
  9073. (defun org-link-prettify (link)
  9074. "Return a human-readable representation of LINK.
  9075. The car of LINK must be a raw link.
  9076. The cdr of LINK must be either a link description or nil."
  9077. (let ((desc (or (cadr link) "<no description>")))
  9078. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9079. "<" (car link) ">")))
  9080. ;;;###autoload
  9081. (defun org-insert-link-global ()
  9082. "Insert a link like Org-mode does.
  9083. This command can be called in any mode to insert a link in Org-mode syntax."
  9084. (interactive)
  9085. (org-load-modules-maybe)
  9086. (org-run-like-in-org-mode 'org-insert-link))
  9087. (defun org-insert-all-links (arg &optional pre post)
  9088. "Insert all links in `org-stored-links'.
  9089. When a universal prefix, do not delete the links from `org-stored-links'.
  9090. When `ARG' is a number, insert the last N link(s).
  9091. `PRE' and `POST' are optional arguments to define a string to
  9092. prepend or to append."
  9093. (interactive "P")
  9094. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9095. (links (copy-seq org-stored-links))
  9096. (pr (or pre "- "))
  9097. (po (or post "\n"))
  9098. (cnt 1) l)
  9099. (if (null org-stored-links)
  9100. (message "No link to insert")
  9101. (while (and (or (listp arg) (>= arg cnt))
  9102. (setq l (if (listp arg)
  9103. (pop links)
  9104. (pop org-stored-links))))
  9105. (setq cnt (1+ cnt))
  9106. (insert pr)
  9107. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9108. (insert po)))))
  9109. (defun org-insert-last-stored-link (arg)
  9110. "Insert the last link stored in `org-stored-links'."
  9111. (interactive "p")
  9112. (org-insert-all-links arg "" "\n"))
  9113. (defun org-link-fontify-links-to-this-file ()
  9114. "Fontify links to the current file in `org-stored-links'."
  9115. (let ((f (buffer-file-name)) a b)
  9116. (setq a (mapcar (lambda(l)
  9117. (let ((ll (car l)))
  9118. (when (and (string-match "^file:\\(.+\\)::" ll)
  9119. (equal f (expand-file-name (match-string 1 ll))))
  9120. ll)))
  9121. org-stored-links))
  9122. (when (featurep 'org-id)
  9123. (setq b (mapcar (lambda(l)
  9124. (let ((ll (car l)))
  9125. (when (and (string-match "^id:\\(.+\\)$" ll)
  9126. (equal f (expand-file-name
  9127. (or (org-id-find-id-file
  9128. (match-string 1 ll)) ""))))
  9129. ll)))
  9130. org-stored-links)))
  9131. (mapcar (lambda(l)
  9132. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9133. (delq nil (append a b)))))
  9134. (defvar org-link-links-in-this-file nil)
  9135. (defun org-insert-link (&optional complete-file link-location default-description)
  9136. "Insert a link. At the prompt, enter the link.
  9137. Completion can be used to insert any of the link protocol prefixes like
  9138. http or ftp in use.
  9139. The history can be used to select a link previously stored with
  9140. `org-store-link'. When the empty string is entered (i.e. if you just
  9141. press RET at the prompt), the link defaults to the most recently
  9142. stored link. As SPC triggers completion in the minibuffer, you need to
  9143. use M-SPC or C-q SPC to force the insertion of a space character.
  9144. You will also be prompted for a description, and if one is given, it will
  9145. be displayed in the buffer instead of the link.
  9146. If there is already a link at point, this command will allow you to edit link
  9147. and description parts.
  9148. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9149. be selected using completion. The path to the file will be relative to the
  9150. current directory if the file is in the current directory or a subdirectory.
  9151. Otherwise, the link will be the absolute path as completed in the minibuffer
  9152. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9153. option `org-link-file-path-type'.
  9154. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9155. the current directory or below.
  9156. With three \\[universal-argument] prefixes, negate the meaning of
  9157. `org-keep-stored-link-after-insertion'.
  9158. If `org-make-link-description-function' is non-nil, this function will be
  9159. called with the link target, and the result will be the default
  9160. link description.
  9161. If the LINK-LOCATION parameter is non-nil, this value will be
  9162. used as the link location instead of reading one interactively.
  9163. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9164. be used as the default description."
  9165. (interactive "P")
  9166. (let* ((wcf (current-window-configuration))
  9167. (origbuf (current-buffer))
  9168. (region (if (org-region-active-p)
  9169. (buffer-substring (region-beginning) (region-end))))
  9170. (remove (and region (list (region-beginning) (region-end))))
  9171. (desc region)
  9172. tmphist ; byte-compile incorrectly complains about this
  9173. (link link-location)
  9174. (abbrevs org-link-abbrev-alist-local)
  9175. entry file all-prefixes auto-desc)
  9176. (cond
  9177. (link-location) ; specified by arg, just use it.
  9178. ((org-in-regexp org-bracket-link-regexp 1)
  9179. ;; We do have a link at point, and we are going to edit it.
  9180. (setq remove (list (match-beginning 0) (match-end 0)))
  9181. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9182. (setq link (read-string "Link: "
  9183. (org-link-unescape
  9184. (org-match-string-no-properties 1)))))
  9185. ((or (org-in-regexp org-angle-link-re)
  9186. (org-in-regexp org-plain-link-re))
  9187. ;; Convert to bracket link
  9188. (setq remove (list (match-beginning 0) (match-end 0))
  9189. link (read-string "Link: "
  9190. (org-remove-angle-brackets (match-string 0)))))
  9191. ((member complete-file '((4) (16)))
  9192. ;; Completing read for file names.
  9193. (setq link (org-file-complete-link complete-file)))
  9194. (t
  9195. ;; Read link, with completion for stored links.
  9196. (org-link-fontify-links-to-this-file)
  9197. (org-switch-to-buffer-other-window "*Org Links*")
  9198. (with-current-buffer "*Org Links*"
  9199. (erase-buffer)
  9200. (insert "Insert a link.
  9201. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9202. (when org-stored-links
  9203. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9204. (insert (mapconcat 'org-link-prettify
  9205. (reverse org-stored-links) "\n")))
  9206. (goto-char (point-min)))
  9207. (let ((cw (selected-window)))
  9208. (select-window (get-buffer-window "*Org Links*" 'visible))
  9209. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9210. (unless (pos-visible-in-window-p (point-max))
  9211. (org-fit-window-to-buffer))
  9212. (and (window-live-p cw) (select-window cw)))
  9213. ;; Fake a link history, containing the stored links.
  9214. (setq tmphist (append (mapcar 'car org-stored-links)
  9215. org-insert-link-history))
  9216. (setq all-prefixes (append (mapcar 'car abbrevs)
  9217. (mapcar 'car org-link-abbrev-alist)
  9218. org-link-types))
  9219. (unwind-protect
  9220. (progn
  9221. (setq link
  9222. (org-completing-read
  9223. "Link: "
  9224. (append
  9225. (mapcar (lambda (x) (concat x ":"))
  9226. all-prefixes)
  9227. (mapcar 'car org-stored-links))
  9228. nil nil nil
  9229. 'tmphist
  9230. (caar org-stored-links)))
  9231. (if (not (string-match "\\S-" link))
  9232. (user-error "No link selected"))
  9233. (mapc (lambda(l)
  9234. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9235. org-stored-links)
  9236. (if (or (member link all-prefixes)
  9237. (and (equal ":" (substring link -1))
  9238. (member (substring link 0 -1) all-prefixes)
  9239. (setq link (substring link 0 -1))))
  9240. (setq link (with-current-buffer origbuf
  9241. (org-link-try-special-completion link)))))
  9242. (set-window-configuration wcf)
  9243. (kill-buffer "*Org Links*"))
  9244. (setq entry (assoc link org-stored-links))
  9245. (or entry (push link org-insert-link-history))
  9246. (setq desc (or desc (nth 1 entry)))))
  9247. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9248. (not org-keep-stored-link-after-insertion))
  9249. (setq org-stored-links (delq (assoc link org-stored-links)
  9250. org-stored-links)))
  9251. (if (and (string-match org-plain-link-re link)
  9252. (not (string-match org-ts-regexp link)))
  9253. ;; URL-like link, normalize the use of angular brackets.
  9254. (setq link (org-remove-angle-brackets link)))
  9255. ;; Check if we are linking to the current file with a search
  9256. ;; option If yes, simplify the link by using only the search
  9257. ;; option.
  9258. (when (and buffer-file-name
  9259. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9260. (let* ((path (match-string 1 link))
  9261. (case-fold-search nil)
  9262. (search (match-string 2 link)))
  9263. (save-match-data
  9264. (if (equal (file-truename buffer-file-name) (file-truename path))
  9265. ;; We are linking to this same file, with a search option
  9266. (setq link search)))))
  9267. ;; Check if we can/should use a relative path. If yes, simplify the link
  9268. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9269. (let* ((type (match-string 1 link))
  9270. (path (match-string 2 link))
  9271. (origpath path)
  9272. (case-fold-search nil))
  9273. (cond
  9274. ((or (eq org-link-file-path-type 'absolute)
  9275. (equal complete-file '(16)))
  9276. (setq path (abbreviate-file-name (expand-file-name path))))
  9277. ((eq org-link-file-path-type 'noabbrev)
  9278. (setq path (expand-file-name path)))
  9279. ((eq org-link-file-path-type 'relative)
  9280. (setq path (file-relative-name path)))
  9281. (t
  9282. (save-match-data
  9283. (if (string-match (concat "^" (regexp-quote
  9284. (expand-file-name
  9285. (file-name-as-directory
  9286. default-directory))))
  9287. (expand-file-name path))
  9288. ;; We are linking a file with relative path name.
  9289. (setq path (substring (expand-file-name path)
  9290. (match-end 0)))
  9291. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9292. (setq link (concat type path))
  9293. (if (equal desc origpath)
  9294. (setq desc path))))
  9295. (if org-make-link-description-function
  9296. (setq desc
  9297. (or (condition-case nil
  9298. (funcall org-make-link-description-function link desc)
  9299. (error (progn (message "Can't get link description from `%s'"
  9300. (symbol-name org-make-link-description-function))
  9301. (sit-for 2) nil)))
  9302. (read-string "Description: " default-description)))
  9303. (if default-description (setq desc default-description)
  9304. (setq desc (or (and auto-desc desc)
  9305. (read-string "Description: " desc)))))
  9306. (unless (string-match "\\S-" desc) (setq desc nil))
  9307. (if remove (apply 'delete-region remove))
  9308. (insert (org-make-link-string link desc))))
  9309. (defun org-link-try-special-completion (type)
  9310. "If there is completion support for link type TYPE, offer it."
  9311. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9312. (if (functionp fun)
  9313. (funcall fun)
  9314. (read-string "Link (no completion support): " (concat type ":")))))
  9315. (defun org-file-complete-link (&optional arg)
  9316. "Create a file link using completion."
  9317. (let (file link)
  9318. (setq file (org-iread-file-name "File: "))
  9319. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9320. (pwd1 (file-name-as-directory (abbreviate-file-name
  9321. (expand-file-name ".")))))
  9322. (cond
  9323. ((equal arg '(16))
  9324. (setq link (concat
  9325. "file:"
  9326. (abbreviate-file-name (expand-file-name file)))))
  9327. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9328. (setq link (concat "file:" (match-string 1 file))))
  9329. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9330. (expand-file-name file))
  9331. (setq link (concat
  9332. "file:" (match-string 1 (expand-file-name file)))))
  9333. (t (setq link (concat "file:" file)))))
  9334. link))
  9335. (defun org-iread-file-name (&rest args)
  9336. "Read-file-name using `ido-mode' speedup if available.
  9337. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9338. See `read-file-name' for a description of parameters."
  9339. (org-without-partial-completion
  9340. (if (and org-completion-use-ido
  9341. (fboundp 'ido-read-file-name)
  9342. (boundp 'ido-mode) ido-mode
  9343. (listp (second args)))
  9344. (let ((ido-enter-matching-directory nil))
  9345. (apply 'ido-read-file-name args))
  9346. (apply 'read-file-name args))))
  9347. (defun org-completing-read (&rest args)
  9348. "Completing-read with SPACE being a normal character."
  9349. (let ((enable-recursive-minibuffers t)
  9350. (minibuffer-local-completion-map
  9351. (copy-keymap minibuffer-local-completion-map)))
  9352. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9353. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9354. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9355. (apply 'org-icompleting-read args)))
  9356. (defun org-completing-read-no-i (&rest args)
  9357. (let (org-completion-use-ido org-completion-use-iswitchb)
  9358. (apply 'org-completing-read args)))
  9359. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9360. "Use iswitch as a completing-read replacement to choose from choices.
  9361. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9362. from."
  9363. (let* ((iswitchb-use-virtual-buffers nil)
  9364. (iswitchb-make-buflist-hook
  9365. (lambda ()
  9366. (setq iswitchb-temp-buflist choices))))
  9367. (iswitchb-read-buffer prompt)))
  9368. (defun org-icompleting-read (&rest args)
  9369. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9370. (org-without-partial-completion
  9371. (if (and org-completion-use-ido
  9372. (fboundp 'ido-completing-read)
  9373. (boundp 'ido-mode) ido-mode
  9374. (listp (second args)))
  9375. (let ((ido-enter-matching-directory nil))
  9376. (apply 'ido-completing-read (concat (car args))
  9377. (if (consp (car (nth 1 args)))
  9378. (mapcar 'car (nth 1 args))
  9379. (nth 1 args))
  9380. (cddr args)))
  9381. (if (and org-completion-use-iswitchb
  9382. (boundp 'iswitchb-mode) iswitchb-mode
  9383. (listp (second args)))
  9384. (apply 'org-iswitchb-completing-read (concat (car args))
  9385. (if (consp (car (nth 1 args)))
  9386. (mapcar 'car (nth 1 args))
  9387. (nth 1 args))
  9388. (cddr args))
  9389. (apply 'completing-read args)))))
  9390. (defun org-extract-attributes (s)
  9391. "Extract the attributes cookie from a string and set as text property."
  9392. (let (a attr (start 0) key value)
  9393. (save-match-data
  9394. (when (string-match "{{\\([^}]+\\)}}$" s)
  9395. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9396. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9397. (setq key (match-string 1 a) value (match-string 2 a)
  9398. start (match-end 0)
  9399. attr (plist-put attr (intern key) value))))
  9400. (org-add-props s nil 'org-attr attr))
  9401. s))
  9402. ;;; Opening/following a link
  9403. (defvar org-link-search-failed nil)
  9404. (defvar org-open-link-functions nil
  9405. "Hook for functions finding a plain text link.
  9406. These functions must take a single argument, the link content.
  9407. They will be called for links that look like [[link text][description]]
  9408. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9409. like a filename (e.g. \"./blue.png\").
  9410. These functions will be called *before* Org attempts to resolve the
  9411. link by doing text searches in the current buffer - so if you want a
  9412. link \"[[target]]\" to still find \"<<target>>\", your function should
  9413. handle this as a special case.
  9414. When the function does handle the link, it must return a non-nil value.
  9415. If it decides that it is not responsible for this link, it must return
  9416. nil to indicate that that Org-mode can continue with other options
  9417. like exact and fuzzy text search.")
  9418. (defun org-next-link (&optional search-backward)
  9419. "Move forward to the next link.
  9420. If the link is in hidden text, expose it."
  9421. (interactive "P")
  9422. (when (and org-link-search-failed (eq this-command last-command))
  9423. (goto-char (point-min))
  9424. (message "Link search wrapped back to beginning of buffer"))
  9425. (setq org-link-search-failed nil)
  9426. (let* ((pos (point))
  9427. (ct (org-context))
  9428. (a (assoc :link ct))
  9429. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9430. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9431. ((looking-at org-any-link-re)
  9432. ;; Don't stay stuck at link without an org-link face
  9433. (forward-char (if search-backward -1 1))))
  9434. (if (funcall srch-fun org-any-link-re nil t)
  9435. (progn
  9436. (goto-char (match-beginning 0))
  9437. (if (outline-invisible-p) (org-show-context)))
  9438. (goto-char pos)
  9439. (setq org-link-search-failed t)
  9440. (message "No further link found"))))
  9441. (defun org-previous-link ()
  9442. "Move backward to the previous link.
  9443. If the link is in hidden text, expose it."
  9444. (interactive)
  9445. (funcall 'org-next-link t))
  9446. (defun org-translate-link (s)
  9447. "Translate a link string if a translation function has been defined."
  9448. (if (and org-link-translation-function
  9449. (fboundp org-link-translation-function)
  9450. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9451. (progn
  9452. (setq s (funcall org-link-translation-function
  9453. (match-string 1 s) (match-string 2 s)))
  9454. (concat (car s) ":" (cdr s)))
  9455. s))
  9456. (defun org-translate-link-from-planner (type path)
  9457. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9458. This is still an experimental function, your mileage may vary."
  9459. (cond
  9460. ((member type '("http" "https" "news" "ftp"))
  9461. ;; standard Internet links are the same.
  9462. nil)
  9463. ((and (equal type "irc") (string-match "^//" path))
  9464. ;; Planner has two / at the beginning of an irc link, we have 1.
  9465. ;; We should have zero, actually....
  9466. (setq path (substring path 1)))
  9467. ((and (equal type "lisp") (string-match "^/" path))
  9468. ;; Planner has a slash, we do not.
  9469. (setq type "elisp" path (substring path 1)))
  9470. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9471. ;; A typical message link. Planner has the id after the final slash,
  9472. ;; we separate it with a hash mark
  9473. (setq path (concat (match-string 1 path) "#"
  9474. (org-remove-angle-brackets (match-string 2 path))))))
  9475. (cons type path))
  9476. (defun org-find-file-at-mouse (ev)
  9477. "Open file link or URL at mouse."
  9478. (interactive "e")
  9479. (mouse-set-point ev)
  9480. (org-open-at-point 'in-emacs))
  9481. (defun org-open-at-mouse (ev)
  9482. "Open file link or URL at mouse.
  9483. See the docstring of `org-open-file' for details."
  9484. (interactive "e")
  9485. (mouse-set-point ev)
  9486. (if (eq major-mode 'org-agenda-mode)
  9487. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9488. (org-open-at-point))
  9489. (defvar org-window-config-before-follow-link nil
  9490. "The window configuration before following a link.
  9491. This is saved in case the need arises to restore it.")
  9492. (defvar org-open-link-marker (make-marker)
  9493. "Marker pointing to the location where `org-open-at-point' was called.")
  9494. ;;;###autoload
  9495. (defun org-open-at-point-global ()
  9496. "Follow a link like Org-mode does.
  9497. This command can be called in any mode to follow a link that has
  9498. Org-mode syntax."
  9499. (interactive)
  9500. (org-run-like-in-org-mode 'org-open-at-point))
  9501. ;;;###autoload
  9502. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9503. "Open a link in the string S, as if it was in Org-mode."
  9504. (interactive "sLink: \nP")
  9505. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9506. (with-temp-buffer
  9507. (let ((org-inhibit-startup (not reference-buffer)))
  9508. (org-mode)
  9509. (insert s)
  9510. (goto-char (point-min))
  9511. (when reference-buffer
  9512. (setq org-link-abbrev-alist-local
  9513. (with-current-buffer reference-buffer
  9514. org-link-abbrev-alist-local)))
  9515. (org-open-at-point arg reference-buffer)))))
  9516. (defvar org-open-at-point-functions nil
  9517. "Hook that is run when following a link at point.
  9518. Functions in this hook must return t if they identify and follow
  9519. a link at point. If they don't find anything interesting at point,
  9520. they must return nil.")
  9521. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9522. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9523. (defun org-open-at-point (&optional arg reference-buffer)
  9524. "Open link, timestamp, footnote or tags at point.
  9525. When point is on a link, follow it. Normally, files will be
  9526. opened by an appropriate application. If the optional prefix
  9527. argument ARG is non-nil, Emacs will visit the file. With
  9528. a double prefix argument, try to open outside of Emacs, in the
  9529. application the system uses for this file type.
  9530. When point is on a timestamp, open the agenda at the day
  9531. specified.
  9532. When point is a footnote definition, move to the first reference
  9533. found. If it is on a reference, move to the associated
  9534. definition.
  9535. When point is on a headline, display a list of every link in the
  9536. entry, so it is possible to pick one, or all, of them. If point
  9537. is on a tag, call `org-tags-view' instead.
  9538. When optional argument REFERENCE-BUFFER is non-nil, it should
  9539. specify a buffer from where the link search should happen. This
  9540. is used internally by `org-open-link-from-string'.
  9541. On top of syntactically correct links, this function will open
  9542. the link at point in comments or comment blocks and the first
  9543. link in a property drawer line."
  9544. (interactive "P")
  9545. ;; On a code block, open block's results.
  9546. (unless (call-interactively 'org-babel-open-src-block-result)
  9547. (org-load-modules-maybe)
  9548. (move-marker org-open-link-marker (point))
  9549. (setq org-window-config-before-follow-link (current-window-configuration))
  9550. (org-remove-occur-highlights nil nil t)
  9551. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9552. (let* ((context (org-element-context)) type value)
  9553. ;; On an unsupported type, check if point is contained within
  9554. ;; a support one.
  9555. (while (and (not (memq (setq type (org-element-type context))
  9556. '(comment comment-block
  9557. headline inlinetask link
  9558. footnote-definition footnote-reference
  9559. node-property timestamp)))
  9560. (setq context (org-element-property :parent context))))
  9561. (setq value (org-element-property :value context))
  9562. (cond
  9563. ;; Blank lines at the beginning of buffer: bail out.
  9564. ((not context) (user-error "No link found"))
  9565. ;; Exception n°1: links in property drawers
  9566. ((eq type 'node-property)
  9567. (org-open-link-from-string
  9568. (and (string-match org-any-link-re value)
  9569. (match-string-no-properties 0 value))))
  9570. ;; Exception n°2: links in comments.
  9571. ((memq type '(comment comment-block))
  9572. (save-excursion
  9573. (skip-chars-forward "\\S-" (point-at-eol))
  9574. (let ((string-rear (replace-regexp-in-string
  9575. "^[ \t]*# [ \t]*" ""
  9576. (buffer-substring (point) (line-beginning-position))))
  9577. (string-front (buffer-substring (point) (line-end-position))))
  9578. (with-temp-buffer
  9579. (let ((org-inhibit-startup t)) (org-mode))
  9580. (insert value)
  9581. (goto-char (point-min))
  9582. (when (and (search-forward string-rear nil t)
  9583. (search-forward string-front (line-end-position) t))
  9584. (goto-char (match-beginning 0))
  9585. (org-open-at-point)
  9586. (when (string= string-rear "") (forward-char)))))))
  9587. ;; On a headline or an inlinetask, but not on a timestamp,
  9588. ;; a link, a footnote reference or on tags.
  9589. ((and (memq type '(headline inlinetask))
  9590. ;; Not on tags.
  9591. (progn (save-excursion (beginning-of-line)
  9592. (looking-at org-complex-heading-regexp))
  9593. (or (not (match-beginning 5))
  9594. (< (point) (match-beginning 5)))))
  9595. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9596. (links (car data))
  9597. (links-end (cdr data)))
  9598. (if links
  9599. (dolist (link (if (stringp links) (list links) links))
  9600. (search-forward link nil links-end)
  9601. (goto-char (match-beginning 0))
  9602. (org-open-at-point))
  9603. (require 'org-attach)
  9604. (org-attach-reveal 'if-exists))))
  9605. ;; Do nothing on white spaces after an object, unless point
  9606. ;; is right after it.
  9607. ((> (point)
  9608. (save-excursion
  9609. (goto-char (org-element-property :end context))
  9610. (skip-chars-backward " \t")
  9611. (point)))
  9612. (user-error "No link found"))
  9613. ((eq type 'timestamp) (org-follow-timestamp-link))
  9614. ;; On tags within a headline or an inlinetask.
  9615. ((and (memq type '(headline inlinetask))
  9616. (progn (save-excursion (beginning-of-line)
  9617. (looking-at org-complex-heading-regexp))
  9618. (and (match-beginning 5)
  9619. (>= (point) (match-beginning 5)))))
  9620. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9621. ((eq type 'link)
  9622. (let ((type (org-element-property :type context))
  9623. (path (org-link-unescape (org-element-property :path context))))
  9624. ;; Switch back to REFERENCE-BUFFER needed when called in
  9625. ;; a temporary buffer through `org-open-link-from-string'.
  9626. (with-current-buffer (or reference-buffer (current-buffer))
  9627. (cond
  9628. ((equal type "file")
  9629. (if (string-match "[*?{]" (file-name-nondirectory path))
  9630. (dired path)
  9631. ;; Look into `org-link-protocols' in order to find
  9632. ;; a DEDICATED-FUNCTION to open file. The function
  9633. ;; will be applied on raw link instead of parsed
  9634. ;; link due to the limitation in `org-add-link-type'
  9635. ;; ("open" function called with a single argument).
  9636. ;; If no such function is found, fallback to
  9637. ;; `org-open-file'.
  9638. ;;
  9639. ;; Note : "file+emacs" and "file+sys" types are
  9640. ;; hard-coded in order to escape the previous
  9641. ;; limitation.
  9642. (let* ((option (org-element-property :search-option context))
  9643. (app (org-element-property :application context))
  9644. (dedicated-function
  9645. (nth 1 (assoc app org-link-protocols))))
  9646. (if dedicated-function
  9647. (funcall dedicated-function
  9648. (concat path
  9649. (and option (concat "::" option))))
  9650. (apply 'org-open-file
  9651. path
  9652. (cond (arg)
  9653. ((equal app "emacs") 'emacs)
  9654. ((equal app "sys") 'system))
  9655. (cond ((not option) nil)
  9656. ((org-string-match-p "\\`[0-9]+\\'" option)
  9657. (list (string-to-number option)))
  9658. (t (list nil
  9659. (org-link-unescape option)))))))))
  9660. ((assoc type org-link-protocols)
  9661. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9662. ((equal type "help")
  9663. (let ((f-or-v (intern path)))
  9664. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9665. ((boundp f-or-v) (describe-variable f-or-v))
  9666. (t (error "Not a known function or variable")))))
  9667. ((member type '("http" "https" "ftp" "mailto" "news"))
  9668. (browse-url (org-link-escape-browser (concat type ":" path))))
  9669. ((equal type "doi")
  9670. (browse-url
  9671. (org-link-escape-browser (concat org-doi-server-url path))))
  9672. ((equal type "message") (browse-url (concat type ":" path)))
  9673. ((equal type "shell")
  9674. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9675. (cmd path))
  9676. (if (or (and (org-string-nw-p
  9677. org-confirm-shell-link-not-regexp)
  9678. (string-match
  9679. org-confirm-shell-link-not-regexp cmd))
  9680. (not org-confirm-shell-link-function)
  9681. (funcall org-confirm-shell-link-function
  9682. (format "Execute \"%s\" in shell? "
  9683. (org-add-props cmd nil
  9684. 'face 'org-warning))))
  9685. (progn
  9686. (message "Executing %s" cmd)
  9687. (shell-command cmd buf)
  9688. (when (featurep 'midnight)
  9689. (setq clean-buffer-list-kill-buffer-names
  9690. (cons buf
  9691. clean-buffer-list-kill-buffer-names))))
  9692. (user-error "Abort"))))
  9693. ((equal type "elisp")
  9694. (let ((cmd path))
  9695. (if (or (and (org-string-nw-p
  9696. org-confirm-elisp-link-not-regexp)
  9697. (org-string-match-p
  9698. org-confirm-elisp-link-not-regexp cmd))
  9699. (not org-confirm-elisp-link-function)
  9700. (funcall org-confirm-elisp-link-function
  9701. (format "Execute \"%s\" as elisp? "
  9702. (org-add-props cmd nil
  9703. 'face 'org-warning))))
  9704. (message "%s => %s" cmd
  9705. (if (eq (string-to-char cmd) ?\()
  9706. (eval (read cmd))
  9707. (call-interactively (read cmd))))
  9708. (user-error "Abort"))))
  9709. ((equal type "id")
  9710. (require 'ord-id)
  9711. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9712. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9713. (unless (run-hook-with-args-until-success
  9714. 'org-open-link-functions path)
  9715. (if (not arg) (org-mark-ring-push)
  9716. (switch-to-buffer-other-window
  9717. (org-get-buffer-for-internal-link (current-buffer))))
  9718. (let ((cmd `(org-link-search
  9719. ,(if (member type '("custom-id" "coderef"))
  9720. (org-element-property :raw-link context)
  9721. path)
  9722. ,(cond ((equal arg '(4)) 'occur)
  9723. ((equal arg '(16)) 'org-occur))
  9724. ,(org-element-property :begin context))))
  9725. (condition-case nil
  9726. (let ((org-link-search-inhibit-query t))
  9727. (eval cmd))
  9728. (error (progn (widen) (eval cmd)))))))
  9729. (t (browse-url-at-point))))))
  9730. ;; On a footnote reference or at a footnote definition's label.
  9731. ((or (eq type 'footnote-reference)
  9732. (and (eq type 'footnote-definition)
  9733. (save-excursion
  9734. ;; Do not validate action when point is on the
  9735. ;; spaces right after the footnote label, in
  9736. ;; order to be on par with behaviour on links.
  9737. (skip-chars-forward " \t")
  9738. (let ((begin
  9739. (org-element-property :contents-begin context)))
  9740. (if begin (< (point) begin)
  9741. (= (org-element-property :post-affiliated context)
  9742. (line-beginning-position)))))))
  9743. (org-footnote-action))
  9744. (t (user-error "No link found")))))
  9745. (move-marker org-open-link-marker nil)
  9746. (run-hook-with-args 'org-follow-link-hook)))
  9747. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9748. "Offer links in the current entry and return the selected link.
  9749. If there is only one link, return it.
  9750. If NTH is an integer, return the NTH link found.
  9751. If ZERO is a string, check also this string for a link, and if
  9752. there is one, return it."
  9753. (with-current-buffer buffer
  9754. (save-excursion
  9755. (save-restriction
  9756. (widen)
  9757. (goto-char marker)
  9758. (let ((cnt ?0)
  9759. (in-emacs (if (integerp nth) nil nth))
  9760. have-zero end links link c)
  9761. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9762. (push (match-string 0 zero) links)
  9763. (setq cnt (1- cnt) have-zero t))
  9764. (save-excursion
  9765. (org-back-to-heading t)
  9766. (setq end (save-excursion (outline-next-heading) (point)))
  9767. (while (re-search-forward org-any-link-re end t)
  9768. (push (match-string 0) links))
  9769. (setq links (org-uniquify (reverse links))))
  9770. (cond
  9771. ((null links)
  9772. (message "No links"))
  9773. ((equal (length links) 1)
  9774. (setq link (car links)))
  9775. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9776. (setq link (nth (if have-zero nth (1- nth)) links)))
  9777. (t ; we have to select a link
  9778. (save-excursion
  9779. (save-window-excursion
  9780. (delete-other-windows)
  9781. (with-output-to-temp-buffer "*Select Link*"
  9782. (mapc (lambda (l)
  9783. (if (not (string-match org-bracket-link-regexp l))
  9784. (princ (format "[%c] %s\n" (incf cnt)
  9785. (org-remove-angle-brackets l)))
  9786. (if (match-end 3)
  9787. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9788. (match-string 3 l) (match-string 1 l)))
  9789. (princ (format "[%c] %s\n" (incf cnt)
  9790. (match-string 1 l))))))
  9791. links))
  9792. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9793. (message "Select link to open, RET to open all:")
  9794. (setq c (read-char-exclusive))
  9795. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9796. (when (equal c ?q) (user-error "Abort"))
  9797. (if (equal c ?\C-m)
  9798. (setq link links)
  9799. (setq nth (- c ?0))
  9800. (if have-zero (setq nth (1+ nth)))
  9801. (unless (and (integerp nth) (>= (length links) nth))
  9802. (user-error "Invalid link selection"))
  9803. (setq link (nth (1- nth) links)))))
  9804. (cons link end))))))
  9805. ;; TODO: These functions are deprecated since `org-open-at-point'
  9806. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9807. (defun org-open-file-with-system (path)
  9808. "Open file at PATH using the system way of opening it."
  9809. (org-open-file path 'system))
  9810. (defun org-open-file-with-emacs (path)
  9811. "Open file at PATH in Emacs."
  9812. (org-open-file path 'emacs))
  9813. ;;; File search
  9814. (defvar org-create-file-search-functions nil
  9815. "List of functions to construct the right search string for a file link.
  9816. These functions are called in turn with point at the location to
  9817. which the link should point.
  9818. A function in the hook should first test if it would like to
  9819. handle this file type, for example by checking the `major-mode'
  9820. or the file extension. If it decides not to handle this file, it
  9821. should just return nil to give other functions a chance. If it
  9822. does handle the file, it must return the search string to be used
  9823. when following the link. The search string will be part of the
  9824. file link, given after a double colon, and `org-open-at-point'
  9825. will automatically search for it. If special measures must be
  9826. taken to make the search successful, another function should be
  9827. added to the companion hook `org-execute-file-search-functions',
  9828. which see.
  9829. A function in this hook may also use `setq' to set the variable
  9830. `description' to provide a suggestion for the descriptive text to
  9831. be used for this link when it gets inserted into an Org-mode
  9832. buffer with \\[org-insert-link].")
  9833. (defvar org-execute-file-search-functions nil
  9834. "List of functions to execute a file search triggered by a link.
  9835. Functions added to this hook must accept a single argument, the
  9836. search string that was part of the file link, the part after the
  9837. double colon. The function must first check if it would like to
  9838. handle this search, for example by checking the `major-mode' or
  9839. the file extension. If it decides not to handle this search, it
  9840. should just return nil to give other functions a chance. If it
  9841. does handle the search, it must return a non-nil value to keep
  9842. other functions from trying.
  9843. Each function can access the current prefix argument through the
  9844. variable `current-prefix-arg'. Note that a single prefix is used
  9845. to force opening a link in Emacs, so it may be good to only use a
  9846. numeric or double prefix to guide the search function.
  9847. In case this is needed, a function in this hook can also restore
  9848. the window configuration before `org-open-at-point' was called using:
  9849. (set-window-configuration org-window-config-before-follow-link)")
  9850. (defun org-link-search (s &optional type avoid-pos stealth)
  9851. "Search for a link search option.
  9852. If S is surrounded by forward slashes, it is interpreted as a
  9853. regular expression. In org-mode files, this will create an `org-occur'
  9854. sparse tree. In ordinary files, `occur' will be used to list matches.
  9855. If the current buffer is in `dired-mode', grep will be used to search
  9856. in all files. If AVOID-POS is given, ignore matches near that position.
  9857. When optional argument STEALTH is non-nil, do not modify
  9858. visibility around point, thus ignoring
  9859. `org-show-hierarchy-above', `org-show-following-heading' and
  9860. `org-show-siblings' variables."
  9861. (let ((case-fold-search t)
  9862. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9863. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9864. (append '(("") (" ") ("\t") ("\n"))
  9865. org-emphasis-alist)
  9866. "\\|") "\\)"))
  9867. (pos (point))
  9868. (pre nil) (post nil)
  9869. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9870. (cond
  9871. ;; First check if there are any special search functions
  9872. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9873. ;; Now try the builtin stuff
  9874. ((and (equal (string-to-char s0) ?#)
  9875. (> (length s0) 1)
  9876. (save-excursion
  9877. (goto-char (point-min))
  9878. (and
  9879. (re-search-forward
  9880. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9881. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9882. (setq type 'dedicated
  9883. pos (match-beginning 0))))
  9884. ;; There is an exact target for this
  9885. (goto-char pos)
  9886. (org-back-to-heading t)))
  9887. ((save-excursion
  9888. (goto-char (point-min))
  9889. (and
  9890. (re-search-forward
  9891. (concat "<<" (regexp-quote s0) ">>") nil t)
  9892. (setq type 'dedicated
  9893. pos (match-beginning 0))))
  9894. ;; There is an exact target for this
  9895. (goto-char pos))
  9896. ((save-excursion
  9897. (goto-char (point-min))
  9898. (and
  9899. (re-search-forward
  9900. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9901. (setq type 'dedicated pos (match-beginning 0))))
  9902. ;; Found an element with a matching #+name affiliated keyword.
  9903. (goto-char pos))
  9904. ((and (string-match "^(\\(.*\\))$" s0)
  9905. (save-excursion
  9906. (goto-char (point-min))
  9907. (and
  9908. (re-search-forward
  9909. (concat "[^[]" (regexp-quote
  9910. (format org-coderef-label-format
  9911. (match-string 1 s0))))
  9912. nil t)
  9913. (setq type 'dedicated
  9914. pos (1+ (match-beginning 0))))))
  9915. ;; There is a coderef target for this
  9916. (goto-char pos))
  9917. ((string-match "^/\\(.*\\)/$" s)
  9918. ;; A regular expression
  9919. (cond
  9920. ((derived-mode-p 'org-mode)
  9921. (org-occur (match-string 1 s)))
  9922. (t (org-do-occur (match-string 1 s)))))
  9923. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9924. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9925. (goto-char (point-min))
  9926. (cond
  9927. ((let (case-fold-search)
  9928. (re-search-forward (format org-complex-heading-regexp-format
  9929. (regexp-quote s))
  9930. nil t))
  9931. ;; OK, found a match
  9932. (setq type 'dedicated)
  9933. (goto-char (match-beginning 0)))
  9934. ((and (not org-link-search-inhibit-query)
  9935. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9936. (y-or-n-p "No match - create this as a new heading? "))
  9937. (goto-char (point-max))
  9938. (or (bolp) (newline))
  9939. (insert "* " s "\n")
  9940. (beginning-of-line 0))
  9941. (t
  9942. (goto-char pos)
  9943. (error "No match"))))
  9944. (t
  9945. ;; A normal search string
  9946. (when (equal (string-to-char s) ?*)
  9947. ;; Anchor on headlines, post may include tags.
  9948. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9949. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9950. s (substring s 1)))
  9951. (remove-text-properties
  9952. 0 (length s)
  9953. '(face nil mouse-face nil keymap nil fontified nil) s)
  9954. ;; Make a series of regular expressions to find a match
  9955. (setq words (org-split-string s "[ \n\r\t]+")
  9956. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9957. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9958. "\\)" markers)
  9959. re2a_ (concat "\\(" (mapconcat 'downcase words
  9960. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9961. re2a (concat "[ \t\r\n]" re2a_)
  9962. re4_ (concat "\\(" (mapconcat 'downcase words
  9963. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9964. re4 (concat "[^a-zA-Z_]" re4_)
  9965. re1 (concat pre re2 post)
  9966. re3 (concat pre (if pre re4_ re4) post)
  9967. re5 (concat pre ".*" re4)
  9968. re2 (concat pre re2)
  9969. re2a (concat pre (if pre re2a_ re2a))
  9970. re4 (concat pre (if pre re4_ re4))
  9971. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9972. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9973. re5 "\\)"))
  9974. (cond
  9975. ((eq type 'org-occur) (org-occur reall))
  9976. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9977. (t (goto-char (point-min))
  9978. (setq type 'fuzzy)
  9979. (if (or (and (org-search-not-self 1 re0 nil t)
  9980. (setq type 'dedicated))
  9981. (org-search-not-self 1 re1 nil t)
  9982. (org-search-not-self 1 re2 nil t)
  9983. (org-search-not-self 1 re2a nil t)
  9984. (org-search-not-self 1 re3 nil t)
  9985. (org-search-not-self 1 re4 nil t)
  9986. (org-search-not-self 1 re5 nil t))
  9987. (goto-char (match-beginning 1))
  9988. (goto-char pos)
  9989. (error "No match"))))))
  9990. (and (derived-mode-p 'org-mode)
  9991. (not stealth)
  9992. (org-show-context 'link-search))
  9993. type))
  9994. (defun org-search-not-self (group &rest args)
  9995. "Execute `re-search-forward', but only accept matches that do not
  9996. enclose the position of `org-open-link-marker'."
  9997. (let ((m org-open-link-marker))
  9998. (catch 'exit
  9999. (while (apply 're-search-forward args)
  10000. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10001. (goto-char (match-end group))
  10002. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10003. (> (match-beginning 0) (marker-position m))
  10004. (< (match-end 0) (marker-position m)))
  10005. (save-match-data
  10006. (or (not (org-in-regexp
  10007. org-bracket-link-analytic-regexp 1))
  10008. (not (match-end 4)) ; no description
  10009. (and (<= (match-beginning 4) (point))
  10010. (>= (match-end 4) (point))))))
  10011. (throw 'exit (point))))))))
  10012. (defun org-get-buffer-for-internal-link (buffer)
  10013. "Return a buffer to be used for displaying the link target of internal links."
  10014. (cond
  10015. ((not org-display-internal-link-with-indirect-buffer)
  10016. buffer)
  10017. ((string-match "(Clone)$" (buffer-name buffer))
  10018. (message "Buffer is already a clone, not making another one")
  10019. ;; we also do not modify visibility in this case
  10020. buffer)
  10021. (t ; make a new indirect buffer for displaying the link
  10022. (let* ((bn (buffer-name buffer))
  10023. (ibn (concat bn "(Clone)"))
  10024. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10025. (with-current-buffer ib (org-overview))
  10026. ib))))
  10027. (defun org-do-occur (regexp &optional cleanup)
  10028. "Call the Emacs command `occur'.
  10029. If CLEANUP is non-nil, remove the printout of the regular expression
  10030. in the *Occur* buffer. This is useful if the regex is long and not useful
  10031. to read."
  10032. (occur regexp)
  10033. (when cleanup
  10034. (let ((cwin (selected-window)) win beg end)
  10035. (when (setq win (get-buffer-window "*Occur*"))
  10036. (select-window win))
  10037. (goto-char (point-min))
  10038. (when (re-search-forward "match[a-z]+" nil t)
  10039. (setq beg (match-end 0))
  10040. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10041. (setq end (1- (match-beginning 0)))))
  10042. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10043. (goto-char (point-min))
  10044. (select-window cwin))))
  10045. ;;; The mark ring for links jumps
  10046. (defvar org-mark-ring nil
  10047. "Mark ring for positions before jumps in Org-mode.")
  10048. (defvar org-mark-ring-last-goto nil
  10049. "Last position in the mark ring used to go back.")
  10050. ;; Fill and close the ring
  10051. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10052. (loop for i from 1 to org-mark-ring-length do
  10053. (push (make-marker) org-mark-ring))
  10054. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10055. org-mark-ring)
  10056. (defun org-mark-ring-push (&optional pos buffer)
  10057. "Put the current position or POS into the mark ring and rotate it."
  10058. (interactive)
  10059. (setq pos (or pos (point)))
  10060. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10061. (move-marker (car org-mark-ring)
  10062. (or pos (point))
  10063. (or buffer (current-buffer)))
  10064. (message "%s"
  10065. (substitute-command-keys
  10066. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10067. (defun org-mark-ring-goto (&optional n)
  10068. "Jump to the previous position in the mark ring.
  10069. With prefix arg N, jump back that many stored positions. When
  10070. called several times in succession, walk through the entire ring.
  10071. Org-mode commands jumping to a different position in the current file,
  10072. or to another Org-mode file, automatically push the old position
  10073. onto the ring."
  10074. (interactive "p")
  10075. (let (p m)
  10076. (if (eq last-command this-command)
  10077. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10078. (setq p org-mark-ring))
  10079. (setq org-mark-ring-last-goto p)
  10080. (setq m (car p))
  10081. (org-pop-to-buffer-same-window (marker-buffer m))
  10082. (goto-char m)
  10083. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10084. (defun org-remove-angle-brackets (s)
  10085. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10086. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10087. s)
  10088. (defun org-add-angle-brackets (s)
  10089. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10090. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10091. s)
  10092. (defun org-remove-double-quotes (s)
  10093. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10094. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10095. s)
  10096. ;;; Following specific links
  10097. (defun org-follow-timestamp-link ()
  10098. "Open an agenda view for the time-stamp date/range at point."
  10099. (cond
  10100. ((org-at-date-range-p t)
  10101. (let ((org-agenda-start-on-weekday)
  10102. (t1 (match-string 1))
  10103. (t2 (match-string 2)) tt1 tt2)
  10104. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10105. tt2 (time-to-days (org-time-string-to-time t2)))
  10106. (let ((org-agenda-buffer-tmp-name
  10107. (format "*Org Agenda(a:%s)"
  10108. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10109. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10110. ((org-at-timestamp-p t)
  10111. (let ((org-agenda-buffer-tmp-name
  10112. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10113. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10114. (substring (match-string 1) 0 10)))
  10115. 1)))
  10116. (t (error "This should not happen"))))
  10117. ;;; Following file links
  10118. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10119. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10120. (declare-function mailcap-mime-info
  10121. "mailcap" (string &optional request no-decode))
  10122. (defvar org-wait nil)
  10123. (defun org-open-file (path &optional in-emacs line search)
  10124. "Open the file at PATH.
  10125. First, this expands any special file name abbreviations. Then the
  10126. configuration variable `org-file-apps' is checked if it contains an
  10127. entry for this file type, and if yes, the corresponding command is launched.
  10128. If no application is found, Emacs simply visits the file.
  10129. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10130. With a double \\[universal-argument] \\[universal-argument] \
  10131. prefix arg, Org tries to avoid opening in Emacs
  10132. and to use an external application to visit the file.
  10133. Optional LINE specifies a line to go to, optional SEARCH a string
  10134. to search for. If LINE or SEARCH is given, the file will be
  10135. opened in Emacs, unless an entry from org-file-apps that makes
  10136. use of groups in a regexp matches.
  10137. If you want to change the way frames are used when following a
  10138. link, please customize `org-link-frame-setup'.
  10139. If the file does not exist, an error is thrown."
  10140. (let* ((file (if (equal path "")
  10141. buffer-file-name
  10142. (substitute-in-file-name (expand-file-name path))))
  10143. (file-apps (append org-file-apps (org-default-apps)))
  10144. (apps (org-remove-if
  10145. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10146. (apps-dlink (org-remove-if-not
  10147. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10148. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10149. (dirp (if remp nil (file-directory-p file)))
  10150. (file (if (and dirp org-open-directory-means-index-dot-org)
  10151. (concat (file-name-as-directory file) "index.org")
  10152. file))
  10153. (a-m-a-p (assq 'auto-mode apps))
  10154. (dfile (downcase file))
  10155. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10156. (link (cond ((and (eq line nil)
  10157. (eq search nil))
  10158. file)
  10159. (line
  10160. (concat file "::" (number-to-string line)))
  10161. (search
  10162. (concat file "::" search))))
  10163. (dlink (downcase link))
  10164. (old-buffer (current-buffer))
  10165. (old-pos (point))
  10166. (old-mode major-mode)
  10167. ext cmd link-match-data)
  10168. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10169. (setq ext (match-string 1 dfile))
  10170. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10171. (setq ext (match-string 1 dfile))))
  10172. (cond
  10173. ((member in-emacs '((16) system))
  10174. (setq cmd (cdr (assoc 'system apps))))
  10175. (in-emacs (setq cmd 'emacs))
  10176. (t
  10177. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10178. (and dirp (cdr (assoc 'directory apps)))
  10179. ; first, try matching against apps-dlink
  10180. ; if we get a match here, store the match data for later
  10181. (let ((match (assoc-default dlink apps-dlink
  10182. 'string-match)))
  10183. (if match
  10184. (progn (setq link-match-data (match-data))
  10185. match)
  10186. (progn (setq in-emacs (or in-emacs line search))
  10187. nil))) ; if we have no match in apps-dlink,
  10188. ; always open the file in emacs if line or search
  10189. ; is given (for backwards compatibility)
  10190. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10191. 'string-match)
  10192. (cdr (assoc ext apps))
  10193. (cdr (assoc t apps))))))
  10194. (when (eq cmd 'system)
  10195. (setq cmd (cdr (assoc 'system apps))))
  10196. (when (eq cmd 'default)
  10197. (setq cmd (cdr (assoc t apps))))
  10198. (when (eq cmd 'mailcap)
  10199. (require 'mailcap)
  10200. (mailcap-parse-mailcaps)
  10201. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10202. (command (mailcap-mime-info mime-type)))
  10203. (if (stringp command)
  10204. (setq cmd command)
  10205. (setq cmd 'emacs))))
  10206. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10207. (not (file-exists-p file))
  10208. (not org-open-non-existing-files))
  10209. (user-error "No such file: %s" file))
  10210. (cond
  10211. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10212. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10213. (while (string-match "['\"]%s['\"]" cmd)
  10214. (setq cmd (replace-match "%s" t t cmd)))
  10215. (while (string-match "%s" cmd)
  10216. (setq cmd (replace-match
  10217. (save-match-data
  10218. (shell-quote-argument
  10219. (convert-standard-filename file)))
  10220. t t cmd)))
  10221. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10222. (save-match-data
  10223. (let ((match-index 1)
  10224. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10225. (set-match-data link-match-data)
  10226. (while (<= match-index number-of-groups)
  10227. (let ((regex (concat "%" (number-to-string match-index)))
  10228. (replace-with (match-string match-index dlink)))
  10229. (while (string-match regex cmd)
  10230. (setq cmd (replace-match replace-with t t cmd))))
  10231. (setq match-index (+ match-index 1)))))
  10232. (save-window-excursion
  10233. (message "Running %s...done" cmd)
  10234. (start-process-shell-command cmd nil cmd)
  10235. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10236. ((or (stringp cmd)
  10237. (eq cmd 'emacs))
  10238. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10239. (widen)
  10240. (if line (progn (org-goto-line line)
  10241. (if (derived-mode-p 'org-mode)
  10242. (org-reveal)))
  10243. (if search (org-link-search search))))
  10244. ((consp cmd)
  10245. (let ((file (convert-standard-filename file)))
  10246. (save-match-data
  10247. (set-match-data link-match-data)
  10248. (eval cmd))))
  10249. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10250. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10251. (or (not (equal old-buffer (current-buffer)))
  10252. (not (equal old-pos (point))))
  10253. (org-mark-ring-push old-pos old-buffer))))
  10254. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10255. "This function returns non-nil if `entry' uses a regular
  10256. expression which should be matched against the whole link by
  10257. org-open-file.
  10258. It assumes that is the case when the entry uses a regular
  10259. expression which has at least one grouping construct and the
  10260. action is either a lisp form or a command string containing
  10261. '%1', i.e. using at least one subexpression match as a
  10262. parameter."
  10263. (let ((selector (car entry))
  10264. (action (cdr entry)))
  10265. (if (stringp selector)
  10266. (and (> (regexp-opt-depth selector) 0)
  10267. (or (and (stringp action)
  10268. (string-match "%[0-9]" action))
  10269. (consp action)))
  10270. nil)))
  10271. (defun org-default-apps ()
  10272. "Return the default applications for this operating system."
  10273. (cond
  10274. ((eq system-type 'darwin)
  10275. org-file-apps-defaults-macosx)
  10276. ((eq system-type 'windows-nt)
  10277. org-file-apps-defaults-windowsnt)
  10278. (t org-file-apps-defaults-gnu)))
  10279. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10280. "Convert extensions to regular expressions in the cars of LIST.
  10281. Also, weed out any non-string entries, because the return value is used
  10282. only for regexp matching.
  10283. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10284. point to the symbol `emacs', indicating that the file should
  10285. be opened in Emacs."
  10286. (append
  10287. (delq nil
  10288. (mapcar (lambda (x)
  10289. (if (not (stringp (car x)))
  10290. nil
  10291. (if (string-match "\\W" (car x))
  10292. x
  10293. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10294. list))
  10295. (if add-auto-mode
  10296. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10297. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10298. (defun org-file-remote-p (file)
  10299. "Test whether FILE specifies a location on a remote system.
  10300. Return non-nil if the location is indeed remote.
  10301. For example, the filename \"/user@host:/foo\" specifies a location
  10302. on the system \"/user@host:\"."
  10303. (cond ((fboundp 'file-remote-p)
  10304. (file-remote-p file))
  10305. ((fboundp 'tramp-handle-file-remote-p)
  10306. (tramp-handle-file-remote-p file))
  10307. ((and (boundp 'ange-ftp-name-format)
  10308. (string-match (car ange-ftp-name-format) file))
  10309. t)))
  10310. ;;;; Refiling
  10311. (defun org-get-org-file ()
  10312. "Read a filename, with default directory `org-directory'."
  10313. (let ((default (or org-default-notes-file remember-data-file)))
  10314. (read-file-name (format "File name [%s]: " default)
  10315. (file-name-as-directory org-directory)
  10316. default)))
  10317. (defun org-notes-order-reversed-p ()
  10318. "Check if the current file should receive notes in reversed order."
  10319. (cond
  10320. ((not org-reverse-note-order) nil)
  10321. ((eq t org-reverse-note-order) t)
  10322. ((not (listp org-reverse-note-order)) nil)
  10323. (t (catch 'exit
  10324. (let ((all org-reverse-note-order)
  10325. entry)
  10326. (while (setq entry (pop all))
  10327. (if (string-match (car entry) buffer-file-name)
  10328. (throw 'exit (cdr entry))))
  10329. nil)))))
  10330. (defvar org-refile-target-table nil
  10331. "The list of refile targets, created by `org-refile'.")
  10332. (defvar org-agenda-new-buffers nil
  10333. "Buffers created to visit agenda files.")
  10334. (defvar org-refile-cache nil
  10335. "Cache for refile targets.")
  10336. (defvar org-refile-markers nil
  10337. "All the markers used for caching refile locations.")
  10338. (defun org-refile-marker (pos)
  10339. "Get a new refile marker, but only if caching is in use."
  10340. (if (not org-refile-use-cache)
  10341. pos
  10342. (let ((m (make-marker)))
  10343. (move-marker m pos)
  10344. (push m org-refile-markers)
  10345. m)))
  10346. (defun org-refile-cache-clear ()
  10347. "Clear the refile cache and disable all the markers."
  10348. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10349. (setq org-refile-markers nil)
  10350. (setq org-refile-cache nil)
  10351. (message "Refile cache has been cleared"))
  10352. (defun org-refile-cache-check-set (set)
  10353. "Check if all the markers in the cache still have live buffers."
  10354. (let (marker)
  10355. (catch 'exit
  10356. (while (and set (setq marker (nth 3 (pop set))))
  10357. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10358. (when (and marker (null (marker-buffer marker)))
  10359. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10360. (sit-for 3)
  10361. (throw 'exit nil)))
  10362. t)))
  10363. (defun org-refile-cache-put (set &rest identifiers)
  10364. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10365. (let* ((key (sha1 (prin1-to-string identifiers)))
  10366. (entry (assoc key org-refile-cache)))
  10367. (if entry
  10368. (setcdr entry set)
  10369. (push (cons key set) org-refile-cache))))
  10370. (defun org-refile-cache-get (&rest identifiers)
  10371. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10372. (cond
  10373. ((not org-refile-cache) nil)
  10374. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10375. (t
  10376. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10377. org-refile-cache))))
  10378. (and set (org-refile-cache-check-set set) set)))))
  10379. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10380. "Produce a table with refile targets."
  10381. (let ((case-fold-search nil)
  10382. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10383. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10384. targets tgs txt re files f desc descre fast-path-p level pos0)
  10385. (message "Getting targets...")
  10386. (with-current-buffer (or default-buffer (current-buffer))
  10387. (while (setq entry (pop entries))
  10388. (setq files (car entry) desc (cdr entry))
  10389. (setq fast-path-p nil)
  10390. (cond
  10391. ((null files) (setq files (list (current-buffer))))
  10392. ((eq files 'org-agenda-files)
  10393. (setq files (org-agenda-files 'unrestricted)))
  10394. ((and (symbolp files) (fboundp files))
  10395. (setq files (funcall files)))
  10396. ((and (symbolp files) (boundp files))
  10397. (setq files (symbol-value files))))
  10398. (if (stringp files) (setq files (list files)))
  10399. (cond
  10400. ((eq (car desc) :tag)
  10401. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10402. ((eq (car desc) :todo)
  10403. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10404. ((eq (car desc) :regexp)
  10405. (setq descre (cdr desc)))
  10406. ((eq (car desc) :level)
  10407. (setq descre (concat "^\\*\\{" (number-to-string
  10408. (if org-odd-levels-only
  10409. (1- (* 2 (cdr desc)))
  10410. (cdr desc)))
  10411. "\\}[ \t]")))
  10412. ((eq (car desc) :maxlevel)
  10413. (setq fast-path-p t)
  10414. (setq descre (concat "^\\*\\{1," (number-to-string
  10415. (if org-odd-levels-only
  10416. (1- (* 2 (cdr desc)))
  10417. (cdr desc)))
  10418. "\\}[ \t]")))
  10419. (t (error "Bad refiling target description %s" desc)))
  10420. (while (setq f (pop files))
  10421. (with-current-buffer
  10422. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10423. (or
  10424. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10425. (progn
  10426. (if (bufferp f) (setq f (buffer-file-name
  10427. (buffer-base-buffer f))))
  10428. (setq f (and f (expand-file-name f)))
  10429. (if (eq org-refile-use-outline-path 'file)
  10430. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10431. (save-excursion
  10432. (save-restriction
  10433. (widen)
  10434. (goto-char (point-min))
  10435. (while (re-search-forward descre nil t)
  10436. (goto-char (setq pos0 (point-at-bol)))
  10437. (catch 'next
  10438. (when org-refile-target-verify-function
  10439. (save-match-data
  10440. (or (funcall org-refile-target-verify-function)
  10441. (throw 'next t))))
  10442. (when (and (looking-at org-complex-heading-regexp)
  10443. (not (member (match-string 4) excluded-entries))
  10444. (match-string 4))
  10445. (setq level (org-reduced-level
  10446. (- (match-end 1) (match-beginning 1)))
  10447. txt (org-link-display-format (match-string 4))
  10448. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10449. re (format org-complex-heading-regexp-format
  10450. (regexp-quote (match-string 4))))
  10451. (when org-refile-use-outline-path
  10452. (setq txt (mapconcat
  10453. 'org-protect-slash
  10454. (append
  10455. (if (eq org-refile-use-outline-path
  10456. 'file)
  10457. (list (file-name-nondirectory
  10458. (buffer-file-name
  10459. (buffer-base-buffer))))
  10460. (if (eq org-refile-use-outline-path
  10461. 'full-file-path)
  10462. (list (buffer-file-name
  10463. (buffer-base-buffer)))))
  10464. (org-get-outline-path fast-path-p
  10465. level txt)
  10466. (list txt))
  10467. "/")))
  10468. (push (list txt f re (org-refile-marker (point)))
  10469. tgs)))
  10470. (when (= (point) pos0)
  10471. ;; verification function has not moved point
  10472. (goto-char (point-at-eol))))))))
  10473. (when org-refile-use-cache
  10474. (org-refile-cache-put tgs (buffer-file-name) descre))
  10475. (setq targets (append tgs targets))))))
  10476. (message "Getting targets...done")
  10477. (nreverse targets)))
  10478. (defun org-protect-slash (s)
  10479. (while (string-match "/" s)
  10480. (setq s (replace-match "\\" t t s)))
  10481. s)
  10482. (defvar org-olpa (make-vector 20 nil))
  10483. (defun org-get-outline-path (&optional fastp level heading)
  10484. "Return the outline path to the current entry, as a list.
  10485. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10486. routine which makes outline path derivations for an entire file,
  10487. avoiding backtracing. Refile target collection makes use of that."
  10488. (if fastp
  10489. (progn
  10490. (if (> level 19)
  10491. (error "Outline path failure, more than 19 levels"))
  10492. (loop for i from level upto 19 do
  10493. (aset org-olpa i nil))
  10494. (prog1
  10495. (delq nil (append org-olpa nil))
  10496. (aset org-olpa level heading)))
  10497. (let (rtn case-fold-search)
  10498. (save-excursion
  10499. (save-restriction
  10500. (widen)
  10501. (while (org-up-heading-safe)
  10502. (when (looking-at org-complex-heading-regexp)
  10503. (push (org-trim
  10504. (replace-regexp-in-string
  10505. ;; Remove statistical/checkboxes cookies
  10506. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10507. (org-match-string-no-properties 4)))
  10508. rtn)))
  10509. rtn)))))
  10510. (defun org-format-outline-path (path &optional width prefix separator)
  10511. "Format the outline path PATH for display.
  10512. WIDTH is the maximum number of characters that is available.
  10513. PREFIX is a prefix to be included in the returned string,
  10514. such as the file name.
  10515. SEPARATOR is inserted between the different parts of the path,
  10516. the default is \"/\"."
  10517. (setq width (or width 79))
  10518. (if prefix (setq width (- width (length prefix))))
  10519. (if (not path)
  10520. (or prefix "")
  10521. (let* ((nsteps (length path))
  10522. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10523. (maxwidth (if (<= total-width width)
  10524. 10000 ;; everything fits
  10525. ;; we need to shorten the level headings
  10526. (/ (- width nsteps) nsteps)))
  10527. (org-odd-levels-only nil)
  10528. (n 0)
  10529. (total (1+ (length prefix))))
  10530. (setq maxwidth (max maxwidth 10))
  10531. (concat prefix
  10532. (if prefix (or separator "/"))
  10533. (mapconcat
  10534. (lambda (h)
  10535. (setq n (1+ n))
  10536. (if (and (= n nsteps) (< maxwidth 10000))
  10537. (setq maxwidth (- total-width total)))
  10538. (if (< (length h) maxwidth)
  10539. (progn (setq total (+ total (length h) 1)) h)
  10540. (setq h (substring h 0 (- maxwidth 2))
  10541. total (+ total maxwidth 1))
  10542. (if (string-match "[ \t]+\\'" h)
  10543. (setq h (substring h 0 (match-beginning 0))))
  10544. (setq h (concat h "..")))
  10545. (org-add-props h nil 'face
  10546. (nth (% (1- n) org-n-level-faces)
  10547. org-level-faces))
  10548. h)
  10549. path (or separator "/"))))))
  10550. (defun org-display-outline-path (&optional file current separator just-return-string)
  10551. "Display the current outline path in the echo area.
  10552. If FILE is non-nil, prepend the output with the file name.
  10553. If CURRENT is non-nil, append the current heading to the output.
  10554. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10555. the different parts of the path and defaults to \"/\".
  10556. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10557. (interactive "P")
  10558. (let* (case-fold-search
  10559. (bfn (buffer-file-name (buffer-base-buffer)))
  10560. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10561. res)
  10562. (if current (setq path (append path
  10563. (save-excursion
  10564. (org-back-to-heading t)
  10565. (if (looking-at org-complex-heading-regexp)
  10566. (list (match-string 4)))))))
  10567. (setq res
  10568. (org-format-outline-path
  10569. path
  10570. (1- (frame-width))
  10571. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10572. separator))
  10573. (if just-return-string
  10574. (org-no-properties res)
  10575. (org-unlogged-message "%s" res))))
  10576. (defvar org-refile-history nil
  10577. "History for refiling operations.")
  10578. (defvar org-after-refile-insert-hook nil
  10579. "Hook run after `org-refile' has inserted its stuff at the new location.
  10580. Note that this is still *before* the stuff will be removed from
  10581. the *old* location.")
  10582. (defvar org-capture-last-stored-marker)
  10583. (defvar org-refile-keep nil
  10584. "Non-nil means `org-refile' will copy instead of refile.")
  10585. (defun org-copy ()
  10586. "Like `org-refile', but copy."
  10587. (interactive)
  10588. (let ((org-refile-keep t))
  10589. (funcall 'org-refile nil nil nil "Copy")))
  10590. (defun org-refile (&optional arg default-buffer rfloc msg)
  10591. "Move the entry or entries at point to another heading.
  10592. The list of target headings is compiled using the information in
  10593. `org-refile-targets', which see.
  10594. At the target location, the entry is filed as a subitem of the
  10595. target heading. Depending on `org-reverse-note-order', the new
  10596. subitem will either be the first or the last subitem.
  10597. If there is an active region, all entries in that region will be
  10598. refiled. However, the region must fulfill the requirement that
  10599. the first heading sets the top-level of the moved text.
  10600. With prefix arg ARG, the command will only visit the target
  10601. location and not actually move anything.
  10602. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10603. refiling operation has put the subtree.
  10604. With a numeric prefix argument of `2', refile to the running clock.
  10605. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10606. being set to `t' and copy to the target location, don't move it.
  10607. Beware that keeping refiled entries may result in duplicated ID
  10608. properties.
  10609. RFLOC can be a refile location obtained in a different way.
  10610. MSG is a string to replace \"Refile\" in the default prompt with
  10611. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10612. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10613. If you are using target caching (see `org-refile-use-cache'), you
  10614. have to clear the target cache in order to find new targets.
  10615. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10616. prefix argument (`C-u C-u C-u C-c C-w')."
  10617. (interactive "P")
  10618. (if (member arg '(0 (64)))
  10619. (org-refile-cache-clear)
  10620. (let* ((actionmsg (cond (msg msg)
  10621. ((equal arg 3) "Refile (and keep)")
  10622. (t "Refile")))
  10623. (cbuf (current-buffer))
  10624. (regionp (org-region-active-p))
  10625. (region-start (and regionp (region-beginning)))
  10626. (region-end (and regionp (region-end)))
  10627. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10628. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10629. pos it nbuf file re level reversed)
  10630. (setq last-command nil)
  10631. (when regionp
  10632. (goto-char region-start)
  10633. (or (bolp) (goto-char (point-at-bol)))
  10634. (setq region-start (point))
  10635. (unless (or (org-kill-is-subtree-p
  10636. (buffer-substring region-start region-end))
  10637. (prog1 org-refile-active-region-within-subtree
  10638. (let ((s (point-at-eol)))
  10639. (org-toggle-heading)
  10640. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10641. (user-error "The region is not a (sequence of) subtree(s)")))
  10642. (if (equal arg '(16))
  10643. (org-refile-goto-last-stored)
  10644. (when (or
  10645. (and (equal arg 2)
  10646. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10647. (prog1
  10648. (setq it (list (or org-clock-heading "running clock")
  10649. (buffer-file-name
  10650. (marker-buffer org-clock-hd-marker))
  10651. ""
  10652. (marker-position org-clock-hd-marker)))
  10653. (setq arg nil)))
  10654. (setq it (or rfloc
  10655. (let (heading-text)
  10656. (save-excursion
  10657. (unless (and arg (listp arg))
  10658. (org-back-to-heading t)
  10659. (setq heading-text
  10660. (replace-regexp-in-string
  10661. org-bracket-link-regexp
  10662. "\\3"
  10663. (nth 4 (org-heading-components)))))
  10664. (org-refile-get-location
  10665. (cond ((and arg (listp arg)) "Goto")
  10666. (regionp (concat actionmsg " region to"))
  10667. (t (concat actionmsg " subtree \""
  10668. heading-text "\" to")))
  10669. default-buffer
  10670. (and (not (equal '(4) arg))
  10671. org-refile-allow-creating-parent-nodes)
  10672. arg))))))
  10673. (setq file (nth 1 it)
  10674. re (nth 2 it)
  10675. pos (nth 3 it))
  10676. (if (and (not arg)
  10677. pos
  10678. (equal (buffer-file-name) file)
  10679. (if regionp
  10680. (and (>= pos region-start)
  10681. (<= pos region-end))
  10682. (and (>= pos (point))
  10683. (< pos (save-excursion
  10684. (org-end-of-subtree t t))))))
  10685. (error "Cannot refile to position inside the tree or region"))
  10686. (setq nbuf (or (find-buffer-visiting file)
  10687. (find-file-noselect file)))
  10688. (if (and arg (not (equal arg 3)))
  10689. (progn
  10690. (org-pop-to-buffer-same-window nbuf)
  10691. (goto-char pos)
  10692. (org-show-context 'org-goto))
  10693. (if regionp
  10694. (progn
  10695. (org-kill-new (buffer-substring region-start region-end))
  10696. (org-save-markers-in-region region-start region-end))
  10697. (org-copy-subtree 1 nil t))
  10698. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10699. (find-file-noselect file)))
  10700. (setq reversed (org-notes-order-reversed-p))
  10701. (save-excursion
  10702. (save-restriction
  10703. (widen)
  10704. (if pos
  10705. (progn
  10706. (goto-char pos)
  10707. (looking-at org-outline-regexp)
  10708. (setq level (org-get-valid-level (funcall outline-level) 1))
  10709. (goto-char
  10710. (if reversed
  10711. (or (outline-next-heading) (point-max))
  10712. (or (save-excursion (org-get-next-sibling))
  10713. (org-end-of-subtree t t)
  10714. (point-max)))))
  10715. (setq level 1)
  10716. (if (not reversed)
  10717. (goto-char (point-max))
  10718. (goto-char (point-min))
  10719. (or (outline-next-heading) (goto-char (point-max)))))
  10720. (if (not (bolp)) (newline))
  10721. (org-paste-subtree level nil nil t)
  10722. (when org-log-refile
  10723. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10724. (unless (eq org-log-refile 'note)
  10725. (save-excursion (org-add-log-note))))
  10726. (and org-auto-align-tags
  10727. (let ((org-loop-over-headlines-in-active-region nil))
  10728. (org-set-tags nil t)))
  10729. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10730. :last-refile)))
  10731. (when bookmark-name
  10732. (with-demoted-errors
  10733. (bookmark-set bookmark-name))))
  10734. ;; If we are refiling for capture, make sure that the
  10735. ;; last-capture pointers point here
  10736. (when (org-bound-and-true-p org-refile-for-capture)
  10737. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10738. :last-capture-marker)))
  10739. (when bookmark-name
  10740. (with-demoted-errors
  10741. (bookmark-set bookmark-name))))
  10742. (move-marker org-capture-last-stored-marker (point)))
  10743. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10744. (run-hooks 'org-after-refile-insert-hook))))
  10745. (unless org-refile-keep
  10746. (if regionp
  10747. (delete-region (point) (+ (point) (- region-end region-start)))
  10748. (delete-region
  10749. (and (org-back-to-heading t) (point))
  10750. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10751. (when (featurep 'org-inlinetask)
  10752. (org-inlinetask-remove-END-maybe))
  10753. (setq org-markers-to-move nil)
  10754. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10755. (defun org-refile-goto-last-stored ()
  10756. "Go to the location where the last refile was stored."
  10757. (interactive)
  10758. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10759. (message "This is the location of the last refile"))
  10760. (defun org-refile--get-location (refloc tbl)
  10761. "When user refile to REFLOC, find the associated target in TBL.
  10762. Also check `org-refile-target-table'."
  10763. (car (delq
  10764. nil
  10765. (mapcar
  10766. (lambda (r) (or (assoc r tbl)
  10767. (assoc r org-refile-target-table)))
  10768. (list (replace-regexp-in-string "/$" "" refloc)
  10769. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10770. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10771. no-exclude)
  10772. "Prompt the user for a refile location, using PROMPT.
  10773. PROMPT should not be suffixed with a colon and a space, because
  10774. this function appends the default value from
  10775. `org-refile-history' automatically, if that is not empty.
  10776. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10777. this is used for the GOTO interface."
  10778. (let ((org-refile-targets org-refile-targets)
  10779. (org-refile-use-outline-path org-refile-use-outline-path)
  10780. excluded-entries)
  10781. (when (and (derived-mode-p 'org-mode)
  10782. (not org-refile-use-cache)
  10783. (not no-exclude))
  10784. (org-map-tree
  10785. (lambda()
  10786. (setq excluded-entries
  10787. (append excluded-entries (list (org-get-heading t t)))))))
  10788. (setq org-refile-target-table
  10789. (org-refile-get-targets default-buffer excluded-entries)))
  10790. (unless org-refile-target-table
  10791. (user-error "No refile targets"))
  10792. (let* ((cbuf (current-buffer))
  10793. (partial-completion-mode nil)
  10794. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10795. (cfunc (if (and org-refile-use-outline-path
  10796. org-outline-path-complete-in-steps)
  10797. 'org-olpath-completing-read
  10798. 'org-icompleting-read))
  10799. (extra (if org-refile-use-outline-path "/" ""))
  10800. (cbnex (concat (buffer-name) extra))
  10801. (filename (and cfn (expand-file-name cfn)))
  10802. (tbl (mapcar
  10803. (lambda (x)
  10804. (if (and (not (member org-refile-use-outline-path
  10805. '(file full-file-path)))
  10806. (not (equal filename (nth 1 x))))
  10807. (cons (concat (car x) extra " ("
  10808. (file-name-nondirectory (nth 1 x)) ")")
  10809. (cdr x))
  10810. (cons (concat (car x) extra) (cdr x))))
  10811. org-refile-target-table))
  10812. (completion-ignore-case t)
  10813. cdef
  10814. (prompt (concat prompt
  10815. (or (and (car org-refile-history)
  10816. (concat " (default " (car org-refile-history) ")"))
  10817. (and (assoc cbnex tbl) (setq cdef cbnex)
  10818. (concat " (default " cbnex ")"))) ": "))
  10819. pa answ parent-target child parent old-hist)
  10820. (setq old-hist org-refile-history)
  10821. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10822. nil 'org-refile-history (or cdef (car org-refile-history))))
  10823. (if (setq pa (org-refile--get-location answ tbl))
  10824. (progn
  10825. (org-refile-check-position pa)
  10826. (when (or (not org-refile-history)
  10827. (not (eq old-hist org-refile-history))
  10828. (not (equal (car pa) (car org-refile-history))))
  10829. (setq org-refile-history
  10830. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10831. org-refile-history
  10832. (cdr org-refile-history))))
  10833. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10834. (pop org-refile-history)))
  10835. pa)
  10836. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10837. (progn
  10838. (setq parent (match-string 1 answ)
  10839. child (match-string 2 answ))
  10840. (setq parent-target (org-refile--get-location parent tbl))
  10841. (when (and parent-target
  10842. (or (eq new-nodes t)
  10843. (and (eq new-nodes 'confirm)
  10844. (y-or-n-p (format "Create new node \"%s\"? "
  10845. child)))))
  10846. (org-refile-new-child parent-target child)))
  10847. (user-error "Invalid target location")))))
  10848. (declare-function org-string-nw-p "org-macs" (s))
  10849. (defun org-refile-check-position (refile-pointer)
  10850. "Check if the refile pointer matches the headline to which it points."
  10851. (let* ((file (nth 1 refile-pointer))
  10852. (re (nth 2 refile-pointer))
  10853. (pos (nth 3 refile-pointer))
  10854. buffer)
  10855. (if (and (not (markerp pos)) (not file))
  10856. (user-error "Please indicate a target file in the refile path")
  10857. (when (org-string-nw-p re)
  10858. (setq buffer (if (markerp pos)
  10859. (marker-buffer pos)
  10860. (or (find-buffer-visiting file)
  10861. (find-file-noselect file))))
  10862. (with-current-buffer buffer
  10863. (save-excursion
  10864. (save-restriction
  10865. (widen)
  10866. (goto-char pos)
  10867. (beginning-of-line 1)
  10868. (unless (org-looking-at-p re)
  10869. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10870. (defun org-refile-new-child (parent-target child)
  10871. "Use refile target PARENT-TARGET to add new CHILD below it."
  10872. (unless parent-target
  10873. (error "Cannot find parent for new node"))
  10874. (let ((file (nth 1 parent-target))
  10875. (pos (nth 3 parent-target))
  10876. level)
  10877. (with-current-buffer (or (find-buffer-visiting file)
  10878. (find-file-noselect file))
  10879. (save-excursion
  10880. (save-restriction
  10881. (widen)
  10882. (if pos
  10883. (goto-char pos)
  10884. (goto-char (point-max))
  10885. (if (not (bolp)) (newline)))
  10886. (when (looking-at org-outline-regexp)
  10887. (setq level (funcall outline-level))
  10888. (org-end-of-subtree t t))
  10889. (org-back-over-empty-lines)
  10890. (insert "\n" (make-string
  10891. (if pos (org-get-valid-level level 1) 1) ?*)
  10892. " " child "\n")
  10893. (beginning-of-line 0)
  10894. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10895. (defun org-olpath-completing-read (prompt collection &rest args)
  10896. "Read an outline path like a file name."
  10897. (let ((thetable collection)
  10898. (org-completion-use-ido nil) ; does not work with ido.
  10899. (org-completion-use-iswitchb nil)) ; or iswitchb
  10900. (apply
  10901. 'org-icompleting-read prompt
  10902. (lambda (string predicate &optional flag)
  10903. (let (rtn r f (l (length string)))
  10904. (cond
  10905. ((eq flag nil)
  10906. ;; try completion
  10907. (try-completion string thetable))
  10908. ((eq flag t)
  10909. ;; all-completions
  10910. (setq rtn (all-completions string thetable predicate))
  10911. (mapcar
  10912. (lambda (x)
  10913. (setq r (substring x l))
  10914. (if (string-match " ([^)]*)$" x)
  10915. (setq f (match-string 0 x))
  10916. (setq f ""))
  10917. (if (string-match "/" r)
  10918. (concat string (substring r 0 (match-end 0)) f)
  10919. x))
  10920. rtn))
  10921. ((eq flag 'lambda)
  10922. ;; exact match?
  10923. (assoc string thetable)))))
  10924. args)))
  10925. ;;;; Dynamic blocks
  10926. (defun org-find-dblock (name)
  10927. "Find the first dynamic block with name NAME in the buffer.
  10928. If not found, stay at current position and return nil."
  10929. (let ((case-fold-search t) pos)
  10930. (save-excursion
  10931. (goto-char (point-min))
  10932. (setq pos (and (re-search-forward
  10933. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10934. (match-beginning 0))))
  10935. (if pos (goto-char pos))
  10936. pos))
  10937. (defun org-create-dblock (plist)
  10938. "Create a dynamic block section, with parameters taken from PLIST.
  10939. PLIST must contain a :name entry which is used as the name of the block."
  10940. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10941. (end-of-line 1)
  10942. (newline))
  10943. (let ((col (current-column))
  10944. (name (plist-get plist :name)))
  10945. (insert "#+BEGIN: " name)
  10946. (while plist
  10947. (if (eq (car plist) :name)
  10948. (setq plist (cddr plist))
  10949. (insert " " (prin1-to-string (pop plist)))))
  10950. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10951. (beginning-of-line -2)))
  10952. (defun org-prepare-dblock ()
  10953. "Prepare dynamic block for refresh.
  10954. This empties the block, puts the cursor at the insert position and returns
  10955. the property list including an extra property :name with the block name."
  10956. (unless (looking-at org-dblock-start-re)
  10957. (user-error "Not at a dynamic block"))
  10958. (let* ((begdel (1+ (match-end 0)))
  10959. (name (org-no-properties (match-string 1)))
  10960. (params (append (list :name name)
  10961. (read (concat "(" (match-string 3) ")")))))
  10962. (save-excursion
  10963. (beginning-of-line 1)
  10964. (skip-chars-forward " \t")
  10965. (setq params (plist-put params :indentation-column (current-column))))
  10966. (unless (re-search-forward org-dblock-end-re nil t)
  10967. (error "Dynamic block not terminated"))
  10968. (setq params
  10969. (append params
  10970. (list :content (buffer-substring
  10971. begdel (match-beginning 0)))))
  10972. (delete-region begdel (match-beginning 0))
  10973. (goto-char begdel)
  10974. (open-line 1)
  10975. params))
  10976. (defun org-map-dblocks (&optional command)
  10977. "Apply COMMAND to all dynamic blocks in the current buffer.
  10978. If COMMAND is not given, use `org-update-dblock'."
  10979. (let ((cmd (or command 'org-update-dblock)))
  10980. (save-excursion
  10981. (goto-char (point-min))
  10982. (while (re-search-forward org-dblock-start-re nil t)
  10983. (goto-char (match-beginning 0))
  10984. (save-excursion
  10985. (condition-case nil
  10986. (funcall cmd)
  10987. (error (message "Error during update of dynamic block"))))
  10988. (unless (re-search-forward org-dblock-end-re nil t)
  10989. (error "Dynamic block not terminated"))))))
  10990. (defun org-dblock-update (&optional arg)
  10991. "User command for updating dynamic blocks.
  10992. Update the dynamic block at point. With prefix ARG, update all dynamic
  10993. blocks in the buffer."
  10994. (interactive "P")
  10995. (if arg
  10996. (org-update-all-dblocks)
  10997. (or (looking-at org-dblock-start-re)
  10998. (org-beginning-of-dblock))
  10999. (org-update-dblock)))
  11000. (defun org-update-dblock ()
  11001. "Update the dynamic block at point.
  11002. This means to empty the block, parse for parameters and then call
  11003. the correct writing function."
  11004. (interactive)
  11005. (save-excursion
  11006. (let* ((win (selected-window))
  11007. (pos (point))
  11008. (line (org-current-line))
  11009. (params (org-prepare-dblock))
  11010. (name (plist-get params :name))
  11011. (indent (plist-get params :indentation-column))
  11012. (cmd (intern (concat "org-dblock-write:" name))))
  11013. (message "Updating dynamic block `%s' at line %d..." name line)
  11014. (funcall cmd params)
  11015. (message "Updating dynamic block `%s' at line %d...done" name line)
  11016. (goto-char pos)
  11017. (when (and indent (> indent 0))
  11018. (setq indent (make-string indent ?\ ))
  11019. (save-excursion
  11020. (select-window win)
  11021. (org-beginning-of-dblock)
  11022. (forward-line 1)
  11023. (while (not (looking-at org-dblock-end-re))
  11024. (insert indent)
  11025. (beginning-of-line 2))
  11026. (when (looking-at org-dblock-end-re)
  11027. (and (looking-at "[ \t]+")
  11028. (replace-match ""))
  11029. (insert indent)))))))
  11030. (defun org-beginning-of-dblock ()
  11031. "Find the beginning of the dynamic block at point.
  11032. Error if there is no such block at point."
  11033. (let ((pos (point))
  11034. beg)
  11035. (end-of-line 1)
  11036. (if (and (re-search-backward org-dblock-start-re nil t)
  11037. (setq beg (match-beginning 0))
  11038. (re-search-forward org-dblock-end-re nil t)
  11039. (> (match-end 0) pos))
  11040. (goto-char beg)
  11041. (goto-char pos)
  11042. (error "Not in a dynamic block"))))
  11043. (defun org-update-all-dblocks ()
  11044. "Update all dynamic blocks in the buffer.
  11045. This function can be used in a hook."
  11046. (interactive)
  11047. (when (derived-mode-p 'org-mode)
  11048. (org-map-dblocks 'org-update-dblock)))
  11049. ;;;; Completion
  11050. (declare-function org-export-backend-name "org-export" (cl-x))
  11051. (declare-function org-export-backend-options "org-export" (cl-x))
  11052. (defun org-get-export-keywords ()
  11053. "Return a list of all currently understood export keywords.
  11054. Export keywords include options, block names, attributes and
  11055. keywords relative to each registered export back-end."
  11056. (let (keywords)
  11057. (dolist (backend
  11058. (org-bound-and-true-p org-export--registered-backends)
  11059. (delq nil keywords))
  11060. ;; Back-end name (for keywords, like #+LATEX:)
  11061. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11062. (dolist (option-entry (org-export-backend-options backend))
  11063. ;; Back-end options.
  11064. (push (nth 1 option-entry) keywords)))))
  11065. (defconst org-options-keywords
  11066. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11067. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11068. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11069. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11070. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11071. (defcustom org-structure-template-alist
  11072. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11073. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11074. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11075. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11076. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11077. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11078. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11079. ("L" "#+LaTeX: ")
  11080. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11081. ("H" "#+HTML: ")
  11082. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11083. ("A" "#+ASCII: ")
  11084. ("i" "#+INDEX: ?")
  11085. ("I" "#+INCLUDE: %file ?"))
  11086. "Structure completion elements.
  11087. This is a list of abbreviation keys and values. The value gets inserted
  11088. if you type `<' followed by the key and then press the completion key,
  11089. usually `TAB'. %file will be replaced by a file name after prompting
  11090. for the file using completion. The cursor will be placed at the position
  11091. of the `?` in the template.
  11092. There are two templates for each key, the first uses the original Org syntax,
  11093. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11094. the default when the /org-mtags.el/ module has been loaded. See also the
  11095. variable `org-mtags-prefer-muse-templates'."
  11096. :group 'org-completion
  11097. :type '(repeat
  11098. (list
  11099. (string :tag "Key")
  11100. (string :tag "Template")))
  11101. :version 24.4
  11102. :package-version '(Org . "8.3"))
  11103. (defun org-try-structure-completion ()
  11104. "Try to complete a structure template before point.
  11105. This looks for strings like \"<e\" on an otherwise empty line and
  11106. expands them."
  11107. (let ((l (buffer-substring (point-at-bol) (point)))
  11108. a)
  11109. (when (and (looking-at "[ \t]*$")
  11110. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11111. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11112. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11113. (match-beginning 1)) a)
  11114. t)))
  11115. (defun org-complete-expand-structure-template (start cell)
  11116. "Expand a structure template."
  11117. (let ((rpl (nth 1 cell))
  11118. (ind ""))
  11119. (delete-region start (point))
  11120. (when (string-match "\\`[ \t]*#\\+" rpl)
  11121. (cond
  11122. ((bolp))
  11123. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11124. (setq ind (buffer-substring (point-at-bol) (point))))
  11125. (t (newline))))
  11126. (setq start (point))
  11127. (if (string-match "%file" rpl)
  11128. (setq rpl (replace-match
  11129. (concat
  11130. "\""
  11131. (save-match-data
  11132. (abbreviate-file-name (read-file-name "Include file: ")))
  11133. "\"")
  11134. t t rpl)))
  11135. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11136. (concat "\n" ind)))
  11137. (insert rpl)
  11138. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11139. ;;;; TODO, DEADLINE, Comments
  11140. (defun org-toggle-comment ()
  11141. "Change the COMMENT state of an entry."
  11142. (interactive)
  11143. (save-excursion
  11144. (org-back-to-heading)
  11145. (looking-at org-complex-heading-regexp)
  11146. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11147. (skip-chars-forward " \t")
  11148. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11149. (if (org-in-commented-heading-p t)
  11150. (delete-region (point)
  11151. (progn (search-forward " " (line-end-position) 'move)
  11152. (skip-chars-forward " \t")
  11153. (point)))
  11154. (insert org-comment-string)
  11155. (unless (eolp) (insert " ")))))
  11156. (defvar org-last-todo-state-is-todo nil
  11157. "This is non-nil when the last TODO state change led to a TODO state.
  11158. If the last change removed the TODO tag or switched to DONE, then
  11159. this is nil.")
  11160. (defvar org-setting-tags nil) ; dynamically skipped
  11161. (defvar org-todo-setup-filter-hook nil
  11162. "Hook for functions that pre-filter todo specs.
  11163. Each function takes a todo spec and returns either nil or the spec
  11164. transformed into canonical form." )
  11165. (defvar org-todo-get-default-hook nil
  11166. "Hook for functions that get a default item for todo.
  11167. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11168. nil or a string to be used for the todo mark." )
  11169. (defvar org-agenda-headline-snapshot-before-repeat)
  11170. (defun org-current-effective-time ()
  11171. "Return current time adjusted for `org-extend-today-until' variable."
  11172. (let* ((ct (org-current-time))
  11173. (dct (decode-time ct))
  11174. (ct1
  11175. (cond
  11176. (org-use-last-clock-out-time-as-effective-time
  11177. (or (org-clock-get-last-clock-out-time) ct))
  11178. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11179. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11180. (t ct))))
  11181. ct1))
  11182. (defun org-todo-yesterday (&optional arg)
  11183. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11184. (interactive "P")
  11185. (if (eq major-mode 'org-agenda-mode)
  11186. (apply 'org-agenda-todo-yesterday arg)
  11187. (let* ((hour (third (decode-time
  11188. (org-current-time))))
  11189. (org-extend-today-until (1+ hour)))
  11190. (org-todo arg))))
  11191. (defvar org-block-entry-blocking ""
  11192. "First entry preventing the TODO state change.")
  11193. (defun org-cancel-repeater ()
  11194. "Cancel a repeater by setting its numeric value to zero."
  11195. (interactive)
  11196. (save-excursion
  11197. (org-back-to-heading t)
  11198. (let ((bound1 (point))
  11199. (bound0 (save-excursion (outline-next-heading) (point))))
  11200. (when (re-search-forward
  11201. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11202. org-deadline-time-regexp "\\)\\|\\("
  11203. org-ts-regexp "\\)")
  11204. bound0 t)
  11205. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11206. (replace-match "0" t nil nil 1))))))
  11207. (defun org-todo (&optional arg)
  11208. "Change the TODO state of an item.
  11209. The state of an item is given by a keyword at the start of the heading,
  11210. like
  11211. *** TODO Write paper
  11212. *** DONE Call mom
  11213. The different keywords are specified in the variable `org-todo-keywords'.
  11214. By default the available states are \"TODO\" and \"DONE\".
  11215. So for this example: when the item starts with TODO, it is changed to DONE.
  11216. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11217. DONE are present, add TODO at the beginning of the heading.
  11218. With \\[universal-argument] prefix arg, use completion to determine the new \
  11219. state.
  11220. With numeric prefix arg, switch to that state.
  11221. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11222. keywords (nextset).
  11223. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11224. With a numeric prefix arg of 0, inhibit note taking for the change.
  11225. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11226. When called through ELisp, arg is also interpreted in the following way:
  11227. 'none -> empty state
  11228. \"\"(empty string) -> switch to empty state
  11229. 'done -> switch to DONE
  11230. 'nextset -> switch to the next set of keywords
  11231. 'previousset -> switch to the previous set of keywords
  11232. \"WAITING\" -> switch to the specified keyword, but only if it
  11233. really is a member of `org-todo-keywords'."
  11234. (interactive "P")
  11235. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11236. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11237. 'region-start-level 'region))
  11238. org-loop-over-headlines-in-active-region)
  11239. (org-map-entries
  11240. `(org-todo ,arg)
  11241. org-loop-over-headlines-in-active-region
  11242. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11243. (if (equal arg '(16)) (setq arg 'nextset))
  11244. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11245. (let ((org-blocker-hook org-blocker-hook)
  11246. commentp
  11247. case-fold-search)
  11248. (when (equal arg '(64))
  11249. (setq arg nil org-blocker-hook nil))
  11250. (when (and org-blocker-hook
  11251. (or org-inhibit-blocking
  11252. (org-entry-get nil "NOBLOCKING")))
  11253. (setq org-blocker-hook nil))
  11254. (save-excursion
  11255. (catch 'exit
  11256. (org-back-to-heading t)
  11257. (when (org-in-commented-heading-p t)
  11258. (org-toggle-comment)
  11259. (setq commentp t))
  11260. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11261. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11262. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11263. (let* ((match-data (match-data))
  11264. (startpos (point-at-bol))
  11265. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11266. (org-log-done org-log-done)
  11267. (org-log-repeat org-log-repeat)
  11268. (org-todo-log-states org-todo-log-states)
  11269. (org-inhibit-logging
  11270. (if (equal arg 0)
  11271. (progn (setq arg nil) 'note) org-inhibit-logging))
  11272. (this (match-string 1))
  11273. (hl-pos (match-beginning 0))
  11274. (head (org-get-todo-sequence-head this))
  11275. (ass (assoc head org-todo-kwd-alist))
  11276. (interpret (nth 1 ass))
  11277. (done-word (nth 3 ass))
  11278. (final-done-word (nth 4 ass))
  11279. (org-last-state (or this ""))
  11280. (completion-ignore-case t)
  11281. (member (member this org-todo-keywords-1))
  11282. (tail (cdr member))
  11283. (org-state (cond
  11284. ((and org-todo-key-trigger
  11285. (or (and (equal arg '(4))
  11286. (eq org-use-fast-todo-selection 'prefix))
  11287. (and (not arg) org-use-fast-todo-selection
  11288. (not (eq org-use-fast-todo-selection
  11289. 'prefix)))))
  11290. ;; Use fast selection
  11291. (org-fast-todo-selection))
  11292. ((and (equal arg '(4))
  11293. (or (not org-use-fast-todo-selection)
  11294. (not org-todo-key-trigger)))
  11295. ;; Read a state with completion
  11296. (org-icompleting-read
  11297. "State: " (mapcar 'list org-todo-keywords-1)
  11298. nil t))
  11299. ((eq arg 'right)
  11300. (if this
  11301. (if tail (car tail) nil)
  11302. (car org-todo-keywords-1)))
  11303. ((eq arg 'left)
  11304. (if (equal member org-todo-keywords-1)
  11305. nil
  11306. (if this
  11307. (nth (- (length org-todo-keywords-1)
  11308. (length tail) 2)
  11309. org-todo-keywords-1)
  11310. (org-last org-todo-keywords-1))))
  11311. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11312. (setq arg nil))) ; hack to fall back to cycling
  11313. (arg
  11314. ;; user or caller requests a specific state
  11315. (cond
  11316. ((equal arg "") nil)
  11317. ((eq arg 'none) nil)
  11318. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11319. ((eq arg 'nextset)
  11320. (or (car (cdr (member head org-todo-heads)))
  11321. (car org-todo-heads)))
  11322. ((eq arg 'previousset)
  11323. (let ((org-todo-heads (reverse org-todo-heads)))
  11324. (or (car (cdr (member head org-todo-heads)))
  11325. (car org-todo-heads))))
  11326. ((car (member arg org-todo-keywords-1)))
  11327. ((stringp arg)
  11328. (user-error "State `%s' not valid in this file" arg))
  11329. ((nth (1- (prefix-numeric-value arg))
  11330. org-todo-keywords-1))))
  11331. ((null member) (or head (car org-todo-keywords-1)))
  11332. ((equal this final-done-word) nil) ;; -> make empty
  11333. ((null tail) nil) ;; -> first entry
  11334. ((memq interpret '(type priority))
  11335. (if (eq this-command last-command)
  11336. (car tail)
  11337. (if (> (length tail) 0)
  11338. (or done-word (car org-done-keywords))
  11339. nil)))
  11340. (t
  11341. (car tail))))
  11342. (org-state (or
  11343. (run-hook-with-args-until-success
  11344. 'org-todo-get-default-hook org-state org-last-state)
  11345. org-state))
  11346. (next (if org-state (concat " " org-state " ") " "))
  11347. (change-plist (list :type 'todo-state-change :from this :to org-state
  11348. :position startpos))
  11349. dolog now-done-p)
  11350. (when org-blocker-hook
  11351. (setq org-last-todo-state-is-todo
  11352. (not (member this org-done-keywords)))
  11353. (unless (save-excursion
  11354. (save-match-data
  11355. (org-with-wide-buffer
  11356. (run-hook-with-args-until-failure
  11357. 'org-blocker-hook change-plist))))
  11358. (if (org-called-interactively-p 'interactive)
  11359. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11360. this org-state org-block-entry-blocking)
  11361. ;; fail silently
  11362. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11363. this org-state org-block-entry-blocking)
  11364. (throw 'exit nil))))
  11365. (store-match-data match-data)
  11366. (replace-match next t t)
  11367. (cond ((equal this org-state)
  11368. (message "TODO state was already %s" (org-trim next)))
  11369. ((pos-visible-in-window-p hl-pos)
  11370. (message "TODO state changed to %s" (org-trim next))))
  11371. (unless head
  11372. (setq head (org-get-todo-sequence-head org-state)
  11373. ass (assoc head org-todo-kwd-alist)
  11374. interpret (nth 1 ass)
  11375. done-word (nth 3 ass)
  11376. final-done-word (nth 4 ass)))
  11377. (when (memq arg '(nextset previousset))
  11378. (message "Keyword-Set %d/%d: %s"
  11379. (- (length org-todo-sets) -1
  11380. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11381. (length org-todo-sets)
  11382. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11383. (setq org-last-todo-state-is-todo
  11384. (not (member org-state org-done-keywords)))
  11385. (setq now-done-p (and (member org-state org-done-keywords)
  11386. (not (member this org-done-keywords))))
  11387. (and logging (org-local-logging logging))
  11388. (when (and (or org-todo-log-states org-log-done)
  11389. (not (eq org-inhibit-logging t))
  11390. (not (memq arg '(nextset previousset))))
  11391. ;; we need to look at recording a time and note
  11392. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11393. (nth 2 (assoc this org-todo-log-states))))
  11394. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11395. (setq dolog 'time))
  11396. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11397. (and org-state
  11398. (member org-state org-not-done-keywords)
  11399. (not (member this org-not-done-keywords))))
  11400. ;; This is now a todo state and was not one before
  11401. ;; If there was a CLOSED time stamp, get rid of it.
  11402. (org-add-planning-info nil nil 'closed))
  11403. (when (and now-done-p org-log-done)
  11404. ;; It is now done, and it was not done before
  11405. (org-add-planning-info 'closed (org-current-effective-time))
  11406. (if (and (not dolog) (eq 'note org-log-done))
  11407. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11408. (when (and org-state dolog)
  11409. ;; This is a non-nil state, and we need to log it
  11410. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11411. ;; Fixup tag positioning
  11412. (org-todo-trigger-tag-changes org-state)
  11413. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11414. (when org-provide-todo-statistics
  11415. (org-update-parent-todo-statistics))
  11416. (run-hooks 'org-after-todo-state-change-hook)
  11417. (if (and arg (not (member org-state org-done-keywords)))
  11418. (setq head (org-get-todo-sequence-head org-state)))
  11419. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11420. ;; Do we need to trigger a repeat?
  11421. (when now-done-p
  11422. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11423. ;; This is for the agenda, take a snapshot of the headline.
  11424. (save-match-data
  11425. (setq org-agenda-headline-snapshot-before-repeat
  11426. (org-get-heading))))
  11427. (org-auto-repeat-maybe org-state))
  11428. ;; Fixup cursor location if close to the keyword
  11429. (if (and (outline-on-heading-p)
  11430. (not (bolp))
  11431. (save-excursion (beginning-of-line 1)
  11432. (looking-at org-todo-line-regexp))
  11433. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11434. (progn
  11435. (goto-char (or (match-end 2) (match-end 1)))
  11436. (and (looking-at " ") (just-one-space))))
  11437. (when org-trigger-hook
  11438. (save-excursion
  11439. (run-hook-with-args 'org-trigger-hook change-plist)))
  11440. (when commentp (org-toggle-comment))))))))
  11441. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11442. "Block turning an entry into a TODO, using the hierarchy.
  11443. This checks whether the current task should be blocked from state
  11444. changes. Such blocking occurs when:
  11445. 1. The task has children which are not all in a completed state.
  11446. 2. A task has a parent with the property :ORDERED:, and there
  11447. are siblings prior to the current task with incomplete
  11448. status.
  11449. 3. The parent of the task is blocked because it has siblings that should
  11450. be done first, or is child of a block grandparent TODO entry."
  11451. (if (not org-enforce-todo-dependencies)
  11452. t ; if locally turned off don't block
  11453. (catch 'dont-block
  11454. ;; If this is not a todo state change, or if this entry is already DONE,
  11455. ;; do not block
  11456. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11457. (member (plist-get change-plist :from)
  11458. (cons 'done org-done-keywords))
  11459. (member (plist-get change-plist :to)
  11460. (cons 'todo org-not-done-keywords))
  11461. (not (plist-get change-plist :to)))
  11462. (throw 'dont-block t))
  11463. ;; If this task has children, and any are undone, it's blocked
  11464. (save-excursion
  11465. (org-back-to-heading t)
  11466. (let ((this-level (funcall outline-level)))
  11467. (outline-next-heading)
  11468. (let ((child-level (funcall outline-level)))
  11469. (while (and (not (eobp))
  11470. (> child-level this-level))
  11471. ;; this todo has children, check whether they are all
  11472. ;; completed
  11473. (if (and (not (org-entry-is-done-p))
  11474. (org-entry-is-todo-p))
  11475. (progn (setq org-block-entry-blocking (org-get-heading))
  11476. (throw 'dont-block nil)))
  11477. (outline-next-heading)
  11478. (setq child-level (funcall outline-level))))))
  11479. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11480. ;; any previous siblings are undone, it's blocked
  11481. (save-excursion
  11482. (org-back-to-heading t)
  11483. (let* ((pos (point))
  11484. (parent-pos (and (org-up-heading-safe) (point))))
  11485. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11486. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11487. (forward-line 1)
  11488. (re-search-forward org-not-done-heading-regexp pos t))
  11489. (setq org-block-entry-blocking (match-string 0))
  11490. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11491. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11492. (while t
  11493. (goto-char parent-pos)
  11494. (if (not (looking-at org-not-done-heading-regexp))
  11495. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11496. (setq pos (point))
  11497. (setq parent-pos (and (org-up-heading-safe) (point)))
  11498. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11499. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11500. (forward-line 1)
  11501. (re-search-forward org-not-done-heading-regexp pos t)
  11502. (setq org-block-entry-blocking (org-get-heading)))
  11503. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11504. (defcustom org-track-ordered-property-with-tag nil
  11505. "Should the ORDERED property also be shown as a tag?
  11506. The ORDERED property decides if an entry should require subtasks to be
  11507. completed in sequence. Since a property is not very visible, setting
  11508. this option means that toggling the ORDERED property with the command
  11509. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11510. not relevant for the behavior, but it makes things more visible.
  11511. Note that toggling the tag with tags commands will not change the property
  11512. and therefore not influence behavior!
  11513. This can be t, meaning the tag ORDERED should be used, It can also be a
  11514. string to select a different tag for this task."
  11515. :group 'org-todo
  11516. :type '(choice
  11517. (const :tag "No tracking" nil)
  11518. (const :tag "Track with ORDERED tag" t)
  11519. (string :tag "Use other tag")))
  11520. (defun org-toggle-ordered-property ()
  11521. "Toggle the ORDERED property of the current entry.
  11522. For better visibility, you can track the value of this property with a tag.
  11523. See variable `org-track-ordered-property-with-tag'."
  11524. (interactive)
  11525. (let* ((t1 org-track-ordered-property-with-tag)
  11526. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11527. (save-excursion
  11528. (org-back-to-heading)
  11529. (if (org-entry-get nil "ORDERED")
  11530. (progn
  11531. (org-delete-property "ORDERED")
  11532. (and tag (org-toggle-tag tag 'off))
  11533. (message "Subtasks can be completed in arbitrary order"))
  11534. (org-entry-put nil "ORDERED" "t")
  11535. (and tag (org-toggle-tag tag 'on))
  11536. (message "Subtasks must be completed in sequence")))))
  11537. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11538. (defun org-block-todo-from-checkboxes (change-plist)
  11539. "Block turning an entry into a TODO, using checkboxes.
  11540. This checks whether the current task should be blocked from state
  11541. changes because there are unchecked boxes in this entry."
  11542. (if (not org-enforce-todo-checkbox-dependencies)
  11543. t ; if locally turned off don't block
  11544. (catch 'dont-block
  11545. ;; If this is not a todo state change, or if this entry is already DONE,
  11546. ;; do not block
  11547. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11548. (member (plist-get change-plist :from)
  11549. (cons 'done org-done-keywords))
  11550. (member (plist-get change-plist :to)
  11551. (cons 'todo org-not-done-keywords))
  11552. (not (plist-get change-plist :to)))
  11553. (throw 'dont-block t))
  11554. ;; If this task has checkboxes that are not checked, it's blocked
  11555. (save-excursion
  11556. (org-back-to-heading t)
  11557. (let ((beg (point)) end)
  11558. (outline-next-heading)
  11559. (setq end (point))
  11560. (goto-char beg)
  11561. (if (org-list-search-forward
  11562. (concat (org-item-beginning-re)
  11563. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11564. "\\[[- ]\\]")
  11565. end t)
  11566. (progn
  11567. (if (boundp 'org-blocked-by-checkboxes)
  11568. (setq org-blocked-by-checkboxes t))
  11569. (throw 'dont-block nil)))))
  11570. t))) ; do not block
  11571. (defun org-entry-blocked-p ()
  11572. "Is the current entry blocked?"
  11573. (org-with-silent-modifications
  11574. (if (org-entry-get nil "NOBLOCKING")
  11575. nil ;; Never block this entry
  11576. (not (run-hook-with-args-until-failure
  11577. 'org-blocker-hook
  11578. (list :type 'todo-state-change
  11579. :position (point)
  11580. :from 'todo
  11581. :to 'done))))))
  11582. (defun org-update-statistics-cookies (all)
  11583. "Update the statistics cookie, either from TODO or from checkboxes.
  11584. This should be called with the cursor in a line with a statistics cookie."
  11585. (interactive "P")
  11586. (if all
  11587. (progn
  11588. (org-update-checkbox-count 'all)
  11589. (org-map-entries 'org-update-parent-todo-statistics))
  11590. (if (not (org-at-heading-p))
  11591. (org-update-checkbox-count)
  11592. (let ((pos (point-marker))
  11593. end l1 l2)
  11594. (ignore-errors (org-back-to-heading t))
  11595. (if (not (org-at-heading-p))
  11596. (org-update-checkbox-count)
  11597. (setq l1 (org-outline-level))
  11598. (setq end (save-excursion
  11599. (outline-next-heading)
  11600. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11601. (point)))
  11602. (if (and (save-excursion
  11603. (re-search-forward
  11604. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11605. (not (save-excursion (re-search-forward
  11606. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11607. (org-update-checkbox-count)
  11608. (if (and l2 (> l2 l1))
  11609. (progn
  11610. (goto-char end)
  11611. (org-update-parent-todo-statistics))
  11612. (goto-char pos)
  11613. (beginning-of-line 1)
  11614. (while (re-search-forward
  11615. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11616. (point-at-eol) t)
  11617. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11618. (goto-char pos)
  11619. (move-marker pos nil)))))
  11620. (defvar org-entry-property-inherited-from) ;; defined below
  11621. (defun org-update-parent-todo-statistics ()
  11622. "Update any statistics cookie in the parent of the current headline.
  11623. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11624. the entire subtree and this will travel up the hierarchy and update
  11625. statistics everywhere."
  11626. (let* ((prop (save-excursion (org-up-heading-safe)
  11627. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11628. (recursive (or (not org-hierarchical-todo-statistics)
  11629. (and prop (string-match "\\<recursive\\>" prop))))
  11630. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11631. 0))
  11632. (first t)
  11633. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11634. level ltoggle l1 new ndel
  11635. (cnt-all 0) (cnt-done 0) is-percent kwd
  11636. checkbox-beg ov ovs ove cookie-present)
  11637. (catch 'exit
  11638. (save-excursion
  11639. (beginning-of-line 1)
  11640. (setq ltoggle (funcall outline-level))
  11641. ;; Three situations are to consider:
  11642. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11643. ;; to the top-level ancestor on the headline;
  11644. ;; 2. If parent has "recursive" property, repeat up to the
  11645. ;; headline setting that property, taking inheritance into
  11646. ;; account;
  11647. ;; 3. Else, move up to direct parent and proceed only once.
  11648. (while (and (setq level (org-up-heading-safe))
  11649. (or recursive first)
  11650. (>= (point) lim))
  11651. (setq first nil cookie-present nil)
  11652. (unless (and level
  11653. (not (string-match
  11654. "\\<checkbox\\>"
  11655. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11656. "")))))
  11657. (throw 'exit nil))
  11658. (while (re-search-forward box-re (point-at-eol) t)
  11659. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11660. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11661. (save-match-data
  11662. (unless (outline-next-heading) (throw 'exit nil))
  11663. (while (and (looking-at org-complex-heading-regexp)
  11664. (> (setq l1 (length (match-string 1))) level))
  11665. (setq kwd (and (or recursive (= l1 ltoggle))
  11666. (match-string 2)))
  11667. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11668. (and (eq org-provide-todo-statistics t)
  11669. (or (member kwd org-done-keywords)))
  11670. (and (listp org-provide-todo-statistics)
  11671. (stringp (car org-provide-todo-statistics))
  11672. (or (member kwd org-provide-todo-statistics)
  11673. (member kwd org-done-keywords)))
  11674. (and (listp org-provide-todo-statistics)
  11675. (listp (car org-provide-todo-statistics))
  11676. (or (member kwd (car org-provide-todo-statistics))
  11677. (and (member kwd org-done-keywords)
  11678. (member kwd (cadr org-provide-todo-statistics))))))
  11679. (setq cnt-all (1+ cnt-all))
  11680. (if (eq org-provide-todo-statistics t)
  11681. (and kwd (setq cnt-all (1+ cnt-all)))))
  11682. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11683. (member kwd org-done-keywords))
  11684. (and (listp org-provide-todo-statistics)
  11685. (listp (car org-provide-todo-statistics))
  11686. (member kwd org-done-keywords)
  11687. (member kwd (cadr org-provide-todo-statistics)))
  11688. (and (listp org-provide-todo-statistics)
  11689. (stringp (car org-provide-todo-statistics))
  11690. (member kwd org-done-keywords)))
  11691. (setq cnt-done (1+ cnt-done)))
  11692. (outline-next-heading)))
  11693. (setq new
  11694. (if is-percent
  11695. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11696. (format "[%d/%d]" cnt-done cnt-all))
  11697. ndel (- (match-end 0) checkbox-beg))
  11698. ;; handle overlays when updating cookie from column view
  11699. (when (setq ov (car (overlays-at checkbox-beg)))
  11700. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11701. (delete-overlay ov))
  11702. (goto-char checkbox-beg)
  11703. (insert new)
  11704. (delete-region (point) (+ (point) ndel))
  11705. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11706. (when ov (move-overlay ov ovs ove)))
  11707. (when cookie-present
  11708. (run-hook-with-args 'org-after-todo-statistics-hook
  11709. cnt-done (- cnt-all cnt-done))))))
  11710. (run-hooks 'org-todo-statistics-hook)))
  11711. (defvar org-after-todo-statistics-hook nil
  11712. "Hook that is called after a TODO statistics cookie has been updated.
  11713. Each function is called with two arguments: the number of not-done entries
  11714. and the number of done entries.
  11715. For example, the following function, when added to this hook, will switch
  11716. an entry to DONE when all children are done, and back to TODO when new
  11717. entries are set to a TODO status. Note that this hook is only called
  11718. when there is a statistics cookie in the headline!
  11719. (defun org-summary-todo (n-done n-not-done)
  11720. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11721. (let (org-log-done org-log-states) ; turn off logging
  11722. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11723. ")
  11724. (defvar org-todo-statistics-hook nil
  11725. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11726. This hook runs even if there is no statistics cookie present, in which case
  11727. `org-after-todo-statistics-hook' would not run.")
  11728. (defun org-todo-trigger-tag-changes (state)
  11729. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11730. (let ((l org-todo-state-tags-triggers)
  11731. changes)
  11732. (when (or (not state) (equal state ""))
  11733. (setq changes (append changes (cdr (assoc "" l)))))
  11734. (when (and (stringp state) (> (length state) 0))
  11735. (setq changes (append changes (cdr (assoc state l)))))
  11736. (when (member state org-not-done-keywords)
  11737. (setq changes (append changes (cdr (assoc 'todo l)))))
  11738. (when (member state org-done-keywords)
  11739. (setq changes (append changes (cdr (assoc 'done l)))))
  11740. (dolist (c changes)
  11741. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11742. (defun org-local-logging (value)
  11743. "Get logging settings from a property VALUE."
  11744. (let* (words w a)
  11745. ;; directly set the variables, they are already local.
  11746. (setq org-log-done nil
  11747. org-log-repeat nil
  11748. org-todo-log-states nil)
  11749. (setq words (org-split-string value))
  11750. (while (setq w (pop words))
  11751. (cond
  11752. ((setq a (assoc w org-startup-options))
  11753. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11754. (set (nth 1 a) (nth 2 a))))
  11755. ((setq a (org-extract-log-state-settings w))
  11756. (and (member (car a) org-todo-keywords-1)
  11757. (push a org-todo-log-states)))))))
  11758. (defun org-get-todo-sequence-head (kwd)
  11759. "Return the head of the TODO sequence to which KWD belongs.
  11760. If KWD is not set, check if there is a text property remembering the
  11761. right sequence."
  11762. (let (p)
  11763. (cond
  11764. ((not kwd)
  11765. (or (get-text-property (point-at-bol) 'org-todo-head)
  11766. (progn
  11767. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11768. nil (point-at-eol)))
  11769. (get-text-property p 'org-todo-head))))
  11770. ((not (member kwd org-todo-keywords-1))
  11771. (car org-todo-keywords-1))
  11772. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11773. (defun org-fast-todo-selection ()
  11774. "Fast TODO keyword selection with single keys.
  11775. Returns the new TODO keyword, or nil if no state change should occur."
  11776. (let* ((fulltable org-todo-key-alist)
  11777. (done-keywords org-done-keywords) ;; needed for the faces.
  11778. (maxlen (apply 'max (mapcar
  11779. (lambda (x)
  11780. (if (stringp (car x)) (string-width (car x)) 0))
  11781. fulltable)))
  11782. (expert nil)
  11783. (fwidth (+ maxlen 3 1 3))
  11784. (ncol (/ (- (window-width) 4) fwidth))
  11785. tg cnt e c tbl
  11786. groups ingroup)
  11787. (save-excursion
  11788. (save-window-excursion
  11789. (if expert
  11790. (set-buffer (get-buffer-create " *Org todo*"))
  11791. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11792. (erase-buffer)
  11793. (org-set-local 'org-done-keywords done-keywords)
  11794. (setq tbl fulltable cnt 0)
  11795. (while (setq e (pop tbl))
  11796. (cond
  11797. ((equal e '(:startgroup))
  11798. (push '() groups) (setq ingroup t)
  11799. (when (not (= cnt 0))
  11800. (setq cnt 0)
  11801. (insert "\n"))
  11802. (insert "{ "))
  11803. ((equal e '(:endgroup))
  11804. (setq ingroup nil cnt 0)
  11805. (insert "}\n"))
  11806. ((equal e '(:newline))
  11807. (when (not (= cnt 0))
  11808. (setq cnt 0)
  11809. (insert "\n")
  11810. (setq e (car tbl))
  11811. (while (equal (car tbl) '(:newline))
  11812. (insert "\n")
  11813. (setq tbl (cdr tbl)))))
  11814. (t
  11815. (setq tg (car e) c (cdr e))
  11816. (if ingroup (push tg (car groups)))
  11817. (setq tg (org-add-props tg nil 'face
  11818. (org-get-todo-face tg)))
  11819. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11820. (insert "[" c "] " tg (make-string
  11821. (- fwidth 4 (length tg)) ?\ ))
  11822. (when (= (setq cnt (1+ cnt)) ncol)
  11823. (insert "\n")
  11824. (if ingroup (insert " "))
  11825. (setq cnt 0)))))
  11826. (insert "\n")
  11827. (goto-char (point-min))
  11828. (if (not expert) (org-fit-window-to-buffer))
  11829. (message "[a-z..]:Set [SPC]:clear")
  11830. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11831. (cond
  11832. ((or (= c ?\C-g)
  11833. (and (= c ?q) (not (rassoc c fulltable))))
  11834. (setq quit-flag t))
  11835. ((= c ?\ ) nil)
  11836. ((setq e (rassoc c fulltable) tg (car e))
  11837. tg)
  11838. (t (setq quit-flag t)))))))
  11839. (defun org-entry-is-todo-p ()
  11840. (member (org-get-todo-state) org-not-done-keywords))
  11841. (defun org-entry-is-done-p ()
  11842. (member (org-get-todo-state) org-done-keywords))
  11843. (defun org-get-todo-state ()
  11844. "Return the TODO keyword of the current subtree."
  11845. (save-excursion
  11846. (org-back-to-heading t)
  11847. (and (looking-at org-todo-line-regexp)
  11848. (match-end 2)
  11849. (match-string 2))))
  11850. (defun org-at-date-range-p (&optional inactive-ok)
  11851. "Is the cursor inside a date range?"
  11852. (interactive)
  11853. (save-excursion
  11854. (catch 'exit
  11855. (let ((pos (point)))
  11856. (skip-chars-backward "^[<\r\n")
  11857. (skip-chars-backward "<[")
  11858. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11859. (>= (match-end 0) pos)
  11860. (throw 'exit t))
  11861. (skip-chars-backward "^<[\r\n")
  11862. (skip-chars-backward "<[")
  11863. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11864. (>= (match-end 0) pos)
  11865. (throw 'exit t)))
  11866. nil)))
  11867. (defun org-get-repeat (&optional tagline)
  11868. "Check if there is a deadline/schedule with repeater in this entry."
  11869. (save-match-data
  11870. (save-excursion
  11871. (org-back-to-heading t)
  11872. (and (re-search-forward (if tagline
  11873. (concat tagline "\\s-*" org-repeat-re)
  11874. org-repeat-re)
  11875. (org-entry-end-position) t)
  11876. (match-string-no-properties 1)))))
  11877. (defvar org-last-changed-timestamp)
  11878. (defvar org-last-inserted-timestamp)
  11879. (defvar org-log-post-message)
  11880. (defvar org-log-note-purpose)
  11881. (defvar org-log-note-how nil)
  11882. (defvar org-log-note-extra)
  11883. (defun org-auto-repeat-maybe (done-word)
  11884. "Check if the current headline contains a repeated deadline/schedule.
  11885. If yes, set TODO state back to what it was and change the base date
  11886. of repeating deadline/scheduled time stamps to new date.
  11887. This function is run automatically after each state change to a DONE state."
  11888. ;; last-state is dynamically scoped into this function
  11889. (let* ((repeat (org-get-repeat))
  11890. (aa (assoc org-last-state org-todo-kwd-alist))
  11891. (interpret (nth 1 aa))
  11892. (head (nth 2 aa))
  11893. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11894. (msg "Entry repeats: ")
  11895. (org-log-done nil)
  11896. (org-todo-log-states nil)
  11897. re type n what ts time to-state)
  11898. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11899. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11900. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11901. org-todo-repeat-to-state))
  11902. (unless (and to-state (member to-state org-todo-keywords-1))
  11903. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11904. (org-todo to-state)
  11905. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11906. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11907. (org-time-stamp-format t t))))
  11908. (when org-log-repeat
  11909. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11910. (memq 'org-add-log-note post-command-hook))
  11911. ;; OK, we are already setup for some record
  11912. (if (eq org-log-repeat 'note)
  11913. ;; make sure we take a note, not only a time stamp
  11914. (setq org-log-note-how 'note))
  11915. ;; Set up for taking a record
  11916. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11917. org-last-state
  11918. 'findpos org-log-repeat)))
  11919. (org-back-to-heading t)
  11920. (org-add-planning-info nil nil 'closed)
  11921. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11922. org-deadline-time-regexp "\\)\\|\\("
  11923. org-ts-regexp "\\)"))
  11924. (while (re-search-forward
  11925. re (save-excursion (outline-next-heading) (point)) t)
  11926. (setq type (if (match-end 1) org-scheduled-string
  11927. (if (match-end 3) org-deadline-string "Plain:"))
  11928. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11929. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11930. (setq n (string-to-number (match-string 2 ts))
  11931. what (match-string 3 ts))
  11932. (if (equal what "w") (setq n (* n 7) what "d"))
  11933. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11934. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11935. ;; Preparation, see if we need to modify the start date for the change
  11936. (when (match-end 1)
  11937. (setq time (save-match-data (org-time-string-to-time ts)))
  11938. (cond
  11939. ((equal (match-string 1 ts) ".")
  11940. ;; Shift starting date to today
  11941. (org-timestamp-change
  11942. (- (org-today) (time-to-days time))
  11943. 'day))
  11944. ((equal (match-string 1 ts) "+")
  11945. (let ((nshiftmax 10) (nshift 0))
  11946. (while (or (= nshift 0)
  11947. (<= (time-to-days time)
  11948. (time-to-days (current-time))))
  11949. (when (= (incf nshift) nshiftmax)
  11950. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11951. (user-error "Abort")))
  11952. (org-timestamp-change n (cdr (assoc what whata)))
  11953. (org-at-timestamp-p t)
  11954. (setq ts (match-string 1))
  11955. (setq time (save-match-data (org-time-string-to-time ts)))))
  11956. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11957. ;; rematch, so that we have everything in place for the real shift
  11958. (org-at-timestamp-p t)
  11959. (setq ts (match-string 1))
  11960. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11961. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11962. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11963. (setq org-log-post-message msg)
  11964. (message "%s" msg))))
  11965. (defun org-show-todo-tree (arg)
  11966. "Make a compact tree which shows all headlines marked with TODO.
  11967. The tree will show the lines where the regexp matches, and all higher
  11968. headlines above the match.
  11969. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11970. With a numeric prefix N, construct a sparse tree for the Nth element
  11971. of `org-todo-keywords-1'."
  11972. (interactive "P")
  11973. (let ((case-fold-search nil)
  11974. (kwd-re
  11975. (cond ((null arg) org-not-done-regexp)
  11976. ((equal arg '(4))
  11977. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11978. (mapcar 'list org-todo-keywords-1))))
  11979. (concat "\\("
  11980. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11981. "\\)\\>")))
  11982. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11983. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11984. org-todo-keywords-1)))
  11985. (t (user-error "Invalid prefix argument: %s" arg)))))
  11986. (message "%d TODO entries found"
  11987. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11988. (defun org-deadline (arg &optional time)
  11989. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11990. With one universal prefix argument, remove any deadline from the item.
  11991. With two universal prefix arguments, prompt for a warning delay.
  11992. With argument TIME, set the deadline at the corresponding date. TIME
  11993. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11994. (interactive "P")
  11995. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11996. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11997. 'region-start-level 'region))
  11998. org-loop-over-headlines-in-active-region)
  11999. (org-map-entries
  12000. `(org-deadline ',arg ,time)
  12001. org-loop-over-headlines-in-active-region
  12002. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12003. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12004. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12005. (repeater (and old-date
  12006. (string-match
  12007. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12008. old-date)
  12009. (match-string 1 old-date))))
  12010. (cond
  12011. ((equal arg '(4))
  12012. (when (and old-date org-log-redeadline)
  12013. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12014. org-log-redeadline))
  12015. (org-remove-timestamp-with-keyword org-deadline-string)
  12016. (message "Item no longer has a deadline."))
  12017. ((equal arg '(16))
  12018. (save-excursion
  12019. (org-back-to-heading t)
  12020. (if (re-search-forward
  12021. org-deadline-time-regexp
  12022. (save-excursion (outline-next-heading) (point)) t)
  12023. (let* ((rpl0 (match-string 1))
  12024. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12025. (replace-match
  12026. (concat org-deadline-string
  12027. " <" rpl
  12028. (format " -%dd"
  12029. (abs
  12030. (- (time-to-days
  12031. (save-match-data
  12032. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12033. (time-to-days old-date-time))))
  12034. ">") t t))
  12035. (user-error "No deadline information to update"))))
  12036. (t
  12037. (org-add-planning-info 'deadline time 'closed)
  12038. (when (and old-date org-log-redeadline
  12039. (not (equal old-date
  12040. (substring org-last-inserted-timestamp 1 -1))))
  12041. (org-add-log-setup 'redeadline nil old-date 'findpos
  12042. org-log-redeadline))
  12043. (when repeater
  12044. (save-excursion
  12045. (org-back-to-heading t)
  12046. (when (re-search-forward (concat org-deadline-string " "
  12047. org-last-inserted-timestamp)
  12048. (save-excursion
  12049. (outline-next-heading) (point)) t)
  12050. (goto-char (1- (match-end 0)))
  12051. (insert " " repeater)
  12052. (setq org-last-inserted-timestamp
  12053. (concat (substring org-last-inserted-timestamp 0 -1)
  12054. " " repeater
  12055. (substring org-last-inserted-timestamp -1))))))
  12056. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12057. (defun org-schedule (arg &optional time)
  12058. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12059. With one universal prefix argument, remove any scheduling date from the item.
  12060. With two universal prefix arguments, prompt for a delay cookie.
  12061. With argument TIME, scheduled at the corresponding date. TIME can
  12062. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12063. (interactive "P")
  12064. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12065. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12066. 'region-start-level 'region))
  12067. org-loop-over-headlines-in-active-region)
  12068. (org-map-entries
  12069. `(org-schedule ',arg ,time)
  12070. org-loop-over-headlines-in-active-region
  12071. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12072. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12073. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12074. (repeater (and old-date
  12075. (string-match
  12076. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12077. old-date)
  12078. (match-string 1 old-date))))
  12079. (cond
  12080. ((equal arg '(4))
  12081. (progn
  12082. (when (and old-date org-log-reschedule)
  12083. (org-add-log-setup 'delschedule nil old-date 'findpos
  12084. org-log-reschedule))
  12085. (org-remove-timestamp-with-keyword org-scheduled-string)
  12086. (message "Item is no longer scheduled.")))
  12087. ((equal arg '(16))
  12088. (save-excursion
  12089. (org-back-to-heading t)
  12090. (if (re-search-forward
  12091. org-scheduled-time-regexp
  12092. (save-excursion (outline-next-heading) (point)) t)
  12093. (let* ((rpl0 (match-string 1))
  12094. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12095. (replace-match
  12096. (concat org-scheduled-string
  12097. " <" rpl
  12098. (format " -%dd"
  12099. (abs
  12100. (- (time-to-days
  12101. (save-match-data
  12102. (org-read-date nil t nil "Delay until" old-date-time)))
  12103. (time-to-days old-date-time))))
  12104. ">") t t))
  12105. (user-error "No scheduled information to update"))))
  12106. (t
  12107. (org-add-planning-info 'scheduled time 'closed)
  12108. (when (and old-date org-log-reschedule
  12109. (not (equal old-date
  12110. (substring org-last-inserted-timestamp 1 -1))))
  12111. (org-add-log-setup 'reschedule nil old-date 'findpos
  12112. org-log-reschedule))
  12113. (when repeater
  12114. (save-excursion
  12115. (org-back-to-heading t)
  12116. (when (re-search-forward (concat org-scheduled-string " "
  12117. org-last-inserted-timestamp)
  12118. (save-excursion
  12119. (outline-next-heading) (point)) t)
  12120. (goto-char (1- (match-end 0)))
  12121. (insert " " repeater)
  12122. (setq org-last-inserted-timestamp
  12123. (concat (substring org-last-inserted-timestamp 0 -1)
  12124. " " repeater
  12125. (substring org-last-inserted-timestamp -1))))))
  12126. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12127. (defun org-get-scheduled-time (pom &optional inherit)
  12128. "Get the scheduled time as a time tuple, of a format suitable
  12129. for calling org-schedule with, or if there is no scheduling,
  12130. returns nil."
  12131. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12132. (when time
  12133. (apply 'encode-time (org-parse-time-string time)))))
  12134. (defun org-get-deadline-time (pom &optional inherit)
  12135. "Get the deadline as a time tuple, of a format suitable for
  12136. calling org-deadline with, or if there is no scheduling, returns
  12137. nil."
  12138. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12139. (when time
  12140. (apply 'encode-time (org-parse-time-string time)))))
  12141. (defun org-remove-timestamp-with-keyword (keyword)
  12142. "Remove all time stamps with KEYWORD in the current entry."
  12143. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12144. beg)
  12145. (save-excursion
  12146. (org-back-to-heading t)
  12147. (setq beg (point))
  12148. (outline-next-heading)
  12149. (while (re-search-backward re beg t)
  12150. (replace-match "")
  12151. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12152. (equal (char-before) ?\ ))
  12153. (backward-delete-char 1)
  12154. (if (string-match "^[ \t]*$" (buffer-substring
  12155. (point-at-bol) (point-at-eol)))
  12156. (delete-region (point-at-bol)
  12157. (min (point-max) (1+ (point-at-eol))))))))))
  12158. (defvar org-time-was-given) ; dynamically scoped parameter
  12159. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12160. (defun org-add-planning-info (what &optional time &rest remove)
  12161. "Insert new timestamp with keyword in the line directly after the headline.
  12162. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  12163. If non is given, the user is prompted for a date.
  12164. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12165. be removed."
  12166. (interactive)
  12167. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12168. (catch 'exit
  12169. (when (and (memq what '(scheduled deadline))
  12170. (or (not time)
  12171. (and (stringp time)
  12172. (string-match "^[-+]+[0-9]" time))))
  12173. ;; Try to get a default date/time from existing timestamp
  12174. (save-excursion
  12175. (org-back-to-heading t)
  12176. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12177. (when (re-search-forward (if (eq what 'scheduled)
  12178. org-scheduled-time-regexp
  12179. org-deadline-time-regexp)
  12180. end t)
  12181. (setq ts (match-string 1)
  12182. default-time (apply 'encode-time (org-parse-time-string ts))
  12183. default-input (and ts (org-get-compact-tod ts)))))))
  12184. (when what
  12185. (setq time
  12186. (if (stringp time)
  12187. ;; This is a string (relative or absolute), set proper date
  12188. (apply 'encode-time
  12189. (org-read-date-analyze
  12190. time default-time (decode-time default-time)))
  12191. ;; If necessary, get the time from the user
  12192. (or time (org-read-date nil 'to-time nil nil
  12193. default-time default-input)))))
  12194. (when (and org-insert-labeled-timestamps-at-point
  12195. (memq what '(scheduled deadline)))
  12196. (insert
  12197. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12198. (org-insert-time-stamp time org-time-was-given
  12199. nil nil nil (list org-end-time-was-given))
  12200. (setq what nil))
  12201. (org-with-wide-buffer
  12202. (let (col list elt ts buffer-invisibility-spec)
  12203. (org-back-to-heading t)
  12204. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12205. (goto-char (match-end 1))
  12206. (setq col (current-column))
  12207. (goto-char (match-end 0))
  12208. (if (eobp) (insert "\n") (forward-char 1))
  12209. (unless (or what (org-looking-at-p org-planning-line-re))
  12210. ;; Nothing to add, nothing to remove...... :-)
  12211. (throw 'exit nil))
  12212. (if (and (not (looking-at org-outline-regexp))
  12213. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12214. "[^\r\n]*"))
  12215. (not (equal (match-string 1) org-clock-string)))
  12216. (narrow-to-region (match-beginning 0) (match-end 0))
  12217. (insert-before-markers "\n")
  12218. (backward-char 1)
  12219. (narrow-to-region (point) (point))
  12220. (and org-adapt-indentation (org-indent-to-column col)))
  12221. ;; Check if we have to remove something.
  12222. (setq list (cons what remove))
  12223. (while list
  12224. (setq elt (pop list))
  12225. (when (or (and (eq elt 'scheduled)
  12226. (re-search-forward org-scheduled-time-regexp nil t))
  12227. (and (eq elt 'deadline)
  12228. (re-search-forward org-deadline-time-regexp nil t))
  12229. (and (eq elt 'closed)
  12230. (re-search-forward org-closed-time-regexp nil t)))
  12231. (replace-match "")
  12232. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12233. (and (looking-at "[ \t]+") (replace-match ""))
  12234. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12235. (when what
  12236. (insert
  12237. (if (or (bolp) (eq (char-before) ?\s)) "" " ")
  12238. (cond ((eq what 'scheduled) org-scheduled-string)
  12239. ((eq what 'deadline) org-deadline-string)
  12240. ((eq what 'closed) org-closed-string))
  12241. " ")
  12242. (setq ts (org-insert-time-stamp
  12243. time
  12244. (or org-time-was-given
  12245. (and (eq what 'closed) org-log-done-with-time))
  12246. (eq what 'closed)
  12247. nil nil (list org-end-time-was-given)))
  12248. (unless (or (bolp)
  12249. (eq (char-before) ?\s)
  12250. (memq (char-after) '(?\n ?\s))
  12251. (eobp))
  12252. (insert " "))
  12253. (end-of-line 1))
  12254. (goto-char (point-min))
  12255. (widen)
  12256. (when (and (looking-at "[ \t]*\n") (eq (char-before) ?\n))
  12257. (delete-region (1- (point)) (line-end-position)))
  12258. ts)))))
  12259. (defvar org-log-note-marker (make-marker))
  12260. (defvar org-log-note-purpose nil)
  12261. (defvar org-log-note-state nil)
  12262. (defvar org-log-note-previous-state nil)
  12263. (defvar org-log-note-extra nil)
  12264. (defvar org-log-note-window-configuration nil)
  12265. (defvar org-log-note-return-to (make-marker))
  12266. (defvar org-log-note-effective-time nil
  12267. "Remembered current time so that dynamically scoped
  12268. `org-extend-today-until' affects tha timestamps in state change
  12269. log")
  12270. (defvar org-log-post-message nil
  12271. "Message to be displayed after a log note has been stored.
  12272. The auto-repeater uses this.")
  12273. (defun org-add-note ()
  12274. "Add a note to the current entry.
  12275. This is done in the same way as adding a state change note."
  12276. (interactive)
  12277. (org-add-log-setup 'note nil nil 'findpos nil))
  12278. (defun org-log-beginning (&optional create)
  12279. "Return expected start of log notes in current entry.
  12280. When optional argument CREATE is non-nil, the function creates
  12281. a drawer to store notes, if necessary. Returned position ignores
  12282. narrowing."
  12283. (org-with-wide-buffer
  12284. (org-back-to-heading t)
  12285. ;; Skip planning info and property drawer.
  12286. (forward-line)
  12287. (when (org-looking-at-p org-planning-line-re) (forward-line))
  12288. (when (looking-at org-property-drawer-re)
  12289. (goto-char (match-end 0))
  12290. (forward-line))
  12291. (if (org-at-heading-p) (point)
  12292. (let ((end (save-excursion (outline-next-heading) (point)))
  12293. (drawer (cond ((stringp org-log-into-drawer) org-log-into-drawer)
  12294. (org-log-into-drawer "LOGBOOK"))))
  12295. (cond
  12296. (drawer
  12297. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12298. (case-fold-search t))
  12299. (catch 'exit
  12300. ;; Try to find existing drawer.
  12301. (while (re-search-forward regexp end t)
  12302. (let ((element (org-element-at-point)))
  12303. (when (eq (org-element-type element) 'drawer)
  12304. (let ((cend (org-element-property :contents-end element)))
  12305. (when (and (not org-log-states-order-reversed) cend)
  12306. (goto-char cend)))
  12307. (throw 'exit nil))))
  12308. ;; No drawer found. Create one, if permitted.
  12309. (when create
  12310. (unless (bolp) (insert "\n"))
  12311. (let ((beg (point)))
  12312. (insert ":" drawer ":\n:END:\n")
  12313. (org-indent-region beg (point)))
  12314. (end-of-line -1)))))
  12315. (org-log-state-notes-insert-after-drawers
  12316. (while (and (looking-at org-drawer-regexp)
  12317. (progn (goto-char (match-end 0))
  12318. (re-search-forward org-property-end-re end t)))
  12319. (forward-line)))))
  12320. (if (bolp) (point) (line-beginning-position 2)))))
  12321. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12322. "Set up the post command hook to take a note.
  12323. If this is about to TODO state change, the new state is expected in STATE.
  12324. When FINDPOS is non-nil, find the correct position for the note in
  12325. the current entry. If not, assume that it can be inserted at point.
  12326. HOW is an indicator what kind of note should be created.
  12327. EXTRA is additional text that will be inserted into the notes buffer."
  12328. (org-with-wide-buffer
  12329. (when findpos
  12330. (goto-char (org-log-beginning t))
  12331. (unless org-log-states-order-reversed
  12332. (org-skip-over-state-notes)
  12333. (skip-chars-backward " \t\n\r")
  12334. (forward-line)))
  12335. (move-marker org-log-note-marker (point))
  12336. (setq org-log-note-purpose purpose
  12337. org-log-note-state state
  12338. org-log-note-previous-state prev-state
  12339. org-log-note-how how
  12340. org-log-note-extra extra
  12341. org-log-note-effective-time (org-current-effective-time))
  12342. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12343. (defun org-skip-over-state-notes ()
  12344. "Skip past the list of State notes in an entry."
  12345. (when (ignore-errors (goto-char (org-in-item-p)))
  12346. (let* ((struct (org-list-struct))
  12347. (prevs (org-list-prevs-alist struct))
  12348. (regexp
  12349. (concat "[ \t]*- +"
  12350. (replace-regexp-in-string
  12351. " +" " +"
  12352. (org-replace-escapes
  12353. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12354. `(("%d" . ,org-ts-regexp-inactive)
  12355. ("%D" . ,org-ts-regexp)
  12356. ("%s" . "\"\\S-+\"")
  12357. ("%S" . "\"\\S-+\"")
  12358. ("%t" . ,org-ts-regexp-inactive)
  12359. ("%T" . ,org-ts-regexp)
  12360. ("%u" . ".*?")
  12361. ("%U" . ".*?")))))))
  12362. (while (org-looking-at-p regexp)
  12363. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12364. (org-list-get-item-end (point) struct)))))))
  12365. (defun org-add-log-note (&optional purpose)
  12366. "Pop up a window for taking a note, and add this note later at point."
  12367. (remove-hook 'post-command-hook 'org-add-log-note)
  12368. (setq org-log-note-window-configuration (current-window-configuration))
  12369. (delete-other-windows)
  12370. (move-marker org-log-note-return-to (point))
  12371. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12372. (goto-char org-log-note-marker)
  12373. (org-switch-to-buffer-other-window "*Org Note*")
  12374. (erase-buffer)
  12375. (if (memq org-log-note-how '(time state))
  12376. (let (current-prefix-arg) (org-store-log-note))
  12377. (let ((org-inhibit-startup t)) (org-mode))
  12378. (insert (format "# Insert note for %s.
  12379. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12380. (cond
  12381. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12382. ((eq org-log-note-purpose 'done) "closed todo item")
  12383. ((eq org-log-note-purpose 'state)
  12384. (format "state change from \"%s\" to \"%s\""
  12385. (or org-log-note-previous-state "")
  12386. (or org-log-note-state "")))
  12387. ((eq org-log-note-purpose 'reschedule)
  12388. "rescheduling")
  12389. ((eq org-log-note-purpose 'delschedule)
  12390. "no longer scheduled")
  12391. ((eq org-log-note-purpose 'redeadline)
  12392. "changing deadline")
  12393. ((eq org-log-note-purpose 'deldeadline)
  12394. "removing deadline")
  12395. ((eq org-log-note-purpose 'refile)
  12396. "refiling")
  12397. ((eq org-log-note-purpose 'note)
  12398. "this entry")
  12399. (t (error "This should not happen")))))
  12400. (if org-log-note-extra (insert org-log-note-extra))
  12401. (org-set-local 'org-finish-function 'org-store-log-note)
  12402. (run-hooks 'org-log-buffer-setup-hook)))
  12403. (defvar org-note-abort nil) ; dynamically scoped
  12404. (defun org-store-log-note ()
  12405. "Finish taking a log note, and insert it to where it belongs."
  12406. (let ((txt (buffer-string)))
  12407. (kill-buffer (current-buffer))
  12408. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12409. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12410. (setq txt (replace-match "" t t txt)))
  12411. (if (string-match "\\s-+\\'" txt)
  12412. (setq txt (replace-match "" t t txt)))
  12413. (setq lines (org-split-string txt "\n"))
  12414. (when (and note (string-match "\\S-" note))
  12415. (setq note
  12416. (org-replace-escapes
  12417. note
  12418. (list (cons "%u" (user-login-name))
  12419. (cons "%U" user-full-name)
  12420. (cons "%t" (format-time-string
  12421. (org-time-stamp-format 'long 'inactive)
  12422. org-log-note-effective-time))
  12423. (cons "%T" (format-time-string
  12424. (org-time-stamp-format 'long nil)
  12425. org-log-note-effective-time))
  12426. (cons "%d" (format-time-string
  12427. (org-time-stamp-format nil 'inactive)
  12428. org-log-note-effective-time))
  12429. (cons "%D" (format-time-string
  12430. (org-time-stamp-format nil nil)
  12431. org-log-note-effective-time))
  12432. (cons "%s" (if org-log-note-state
  12433. (concat "\"" org-log-note-state "\"")
  12434. ""))
  12435. (cons "%S" (if org-log-note-previous-state
  12436. (concat "\"" org-log-note-previous-state "\"")
  12437. "\"\"")))))
  12438. (when lines (setq note (concat note " \\\\")))
  12439. (push note lines))
  12440. (when (or current-prefix-arg org-note-abort)
  12441. (when (org-log-into-drawer)
  12442. (org-remove-empty-drawer-at org-log-note-marker))
  12443. (setq lines nil))
  12444. (when lines
  12445. (with-current-buffer (marker-buffer org-log-note-marker)
  12446. (save-excursion
  12447. (goto-char org-log-note-marker)
  12448. (move-marker org-log-note-marker nil)
  12449. ;; Make sure point is at the beginning of an empty line.
  12450. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12451. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12452. ;; In an existing list, add a new item at the top level.
  12453. ;; Otherwise, indent line like a regular one.
  12454. (let ((itemp (org-in-item-p)))
  12455. (if itemp
  12456. (org-indent-line-to
  12457. (let ((struct (save-excursion
  12458. (goto-char itemp) (org-list-struct))))
  12459. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12460. (org-indent-line)))
  12461. (insert (org-list-bullet-string "-") (pop lines))
  12462. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12463. (dolist (line lines)
  12464. (insert "\n")
  12465. (org-indent-line-to ind)
  12466. (insert line)))
  12467. (message "Note stored")
  12468. (org-back-to-heading t)
  12469. (org-cycle-hide-drawers 'children))
  12470. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12471. ;; from within `org-add-log-note' because `buffer-undo-list'
  12472. ;; is then modified outside of `org-with-remote-undo'.
  12473. (when (eq this-command 'org-agenda-todo)
  12474. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12475. ;; Don't add undo information when called from `org-agenda-todo'
  12476. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12477. (set-window-configuration org-log-note-window-configuration)
  12478. (with-current-buffer (marker-buffer org-log-note-return-to)
  12479. (goto-char org-log-note-return-to))
  12480. (move-marker org-log-note-return-to nil)
  12481. (and org-log-post-message (message "%s" org-log-post-message))))
  12482. (defun org-remove-empty-drawer-at (pos)
  12483. "Remove an empty drawer at position POS.
  12484. POS may also be a marker."
  12485. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12486. (org-with-wide-buffer
  12487. (goto-char pos)
  12488. (let ((drawer (org-element-at-point)))
  12489. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12490. (not (org-element-property :contents-begin drawer)))
  12491. (delete-region (org-element-property :begin drawer)
  12492. (progn (goto-char (org-element-property :end drawer))
  12493. (skip-chars-backward " \r\t\n")
  12494. (forward-line)
  12495. (point))))))))
  12496. (defvar org-ts-type nil)
  12497. (defun org-sparse-tree (&optional arg type)
  12498. "Create a sparse tree, prompt for the details.
  12499. This command can create sparse trees. You first need to select the type
  12500. of match used to create the tree:
  12501. t Show all TODO entries.
  12502. T Show entries with a specific TODO keyword.
  12503. m Show entries selected by a tags/property match.
  12504. p Enter a property name and its value (both with completion on existing
  12505. names/values) and show entries with that property.
  12506. r Show entries matching a regular expression (`/' can be used as well).
  12507. b Show deadlines and scheduled items before a date.
  12508. a Show deadlines and scheduled items after a date.
  12509. d Show deadlines due within `org-deadline-warning-days'.
  12510. D Show deadlines and scheduled items between a date range."
  12511. (interactive "P")
  12512. (setq type (or type org-sparse-tree-default-date-type))
  12513. (setq org-ts-type type)
  12514. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12515. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12516. [c]ycle through date types: %s"
  12517. (case type
  12518. (all "all timestamps")
  12519. (scheduled "only scheduled")
  12520. (deadline "only deadline")
  12521. (active "only active timestamps")
  12522. (inactive "only inactive timestamps")
  12523. (scheduled-or-deadline "scheduled/deadline")
  12524. (closed "with a closed time-stamp")
  12525. (otherwise "scheduled/deadline")))
  12526. (let ((answer (read-char-exclusive)))
  12527. (case answer
  12528. (?c
  12529. (org-sparse-tree
  12530. arg
  12531. (cadr (memq type '(scheduled-or-deadline all scheduled deadline active
  12532. inactive closed)))))
  12533. (?d (call-interactively 'org-check-deadlines))
  12534. (?b (call-interactively 'org-check-before-date))
  12535. (?a (call-interactively 'org-check-after-date))
  12536. (?D (call-interactively 'org-check-dates-range))
  12537. (?t (call-interactively 'org-show-todo-tree))
  12538. (?T (org-show-todo-tree '(4)))
  12539. (?m (call-interactively 'org-match-sparse-tree))
  12540. ((?p ?P)
  12541. (let* ((kwd (org-icompleting-read
  12542. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12543. (value (org-icompleting-read
  12544. "Value: " (mapcar 'list (org-property-values kwd)))))
  12545. (unless (string-match "\\`{.*}\\'" value)
  12546. (setq value (concat "\"" value "\"")))
  12547. (org-match-sparse-tree arg (concat kwd "=" value))))
  12548. ((?r ?R ?/) (call-interactively 'org-occur))
  12549. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12550. (defvar org-occur-highlights nil
  12551. "List of overlays used for occur matches.")
  12552. (make-variable-buffer-local 'org-occur-highlights)
  12553. (defvar org-occur-parameters nil
  12554. "Parameters of the active org-occur calls.
  12555. This is a list, each call to org-occur pushes as cons cell,
  12556. containing the regular expression and the callback, onto the list.
  12557. The list can contain several entries if `org-occur' has been called
  12558. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12559. will only contain one set of parameters. When the highlights are
  12560. removed (for example with `C-c C-c', or with the next edit (depending
  12561. on `org-remove-highlights-with-change'), this variable is emptied
  12562. as well.")
  12563. (make-variable-buffer-local 'org-occur-parameters)
  12564. (defun org-occur (regexp &optional keep-previous callback)
  12565. "Make a compact tree which shows all matches of REGEXP.
  12566. The tree will show the lines where the regexp matches, and all higher
  12567. headlines above the match. It will also show the heading after the match,
  12568. to make sure editing the matching entry is easy.
  12569. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12570. call to `org-occur' will be kept, to allow stacking of calls to this
  12571. command.
  12572. If CALLBACK is non-nil, it is a function which is called to confirm
  12573. that the match should indeed be shown."
  12574. (interactive "sRegexp: \nP")
  12575. (when (equal regexp "")
  12576. (user-error "Regexp cannot be empty"))
  12577. (unless keep-previous
  12578. (org-remove-occur-highlights nil nil t))
  12579. (push (cons regexp callback) org-occur-parameters)
  12580. (let ((cnt 0))
  12581. (save-excursion
  12582. (goto-char (point-min))
  12583. (if (or (not keep-previous) ; do not want to keep
  12584. (not org-occur-highlights)) ; no previous matches
  12585. ;; hide everything
  12586. (org-overview))
  12587. (while (re-search-forward regexp nil t)
  12588. (when (or (not callback)
  12589. (save-match-data (funcall callback)))
  12590. (setq cnt (1+ cnt))
  12591. (when org-highlight-sparse-tree-matches
  12592. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12593. (org-show-context 'occur-tree))))
  12594. (when org-remove-highlights-with-change
  12595. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12596. nil 'local))
  12597. (unless org-sparse-tree-open-archived-trees
  12598. (org-hide-archived-subtrees (point-min) (point-max)))
  12599. (run-hooks 'org-occur-hook)
  12600. (if (org-called-interactively-p 'interactive)
  12601. (message "%d match(es) for regexp %s" cnt regexp))
  12602. cnt))
  12603. (defun org-occur-next-match (&optional n reset)
  12604. "Function for `next-error-function' to find sparse tree matches.
  12605. N is the number of matches to move, when negative move backwards.
  12606. RESET is entirely ignored - this function always goes back to the
  12607. starting point when no match is found."
  12608. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12609. (search-func (if (< n 0)
  12610. 'previous-single-char-property-change
  12611. 'next-single-char-property-change))
  12612. (n (abs n))
  12613. (pos (point))
  12614. p1)
  12615. (catch 'exit
  12616. (while (setq p1 (funcall search-func (point) 'org-type))
  12617. (when (equal p1 limit)
  12618. (goto-char pos)
  12619. (user-error "No more matches"))
  12620. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12621. (setq n (1- n))
  12622. (when (= n 0)
  12623. (goto-char p1)
  12624. (throw 'exit (point))))
  12625. (goto-char p1))
  12626. (goto-char p1)
  12627. (user-error "No more matches"))))
  12628. (defun org-show-context (&optional key)
  12629. "Make sure point and context are visible.
  12630. How much context is shown depends upon the variables
  12631. `org-show-hierarchy-above', `org-show-following-heading',
  12632. `org-show-entry-below' and `org-show-siblings'."
  12633. (let ((heading-p (org-at-heading-p t))
  12634. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12635. (following-p (org-get-alist-option org-show-following-heading key))
  12636. (entry-p (org-get-alist-option org-show-entry-below key))
  12637. (siblings-p (org-get-alist-option org-show-siblings key)))
  12638. ;; Show heading or entry text
  12639. (if (and heading-p (not entry-p))
  12640. (org-flag-heading nil) ; only show the heading
  12641. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12642. (org-show-hidden-entry))) ; show entire entry
  12643. (when following-p
  12644. ;; Show next sibling, or heading below text
  12645. (save-excursion
  12646. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12647. (org-flag-heading nil))))
  12648. (when siblings-p (org-show-siblings))
  12649. (when hierarchy-p
  12650. ;; show all higher headings, possibly with siblings
  12651. (save-excursion
  12652. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12653. (not (bobp)))
  12654. (org-flag-heading nil)
  12655. (when siblings-p (org-show-siblings)))))))
  12656. (defvar org-reveal-start-hook nil
  12657. "Hook run before revealing a location.")
  12658. (defun org-reveal (&optional siblings)
  12659. "Show current entry, hierarchy above it, and the following headline.
  12660. This can be used to show a consistent set of context around locations
  12661. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12662. not t for the search context.
  12663. With optional argument SIBLINGS, on each level of the hierarchy all
  12664. siblings are shown. This repairs the tree structure to what it would
  12665. look like when opened with hierarchical calls to `org-cycle'.
  12666. With double optional argument \\[universal-argument] \\[universal-argument], \
  12667. go to the parent and show the
  12668. entire tree."
  12669. (interactive "P")
  12670. (run-hooks 'org-reveal-start-hook)
  12671. (let ((org-show-hierarchy-above t)
  12672. (org-show-following-heading t)
  12673. (org-show-siblings (if siblings t org-show-siblings)))
  12674. (org-show-context nil))
  12675. (when (equal siblings '(16))
  12676. (save-excursion
  12677. (when (org-up-heading-safe)
  12678. (org-show-subtree)
  12679. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12680. (defun org-highlight-new-match (beg end)
  12681. "Highlight from BEG to END and mark the highlight is an occur headline."
  12682. (let ((ov (make-overlay beg end)))
  12683. (overlay-put ov 'face 'secondary-selection)
  12684. (overlay-put ov 'org-type 'org-occur)
  12685. (push ov org-occur-highlights)))
  12686. (defun org-remove-occur-highlights (&optional beg end noremove)
  12687. "Remove the occur highlights from the buffer.
  12688. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12689. from the `before-change-functions' in the current buffer."
  12690. (interactive)
  12691. (unless org-inhibit-highlight-removal
  12692. (mapc 'delete-overlay org-occur-highlights)
  12693. (setq org-occur-highlights nil)
  12694. (setq org-occur-parameters nil)
  12695. (unless noremove
  12696. (remove-hook 'before-change-functions
  12697. 'org-remove-occur-highlights 'local))))
  12698. ;;;; Priorities
  12699. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12700. "Regular expression matching the priority indicator.")
  12701. (defvar org-remove-priority-next-time nil)
  12702. (defun org-priority-up ()
  12703. "Increase the priority of the current item."
  12704. (interactive)
  12705. (org-priority 'up))
  12706. (defun org-priority-down ()
  12707. "Decrease the priority of the current item."
  12708. (interactive)
  12709. (org-priority 'down))
  12710. (defun org-priority (&optional action show)
  12711. "Change the priority of an item.
  12712. ACTION can be `set', `up', `down', or a character."
  12713. (interactive "P")
  12714. (if (equal action '(4))
  12715. (org-show-priority)
  12716. (unless org-enable-priority-commands
  12717. (user-error "Priority commands are disabled"))
  12718. (setq action (or action 'set))
  12719. (let (current new news have remove)
  12720. (save-excursion
  12721. (org-back-to-heading t)
  12722. (if (looking-at org-priority-regexp)
  12723. (setq current (string-to-char (match-string 2))
  12724. have t))
  12725. (cond
  12726. ((eq action 'remove)
  12727. (setq remove t new ?\ ))
  12728. ((or (eq action 'set)
  12729. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12730. (if (not (eq action 'set))
  12731. (setq new action)
  12732. (message "Priority %c-%c, SPC to remove: "
  12733. org-highest-priority org-lowest-priority)
  12734. (save-match-data
  12735. (setq new (read-char-exclusive))))
  12736. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12737. (= (upcase org-lowest-priority) org-lowest-priority))
  12738. (setq new (upcase new)))
  12739. (cond ((equal new ?\ ) (setq remove t))
  12740. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12741. (user-error "Priority must be between `%c' and `%c'"
  12742. org-highest-priority org-lowest-priority))))
  12743. ((eq action 'up)
  12744. (setq new (if have
  12745. (1- current) ; normal cycling
  12746. ;; last priority was empty
  12747. (if (eq last-command this-command)
  12748. org-lowest-priority ; wrap around empty to lowest
  12749. ;; default
  12750. (if org-priority-start-cycle-with-default
  12751. org-default-priority
  12752. (1- org-default-priority))))))
  12753. ((eq action 'down)
  12754. (setq new (if have
  12755. (1+ current) ; normal cycling
  12756. ;; last priority was empty
  12757. (if (eq last-command this-command)
  12758. org-highest-priority ; wrap around empty to highest
  12759. ;; default
  12760. (if org-priority-start-cycle-with-default
  12761. org-default-priority
  12762. (1+ org-default-priority))))))
  12763. (t (user-error "Invalid action")))
  12764. (if (or (< (upcase new) org-highest-priority)
  12765. (> (upcase new) org-lowest-priority))
  12766. (if (and (memq action '(up down))
  12767. (not have) (not (eq last-command this-command)))
  12768. ;; `new' is from default priority
  12769. (error
  12770. "The default can not be set, see `org-default-priority' why")
  12771. ;; normal cycling: `new' is beyond highest/lowest priority
  12772. ;; and is wrapped around to the empty priority
  12773. (setq remove t)))
  12774. (setq news (format "%c" new))
  12775. (if have
  12776. (if remove
  12777. (replace-match "" t t nil 1)
  12778. (replace-match news t t nil 2))
  12779. (if remove
  12780. (user-error "No priority cookie found in line")
  12781. (let ((case-fold-search nil))
  12782. (looking-at org-todo-line-regexp))
  12783. (if (match-end 2)
  12784. (progn
  12785. (goto-char (match-end 2))
  12786. (insert " [#" news "]"))
  12787. (goto-char (match-beginning 3))
  12788. (insert "[#" news "] "))))
  12789. (org-set-tags nil 'align))
  12790. (if remove
  12791. (message "Priority removed")
  12792. (message "Priority of current item set to %s" news)))))
  12793. (defun org-show-priority ()
  12794. "Show the priority of the current item.
  12795. This priority is composed of the main priority given with the [#A] cookies,
  12796. and by additional input from the age of a schedules or deadline entry."
  12797. (interactive)
  12798. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12799. (org-get-at-bol 'priority)
  12800. (save-excursion
  12801. (save-match-data
  12802. (beginning-of-line)
  12803. (and (looking-at org-heading-regexp)
  12804. (org-get-priority (match-string 0))))))))
  12805. (message "Priority is %d" (if pri pri -1000))))
  12806. (defun org-get-priority (s)
  12807. "Find priority cookie and return priority."
  12808. (save-match-data
  12809. (if (functionp org-get-priority-function)
  12810. (funcall org-get-priority-function)
  12811. (if (not (string-match org-priority-regexp s))
  12812. (* 1000 (- org-lowest-priority org-default-priority))
  12813. (* 1000 (- org-lowest-priority
  12814. (string-to-char (match-string 2 s))))))))
  12815. ;;;; Tags
  12816. (defvar org-agenda-archives-mode)
  12817. (defvar org-map-continue-from nil
  12818. "Position from where mapping should continue.
  12819. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12820. (defvar org-scanner-tags nil
  12821. "The current tag list while the tags scanner is running.")
  12822. (defvar org-trust-scanner-tags nil
  12823. "Should `org-get-tags-at' use the tags for the scanner.
  12824. This is for internal dynamical scoping only.
  12825. When this is non-nil, the function `org-get-tags-at' will return the value
  12826. of `org-scanner-tags' instead of building the list by itself. This
  12827. can lead to large speed-ups when the tags scanner is used in a file with
  12828. many entries, and when the list of tags is retrieved, for example to
  12829. obtain a list of properties. Building the tags list for each entry in such
  12830. a file becomes an N^2 operation - but with this variable set, it scales
  12831. as N.")
  12832. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12833. "Scan headline tags with inheritance and produce output ACTION.
  12834. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12835. or `agenda' to produce an entry list for an agenda view. It can also be
  12836. a Lisp form or a function that should be called at each matched headline, in
  12837. this case the return value is a list of all return values from these calls.
  12838. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12839. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12840. only lines with a not-done TODO keyword are included in the output.
  12841. This should be the same variable that was scoped into
  12842. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12843. START-LEVEL can be a string with asterisks, reducing the scope to
  12844. headlines matching this string."
  12845. (require 'org-agenda)
  12846. (let* ((re (concat "^"
  12847. (if start-level
  12848. ;; Get the correct level to match
  12849. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12850. org-outline-regexp)
  12851. " *\\(\\<\\("
  12852. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12853. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12854. (props (list 'face 'default
  12855. 'done-face 'org-agenda-done
  12856. 'undone-face 'default
  12857. 'mouse-face 'highlight
  12858. 'org-not-done-regexp org-not-done-regexp
  12859. 'org-todo-regexp org-todo-regexp
  12860. 'org-complex-heading-regexp org-complex-heading-regexp
  12861. 'help-echo
  12862. (format "mouse-2 or RET jump to org file %s"
  12863. (abbreviate-file-name
  12864. (or (buffer-file-name (buffer-base-buffer))
  12865. (buffer-name (buffer-base-buffer)))))))
  12866. (org-map-continue-from nil)
  12867. lspos tags tags-list
  12868. (tags-alist (list (cons 0 org-file-tags)))
  12869. (llast 0) rtn rtn1 level category i txt
  12870. todo marker entry priority)
  12871. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12872. (setq action (list 'lambda nil action)))
  12873. (save-excursion
  12874. (goto-char (point-min))
  12875. (when (eq action 'sparse-tree)
  12876. (org-overview)
  12877. (org-remove-occur-highlights))
  12878. (while (let (case-fold-search)
  12879. (re-search-forward re nil t))
  12880. (setq org-map-continue-from nil)
  12881. (catch :skip
  12882. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12883. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12884. (goto-char (setq lspos (match-beginning 0)))
  12885. (setq level (org-reduced-level (org-outline-level))
  12886. category (org-get-category))
  12887. (setq i llast llast level)
  12888. ;; remove tag lists from same and sublevels
  12889. (while (>= i level)
  12890. (when (setq entry (assoc i tags-alist))
  12891. (setq tags-alist (delete entry tags-alist)))
  12892. (setq i (1- i)))
  12893. ;; add the next tags
  12894. (when tags
  12895. (setq tags (org-split-string tags ":")
  12896. tags-alist
  12897. (cons (cons level tags) tags-alist)))
  12898. ;; compile tags for current headline
  12899. (setq tags-list
  12900. (if org-use-tag-inheritance
  12901. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12902. tags)
  12903. org-scanner-tags tags-list)
  12904. (when org-use-tag-inheritance
  12905. (setcdr (car tags-alist)
  12906. (mapcar (lambda (x)
  12907. (setq x (copy-sequence x))
  12908. (org-add-prop-inherited x))
  12909. (cdar tags-alist))))
  12910. (when (and tags org-use-tag-inheritance
  12911. (or (not (eq t org-use-tag-inheritance))
  12912. org-tags-exclude-from-inheritance))
  12913. ;; selective inheritance, remove uninherited ones
  12914. (setcdr (car tags-alist)
  12915. (org-remove-uninherited-tags (cdar tags-alist))))
  12916. (when (and
  12917. ;; eval matcher only when the todo condition is OK
  12918. (and (or (not todo-only) (member todo org-not-done-keywords))
  12919. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12920. (eval matcher)))
  12921. ;; Call the skipper, but return t if it does not skip,
  12922. ;; so that the `and' form continues evaluating
  12923. (progn
  12924. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12925. t)
  12926. ;; Check if timestamps are deselecting this entry
  12927. (or (not todo-only)
  12928. (and (member todo org-not-done-keywords)
  12929. (or (not org-agenda-tags-todo-honor-ignore-options)
  12930. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12931. ;; select this headline
  12932. (cond
  12933. ((eq action 'sparse-tree)
  12934. (and org-highlight-sparse-tree-matches
  12935. (org-get-heading) (match-end 0)
  12936. (org-highlight-new-match
  12937. (match-beginning 1) (match-end 1)))
  12938. (org-show-context 'tags-tree))
  12939. ((eq action 'agenda)
  12940. (setq txt (org-agenda-format-item
  12941. ""
  12942. (concat
  12943. (if (eq org-tags-match-list-sublevels 'indented)
  12944. (make-string (1- level) ?.) "")
  12945. (org-get-heading))
  12946. level category
  12947. tags-list)
  12948. priority (org-get-priority txt))
  12949. (goto-char lspos)
  12950. (setq marker (org-agenda-new-marker))
  12951. (org-add-props txt props
  12952. 'org-marker marker 'org-hd-marker marker 'org-category category
  12953. 'todo-state todo
  12954. 'priority priority 'type "tagsmatch")
  12955. (push txt rtn))
  12956. ((functionp action)
  12957. (setq org-map-continue-from nil)
  12958. (save-excursion
  12959. (setq rtn1 (funcall action))
  12960. (push rtn1 rtn)))
  12961. (t (user-error "Invalid action")))
  12962. ;; if we are to skip sublevels, jump to end of subtree
  12963. (unless org-tags-match-list-sublevels
  12964. (org-end-of-subtree t)
  12965. (backward-char 1))))
  12966. ;; Get the correct position from where to continue
  12967. (if org-map-continue-from
  12968. (goto-char org-map-continue-from)
  12969. (and (= (point) lspos) (end-of-line 1)))))
  12970. (when (and (eq action 'sparse-tree)
  12971. (not org-sparse-tree-open-archived-trees))
  12972. (org-hide-archived-subtrees (point-min) (point-max)))
  12973. (nreverse rtn)))
  12974. (defun org-remove-uninherited-tags (tags)
  12975. "Remove all tags that are not inherited from the list TAGS."
  12976. (cond
  12977. ((eq org-use-tag-inheritance t)
  12978. (if org-tags-exclude-from-inheritance
  12979. (org-delete-all org-tags-exclude-from-inheritance tags)
  12980. tags))
  12981. ((not org-use-tag-inheritance) nil)
  12982. ((stringp org-use-tag-inheritance)
  12983. (delq nil (mapcar
  12984. (lambda (x)
  12985. (if (and (string-match org-use-tag-inheritance x)
  12986. (not (member x org-tags-exclude-from-inheritance)))
  12987. x nil))
  12988. tags)))
  12989. ((listp org-use-tag-inheritance)
  12990. (delq nil (mapcar
  12991. (lambda (x)
  12992. (if (member x org-use-tag-inheritance) x nil))
  12993. tags)))))
  12994. (defun org-match-sparse-tree (&optional todo-only match)
  12995. "Create a sparse tree according to tags string MATCH.
  12996. MATCH can contain positive and negative selection of tags, like
  12997. \"+WORK+URGENT-WITHBOSS\".
  12998. If optional argument TODO-ONLY is non-nil, only select lines that are
  12999. also TODO lines."
  13000. (interactive "P")
  13001. (org-agenda-prepare-buffers (list (current-buffer)))
  13002. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13003. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13004. (defvar org-cached-props nil)
  13005. (defun org-cached-entry-get (pom property)
  13006. (if (or (eq t org-use-property-inheritance)
  13007. (and (stringp org-use-property-inheritance)
  13008. (string-match org-use-property-inheritance property))
  13009. (and (listp org-use-property-inheritance)
  13010. (member property org-use-property-inheritance)))
  13011. ;; Caching is not possible, check it directly
  13012. (org-entry-get pom property 'inherit)
  13013. ;; Get all properties, so that we can do complicated checks easily
  13014. (cdr (assoc property (or org-cached-props
  13015. (setq org-cached-props
  13016. (org-entry-properties pom)))))))
  13017. (defun org-global-tags-completion-table (&optional files)
  13018. "Return the list of all tags in all agenda buffer/files.
  13019. Optional FILES argument is a list of files which can be used
  13020. instead of the agenda files."
  13021. (save-excursion
  13022. (org-uniquify
  13023. (delq nil
  13024. (apply 'append
  13025. (mapcar
  13026. (lambda (file)
  13027. (set-buffer (find-file-noselect file))
  13028. (append (org-get-buffer-tags)
  13029. (mapcar (lambda (x) (if (stringp (car-safe x))
  13030. (list (car-safe x)) nil))
  13031. org-tag-alist)))
  13032. (if (and files (car files))
  13033. files
  13034. (org-agenda-files))))))))
  13035. (defun org-make-tags-matcher (match)
  13036. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13037. The variable `todo-only' is scoped dynamically into this function.
  13038. It will be set to t if the matcher restricts matching to TODO entries,
  13039. otherwise will not be touched.
  13040. Returns a cons of the selection string MATCH and the constructed
  13041. lisp form implementing the matcher. The matcher is to be evaluated
  13042. at an Org entry, with point on the headline, and returns t if the
  13043. entry matches the selection string MATCH. The returned lisp form
  13044. references two variables with information about the entry, which
  13045. must be bound around the form's evaluation: todo, the TODO keyword
  13046. at the entry (or nil of none); and tags-list, the list of all tags
  13047. at the entry including inherited ones. Additionally, the category
  13048. of the entry (if any) must be specified as the text property
  13049. 'org-category on the headline.
  13050. See also `org-scan-tags'.
  13051. "
  13052. (declare (special todo-only))
  13053. (unless (boundp 'todo-only)
  13054. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13055. (unless match
  13056. ;; Get a new match request, with completion against the global
  13057. ;; tags table and the local tags in current buffer
  13058. (let ((org-last-tags-completion-table
  13059. (org-uniquify
  13060. (delq nil (append (org-get-buffer-tags)
  13061. (org-global-tags-completion-table))))))
  13062. (setq match (org-completing-read-no-i
  13063. "Match: " 'org-tags-completion-function nil nil nil
  13064. 'org-tags-history))))
  13065. ;; Parse the string and create a lisp form
  13066. (let ((match0 match)
  13067. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13068. minus tag mm
  13069. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13070. orterms term orlist re-p str-p level-p level-op time-p
  13071. prop-p pn pv po gv rest (start 0) (ss 0))
  13072. ;; Expand group tags
  13073. (setq match (org-tags-expand match))
  13074. ;; Check if there is a TODO part of this match, which would be the
  13075. ;; part after a "/". TO make sure that this slash is not part of
  13076. ;; a property value to be matched against, we also check that there
  13077. ;; is no " after that slash.
  13078. ;; First, find the last slash
  13079. (while (string-match "/+" match ss)
  13080. (setq start (match-beginning 0) ss (match-end 0)))
  13081. (if (and (string-match "/+" match start)
  13082. (not (save-match-data (string-match "\"" match start))))
  13083. ;; match contains also a todo-matching request
  13084. (progn
  13085. (setq tagsmatch (substring match 0 (match-beginning 0))
  13086. todomatch (substring match (match-end 0)))
  13087. (if (string-match "^!" todomatch)
  13088. (setq todo-only t todomatch (substring todomatch 1)))
  13089. (if (string-match "^\\s-*$" todomatch)
  13090. (setq todomatch nil)))
  13091. ;; only matching tags
  13092. (setq tagsmatch match todomatch nil))
  13093. ;; Make the tags matcher
  13094. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13095. (setq tagsmatcher t)
  13096. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13097. (while (setq term (pop orterms))
  13098. (while (and (equal (substring term -1) "\\") orterms)
  13099. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13100. (while (string-match re term)
  13101. (setq rest (substring term (match-end 0))
  13102. minus (and (match-end 1)
  13103. (equal (match-string 1 term) "-"))
  13104. tag (save-match-data (replace-regexp-in-string
  13105. "\\\\-" "-"
  13106. (match-string 2 term)))
  13107. re-p (equal (string-to-char tag) ?{)
  13108. level-p (match-end 4)
  13109. prop-p (match-end 5)
  13110. mm (cond
  13111. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13112. (level-p
  13113. (setq level-op (org-op-to-function (match-string 3 term)))
  13114. `(,level-op level ,(string-to-number
  13115. (match-string 4 term))))
  13116. (prop-p
  13117. (setq pn (match-string 5 term)
  13118. po (match-string 6 term)
  13119. pv (match-string 7 term)
  13120. re-p (equal (string-to-char pv) ?{)
  13121. str-p (equal (string-to-char pv) ?\")
  13122. time-p (save-match-data
  13123. (string-match "^\"[[<].*[]>]\"$" pv))
  13124. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13125. (if time-p (setq pv (org-matcher-time pv)))
  13126. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13127. (cond
  13128. ((equal pn "CATEGORY")
  13129. (setq gv '(get-text-property (point) 'org-category)))
  13130. ((equal pn "TODO")
  13131. (setq gv 'todo))
  13132. (t
  13133. (setq gv `(org-cached-entry-get nil ,pn))))
  13134. (if re-p
  13135. (if (eq po 'org<>)
  13136. `(not (string-match ,pv (or ,gv "")))
  13137. `(string-match ,pv (or ,gv "")))
  13138. (if str-p
  13139. `(,po (or ,gv "") ,pv)
  13140. `(,po (string-to-number (or ,gv ""))
  13141. ,(string-to-number pv) ))))
  13142. (t `(member ,tag tags-list)))
  13143. mm (if minus (list 'not mm) mm)
  13144. term rest)
  13145. (push mm tagsmatcher))
  13146. (push (if (> (length tagsmatcher) 1)
  13147. (cons 'and tagsmatcher)
  13148. (car tagsmatcher))
  13149. orlist)
  13150. (setq tagsmatcher nil))
  13151. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13152. (setq tagsmatcher
  13153. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13154. ;; Make the todo matcher
  13155. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13156. (setq todomatcher t)
  13157. (setq orterms (org-split-string todomatch "|") orlist nil)
  13158. (while (setq term (pop orterms))
  13159. (while (string-match re term)
  13160. (setq minus (and (match-end 1)
  13161. (equal (match-string 1 term) "-"))
  13162. kwd (match-string 2 term)
  13163. re-p (equal (string-to-char kwd) ?{)
  13164. term (substring term (match-end 0))
  13165. mm (if re-p
  13166. `(string-match ,(substring kwd 1 -1) todo)
  13167. (list 'equal 'todo kwd))
  13168. mm (if minus (list 'not mm) mm))
  13169. (push mm todomatcher))
  13170. (push (if (> (length todomatcher) 1)
  13171. (cons 'and todomatcher)
  13172. (car todomatcher))
  13173. orlist)
  13174. (setq todomatcher nil))
  13175. (setq todomatcher (if (> (length orlist) 1)
  13176. (cons 'or orlist) (car orlist))))
  13177. ;; Return the string and lisp forms of the matcher
  13178. (setq matcher (if todomatcher
  13179. (list 'and tagsmatcher todomatcher)
  13180. tagsmatcher))
  13181. (when todo-only
  13182. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13183. matcher)))
  13184. (cons match0 matcher)))
  13185. (defun org-tags-expand (match &optional single-as-list downcased)
  13186. "Expand group tags in MATCH.
  13187. This replaces every group tag in MATCH with a regexp tag search.
  13188. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13189. will be replaced like this:
  13190. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13191. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13192. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13193. Replacing by a regexp preserves the structure of the match.
  13194. E.g., this expansion
  13195. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13196. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13197. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13198. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13199. assumed to be a single group tag, and the function will return
  13200. the list of tags in this group.
  13201. When DOWNCASE is non-nil, expand downcased TAGS."
  13202. (if org-group-tags
  13203. (let* ((case-fold-search t)
  13204. (stable org-mode-syntax-table)
  13205. (tal (or org-tag-groups-alist-for-agenda
  13206. org-tag-groups-alist))
  13207. (tal (if downcased
  13208. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13209. (tml (mapcar 'car tal))
  13210. (rtnmatch match) rpl)
  13211. ;; @ and _ are allowed as word-components in tags
  13212. (modify-syntax-entry ?@ "w" stable)
  13213. (modify-syntax-entry ?_ "w" stable)
  13214. (while (and tml
  13215. (with-syntax-table stable
  13216. (string-match
  13217. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13218. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13219. (let* ((dir (match-string 1 rtnmatch))
  13220. (tag (match-string 2 rtnmatch))
  13221. (tag (if downcased (downcase tag) tag)))
  13222. (setq tml (delete tag tml))
  13223. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13224. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13225. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13226. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13227. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13228. (if single-as-list
  13229. (or (reverse rpl) (list rtnmatch))
  13230. rtnmatch))
  13231. (if single-as-list (list (if downcased (downcase match) match))
  13232. match)))
  13233. (defun org-op-to-function (op &optional stringp)
  13234. "Turn an operator into the appropriate function."
  13235. (setq op
  13236. (cond
  13237. ((equal op "<" ) '(< string< org-time<))
  13238. ((equal op ">" ) '(> org-string> org-time>))
  13239. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13240. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13241. ((member op '("=" "==")) '(= string= org-time=))
  13242. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13243. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13244. (defun org<> (a b) (not (= a b)))
  13245. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13246. (defun org-string>= (a b) (not (string< a b)))
  13247. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13248. (defun org-string<> (a b) (not (string= a b)))
  13249. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13250. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13251. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13252. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13253. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13254. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13255. (defun org-2ft (s)
  13256. "Convert S to a floating point time.
  13257. If S is already a number, just return it. If it is a string, parse
  13258. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13259. (cond
  13260. ((numberp s) s)
  13261. ((stringp s)
  13262. (condition-case nil
  13263. (float-time (apply 'encode-time (org-parse-time-string s)))
  13264. (error 0.)))
  13265. (t 0.)))
  13266. (defun org-time-today ()
  13267. "Time in seconds today at 0:00.
  13268. Returns the float number of seconds since the beginning of the
  13269. epoch to the beginning of today (00:00)."
  13270. (float-time (apply 'encode-time
  13271. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13272. (defun org-matcher-time (s)
  13273. "Interpret a time comparison value."
  13274. (save-match-data
  13275. (cond
  13276. ((string= s "<now>") (float-time))
  13277. ((string= s "<today>") (org-time-today))
  13278. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13279. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13280. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13281. (+ (org-time-today)
  13282. (* (string-to-number (match-string 1 s))
  13283. (cdr (assoc (match-string 2 s)
  13284. '(("d" . 86400.0) ("w" . 604800.0)
  13285. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13286. (t (org-2ft s)))))
  13287. (defun org-match-any-p (re list)
  13288. "Does re match any element of list?"
  13289. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13290. (delq nil list))
  13291. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13292. (defvar org-tags-overlay (make-overlay 1 1))
  13293. (org-detach-overlay org-tags-overlay)
  13294. (defun org-get-local-tags-at (&optional pos)
  13295. "Get a list of tags defined in the current headline."
  13296. (org-get-tags-at pos 'local))
  13297. (defun org-get-local-tags ()
  13298. "Get a list of tags defined in the current headline."
  13299. (org-get-tags-at nil 'local))
  13300. (defun org-get-tags-at (&optional pos local)
  13301. "Get a list of all headline tags applicable at POS.
  13302. POS defaults to point. If tags are inherited, the list contains
  13303. the targets in the same sequence as the headlines appear, i.e.
  13304. the tags of the current headline come last.
  13305. When LOCAL is non-nil, only return tags from the current headline,
  13306. ignore inherited ones."
  13307. (interactive)
  13308. (if (and org-trust-scanner-tags
  13309. (or (not pos) (equal pos (point)))
  13310. (not local))
  13311. org-scanner-tags
  13312. (let (tags ltags lastpos parent)
  13313. (save-excursion
  13314. (save-restriction
  13315. (widen)
  13316. (goto-char (or pos (point)))
  13317. (save-match-data
  13318. (catch 'done
  13319. (condition-case nil
  13320. (progn
  13321. (org-back-to-heading t)
  13322. (while (not (equal lastpos (point)))
  13323. (setq lastpos (point))
  13324. (when (looking-at
  13325. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13326. (setq ltags (org-split-string
  13327. (org-match-string-no-properties 1) ":"))
  13328. (when parent
  13329. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13330. (setq tags (append
  13331. (if parent
  13332. (org-remove-uninherited-tags ltags)
  13333. ltags)
  13334. tags)))
  13335. (or org-use-tag-inheritance (throw 'done t))
  13336. (if local (throw 'done t))
  13337. (or (org-up-heading-safe) (error nil))
  13338. (setq parent t)))
  13339. (error nil)))))
  13340. (if local
  13341. tags
  13342. (reverse (delete-dups
  13343. (reverse (append
  13344. (org-remove-uninherited-tags
  13345. org-file-tags) tags)))))))))
  13346. (defun org-add-prop-inherited (s)
  13347. (add-text-properties 0 (length s) '(inherited t) s)
  13348. s)
  13349. (defun org-toggle-tag (tag &optional onoff)
  13350. "Toggle the tag TAG for the current line.
  13351. If ONOFF is `on' or `off', don't toggle but set to this state."
  13352. (let (res current)
  13353. (save-excursion
  13354. (org-back-to-heading t)
  13355. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13356. (point-at-eol) t)
  13357. (progn
  13358. (setq current (match-string 1))
  13359. (replace-match ""))
  13360. (setq current ""))
  13361. (setq current (nreverse (org-split-string current ":")))
  13362. (cond
  13363. ((eq onoff 'on)
  13364. (setq res t)
  13365. (or (member tag current) (push tag current)))
  13366. ((eq onoff 'off)
  13367. (or (not (member tag current)) (setq current (delete tag current))))
  13368. (t (if (member tag current)
  13369. (setq current (delete tag current))
  13370. (setq res t)
  13371. (push tag current))))
  13372. (end-of-line 1)
  13373. (if current
  13374. (progn
  13375. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13376. (org-set-tags nil t))
  13377. (delete-horizontal-space))
  13378. (run-hooks 'org-after-tags-change-hook))
  13379. res))
  13380. (defun org-align-tags-here (to-col)
  13381. ;; Assumes that this is a headline
  13382. "Align tags on the current headline to TO-COL."
  13383. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13384. (beginning-of-line 1)
  13385. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13386. (< pos (match-beginning 2)))
  13387. (progn
  13388. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13389. (goto-char (match-beginning 1))
  13390. (insert " ")
  13391. (delete-region (point) (1+ (match-beginning 2)))
  13392. (setq ncol (max (current-column)
  13393. (1+ col)
  13394. (if (> to-col 0)
  13395. to-col
  13396. (- (abs to-col) tags-l))))
  13397. (setq p (point))
  13398. (insert (make-string (- ncol (current-column)) ?\ ))
  13399. (setq ncol (current-column))
  13400. (when indent-tabs-mode (tabify p (point-at-eol)))
  13401. (org-move-to-column (min ncol col)))
  13402. (goto-char pos))))
  13403. (defun org-set-tags-command (&optional arg just-align)
  13404. "Call the set-tags command for the current entry."
  13405. (interactive "P")
  13406. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13407. (org-set-tags arg just-align)
  13408. (save-excursion
  13409. (unless (and (org-region-active-p)
  13410. org-loop-over-headlines-in-active-region)
  13411. (org-back-to-heading t))
  13412. (org-set-tags arg just-align))))
  13413. (defun org-set-tags-to (data)
  13414. "Set the tags of the current entry to DATA, replacing the current tags.
  13415. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13416. If DATA is nil or the empty string, any tags will be removed."
  13417. (interactive "sTags: ")
  13418. (setq data
  13419. (cond
  13420. ((eq data nil) "")
  13421. ((equal data "") "")
  13422. ((stringp data)
  13423. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13424. ":"))
  13425. ((listp data)
  13426. (concat ":" (mapconcat 'identity data ":") ":"))))
  13427. (when data
  13428. (save-excursion
  13429. (org-back-to-heading t)
  13430. (when (looking-at org-complex-heading-regexp)
  13431. (if (match-end 5)
  13432. (progn
  13433. (goto-char (match-beginning 5))
  13434. (insert data)
  13435. (delete-region (point) (point-at-eol))
  13436. (org-set-tags nil 'align))
  13437. (goto-char (point-at-eol))
  13438. (insert " " data)
  13439. (org-set-tags nil 'align)))
  13440. (beginning-of-line 1)
  13441. (if (looking-at ".*?\\([ \t]+\\)$")
  13442. (delete-region (match-beginning 1) (match-end 1))))))
  13443. (defun org-align-all-tags ()
  13444. "Align the tags i all headings."
  13445. (interactive)
  13446. (save-excursion
  13447. (or (ignore-errors (org-back-to-heading t))
  13448. (outline-next-heading))
  13449. (if (org-at-heading-p)
  13450. (org-set-tags t)
  13451. (message "No headings"))))
  13452. (defvar org-indent-indentation-per-level)
  13453. (defun org-set-tags (&optional arg just-align)
  13454. "Set the tags for the current headline.
  13455. With prefix ARG, realign all tags in headings in the current buffer.
  13456. When JUST-ALIGN is non-nil, only align tags."
  13457. (interactive "P")
  13458. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13459. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13460. 'region-start-level 'region))
  13461. org-loop-over-headlines-in-active-region)
  13462. (org-map-entries
  13463. ;; We don't use ARG and JUST-ALIGN here because these args
  13464. ;; are not useful when looping over headlines.
  13465. `(org-set-tags)
  13466. org-loop-over-headlines-in-active-region
  13467. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13468. (let* ((re org-outline-regexp-bol)
  13469. (current (unless arg (org-get-tags-string)))
  13470. (col (current-column))
  13471. (org-setting-tags t)
  13472. table current-tags inherited-tags ; computed below when needed
  13473. tags p0 c0 c1 rpl di tc level)
  13474. (if arg
  13475. (save-excursion
  13476. (goto-char (point-min))
  13477. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13478. (while (re-search-forward re nil t)
  13479. (org-set-tags nil t)
  13480. (end-of-line 1)))
  13481. (message "All tags realigned to column %d" org-tags-column))
  13482. (if just-align
  13483. (setq tags current)
  13484. ;; Get a new set of tags from the user
  13485. (save-excursion
  13486. (setq table (append org-tag-persistent-alist
  13487. (or org-tag-alist (org-get-buffer-tags))
  13488. (and
  13489. org-complete-tags-always-offer-all-agenda-tags
  13490. (org-global-tags-completion-table
  13491. (org-agenda-files))))
  13492. org-last-tags-completion-table table
  13493. current-tags (org-split-string current ":")
  13494. inherited-tags (nreverse
  13495. (nthcdr (length current-tags)
  13496. (nreverse (org-get-tags-at))))
  13497. tags
  13498. (if (or (eq t org-use-fast-tag-selection)
  13499. (and org-use-fast-tag-selection
  13500. (delq nil (mapcar 'cdr table))))
  13501. (org-fast-tag-selection
  13502. current-tags inherited-tags table
  13503. (if org-fast-tag-selection-include-todo
  13504. org-todo-key-alist))
  13505. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13506. (org-trim
  13507. (org-icompleting-read "Tags: "
  13508. 'org-tags-completion-function
  13509. nil nil current 'org-tags-history))))))
  13510. (while (string-match "[-+&]+" tags)
  13511. ;; No boolean logic, just a list
  13512. (setq tags (replace-match ":" t t tags))))
  13513. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13514. (if org-tags-sort-function
  13515. (setq tags (mapconcat 'identity
  13516. (sort (org-split-string
  13517. tags (org-re "[^[:alnum:]_@#%]+"))
  13518. org-tags-sort-function) ":")))
  13519. (if (string-match "\\`[\t ]*\\'" tags)
  13520. (setq tags "")
  13521. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13522. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13523. ;; Insert new tags at the correct column
  13524. (beginning-of-line 1)
  13525. (setq level (or (and (looking-at org-outline-regexp)
  13526. (- (match-end 0) (point) 1))
  13527. 1))
  13528. (cond
  13529. ((and (equal current "") (equal tags "")))
  13530. ((re-search-forward
  13531. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13532. (point-at-eol) t)
  13533. (if (equal tags "")
  13534. (setq rpl "")
  13535. (goto-char (match-beginning 0))
  13536. (setq c0 (current-column)
  13537. ;; compute offset for the case of org-indent-mode active
  13538. di (if (org-bound-and-true-p org-indent-mode)
  13539. (* (1- org-indent-indentation-per-level) (1- level))
  13540. 0)
  13541. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13542. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13543. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13544. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13545. (replace-match rpl t t)
  13546. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13547. tags)
  13548. (t (error "Tags alignment failed")))
  13549. (org-move-to-column col)
  13550. (unless just-align
  13551. (run-hooks 'org-after-tags-change-hook))))))
  13552. (defun org-change-tag-in-region (beg end tag off)
  13553. "Add or remove TAG for each entry in the region.
  13554. This works in the agenda, and also in an org-mode buffer."
  13555. (interactive
  13556. (list (region-beginning) (region-end)
  13557. (let ((org-last-tags-completion-table
  13558. (if (derived-mode-p 'org-mode)
  13559. (org-uniquify
  13560. (delq nil (append (org-get-buffer-tags)
  13561. (org-global-tags-completion-table))))
  13562. (org-global-tags-completion-table))))
  13563. (org-icompleting-read
  13564. "Tag: " 'org-tags-completion-function nil nil nil
  13565. 'org-tags-history))
  13566. (progn
  13567. (message "[s]et or [r]emove? ")
  13568. (equal (read-char-exclusive) ?r))))
  13569. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13570. (let ((agendap (equal major-mode 'org-agenda-mode))
  13571. l1 l2 m buf pos newhead (cnt 0))
  13572. (goto-char end)
  13573. (setq l2 (1- (org-current-line)))
  13574. (goto-char beg)
  13575. (setq l1 (org-current-line))
  13576. (loop for l from l1 to l2 do
  13577. (org-goto-line l)
  13578. (setq m (get-text-property (point) 'org-hd-marker))
  13579. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13580. (and agendap m))
  13581. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13582. pos (if agendap m (point)))
  13583. (with-current-buffer buf
  13584. (save-excursion
  13585. (save-restriction
  13586. (goto-char pos)
  13587. (setq cnt (1+ cnt))
  13588. (org-toggle-tag tag (if off 'off 'on))
  13589. (setq newhead (org-get-heading)))))
  13590. (and agendap (org-agenda-change-all-lines newhead m))))
  13591. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13592. (defun org-tags-completion-function (string predicate &optional flag)
  13593. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13594. (confirm (lambda (x) (stringp (car x)))))
  13595. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13596. (setq s1 (match-string 1 string)
  13597. s2 (match-string 2 string))
  13598. (setq s1 "" s2 string))
  13599. (cond
  13600. ((eq flag nil)
  13601. ;; try completion
  13602. (setq rtn (try-completion s2 ctable confirm))
  13603. (if (stringp rtn)
  13604. (setq rtn
  13605. (concat s1 s2 (substring rtn (length s2))
  13606. (if (and org-add-colon-after-tag-completion
  13607. (assoc rtn ctable))
  13608. ":" ""))))
  13609. rtn)
  13610. ((eq flag t)
  13611. ;; all-completions
  13612. (all-completions s2 ctable confirm))
  13613. ((eq flag 'lambda)
  13614. ;; exact match?
  13615. (assoc s2 ctable)))))
  13616. (defun org-fast-tag-insert (kwd tags face &optional end)
  13617. "Insert KDW, and the TAGS, the latter with face FACE.
  13618. Also insert END."
  13619. (insert (format "%-12s" (concat kwd ":"))
  13620. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13621. (or end "")))
  13622. (defun org-fast-tag-show-exit (flag)
  13623. (save-excursion
  13624. (org-goto-line 3)
  13625. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13626. (replace-match ""))
  13627. (when flag
  13628. (end-of-line 1)
  13629. (org-move-to-column (- (window-width) 19) t)
  13630. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13631. (defun org-set-current-tags-overlay (current prefix)
  13632. "Add an overlay to CURRENT tag with PREFIX."
  13633. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13634. (if (featurep 'xemacs)
  13635. (org-overlay-display org-tags-overlay (concat prefix s)
  13636. 'secondary-selection)
  13637. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13638. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13639. (defvar org-last-tag-selection-key nil)
  13640. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13641. "Fast tag selection with single keys.
  13642. CURRENT is the current list of tags in the headline, INHERITED is the
  13643. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13644. possibly with grouping information. TODO-TABLE is a similar table with
  13645. TODO keywords, should these have keys assigned to them.
  13646. If the keys are nil, a-z are automatically assigned.
  13647. Returns the new tags string, or nil to not change the current settings."
  13648. (let* ((fulltable (append table todo-table))
  13649. (maxlen (apply 'max (mapcar
  13650. (lambda (x)
  13651. (if (stringp (car x)) (string-width (car x)) 0))
  13652. fulltable)))
  13653. (buf (current-buffer))
  13654. (expert (eq org-fast-tag-selection-single-key 'expert))
  13655. (buffer-tags nil)
  13656. (fwidth (+ maxlen 3 1 3))
  13657. (ncol (/ (- (window-width) 4) fwidth))
  13658. (i-face 'org-done)
  13659. (c-face 'org-todo)
  13660. tg cnt e c char c1 c2 ntable tbl rtn
  13661. ov-start ov-end ov-prefix
  13662. (exit-after-next org-fast-tag-selection-single-key)
  13663. (done-keywords org-done-keywords)
  13664. groups ingroup)
  13665. (save-excursion
  13666. (beginning-of-line 1)
  13667. (if (looking-at
  13668. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13669. (setq ov-start (match-beginning 1)
  13670. ov-end (match-end 1)
  13671. ov-prefix "")
  13672. (setq ov-start (1- (point-at-eol))
  13673. ov-end (1+ ov-start))
  13674. (skip-chars-forward "^\n\r")
  13675. (setq ov-prefix
  13676. (concat
  13677. (buffer-substring (1- (point)) (point))
  13678. (if (> (current-column) org-tags-column)
  13679. " "
  13680. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13681. (move-overlay org-tags-overlay ov-start ov-end)
  13682. (save-window-excursion
  13683. (if expert
  13684. (set-buffer (get-buffer-create " *Org tags*"))
  13685. (delete-other-windows)
  13686. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13687. (org-switch-to-buffer-other-window " *Org tags*"))
  13688. (erase-buffer)
  13689. (org-set-local 'org-done-keywords done-keywords)
  13690. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13691. (org-fast-tag-insert "Current" current c-face "\n\n")
  13692. (org-fast-tag-show-exit exit-after-next)
  13693. (org-set-current-tags-overlay current ov-prefix)
  13694. (setq tbl fulltable char ?a cnt 0)
  13695. (while (setq e (pop tbl))
  13696. (cond
  13697. ((equal (car e) :startgroup)
  13698. (push '() groups) (setq ingroup t)
  13699. (when (not (= cnt 0))
  13700. (setq cnt 0)
  13701. (insert "\n"))
  13702. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13703. ((equal (car e) :endgroup)
  13704. (setq ingroup nil cnt 0)
  13705. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13706. ((equal e '(:newline))
  13707. (when (not (= cnt 0))
  13708. (setq cnt 0)
  13709. (insert "\n")
  13710. (setq e (car tbl))
  13711. (while (equal (car tbl) '(:newline))
  13712. (insert "\n")
  13713. (setq tbl (cdr tbl)))))
  13714. ((equal e '(:grouptags)) nil)
  13715. (t
  13716. (setq tg (copy-sequence (car e)) c2 nil)
  13717. (if (cdr e)
  13718. (setq c (cdr e))
  13719. ;; automatically assign a character.
  13720. (setq c1 (string-to-char
  13721. (downcase (substring
  13722. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13723. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13724. (while (or (rassoc char ntable) (rassoc char table))
  13725. (setq char (1+ char)))
  13726. (setq c2 c1))
  13727. (setq c (or c2 char)))
  13728. (if ingroup (push tg (car groups)))
  13729. (setq tg (org-add-props tg nil 'face
  13730. (cond
  13731. ((not (assoc tg table))
  13732. (org-get-todo-face tg))
  13733. ((member tg current) c-face)
  13734. ((member tg inherited) i-face))))
  13735. (if (equal (caar tbl) :grouptags)
  13736. (org-add-props tg nil 'face 'org-tag-group))
  13737. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13738. (insert "[" c "] " tg (make-string
  13739. (- fwidth 4 (length tg)) ?\ ))
  13740. (push (cons tg c) ntable)
  13741. (when (= (setq cnt (1+ cnt)) ncol)
  13742. (insert "\n")
  13743. (if ingroup (insert " "))
  13744. (setq cnt 0)))))
  13745. (setq ntable (nreverse ntable))
  13746. (insert "\n")
  13747. (goto-char (point-min))
  13748. (if (not expert) (org-fit-window-to-buffer))
  13749. (setq rtn
  13750. (catch 'exit
  13751. (while t
  13752. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13753. (if (not groups) "no " "")
  13754. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13755. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13756. (setq org-last-tag-selection-key c)
  13757. (cond
  13758. ((= c ?\r) (throw 'exit t))
  13759. ((= c ?!)
  13760. (setq groups (not groups))
  13761. (goto-char (point-min))
  13762. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13763. ((= c ?\C-c)
  13764. (if (not expert)
  13765. (org-fast-tag-show-exit
  13766. (setq exit-after-next (not exit-after-next)))
  13767. (setq expert nil)
  13768. (delete-other-windows)
  13769. (set-window-buffer (split-window-vertically) " *Org tags*")
  13770. (org-switch-to-buffer-other-window " *Org tags*")
  13771. (org-fit-window-to-buffer)))
  13772. ((or (= c ?\C-g)
  13773. (and (= c ?q) (not (rassoc c ntable))))
  13774. (org-detach-overlay org-tags-overlay)
  13775. (setq quit-flag t))
  13776. ((= c ?\ )
  13777. (setq current nil)
  13778. (if exit-after-next (setq exit-after-next 'now)))
  13779. ((= c ?\t)
  13780. (condition-case nil
  13781. (setq tg (org-icompleting-read
  13782. "Tag: "
  13783. (or buffer-tags
  13784. (with-current-buffer buf
  13785. (org-get-buffer-tags)))))
  13786. (quit (setq tg "")))
  13787. (when (string-match "\\S-" tg)
  13788. (add-to-list 'buffer-tags (list tg))
  13789. (if (member tg current)
  13790. (setq current (delete tg current))
  13791. (push tg current)))
  13792. (if exit-after-next (setq exit-after-next 'now)))
  13793. ((setq e (rassoc c todo-table) tg (car e))
  13794. (with-current-buffer buf
  13795. (save-excursion (org-todo tg)))
  13796. (if exit-after-next (setq exit-after-next 'now)))
  13797. ((setq e (rassoc c ntable) tg (car e))
  13798. (if (member tg current)
  13799. (setq current (delete tg current))
  13800. (loop for g in groups do
  13801. (if (member tg g)
  13802. (mapc (lambda (x)
  13803. (setq current (delete x current)))
  13804. g)))
  13805. (push tg current))
  13806. (if exit-after-next (setq exit-after-next 'now))))
  13807. ;; Create a sorted list
  13808. (setq current
  13809. (sort current
  13810. (lambda (a b)
  13811. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13812. (if (eq exit-after-next 'now) (throw 'exit t))
  13813. (goto-char (point-min))
  13814. (beginning-of-line 2)
  13815. (delete-region (point) (point-at-eol))
  13816. (org-fast-tag-insert "Current" current c-face)
  13817. (org-set-current-tags-overlay current ov-prefix)
  13818. (while (re-search-forward
  13819. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13820. (setq tg (match-string 1))
  13821. (add-text-properties
  13822. (match-beginning 1) (match-end 1)
  13823. (list 'face
  13824. (cond
  13825. ((member tg current) c-face)
  13826. ((member tg inherited) i-face)
  13827. (t (get-text-property (match-beginning 1) 'face))))))
  13828. (goto-char (point-min)))))
  13829. (org-detach-overlay org-tags-overlay)
  13830. (if rtn
  13831. (mapconcat 'identity current ":")
  13832. nil))))
  13833. (defun org-get-tags-string ()
  13834. "Get the TAGS string in the current headline."
  13835. (unless (org-at-heading-p t)
  13836. (user-error "Not on a heading"))
  13837. (save-excursion
  13838. (beginning-of-line 1)
  13839. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13840. (org-match-string-no-properties 1)
  13841. "")))
  13842. (defun org-get-tags ()
  13843. "Get the list of tags specified in the current headline."
  13844. (org-split-string (org-get-tags-string) ":"))
  13845. (defun org-get-buffer-tags ()
  13846. "Get a table of all tags used in the buffer, for completion."
  13847. (let (tags)
  13848. (save-excursion
  13849. (goto-char (point-min))
  13850. (while (re-search-forward
  13851. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13852. (when (equal (char-after (point-at-bol 0)) ?*)
  13853. (mapc (lambda (x) (add-to-list 'tags x))
  13854. (org-split-string (org-match-string-no-properties 1) ":")))))
  13855. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13856. (mapcar 'list tags)))
  13857. ;;;; The mapping API
  13858. (defun org-map-entries (func &optional match scope &rest skip)
  13859. "Call FUNC at each headline selected by MATCH in SCOPE.
  13860. FUNC is a function or a lisp form. The function will be called without
  13861. arguments, with the cursor positioned at the beginning of the headline.
  13862. The return values of all calls to the function will be collected and
  13863. returned as a list.
  13864. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13865. does not need to preserve point. After evaluation, the cursor will be
  13866. moved to the end of the line (presumably of the headline of the
  13867. processed entry) and search continues from there. Under some
  13868. circumstances, this may not produce the wanted results. For example,
  13869. if you have removed (e.g. archived) the current (sub)tree it could
  13870. mean that the next entry will be skipped entirely. In such cases, you
  13871. can specify the position from where search should continue by making
  13872. FUNC set the variable `org-map-continue-from' to the desired buffer
  13873. position.
  13874. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13875. Only headlines that are matched by this query will be considered during
  13876. the iteration. When MATCH is nil or t, all headlines will be
  13877. visited by the iteration.
  13878. SCOPE determines the scope of this command. It can be any of:
  13879. nil The current buffer, respecting the restriction if any
  13880. tree The subtree started with the entry at point
  13881. region The entries within the active region, if any
  13882. region-start-level
  13883. The entries within the active region, but only those at
  13884. the same level than the first one.
  13885. file The current buffer, without restriction
  13886. file-with-archives
  13887. The current buffer, and any archives associated with it
  13888. agenda All agenda files
  13889. agenda-with-archives
  13890. All agenda files with any archive files associated with them
  13891. \(file1 file2 ...)
  13892. If this is a list, all files in the list will be scanned
  13893. The remaining args are treated as settings for the skipping facilities of
  13894. the scanner. The following items can be given here:
  13895. archive skip trees with the archive tag
  13896. comment skip trees with the COMMENT keyword
  13897. function or Emacs Lisp form:
  13898. will be used as value for `org-agenda-skip-function', so
  13899. whenever the function returns a position, FUNC will not be
  13900. called for that entry and search will continue from the
  13901. position returned
  13902. If your function needs to retrieve the tags including inherited tags
  13903. at the *current* entry, you can use the value of the variable
  13904. `org-scanner-tags' which will be much faster than getting the value
  13905. with `org-get-tags-at'. If your function gets properties with
  13906. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13907. to t around the call to `org-entry-properties' to get the same speedup.
  13908. Note that if your function moves around to retrieve tags and properties at
  13909. a *different* entry, you cannot use these techniques."
  13910. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13911. (not (org-region-active-p)))
  13912. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13913. (org-agenda-skip-archived-trees (memq 'archive skip))
  13914. (org-agenda-skip-comment-trees (memq 'comment skip))
  13915. (org-agenda-skip-function
  13916. (car (org-delete-all '(comment archive) skip)))
  13917. (org-tags-match-list-sublevels t)
  13918. (start-level (eq scope 'region-start-level))
  13919. matcher file res
  13920. org-todo-keywords-for-agenda
  13921. org-done-keywords-for-agenda
  13922. org-todo-keyword-alist-for-agenda
  13923. org-tag-alist-for-agenda
  13924. todo-only)
  13925. (cond
  13926. ((eq match t) (setq matcher t))
  13927. ((eq match nil) (setq matcher t))
  13928. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13929. (save-excursion
  13930. (save-restriction
  13931. (cond ((eq scope 'tree)
  13932. (org-back-to-heading t)
  13933. (org-narrow-to-subtree)
  13934. (setq scope nil))
  13935. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13936. (org-region-active-p))
  13937. ;; If needed, set start-level to a string like "2"
  13938. (when start-level
  13939. (save-excursion
  13940. (goto-char (region-beginning))
  13941. (unless (org-at-heading-p) (outline-next-heading))
  13942. (setq start-level (org-current-level))))
  13943. (narrow-to-region (region-beginning)
  13944. (save-excursion
  13945. (goto-char (region-end))
  13946. (unless (and (bolp) (org-at-heading-p))
  13947. (outline-next-heading))
  13948. (point)))
  13949. (setq scope nil)))
  13950. (if (not scope)
  13951. (progn
  13952. (org-agenda-prepare-buffers
  13953. (list (buffer-file-name (current-buffer))))
  13954. (setq res (org-scan-tags func matcher todo-only start-level)))
  13955. ;; Get the right scope
  13956. (cond
  13957. ((and scope (listp scope) (symbolp (car scope)))
  13958. (setq scope (eval scope)))
  13959. ((eq scope 'agenda)
  13960. (setq scope (org-agenda-files t)))
  13961. ((eq scope 'agenda-with-archives)
  13962. (setq scope (org-agenda-files t))
  13963. (setq scope (org-add-archive-files scope)))
  13964. ((eq scope 'file)
  13965. (setq scope (list (buffer-file-name))))
  13966. ((eq scope 'file-with-archives)
  13967. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13968. (org-agenda-prepare-buffers scope)
  13969. (while (setq file (pop scope))
  13970. (with-current-buffer (org-find-base-buffer-visiting file)
  13971. (save-excursion
  13972. (save-restriction
  13973. (widen)
  13974. (goto-char (point-min))
  13975. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13976. res)))
  13977. ;;; Properties API
  13978. (defconst org-special-properties
  13979. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOSED" "PRIORITY"
  13980. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13981. "The special properties valid in Org-mode.
  13982. These are properties that are not defined in the property drawer,
  13983. but in some other way.")
  13984. (defconst org-default-properties
  13985. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13986. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13987. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13988. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13989. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  13990. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13991. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13992. "Some properties that are used by Org mode for various purposes.
  13993. Being in this list makes sure that they are offered for completion.")
  13994. (defun org--update-property-plist (key val props)
  13995. "Associate KEY to VAL in alist PROPS.
  13996. Modifications are made by side-effect. Return new alist."
  13997. (let* ((appending (string= (substring key -1) "+"))
  13998. (key (if appending (substring key 0 -1) key))
  13999. (old (assoc-string key props t)))
  14000. (if (not old) (cons (cons key val) props)
  14001. (setcdr old (if appending (concat (cdr old) " " val) val))
  14002. props)))
  14003. (defun org-get-property-block (&optional beg force)
  14004. "Return the (beg . end) range of the body of the property drawer.
  14005. BEG is the beginning of the current subtree, or of the part
  14006. before the first headline. If it is not given, it will be found.
  14007. If the drawer does not exist, create it if FORCE is non-nil, or
  14008. return nil."
  14009. (org-with-wide-buffer
  14010. (when beg (goto-char beg))
  14011. (unless (org-before-first-heading-p)
  14012. (let ((beg (cond (beg)
  14013. ((or (not (featurep 'org-inlinetask))
  14014. (org-inlinetask-in-task-p))
  14015. (org-back-to-heading t))
  14016. (t (org-with-limited-levels (org-back-to-heading t))))))
  14017. (forward-line)
  14018. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14019. (cond ((looking-at org-property-drawer-re)
  14020. (forward-line)
  14021. (cons (point) (progn (goto-char (match-end 0))
  14022. (line-beginning-position))))
  14023. (force
  14024. (goto-char beg)
  14025. (org-insert-property-drawer)
  14026. (let ((pos (save-excursion (search-forward ":END:")
  14027. (line-beginning-position))))
  14028. (cons pos pos))))))))
  14029. (defun org-at-property-p ()
  14030. "Non-nil when point is inside a property drawer.
  14031. See `org-property-re' for match data, if applicable."
  14032. (save-excursion
  14033. (beginning-of-line)
  14034. (and (looking-at org-property-re)
  14035. (let ((property-drawer (save-match-data (org-get-property-block))))
  14036. (and property-drawer (< (point) (cdr property-drawer)))))))
  14037. (defun org-property-action ()
  14038. "Do an action on properties."
  14039. (interactive)
  14040. (unless (org-at-property-p) (user-error "Not at a property"))
  14041. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14042. (let ((c (read-char-exclusive)))
  14043. (case c
  14044. (?s (call-interactively #'org-set-property))
  14045. (?d (call-interactively #'org-delete-property))
  14046. (?D (call-interactively #'org-delete-property-globally))
  14047. (?c (call-interactively #'org-compute-property-at-point))
  14048. (otherwise (user-error "No such property action %c" c)))))
  14049. (defun org-inc-effort ()
  14050. "Increment the value of the effort property in the current entry."
  14051. (interactive)
  14052. (org-set-effort nil t))
  14053. (defvar org-clock-effort) ; Defined in org-clock.el.
  14054. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14055. (defun org-set-effort (&optional value increment)
  14056. "Set the effort property of the current entry.
  14057. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14058. 10th allowed value.
  14059. When INCREMENT is non-nil, set the property to the next allowed value."
  14060. (interactive "P")
  14061. (if (equal value 0) (setq value 10))
  14062. (let* ((completion-ignore-case t)
  14063. (prop org-effort-property)
  14064. (cur (org-entry-get nil prop))
  14065. (allowed (org-property-get-allowed-values nil prop 'table))
  14066. (existing (mapcar 'list (org-property-values prop)))
  14067. (heading (nth 4 (org-heading-components)))
  14068. rpl
  14069. (val (cond
  14070. ((stringp value) value)
  14071. ((and allowed (integerp value))
  14072. (or (car (nth (1- value) allowed))
  14073. (car (org-last allowed))))
  14074. ((and allowed increment)
  14075. (or (caadr (member (list cur) allowed))
  14076. (user-error "Allowed effort values are not set")))
  14077. (allowed
  14078. (message "Select 1-9,0, [RET%s]: %s"
  14079. (if cur (concat "=" cur) "")
  14080. (mapconcat 'car allowed " "))
  14081. (setq rpl (read-char-exclusive))
  14082. (if (equal rpl ?\r)
  14083. cur
  14084. (setq rpl (- rpl ?0))
  14085. (if (equal rpl 0) (setq rpl 10))
  14086. (if (and (> rpl 0) (<= rpl (length allowed)))
  14087. (car (nth (1- rpl) allowed))
  14088. (org-completing-read "Effort: " allowed nil))))
  14089. (t
  14090. (let (org-completion-use-ido org-completion-use-iswitchb)
  14091. (org-completing-read
  14092. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14093. (concat "[" cur "]") "")
  14094. ": ")
  14095. existing nil nil "" nil cur))))))
  14096. (unless (equal (org-entry-get nil prop) val)
  14097. (org-entry-put nil prop val))
  14098. (org-refresh-property
  14099. '((effort . identity)
  14100. (effort-minutes . org-duration-string-to-minutes))
  14101. val)
  14102. (when (string= heading org-clock-current-task)
  14103. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14104. (org-clock-update-mode-line))
  14105. (message "%s is now %s" prop val)))
  14106. (defun org-entry-properties (&optional pom which)
  14107. "Get all properties of the current entry.
  14108. When POM is a buffer position, get all properties from the entry
  14109. there instead.
  14110. This includes the TODO keyword, the tags, time strings for
  14111. deadline, scheduled, and clocking, and any additional properties
  14112. defined in the entry.
  14113. If WHICH is nil or `all', get all properties. If WHICH is
  14114. `special' or `standard', only get that subclass. If WHICH is
  14115. a string, only get that property.
  14116. Return value is an alist. Keys are properties, as upcased
  14117. strings."
  14118. (org-with-point-at pom
  14119. (when (and (derived-mode-p 'org-mode)
  14120. (ignore-errors (org-back-to-heading t)))
  14121. (catch 'exit
  14122. (let* ((beg (point))
  14123. (specific (and (stringp which) (upcase which)))
  14124. (which (cond ((not specific) which)
  14125. ((member specific org-special-properties) 'special)
  14126. (t 'standard)))
  14127. props)
  14128. ;; Get the special properties, like TODO and TAGS.
  14129. (when (memq which '(nil all special))
  14130. (when (or (not specific) (string= specific "CLOCKSUM"))
  14131. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14132. (when clocksum
  14133. (push (cons "CLOCKSUM"
  14134. (org-columns-number-to-string
  14135. (/ (float clocksum) 60.) 'add_times))
  14136. props)))
  14137. (when specific (throw 'exit props)))
  14138. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14139. (let ((clocksumt (get-text-property (point)
  14140. :org-clock-minutes-today)))
  14141. (when clocksumt
  14142. (push (cons "CLOCKSUM_T"
  14143. (org-columns-number-to-string
  14144. (/ (float clocksumt) 60.) 'add_times))
  14145. props)))
  14146. (when specific (throw 'exit props)))
  14147. (when (or (not specific) (string= specific "TODO"))
  14148. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14149. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14150. (when specific (throw 'exit props)))
  14151. (when (or (not specific) (string= specific "PRIORITY"))
  14152. (when (looking-at org-priority-regexp)
  14153. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14154. (when specific (throw 'exit props)))
  14155. (when (or (not specific) (string= specific "FILE"))
  14156. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14157. props)
  14158. (when specific (throw 'exit props)))
  14159. (when (or (not specific) (string= specific "TAGS"))
  14160. (let ((value (org-string-nw-p (org-get-tags-string))))
  14161. (when value (push (cons "TAGS" value) props)))
  14162. (when specific (throw 'exit props)))
  14163. (when (or (not specific) (string= specific "ALLTAGS"))
  14164. (let ((value (org-get-tags-at)))
  14165. (when value
  14166. (push (cons "ALLTAGS"
  14167. (format ":%s:" (mapconcat #'identity value ":")))
  14168. props)))
  14169. (when specific (throw 'exit props)))
  14170. (when (or (not specific) (string= specific "BLOCKED"))
  14171. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14172. (when specific (throw 'exit props)))
  14173. (when (or (not specific)
  14174. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14175. (forward-line)
  14176. (when (org-looking-at-p org-planning-line-re)
  14177. (end-of-line)
  14178. (let ((bol (line-beginning-position)))
  14179. ;; Backward compatibility: time keywords used to be
  14180. ;; configurable (before 8.3). Make sure we get the
  14181. ;; correct keyword.
  14182. (dolist (k (if (not specific)
  14183. (list org-closed-string
  14184. org-deadline-string
  14185. org-scheduled-string)
  14186. (list (cond ((string= specific "CLOSED")
  14187. org-closed-string)
  14188. ((string= specific "DEADLINE")
  14189. org-deadline-string)
  14190. (t org-scheduled-string)))))
  14191. (save-excursion
  14192. (when (search-backward k bol t)
  14193. (goto-char (match-end 0))
  14194. (skip-chars-forward " \t")
  14195. (and (looking-at org-ts-regexp-both)
  14196. (push (cons specific (match-string 0)) props)))))))
  14197. (when specific (throw 'exit props)))
  14198. (when (or (not specific)
  14199. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14200. (let ((find-ts
  14201. (lambda (end ts)
  14202. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14203. (assoc "TIMESTAMP_IA" ts))
  14204. org-ts-regexp
  14205. org-ts-regexp-both)))
  14206. (catch 'next
  14207. (while (re-search-forward regexp end t)
  14208. (backward-char)
  14209. (let ((object (org-element-context)))
  14210. ;; Accept to match timestamps in node
  14211. ;; properties, too.
  14212. (when (memq (org-element-type object)
  14213. '(node-property timestamp))
  14214. (let ((type
  14215. (org-element-property :type object)))
  14216. (cond
  14217. ((and (memq type '(active active-range))
  14218. (not (equal specific "TIMESTAMP_IA")))
  14219. (unless (assoc "TIMESTAMP" ts)
  14220. (push (cons "TIMESTAMP"
  14221. (org-element-property
  14222. :raw-value object))
  14223. ts)
  14224. (when specific (throw 'exit ts))))
  14225. ((and (memq type '(inactive inactive-range))
  14226. (not (string= specific "TIMESTAMP")))
  14227. (unless (assoc "TIMESTAMP_IA" ts)
  14228. (push (cons "TIMESTAMP_IA"
  14229. (org-element-property
  14230. :raw-value object))
  14231. ts)
  14232. (when specific (throw 'exit ts))))))
  14233. ;; Both timestamp types are found,
  14234. ;; move to next part.
  14235. (when (= (length ts) 2) (throw 'next ts)))))
  14236. ts)))))
  14237. (goto-char beg)
  14238. ;; First look for timestamps within headline.
  14239. (let ((ts (funcall find-ts (line-end-position) nil)))
  14240. (if (= (length ts) 2) (setq props (nconc ts props))
  14241. (forward-line)
  14242. ;; Then find timestamps in the section, skipping
  14243. ;; planning line.
  14244. (when (org-looking-at-p org-planning-line-re)
  14245. (forward-line))
  14246. (let ((end (save-excursion (outline-next-heading))))
  14247. (setq props (nconc (funcall find-ts end ts) props))))))))
  14248. ;; Get the standard properties, like :PROP:.
  14249. (when (memq which '(nil all standard))
  14250. ;; If we are looking after a specific property, delegate
  14251. ;; to `org-entry-get', which is faster. However, make an
  14252. ;; exception for "CATEGORY", since it can be also set
  14253. ;; through keywords (i.e. #+CATEGORY).
  14254. (if (and specific (not (equal specific "CATEGORY")))
  14255. (let ((value (org-entry-get beg specific nil t)))
  14256. (throw 'exit (and value (list (cons specific value)))))
  14257. (let ((range (org-get-property-block beg)))
  14258. (when range
  14259. (let ((end (cdr range)) seen-base)
  14260. (goto-char (car range))
  14261. ;; Unlike to `org--update-property-plist', we
  14262. ;; handle the case where base values is found
  14263. ;; after its extension. We also forbid standard
  14264. ;; properties to be named as special properties.
  14265. (while (re-search-forward org-property-re end t)
  14266. (let* ((key (upcase (org-match-string-no-properties 2)))
  14267. (extendp (org-string-match-p "\\+\\'" key))
  14268. (key-base (if extendp (substring key 0 -1) key))
  14269. (value (org-match-string-no-properties 3)))
  14270. (cond
  14271. ((member-ignore-case key-base org-special-properties))
  14272. (extendp
  14273. (setq props
  14274. (org--update-property-plist key value props)))
  14275. ((member key seen-base))
  14276. (t (push key seen-base)
  14277. (let ((p (assoc-string key props t)))
  14278. (if p (setcdr p (concat value " " (cdr p)))
  14279. (push (cons key value) props))))))))))))
  14280. (unless (assoc "CATEGORY" props)
  14281. (push (cons "CATEGORY" (org-get-category)) props)
  14282. (when (string= specific "CATEGORY") (throw 'exit props)))
  14283. ;; Return value.
  14284. (append (get-text-property beg 'org-summaries) props))))))
  14285. (defun org-entry-get (pom property &optional inherit literal-nil)
  14286. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14287. If INHERIT is non-nil and the entry does not have the property,
  14288. then also check higher levels of the hierarchy. If INHERIT is
  14289. the symbol `selective', use inheritance only if the setting in
  14290. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14291. If the property is present but empty, the return value is the
  14292. empty string. If the property is not present at all, nil is
  14293. returned. In any other case, return the value as a string.
  14294. Search is case-insensitive.
  14295. If LITERAL-NIL is set, return the string value \"nil\" as
  14296. a string, do not interpret it as the list atom nil. This is used
  14297. for inheritance when a \"nil\" value can supersede a non-nil
  14298. value higher up the hierarchy."
  14299. (org-with-point-at pom
  14300. (cond
  14301. ((and inherit
  14302. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14303. (org-entry-get-with-inheritance property literal-nil))
  14304. ((member-ignore-case property org-special-properties)
  14305. ;; We need a special property. Use `org-entry-properties' to
  14306. ;; retrieve it, but specify the wanted property.
  14307. (cdr (assoc-string property (org-entry-properties nil property))))
  14308. (t
  14309. (let ((range (org-get-property-block)))
  14310. (when range
  14311. (let* ((case-fold-search t)
  14312. (end (cdr range))
  14313. (props
  14314. (let ((global
  14315. (or (assoc-string property org-file-properties t)
  14316. (assoc-string property org-global-properties t)
  14317. (assoc-string
  14318. property org-global-properties-fixed t))))
  14319. ;; Make sure to not re-use GLOBAL as
  14320. ;; `org--update-property-plist' would alter it by
  14321. ;; side-effect.
  14322. (and global (list (cons property (cdr global))))))
  14323. (find-value
  14324. (lambda (key)
  14325. (when (re-search-forward (org-re-property key nil t) end t)
  14326. (setq props
  14327. (org--update-property-plist
  14328. key (org-match-string-no-properties 3) props))))))
  14329. (goto-char (car range))
  14330. ;; Find base value.
  14331. (save-excursion (funcall find-value property))
  14332. ;; Find additional values.
  14333. (let ((property+ (concat property "+")))
  14334. (while (funcall find-value property+)))
  14335. ;; Return final value.
  14336. (let ((val (cdr (assoc-string property props t))))
  14337. (if literal-nil val (org-not-nil val))))))))))
  14338. (defun org-property-or-variable-value (var &optional inherit)
  14339. "Check if there is a property fixing the value of VAR.
  14340. If yes, return this value. If not, return the current value of the variable."
  14341. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14342. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14343. (read prop)
  14344. (symbol-value var))))
  14345. (defun org-entry-delete (pom property)
  14346. "Delete the property PROPERTY from entry at point-or-marker POM."
  14347. (unless (member property org-special-properties)
  14348. (org-with-point-at pom
  14349. (let ((range (org-get-property-block)))
  14350. (when range
  14351. (let ((begin (car range))
  14352. (end (copy-marker (cdr range))))
  14353. (goto-char begin)
  14354. (when (re-search-forward (org-re-property property nil t) end t)
  14355. (delete-region (match-beginning 0) (line-beginning-position 2))
  14356. ;; If drawer is empty, remove it altogether.
  14357. (when (= begin end)
  14358. (delete-region (line-beginning-position 0)
  14359. (line-beginning-position 2)))
  14360. (set-marker end nil))))))))
  14361. ;; Multi-values properties are properties that contain multiple values
  14362. ;; These values are assumed to be single words, separated by whitespace.
  14363. (defun org-entry-add-to-multivalued-property (pom property value)
  14364. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14365. (let* ((old (org-entry-get pom property))
  14366. (values (and old (org-split-string old "[ \t]"))))
  14367. (setq value (org-entry-protect-space value))
  14368. (unless (member value values)
  14369. (setq values (append values (list value)))
  14370. (org-entry-put pom property
  14371. (mapconcat 'identity values " ")))))
  14372. (defun org-entry-remove-from-multivalued-property (pom property value)
  14373. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14374. (let* ((old (org-entry-get pom property))
  14375. (values (and old (org-split-string old "[ \t]"))))
  14376. (setq value (org-entry-protect-space value))
  14377. (when (member value values)
  14378. (setq values (delete value values))
  14379. (org-entry-put pom property
  14380. (mapconcat 'identity values " ")))))
  14381. (defun org-entry-member-in-multivalued-property (pom property value)
  14382. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14383. (let* ((old (org-entry-get pom property))
  14384. (values (and old (org-split-string old "[ \t]"))))
  14385. (setq value (org-entry-protect-space value))
  14386. (member value values)))
  14387. (defun org-entry-get-multivalued-property (pom property)
  14388. "Return a list of values in a multivalued property."
  14389. (let* ((value (org-entry-get pom property))
  14390. (values (and value (org-split-string value "[ \t]"))))
  14391. (mapcar 'org-entry-restore-space values)))
  14392. (defun org-entry-put-multivalued-property (pom property &rest values)
  14393. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14394. VALUES should be a list of strings. Spaces will be protected."
  14395. (org-entry-put pom property
  14396. (mapconcat 'org-entry-protect-space values " "))
  14397. (let* ((value (org-entry-get pom property))
  14398. (values (and value (org-split-string value "[ \t]"))))
  14399. (mapcar 'org-entry-restore-space values)))
  14400. (defun org-entry-protect-space (s)
  14401. "Protect spaces and newline in string S."
  14402. (while (string-match " " s)
  14403. (setq s (replace-match "%20" t t s)))
  14404. (while (string-match "\n" s)
  14405. (setq s (replace-match "%0A" t t s)))
  14406. s)
  14407. (defun org-entry-restore-space (s)
  14408. "Restore spaces and newline in string S."
  14409. (while (string-match "%20" s)
  14410. (setq s (replace-match " " t t s)))
  14411. (while (string-match "%0A" s)
  14412. (setq s (replace-match "\n" t t s)))
  14413. s)
  14414. (defvar org-entry-property-inherited-from (make-marker)
  14415. "Marker pointing to the entry from where a property was inherited.
  14416. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14417. location of the entry where the inheritance search matched. If there was
  14418. no match, the marker will point nowhere.
  14419. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14420. is set.")
  14421. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14422. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14423. The search will stop at the first ancestor which has the property defined.
  14424. If the value found is \"nil\", return nil to show that the property
  14425. should be considered as undefined (this is the meaning of nil here).
  14426. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14427. (move-marker org-entry-property-inherited-from nil)
  14428. (let (value)
  14429. (org-with-wide-buffer
  14430. (catch 'exit
  14431. (while t
  14432. (when (setq value (org-entry-get nil property nil literal-nil))
  14433. (org-back-to-heading t)
  14434. (move-marker org-entry-property-inherited-from (point))
  14435. (throw 'exit nil))
  14436. (or (org-up-heading-safe) (throw 'exit nil)))))
  14437. (unless value
  14438. (setq value
  14439. (cdr (or (assoc-string property org-file-properties t)
  14440. (assoc-string property org-global-properties t)
  14441. (assoc-string property org-global-properties-fixed t)))))
  14442. (if literal-nil value (org-not-nil value))))
  14443. (defvar org-property-changed-functions nil
  14444. "Hook called when the value of a property has changed.
  14445. Each hook function should accept two arguments, the name of the property
  14446. and the new value.")
  14447. (defun org-entry-put (pom property value)
  14448. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14449. If the value is `nil', it is converted to the empty string. If
  14450. it is not a string, an error is raised.
  14451. PROPERTY can be any regular property (see
  14452. `org-special-properties'). It can also be \"TODO\",
  14453. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14454. For the last two properties, VALUE may have any of the special
  14455. values \"earlier\" and \"later\". The function then increases or
  14456. decreases scheduled or deadline date by one day."
  14457. (cond ((null value) (setq value ""))
  14458. ((not (stringp value)) (error "Properties values should be strings")))
  14459. (org-with-point-at pom
  14460. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14461. (org-back-to-heading t)
  14462. (org-with-limited-levels (org-back-to-heading t)))
  14463. (let ((beg (point)))
  14464. (cond
  14465. ((equal property "TODO")
  14466. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14467. ((not (member value org-todo-keywords-1))
  14468. (user-error "\"%s\" is not a valid TODO state" value)))
  14469. (org-todo value)
  14470. (org-set-tags nil 'align))
  14471. ((equal property "PRIORITY")
  14472. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14473. (org-set-tags nil 'align))
  14474. ((equal property "SCHEDULED")
  14475. (forward-line)
  14476. (if (and (org-looking-at-p org-planning-line-re)
  14477. (re-search-forward
  14478. org-scheduled-time-regexp (line-end-position) t))
  14479. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14480. ((string= value "later") (org-timestamp-change 1 'day))
  14481. ((string= value "") (org-schedule '(4)))
  14482. (t (org-schedule nil value)))
  14483. (if (member value '("earlier" "later" ""))
  14484. (call-interactively #'org-schedule)
  14485. (org-schedule nil value))))
  14486. ((equal property "DEADLINE")
  14487. (forward-line)
  14488. (if (and (org-looking-at-p org-planning-line-re)
  14489. (re-search-forward
  14490. org-deadline-time-regexp (line-end-position) t))
  14491. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14492. ((string= value "later") (org-timestamp-change 1 'day))
  14493. ((string= value "") (org-deadline '(4)))
  14494. (t (org-deadline nil value)))
  14495. (if (member value '("earlier" "later" ""))
  14496. (call-interactively #'org-deadline)
  14497. (org-deadline nil value))))
  14498. ((member property org-special-properties)
  14499. (error "The %s property cannot be set with `org-entry-put'" property))
  14500. (t
  14501. (let* ((range (org-get-property-block beg 'force))
  14502. (end (cdr range))
  14503. (case-fold-search t))
  14504. (goto-char (car range))
  14505. (if (re-search-forward (org-re-property property nil t) end t)
  14506. (progn (delete-region (match-beginning 0) (match-end 0))
  14507. (goto-char (match-beginning 0)))
  14508. (goto-char end)
  14509. (insert "\n")
  14510. (backward-char))
  14511. (insert ":" property ":")
  14512. (when value (insert " " value))
  14513. (org-indent-line)))))
  14514. (run-hook-with-args 'org-property-changed-functions property value)))
  14515. (defun org-buffer-property-keys (&optional specials defaults columns)
  14516. "Get all property keys in the current buffer.
  14517. When SPECIALS is non-nil, also list the special properties that
  14518. reflect things like tags and TODO state.
  14519. When DEFAULTS is non-nil, also include properties that has
  14520. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14521. DESCRIPTION, LOCATION, and LOGGING and others.
  14522. When COLUMNS in non-nil, also include property names given in
  14523. COLUMN formats in the current buffer."
  14524. (let ((case-fold-search t)
  14525. (props (append
  14526. (and specials org-special-properties)
  14527. (and defaults (cons org-effort-property org-default-properties))
  14528. nil)))
  14529. (org-with-wide-buffer
  14530. (goto-char (point-min))
  14531. (while (re-search-forward org-property-start-re nil t)
  14532. (let ((range (org-get-property-block)))
  14533. (catch 'skip
  14534. (unless range
  14535. (when (and (not (org-before-first-heading-p))
  14536. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14537. (line-beginning-position))))
  14538. (org-get-property-block nil t))
  14539. (throw 'skip nil))
  14540. (goto-char (car range))
  14541. (let ((begin (car range))
  14542. (end (cdr range)))
  14543. ;; Make sure that found property block is not located
  14544. ;; before current point, as it would generate an infloop.
  14545. ;; It can happen, for example, in the following
  14546. ;; situation:
  14547. ;;
  14548. ;; * Headline
  14549. ;; :PROPERTIES:
  14550. ;; ...
  14551. ;; :END:
  14552. ;; *************** Inlinetask
  14553. ;; #+BEGIN_EXAMPLE
  14554. ;; :PROPERTIES:
  14555. ;; #+END_EXAMPLE
  14556. ;;
  14557. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14558. (while (< (point) end)
  14559. (let ((p (progn (looking-at org-property-re)
  14560. (org-match-string-no-properties 2))))
  14561. ;; Only add true property name, not extension symbol.
  14562. (add-to-list 'props
  14563. (if (not (org-string-match-p "\\+\\'" p)) p
  14564. (substring p 0 -1))))
  14565. (forward-line))))
  14566. (outline-next-heading)))
  14567. (when columns
  14568. (goto-char (point-min))
  14569. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14570. (let ((element (org-element-at-point)))
  14571. (when (memq (org-element-type element) '(keyword node-property))
  14572. (let ((value (org-element-property :value element))
  14573. (start 0))
  14574. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14575. (setq start (match-end 0))
  14576. (let ((p (org-match-string-no-properties 1 value)))
  14577. (unless (member-ignore-case
  14578. p (cons "ITEM" org-special-properties))
  14579. (add-to-list 'props p))))))))))
  14580. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14581. (defun org-property-values (key)
  14582. "List all non-nil values of property KEY in current buffer."
  14583. (org-with-wide-buffer
  14584. (goto-char (point-min))
  14585. (let ((case-fold-search t)
  14586. (re (org-re-property key))
  14587. values)
  14588. (while (re-search-forward re nil t)
  14589. (add-to-list 'values (org-entry-get (point) key)))
  14590. values)))
  14591. (defun org-insert-property-drawer ()
  14592. "Insert a property drawer into the current entry."
  14593. (org-with-wide-buffer
  14594. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14595. (org-back-to-heading t)
  14596. (org-with-limited-levels (org-back-to-heading t)))
  14597. (forward-line)
  14598. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14599. (unless (org-looking-at-p org-property-drawer-re)
  14600. (let ((inhibit-read-only t))
  14601. (unless (bolp) (insert "\n"))
  14602. (let ((begin (point)))
  14603. (insert ":PROPERTIES:\n:END:\n")
  14604. (org-indent-region begin (point)))))))
  14605. (defun org-insert-drawer (&optional arg drawer)
  14606. "Insert a drawer at point.
  14607. When optional argument ARG is non-nil, insert a property drawer.
  14608. Optional argument DRAWER, when non-nil, is a string representing
  14609. drawer's name. Otherwise, the user is prompted for a name.
  14610. If a region is active, insert the drawer around that region
  14611. instead.
  14612. Point is left between drawer's boundaries."
  14613. (interactive "P")
  14614. (let* ((drawer (if arg "PROPERTIES"
  14615. (or drawer (read-from-minibuffer "Drawer: ")))))
  14616. (cond
  14617. ;; With C-u, fall back on `org-insert-property-drawer'
  14618. (arg (org-insert-property-drawer))
  14619. ;; Check validity of suggested drawer's name.
  14620. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14621. (user-error "Invalid drawer name"))
  14622. ;; With an active region, insert a drawer at point.
  14623. ((not (org-region-active-p))
  14624. (progn
  14625. (unless (bolp) (insert "\n"))
  14626. (insert (format ":%s:\n\n:END:\n" drawer))
  14627. (forward-line -2)))
  14628. ;; Otherwise, insert the drawer at point
  14629. (t
  14630. (let ((rbeg (region-beginning))
  14631. (rend (copy-marker (region-end))))
  14632. (unwind-protect
  14633. (progn
  14634. (goto-char rbeg)
  14635. (beginning-of-line)
  14636. (when (save-excursion
  14637. (re-search-forward org-outline-regexp-bol rend t))
  14638. (user-error "Drawers cannot contain headlines"))
  14639. ;; Position point at the beginning of the first
  14640. ;; non-blank line in region. Insert drawer's opening
  14641. ;; there, then indent it.
  14642. (org-skip-whitespace)
  14643. (beginning-of-line)
  14644. (insert ":" drawer ":\n")
  14645. (forward-line -1)
  14646. (indent-for-tab-command)
  14647. ;; Move point to the beginning of the first blank line
  14648. ;; after the last non-blank line in region. Insert
  14649. ;; drawer's closing, then indent it.
  14650. (goto-char rend)
  14651. (skip-chars-backward " \r\t\n")
  14652. (insert "\n:END:")
  14653. (deactivate-mark t)
  14654. (indent-for-tab-command)
  14655. (unless (eolp) (insert "\n")))
  14656. ;; Clear marker, whatever the outcome of insertion is.
  14657. (set-marker rend nil)))))))
  14658. (defvar org-property-set-functions-alist nil
  14659. "Property set function alist.
  14660. Each entry should have the following format:
  14661. (PROPERTY . READ-FUNCTION)
  14662. The read function will be called with the same argument as
  14663. `org-completing-read'.")
  14664. (defun org-set-property-function (property)
  14665. "Get the function that should be used to set PROPERTY.
  14666. This is computed according to `org-property-set-functions-alist'."
  14667. (or (cdr (assoc property org-property-set-functions-alist))
  14668. 'org-completing-read))
  14669. (defun org-read-property-value (property)
  14670. "Read PROPERTY value from user."
  14671. (let* ((completion-ignore-case t)
  14672. (allowed (org-property-get-allowed-values nil property 'table))
  14673. (cur (org-entry-get nil property))
  14674. (prompt (concat property " value"
  14675. (if (and cur (string-match "\\S-" cur))
  14676. (concat " [" cur "]") "") ": "))
  14677. (set-function (org-set-property-function property))
  14678. (val (if allowed
  14679. (funcall set-function prompt allowed nil
  14680. (not (get-text-property 0 'org-unrestricted
  14681. (caar allowed))))
  14682. (let (org-completion-use-ido org-completion-use-iswitchb)
  14683. (funcall set-function prompt
  14684. (mapcar 'list (org-property-values property))
  14685. nil nil "" nil cur)))))
  14686. (org-trim val)))
  14687. (defvar org-last-set-property nil)
  14688. (defvar org-last-set-property-value nil)
  14689. (defun org-read-property-name ()
  14690. "Read a property name."
  14691. (let* ((completion-ignore-case t)
  14692. (keys (org-buffer-property-keys nil t t))
  14693. (default-prop (or (save-excursion
  14694. (save-match-data
  14695. (beginning-of-line)
  14696. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14697. (null (string= (match-string 1) "END"))
  14698. (match-string 1))))
  14699. org-last-set-property))
  14700. (property (org-icompleting-read
  14701. (concat "Property"
  14702. (if default-prop (concat " [" default-prop "]") "")
  14703. ": ")
  14704. (mapcar 'list keys)
  14705. nil nil nil nil
  14706. default-prop)))
  14707. (if (member property keys)
  14708. property
  14709. (or (cdr (assoc (downcase property)
  14710. (mapcar (lambda (x) (cons (downcase x) x))
  14711. keys)))
  14712. property))))
  14713. (defun org-set-property-and-value (use-last)
  14714. "Allow to set [PROPERTY]: [value] direction from prompt.
  14715. When use-default, don't even ask, just use the last
  14716. \"[PROPERTY]: [value]\" string from the history."
  14717. (interactive "P")
  14718. (let* ((completion-ignore-case t)
  14719. (pv (or (and use-last org-last-set-property-value)
  14720. (org-completing-read
  14721. "Enter a \"[Property]: [value]\" pair: "
  14722. nil nil nil nil nil
  14723. org-last-set-property-value)))
  14724. prop val)
  14725. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14726. (setq prop (match-string 1 pv)
  14727. val (match-string 2 pv))
  14728. (org-set-property prop val))))
  14729. (defun org-set-property (property value)
  14730. "In the current entry, set PROPERTY to VALUE.
  14731. When called interactively, this will prompt for a property name, offering
  14732. completion on existing and default properties. And then it will prompt
  14733. for a value, offering completion either on allowed values (via an inherited
  14734. xxx_ALL property) or on existing values in other instances of this property
  14735. in the current file."
  14736. (interactive (list nil nil))
  14737. (let* ((property (or property (org-read-property-name)))
  14738. (value (or value (org-read-property-value property)))
  14739. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14740. (setq org-last-set-property property)
  14741. (setq org-last-set-property-value (concat property ": " value))
  14742. ;; Possibly postprocess the inserted value:
  14743. (when fn (setq value (funcall fn value)))
  14744. (unless (equal (org-entry-get nil property) value)
  14745. (org-entry-put nil property value))))
  14746. (defun org-delete-property (property)
  14747. "In the current entry, delete PROPERTY."
  14748. (interactive
  14749. (let* ((completion-ignore-case t)
  14750. (cat (org-entry-get (point) "CATEGORY"))
  14751. (props0 (org-entry-properties nil 'standard))
  14752. (props (if cat props0
  14753. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14754. (prop (if (< 1 (length props))
  14755. (org-icompleting-read "Property: " props nil t)
  14756. (caar props))))
  14757. (list prop)))
  14758. (if (not property)
  14759. (message "No property to delete in this entry")
  14760. (org-entry-delete nil property)
  14761. (message "Property \"%s\" deleted" property)))
  14762. (defun org-delete-property-globally (property)
  14763. "Remove PROPERTY globally, from all entries."
  14764. (interactive
  14765. (let* ((completion-ignore-case t)
  14766. (prop (org-icompleting-read
  14767. "Globally remove property: "
  14768. (mapcar 'list (org-buffer-property-keys)))))
  14769. (list prop)))
  14770. (save-excursion
  14771. (save-restriction
  14772. (widen)
  14773. (goto-char (point-min))
  14774. (let ((cnt 0))
  14775. (while (re-search-forward
  14776. (org-re-property property)
  14777. nil t)
  14778. (setq cnt (1+ cnt))
  14779. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14780. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14781. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14782. (defun org-compute-property-at-point ()
  14783. "Compute the property at point.
  14784. This looks for an enclosing column format, extracts the operator and
  14785. then applies it to the property in the column format's scope."
  14786. (interactive)
  14787. (unless (org-at-property-p)
  14788. (user-error "Not at a property"))
  14789. (let ((prop (org-match-string-no-properties 2)))
  14790. (org-columns-get-format-and-top-level)
  14791. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14792. (user-error "No operator defined for property %s" prop))
  14793. (org-columns-compute prop)))
  14794. (defvar org-property-allowed-value-functions nil
  14795. "Hook for functions supplying allowed values for a specific property.
  14796. The functions must take a single argument, the name of the property, and
  14797. return a flat list of allowed values. If \":ETC\" is one of
  14798. the values, this means that these values are intended as defaults for
  14799. completion, but that other values should be allowed too.
  14800. The functions must return nil if they are not responsible for this
  14801. property.")
  14802. (defun org-property-get-allowed-values (pom property &optional table)
  14803. "Get allowed values for the property PROPERTY.
  14804. When TABLE is non-nil, return an alist that can directly be used for
  14805. completion."
  14806. (let (vals)
  14807. (cond
  14808. ((equal property "TODO")
  14809. (setq vals (org-with-point-at pom
  14810. (append org-todo-keywords-1 '("")))))
  14811. ((equal property "PRIORITY")
  14812. (let ((n org-lowest-priority))
  14813. (while (>= n org-highest-priority)
  14814. (push (char-to-string n) vals)
  14815. (setq n (1- n)))))
  14816. ((member property org-special-properties))
  14817. ((setq vals (run-hook-with-args-until-success
  14818. 'org-property-allowed-value-functions property)))
  14819. (t
  14820. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14821. (when (and vals (string-match "\\S-" vals))
  14822. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14823. (setq vals (mapcar (lambda (x)
  14824. (cond ((stringp x) x)
  14825. ((numberp x) (number-to-string x))
  14826. ((symbolp x) (symbol-name x))
  14827. (t "???")))
  14828. vals)))))
  14829. (when (member ":ETC" vals)
  14830. (setq vals (remove ":ETC" vals))
  14831. (org-add-props (car vals) '(org-unrestricted t)))
  14832. (if table (mapcar 'list vals) vals)))
  14833. (defun org-property-previous-allowed-value (&optional previous)
  14834. "Switch to the next allowed value for this property."
  14835. (interactive)
  14836. (org-property-next-allowed-value t))
  14837. (defun org-property-next-allowed-value (&optional previous)
  14838. "Switch to the next allowed value for this property."
  14839. (interactive)
  14840. (unless (org-at-property-p)
  14841. (user-error "Not at a property"))
  14842. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14843. (key (match-string 2))
  14844. (value (match-string 3))
  14845. (allowed (or (org-property-get-allowed-values (point) key)
  14846. (and (member value '("[ ]" "[-]" "[X]"))
  14847. '("[ ]" "[X]"))))
  14848. (heading (save-match-data (nth 4 (org-heading-components))))
  14849. nval)
  14850. (unless allowed
  14851. (user-error "Allowed values for this property have not been defined"))
  14852. (if previous (setq allowed (reverse allowed)))
  14853. (if (member value allowed)
  14854. (setq nval (car (cdr (member value allowed)))))
  14855. (setq nval (or nval (car allowed)))
  14856. (if (equal nval value)
  14857. (user-error "Only one allowed value for this property"))
  14858. (org-at-property-p)
  14859. (replace-match (concat " :" key ": " nval) t t)
  14860. (org-indent-line)
  14861. (beginning-of-line 1)
  14862. (skip-chars-forward " \t")
  14863. (when (equal prop org-effort-property)
  14864. (org-refresh-property
  14865. '((effort . identity)
  14866. (effort-minutes . org-duration-string-to-minutes))
  14867. nval)
  14868. (when (string= org-clock-current-task heading)
  14869. (setq org-clock-effort nval)
  14870. (org-clock-update-mode-line)))
  14871. (run-hook-with-args 'org-property-changed-functions key nval)))
  14872. (defun org-find-olp (path &optional this-buffer)
  14873. "Return a marker pointing to the entry at outline path OLP.
  14874. If anything goes wrong, throw an error.
  14875. You can wrap this call to catch the error like this:
  14876. (condition-case msg
  14877. (org-mobile-locate-entry (match-string 4))
  14878. (error (nth 1 msg)))
  14879. The return value will then be either a string with the error message,
  14880. or a marker if everything is OK.
  14881. If THIS-BUFFER is set, the outline path does not contain a file,
  14882. only headings."
  14883. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14884. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14885. (level 1)
  14886. (lmin 1)
  14887. (lmax 1)
  14888. limit re end found pos heading cnt flevel)
  14889. (unless buffer (error "File not found :%s" file))
  14890. (with-current-buffer buffer
  14891. (save-excursion
  14892. (save-restriction
  14893. (widen)
  14894. (setq limit (point-max))
  14895. (goto-char (point-min))
  14896. (while (setq heading (pop path))
  14897. (setq re (format org-complex-heading-regexp-format
  14898. (regexp-quote heading)))
  14899. (setq cnt 0 pos (point))
  14900. (while (re-search-forward re end t)
  14901. (setq level (- (match-end 1) (match-beginning 1)))
  14902. (if (and (>= level lmin) (<= level lmax))
  14903. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14904. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14905. lmax heading))
  14906. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14907. lmax heading))
  14908. (goto-char found)
  14909. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14910. (setq end (save-excursion (org-end-of-subtree t t))))
  14911. (when (org-at-heading-p)
  14912. (point-marker)))))))
  14913. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14914. "Find node HEADING in BUFFER.
  14915. Return a marker to the heading if it was found, or nil if not.
  14916. If POS-ONLY is set, return just the position instead of a marker.
  14917. The heading text must match exact, but it may have a TODO keyword,
  14918. a priority cookie and tags in the standard locations."
  14919. (with-current-buffer (or buffer (current-buffer))
  14920. (save-excursion
  14921. (save-restriction
  14922. (widen)
  14923. (goto-char (point-min))
  14924. (let (case-fold-search)
  14925. (if (re-search-forward
  14926. (format org-complex-heading-regexp-format
  14927. (regexp-quote heading)) nil t)
  14928. (if pos-only
  14929. (match-beginning 0)
  14930. (move-marker (make-marker) (match-beginning 0)))))))))
  14931. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14932. "Find Org node headline HEADING in all .org files in directory DIR.
  14933. When the target headline is found, return a marker to this location."
  14934. (let ((files (directory-files (or dir default-directory)
  14935. t "\\`[^.#].*\\.org\\'"))
  14936. file visiting m buffer)
  14937. (catch 'found
  14938. (while (setq file (pop files))
  14939. (message "trying %s" file)
  14940. (setq visiting (org-find-base-buffer-visiting file))
  14941. (setq buffer (or visiting (find-file-noselect file)))
  14942. (setq m (org-find-exact-headline-in-buffer
  14943. heading buffer))
  14944. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14945. (and m (throw 'found m))))))
  14946. (defun org-find-entry-with-id (ident)
  14947. "Locate the entry that contains the ID property with exact value IDENT.
  14948. IDENT can be a string, a symbol or a number, this function will search for
  14949. the string representation of it.
  14950. Return the position where this entry starts, or nil if there is no such entry."
  14951. (interactive "sID: ")
  14952. (let ((id (cond
  14953. ((stringp ident) ident)
  14954. ((symbol-name ident) (symbol-name ident))
  14955. ((numberp ident) (number-to-string ident))
  14956. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14957. (case-fold-search nil))
  14958. (save-excursion
  14959. (save-restriction
  14960. (widen)
  14961. (goto-char (point-min))
  14962. (when (re-search-forward
  14963. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14964. nil t)
  14965. (org-back-to-heading t)
  14966. (point))))))
  14967. ;;;; Timestamps
  14968. (defvar org-last-changed-timestamp nil)
  14969. (defvar org-last-inserted-timestamp nil
  14970. "The last time stamp inserted with `org-insert-time-stamp'.")
  14971. (defvar org-ts-what) ; dynamically scoped parameter
  14972. (defun org-time-stamp (arg &optional inactive)
  14973. "Prompt for a date/time and insert a time stamp.
  14974. If the user specifies a time like HH:MM or if this command is
  14975. called with at least one prefix argument, the time stamp contains
  14976. the date and the time. Otherwise, only the date is be included.
  14977. All parts of a date not specified by the user is filled in from
  14978. the current date/time. So if you just press return without
  14979. typing anything, the time stamp will represent the current
  14980. date/time.
  14981. If there is already a timestamp at the cursor, it will be
  14982. modified.
  14983. With two universal prefix arguments, insert an active timestamp
  14984. with the current time without prompting the user.
  14985. When called from lisp, the timestamp is inactive if INACTIVE is
  14986. non-nil."
  14987. (interactive "P")
  14988. (let* ((ts nil)
  14989. (default-time
  14990. ;; Default time is either today, or, when entering a range,
  14991. ;; the range start.
  14992. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14993. (save-excursion
  14994. (re-search-backward
  14995. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14996. (- (point) 20) t)))
  14997. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14998. (current-time)))
  14999. (default-input (and ts (org-get-compact-tod ts)))
  15000. (repeater (save-excursion
  15001. (save-match-data
  15002. (beginning-of-line)
  15003. (when (re-search-forward
  15004. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  15005. (save-excursion (progn (end-of-line) (point))) t)
  15006. (match-string 0)))))
  15007. org-time-was-given org-end-time-was-given time)
  15008. (cond
  15009. ((and (org-at-timestamp-p t)
  15010. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15011. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15012. (insert "--")
  15013. (setq time (let ((this-command this-command))
  15014. (org-read-date arg 'totime nil nil
  15015. default-time default-input inactive)))
  15016. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15017. ((org-at-timestamp-p t)
  15018. (setq time (let ((this-command this-command))
  15019. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15020. (when (org-at-timestamp-p t) ; just to get the match data
  15021. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  15022. (replace-match "")
  15023. (setq org-last-changed-timestamp
  15024. (org-insert-time-stamp
  15025. time (or org-time-was-given arg)
  15026. inactive nil nil (list org-end-time-was-given)))
  15027. (when repeater (goto-char (1- (point))) (insert " " repeater)
  15028. (setq org-last-changed-timestamp
  15029. (concat (substring org-last-inserted-timestamp 0 -1)
  15030. " " repeater ">"))))
  15031. (message "Timestamp updated"))
  15032. ((equal arg '(16))
  15033. (org-insert-time-stamp (current-time) t inactive))
  15034. (t
  15035. (setq time (let ((this-command this-command))
  15036. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  15037. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  15038. nil nil (list org-end-time-was-given))))))
  15039. ;; FIXME: can we use this for something else, like computing time differences?
  15040. (defun org-get-compact-tod (s)
  15041. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15042. (let* ((t1 (match-string 1 s))
  15043. (h1 (string-to-number (match-string 2 s)))
  15044. (m1 (string-to-number (match-string 3 s)))
  15045. (t2 (and (match-end 4) (match-string 5 s)))
  15046. (h2 (and t2 (string-to-number (match-string 6 s))))
  15047. (m2 (and t2 (string-to-number (match-string 7 s))))
  15048. dh dm)
  15049. (if (not t2)
  15050. t1
  15051. (setq dh (- h2 h1) dm (- m2 m1))
  15052. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15053. (concat t1 "+" (number-to-string dh)
  15054. (and (/= 0 dm) (format ":%02d" dm)))))))
  15055. (defun org-time-stamp-inactive (&optional arg)
  15056. "Insert an inactive time stamp.
  15057. An inactive time stamp is enclosed in square brackets instead of angle
  15058. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15059. does not link to the calendar and cannot be changed with the S-cursor keys.
  15060. So these are more for recording a certain time/date."
  15061. (interactive "P")
  15062. (org-time-stamp arg 'inactive))
  15063. (defvar org-date-ovl (make-overlay 1 1))
  15064. (overlay-put org-date-ovl 'face 'org-date-selected)
  15065. (org-detach-overlay org-date-ovl)
  15066. (defvar org-ans1) ; dynamically scoped parameter
  15067. (defvar org-ans2) ; dynamically scoped parameter
  15068. (defvar org-plain-time-of-day-regexp) ; defined below
  15069. (defvar org-overriding-default-time nil) ; dynamically scoped
  15070. (defvar org-read-date-overlay nil)
  15071. (defvar org-dcst nil) ; dynamically scoped
  15072. (defvar org-read-date-history nil)
  15073. (defvar org-read-date-final-answer nil)
  15074. (defvar org-read-date-analyze-futurep nil)
  15075. (defvar org-read-date-analyze-forced-year nil)
  15076. (defvar org-read-date-inactive)
  15077. (defvar org-read-date-minibuffer-local-map
  15078. (let* ((org-replace-disputed-keys nil)
  15079. (map (make-sparse-keymap)))
  15080. (set-keymap-parent map minibuffer-local-map)
  15081. (org-defkey map (kbd ".")
  15082. (lambda () (interactive)
  15083. ;; Are we at the beginning of the prompt?
  15084. (if (looking-back "^[^:]+: ")
  15085. (org-eval-in-calendar '(calendar-goto-today))
  15086. (insert "."))))
  15087. (org-defkey map (kbd "C-.")
  15088. (lambda () (interactive)
  15089. (org-eval-in-calendar '(calendar-goto-today))))
  15090. (org-defkey map [(meta shift left)]
  15091. (lambda () (interactive)
  15092. (org-eval-in-calendar '(calendar-backward-month 1))))
  15093. (org-defkey map [(meta shift right)]
  15094. (lambda () (interactive)
  15095. (org-eval-in-calendar '(calendar-forward-month 1))))
  15096. (org-defkey map [(meta shift up)]
  15097. (lambda () (interactive)
  15098. (org-eval-in-calendar '(calendar-backward-year 1))))
  15099. (org-defkey map [(meta shift down)]
  15100. (lambda () (interactive)
  15101. (org-eval-in-calendar '(calendar-forward-year 1))))
  15102. (org-defkey map [?\e (shift left)]
  15103. (lambda () (interactive)
  15104. (org-eval-in-calendar '(calendar-backward-month 1))))
  15105. (org-defkey map [?\e (shift right)]
  15106. (lambda () (interactive)
  15107. (org-eval-in-calendar '(calendar-forward-month 1))))
  15108. (org-defkey map [?\e (shift up)]
  15109. (lambda () (interactive)
  15110. (org-eval-in-calendar '(calendar-backward-year 1))))
  15111. (org-defkey map [?\e (shift down)]
  15112. (lambda () (interactive)
  15113. (org-eval-in-calendar '(calendar-forward-year 1))))
  15114. (org-defkey map [(shift up)]
  15115. (lambda () (interactive)
  15116. (org-eval-in-calendar '(calendar-backward-week 1))))
  15117. (org-defkey map [(shift down)]
  15118. (lambda () (interactive)
  15119. (org-eval-in-calendar '(calendar-forward-week 1))))
  15120. (org-defkey map [(shift left)]
  15121. (lambda () (interactive)
  15122. (org-eval-in-calendar '(calendar-backward-day 1))))
  15123. (org-defkey map [(shift right)]
  15124. (lambda () (interactive)
  15125. (org-eval-in-calendar '(calendar-forward-day 1))))
  15126. (org-defkey map "!"
  15127. (lambda () (interactive)
  15128. (org-eval-in-calendar '(diary-view-entries))
  15129. (message "")))
  15130. (org-defkey map ">"
  15131. (lambda () (interactive)
  15132. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15133. (org-defkey map "<"
  15134. (lambda () (interactive)
  15135. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15136. (org-defkey map "\C-v"
  15137. (lambda () (interactive)
  15138. (org-eval-in-calendar
  15139. '(calendar-scroll-left-three-months 1))))
  15140. (org-defkey map "\M-v"
  15141. (lambda () (interactive)
  15142. (org-eval-in-calendar
  15143. '(calendar-scroll-right-three-months 1))))
  15144. map)
  15145. "Keymap for minibuffer commands when using `org-read-date'.")
  15146. (defvar org-def)
  15147. (defvar org-defdecode)
  15148. (defvar org-with-time)
  15149. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15150. default-time default-input inactive)
  15151. "Read a date, possibly a time, and make things smooth for the user.
  15152. The prompt will suggest to enter an ISO date, but you can also enter anything
  15153. which will at least partially be understood by `parse-time-string'.
  15154. Unrecognized parts of the date will default to the current day, month, year,
  15155. hour and minute. If this command is called to replace a timestamp at point,
  15156. or to enter the second timestamp of a range, the default time is taken
  15157. from the existing stamp. Furthermore, the command prefers the future,
  15158. so if you are giving a date where the year is not given, and the day-month
  15159. combination is already past in the current year, it will assume you
  15160. mean next year. For details, see the manual. A few examples:
  15161. 3-2-5 --> 2003-02-05
  15162. feb 15 --> currentyear-02-15
  15163. 2/15 --> currentyear-02-15
  15164. sep 12 9 --> 2009-09-12
  15165. 12:45 --> today 12:45
  15166. 22 sept 0:34 --> currentyear-09-22 0:34
  15167. 12 --> currentyear-currentmonth-12
  15168. Fri --> nearest Friday after today
  15169. -Tue --> last Tuesday
  15170. etc.
  15171. Furthermore you can specify a relative date by giving, as the *first* thing
  15172. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15173. change in days weeks, months, years.
  15174. With a single plus or minus, the date is relative to today. With a double
  15175. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15176. +4d --> four days from today
  15177. +4 --> same as above
  15178. +2w --> two weeks from today
  15179. ++5 --> five days from default date
  15180. The function understands only English month and weekday abbreviations.
  15181. While prompting, a calendar is popped up - you can also select the
  15182. date with the mouse (button 1). The calendar shows a period of three
  15183. months. To scroll it to other months, use the keys `>' and `<'.
  15184. If you don't like the calendar, turn it off with
  15185. \(setq org-read-date-popup-calendar nil)
  15186. With optional argument TO-TIME, the date will immediately be converted
  15187. to an internal time.
  15188. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15189. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15190. still enter a time, and this function will inform the calling routine
  15191. about this change. The calling routine may then choose to change the
  15192. format used to insert the time stamp into the buffer to include the time.
  15193. With optional argument FROM-STRING, read from this string instead from
  15194. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15195. the time/date that is used for everything that is not specified by the
  15196. user."
  15197. (require 'parse-time)
  15198. (let* ((org-time-stamp-rounding-minutes
  15199. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15200. (org-dcst org-display-custom-times)
  15201. (ct (org-current-time))
  15202. (org-def (or org-overriding-default-time default-time ct))
  15203. (org-defdecode (decode-time org-def))
  15204. (dummy (progn
  15205. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15206. (setcar (nthcdr 2 org-defdecode) -1)
  15207. (setcar (nthcdr 1 org-defdecode) 59)
  15208. (setq org-def (apply 'encode-time org-defdecode)
  15209. org-defdecode (decode-time org-def)))))
  15210. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15211. (calendar-frame-setup nil)
  15212. (calendar-setup nil)
  15213. (calendar-move-hook nil)
  15214. (calendar-view-diary-initially-flag nil)
  15215. (calendar-view-holidays-initially-flag nil)
  15216. (timestr (format-time-string
  15217. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15218. (prompt (concat (if prompt (concat prompt " ") "")
  15219. (format "Date+time [%s]: " timestr)))
  15220. ans (org-ans0 "") org-ans1 org-ans2 final)
  15221. (cond
  15222. (from-string (setq ans from-string))
  15223. (org-read-date-popup-calendar
  15224. (save-excursion
  15225. (save-window-excursion
  15226. (calendar)
  15227. (org-eval-in-calendar '(setq cursor-type nil) t)
  15228. (unwind-protect
  15229. (progn
  15230. (calendar-forward-day (- (time-to-days org-def)
  15231. (calendar-absolute-from-gregorian
  15232. (calendar-current-date))))
  15233. (org-eval-in-calendar nil t)
  15234. (let* ((old-map (current-local-map))
  15235. (map (copy-keymap calendar-mode-map))
  15236. (minibuffer-local-map
  15237. (copy-keymap org-read-date-minibuffer-local-map)))
  15238. (org-defkey map (kbd "RET") 'org-calendar-select)
  15239. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15240. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15241. (unwind-protect
  15242. (progn
  15243. (use-local-map map)
  15244. (setq org-read-date-inactive inactive)
  15245. (add-hook 'post-command-hook 'org-read-date-display)
  15246. (setq org-ans0 (read-string prompt default-input
  15247. 'org-read-date-history nil))
  15248. ;; org-ans0: from prompt
  15249. ;; org-ans1: from mouse click
  15250. ;; org-ans2: from calendar motion
  15251. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15252. (remove-hook 'post-command-hook 'org-read-date-display)
  15253. (use-local-map old-map)
  15254. (when org-read-date-overlay
  15255. (delete-overlay org-read-date-overlay)
  15256. (setq org-read-date-overlay nil)))))
  15257. (bury-buffer "*Calendar*")))))
  15258. (t ; Naked prompt only
  15259. (unwind-protect
  15260. (setq ans (read-string prompt default-input
  15261. 'org-read-date-history timestr))
  15262. (when org-read-date-overlay
  15263. (delete-overlay org-read-date-overlay)
  15264. (setq org-read-date-overlay nil)))))
  15265. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15266. (when org-read-date-analyze-forced-year
  15267. (message "Year was forced into %s"
  15268. (if org-read-date-force-compatible-dates
  15269. "compatible range (1970-2037)"
  15270. "range representable on this machine"))
  15271. (ding))
  15272. ;; One round trip to get rid of 34th of August and stuff like that....
  15273. (setq final (decode-time (apply 'encode-time final)))
  15274. (setq org-read-date-final-answer ans)
  15275. (if to-time
  15276. (apply 'encode-time final)
  15277. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15278. (format "%04d-%02d-%02d %02d:%02d"
  15279. (nth 5 final) (nth 4 final) (nth 3 final)
  15280. (nth 2 final) (nth 1 final))
  15281. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15282. (defun org-read-date-display ()
  15283. "Display the current date prompt interpretation in the minibuffer."
  15284. (when org-read-date-display-live
  15285. (when org-read-date-overlay
  15286. (delete-overlay org-read-date-overlay))
  15287. (when (minibufferp (current-buffer))
  15288. (save-excursion
  15289. (end-of-line 1)
  15290. (while (not (equal (buffer-substring
  15291. (max (point-min) (- (point) 4)) (point))
  15292. " "))
  15293. (insert " ")))
  15294. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15295. " " (or org-ans1 org-ans2)))
  15296. (org-end-time-was-given nil)
  15297. (f (org-read-date-analyze ans org-def org-defdecode))
  15298. (fmts (if org-dcst
  15299. org-time-stamp-custom-formats
  15300. org-time-stamp-formats))
  15301. (fmt (if (or org-with-time
  15302. (and (boundp 'org-time-was-given) org-time-was-given))
  15303. (cdr fmts)
  15304. (car fmts)))
  15305. (txt (format-time-string fmt (apply 'encode-time f)))
  15306. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15307. (txt (concat "=> " txt)))
  15308. (when (and org-end-time-was-given
  15309. (string-match org-plain-time-of-day-regexp txt))
  15310. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15311. org-end-time-was-given
  15312. (substring txt (match-end 0)))))
  15313. (when org-read-date-analyze-futurep
  15314. (setq txt (concat txt " (=>F)")))
  15315. (setq org-read-date-overlay
  15316. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15317. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15318. (defun org-read-date-analyze (ans org-def org-defdecode)
  15319. "Analyze the combined answer of the date prompt."
  15320. ;; FIXME: cleanup and comment
  15321. (let ((nowdecode (decode-time (current-time)))
  15322. delta deltan deltaw deltadef year month day
  15323. hour minute second wday pm h2 m2 tl wday1
  15324. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15325. (setq org-read-date-analyze-futurep nil
  15326. org-read-date-analyze-forced-year nil)
  15327. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15328. (setq ans "+0"))
  15329. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15330. (setq ans (replace-match "" t t ans)
  15331. deltan (car delta)
  15332. deltaw (nth 1 delta)
  15333. deltadef (nth 2 delta)))
  15334. ;; Check if there is an iso week date in there. If yes, store the
  15335. ;; info and postpone interpreting it until the rest of the parsing
  15336. ;; is done.
  15337. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15338. (setq iso-year (if (match-end 1)
  15339. (org-small-year-to-year
  15340. (string-to-number (match-string 1 ans))))
  15341. iso-weekday (if (match-end 3)
  15342. (string-to-number (match-string 3 ans)))
  15343. iso-week (string-to-number (match-string 2 ans)))
  15344. (setq ans (replace-match "" t t ans)))
  15345. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15346. (when (string-match
  15347. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15348. (setq year (if (match-end 2)
  15349. (string-to-number (match-string 2 ans))
  15350. (progn (setq kill-year t)
  15351. (string-to-number (format-time-string "%Y"))))
  15352. month (string-to-number (match-string 3 ans))
  15353. day (string-to-number (match-string 4 ans)))
  15354. (if (< year 100) (setq year (+ 2000 year)))
  15355. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15356. t nil ans)))
  15357. ;; Help matching dotted european dates
  15358. (when (string-match
  15359. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15360. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15361. (setq kill-year t)
  15362. (string-to-number (format-time-string "%Y")))
  15363. day (string-to-number (match-string 1 ans))
  15364. month (string-to-number (match-string 2 ans))
  15365. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15366. t nil ans)))
  15367. ;; Help matching american dates, like 5/30 or 5/30/7
  15368. (when (string-match
  15369. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15370. (setq year (if (match-end 4)
  15371. (string-to-number (match-string 4 ans))
  15372. (progn (setq kill-year t)
  15373. (string-to-number (format-time-string "%Y"))))
  15374. month (string-to-number (match-string 1 ans))
  15375. day (string-to-number (match-string 2 ans)))
  15376. (if (< year 100) (setq year (+ 2000 year)))
  15377. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15378. t nil ans)))
  15379. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15380. ;; If there is a time with am/pm, and *no* time without it, we convert
  15381. ;; so that matching will be successful.
  15382. (loop for i from 1 to 2 do ; twice, for end time as well
  15383. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15384. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15385. (setq hour (string-to-number (match-string 1 ans))
  15386. minute (if (match-end 3)
  15387. (string-to-number (match-string 3 ans))
  15388. 0)
  15389. pm (equal ?p
  15390. (string-to-char (downcase (match-string 4 ans)))))
  15391. (if (and (= hour 12) (not pm))
  15392. (setq hour 0)
  15393. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15394. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15395. t t ans))))
  15396. ;; Check if a time range is given as a duration
  15397. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15398. (setq hour (string-to-number (match-string 1 ans))
  15399. h2 (+ hour (string-to-number (match-string 3 ans)))
  15400. minute (string-to-number (match-string 2 ans))
  15401. m2 (+ minute (if (match-end 5) (string-to-number
  15402. (match-string 5 ans))0)))
  15403. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15404. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15405. t t ans)))
  15406. ;; Check if there is a time range
  15407. (when (boundp 'org-end-time-was-given)
  15408. (setq org-time-was-given nil)
  15409. (when (and (string-match org-plain-time-of-day-regexp ans)
  15410. (match-end 8))
  15411. (setq org-end-time-was-given (match-string 8 ans))
  15412. (setq ans (concat (substring ans 0 (match-beginning 7))
  15413. (substring ans (match-end 7))))))
  15414. (setq tl (parse-time-string ans)
  15415. day (or (nth 3 tl) (nth 3 org-defdecode))
  15416. month (or (nth 4 tl)
  15417. (if (and org-read-date-prefer-future
  15418. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15419. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15420. (nth 4 org-defdecode)))
  15421. year (or (and (not kill-year) (nth 5 tl))
  15422. (if (and org-read-date-prefer-future
  15423. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15424. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15425. (nth 5 org-defdecode)))
  15426. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15427. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15428. second (or (nth 0 tl) 0)
  15429. wday (nth 6 tl))
  15430. (when (and (eq org-read-date-prefer-future 'time)
  15431. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15432. (equal day (nth 3 nowdecode))
  15433. (equal month (nth 4 nowdecode))
  15434. (equal year (nth 5 nowdecode))
  15435. (nth 2 tl)
  15436. (or (< (nth 2 tl) (nth 2 nowdecode))
  15437. (and (= (nth 2 tl) (nth 2 nowdecode))
  15438. (nth 1 tl)
  15439. (< (nth 1 tl) (nth 1 nowdecode)))))
  15440. (setq day (1+ day)
  15441. futurep t))
  15442. ;; Special date definitions below
  15443. (cond
  15444. (iso-week
  15445. ;; There was an iso week
  15446. (require 'cal-iso)
  15447. (setq futurep nil)
  15448. (setq year (or iso-year year)
  15449. day (or iso-weekday wday 1)
  15450. wday nil ; to make sure that the trigger below does not match
  15451. iso-date (calendar-gregorian-from-absolute
  15452. (calendar-absolute-from-iso
  15453. (list iso-week day year))))
  15454. ; FIXME: Should we also push ISO weeks into the future?
  15455. ; (when (and org-read-date-prefer-future
  15456. ; (not iso-year)
  15457. ; (< (calendar-absolute-from-gregorian iso-date)
  15458. ; (time-to-days (current-time))))
  15459. ; (setq year (1+ year)
  15460. ; iso-date (calendar-gregorian-from-absolute
  15461. ; (calendar-absolute-from-iso
  15462. ; (list iso-week day year)))))
  15463. (setq month (car iso-date)
  15464. year (nth 2 iso-date)
  15465. day (nth 1 iso-date)))
  15466. (deltan
  15467. (setq futurep nil)
  15468. (unless deltadef
  15469. (let ((now (decode-time (current-time))))
  15470. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15471. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15472. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15473. ((equal deltaw "m") (setq month (+ month deltan)))
  15474. ((equal deltaw "y") (setq year (+ year deltan)))))
  15475. ((and wday (not (nth 3 tl)))
  15476. ;; Weekday was given, but no day, so pick that day in the week
  15477. ;; on or after the derived date.
  15478. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15479. (unless (equal wday wday1)
  15480. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15481. (if (and (boundp 'org-time-was-given)
  15482. (nth 2 tl))
  15483. (setq org-time-was-given t))
  15484. (if (< year 100) (setq year (+ 2000 year)))
  15485. ;; Check of the date is representable
  15486. (if org-read-date-force-compatible-dates
  15487. (progn
  15488. (if (< year 1970)
  15489. (setq year 1970 org-read-date-analyze-forced-year t))
  15490. (if (> year 2037)
  15491. (setq year 2037 org-read-date-analyze-forced-year t)))
  15492. (condition-case nil
  15493. (ignore (encode-time second minute hour day month year))
  15494. (error
  15495. (setq year (nth 5 org-defdecode))
  15496. (setq org-read-date-analyze-forced-year t))))
  15497. (setq org-read-date-analyze-futurep futurep)
  15498. (list second minute hour day month year)))
  15499. (defvar parse-time-weekdays)
  15500. (defun org-read-date-get-relative (s today default)
  15501. "Check string S for special relative date string.
  15502. TODAY and DEFAULT are internal times, for today and for a default.
  15503. Return shift list (N what def-flag)
  15504. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15505. N is the number of WHATs to shift.
  15506. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15507. the DEFAULT date rather than TODAY."
  15508. (require 'parse-time)
  15509. (when (and
  15510. (string-match
  15511. (concat
  15512. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15513. "\\([0-9]+\\)?"
  15514. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15515. "\\([ \t]\\|$\\)") s)
  15516. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15517. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15518. (string-to-char (substring (match-string 1 s) -1))
  15519. ?+))
  15520. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15521. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15522. (what (if (match-end 3) (match-string 3 s) "d"))
  15523. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15524. (date (if rel default today))
  15525. (wday (nth 6 (decode-time date)))
  15526. delta)
  15527. (if wday1
  15528. (progn
  15529. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15530. (if (= delta 0) (setq delta 7))
  15531. (if (= dir ?-)
  15532. (progn
  15533. (setq delta (- delta 7))
  15534. (if (= delta 0) (setq delta -7))))
  15535. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15536. (list delta "d" rel))
  15537. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15538. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15539. "Turn a user-specified date into the internal representation.
  15540. The internal representation needed by the calendar is (month day year).
  15541. This is a wrapper to handle the brain-dead convention in calendar that
  15542. user function argument order change dependent on argument order."
  15543. (if (boundp 'calendar-date-style)
  15544. (cond
  15545. ((eq calendar-date-style 'american)
  15546. (list arg1 arg2 arg3))
  15547. ((eq calendar-date-style 'european)
  15548. (list arg2 arg1 arg3))
  15549. ((eq calendar-date-style 'iso)
  15550. (list arg2 arg3 arg1)))
  15551. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15552. (if (org-bound-and-true-p european-calendar-style)
  15553. (list arg2 arg1 arg3)
  15554. (list arg1 arg2 arg3)))))
  15555. (defun org-eval-in-calendar (form &optional keepdate)
  15556. "Eval FORM in the calendar window and return to current window.
  15557. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15558. otherwise stick to the current value of `org-ans2'."
  15559. (let ((sf (selected-frame))
  15560. (sw (selected-window)))
  15561. (select-window (get-buffer-window "*Calendar*" t))
  15562. (eval form)
  15563. (when (and (not keepdate) (calendar-cursor-to-date))
  15564. (let* ((date (calendar-cursor-to-date))
  15565. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15566. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15567. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15568. (select-window sw)
  15569. (org-select-frame-set-input-focus sf)))
  15570. (defun org-calendar-select ()
  15571. "Return to `org-read-date' with the date currently selected.
  15572. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15573. (interactive)
  15574. (when (calendar-cursor-to-date)
  15575. (let* ((date (calendar-cursor-to-date))
  15576. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15577. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15578. (if (active-minibuffer-window) (exit-minibuffer))))
  15579. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15580. "Insert a date stamp for the date given by the internal TIME.
  15581. WITH-HM means use the stamp format that includes the time of the day.
  15582. INACTIVE means use square brackets instead of angular ones, so that the
  15583. stamp will not contribute to the agenda.
  15584. PRE and POST are optional strings to be inserted before and after the
  15585. stamp.
  15586. The command returns the inserted time stamp."
  15587. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15588. stamp)
  15589. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15590. (insert-before-markers (or pre ""))
  15591. (when (listp extra)
  15592. (setq extra (car extra))
  15593. (if (and (stringp extra)
  15594. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15595. (setq extra (format "-%02d:%02d"
  15596. (string-to-number (match-string 1 extra))
  15597. (string-to-number (match-string 2 extra))))
  15598. (setq extra nil)))
  15599. (when extra
  15600. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15601. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15602. (insert-before-markers (or post ""))
  15603. (setq org-last-inserted-timestamp stamp)))
  15604. (defun org-toggle-time-stamp-overlays ()
  15605. "Toggle the use of custom time stamp formats."
  15606. (interactive)
  15607. (setq org-display-custom-times (not org-display-custom-times))
  15608. (unless org-display-custom-times
  15609. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15610. (while (setq p (next-single-property-change p 'display))
  15611. (if (and (get-text-property p 'display)
  15612. (eq (get-text-property p 'face) 'org-date))
  15613. (remove-text-properties
  15614. p (setq p (next-single-property-change p 'display))
  15615. '(display t))))
  15616. (set-buffer-modified-p bmp)))
  15617. (if (featurep 'xemacs)
  15618. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15619. (org-restart-font-lock)
  15620. (setq org-table-may-need-update t)
  15621. (if org-display-custom-times
  15622. (message "Time stamps are overlaid with custom format")
  15623. (message "Time stamp overlays removed")))
  15624. (defun org-display-custom-time (beg end)
  15625. "Overlay modified time stamp format over timestamp between BEG and END."
  15626. (let* ((ts (buffer-substring beg end))
  15627. t1 w1 with-hm tf time str w2 (off 0))
  15628. (save-match-data
  15629. (setq t1 (org-parse-time-string ts t))
  15630. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15631. (setq off (- (match-end 0) (match-beginning 0)))))
  15632. (setq end (- end off))
  15633. (setq w1 (- end beg)
  15634. with-hm (and (nth 1 t1) (nth 2 t1))
  15635. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15636. time (org-fix-decoded-time t1)
  15637. str (org-add-props
  15638. (format-time-string
  15639. (substring tf 1 -1) (apply 'encode-time time))
  15640. nil 'mouse-face 'highlight)
  15641. w2 (length str))
  15642. (if (not (= w2 w1))
  15643. (add-text-properties (1+ beg) (+ 2 beg)
  15644. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15645. (if (featurep 'xemacs)
  15646. (progn
  15647. (put-text-property beg end 'invisible t)
  15648. (put-text-property beg end 'end-glyph (make-glyph str)))
  15649. (put-text-property beg end 'display str))))
  15650. (defun org-translate-time (string)
  15651. "Translate all timestamps in STRING to custom format.
  15652. But do this only if the variable `org-display-custom-times' is set."
  15653. (when org-display-custom-times
  15654. (save-match-data
  15655. (let* ((start 0)
  15656. (re org-ts-regexp-both)
  15657. t1 with-hm inactive tf time str beg end)
  15658. (while (setq start (string-match re string start))
  15659. (setq beg (match-beginning 0)
  15660. end (match-end 0)
  15661. t1 (save-match-data
  15662. (org-parse-time-string (substring string beg end) t))
  15663. with-hm (and (nth 1 t1) (nth 2 t1))
  15664. inactive (equal (substring string beg (1+ beg)) "[")
  15665. tf (funcall (if with-hm 'cdr 'car)
  15666. org-time-stamp-custom-formats)
  15667. time (org-fix-decoded-time t1)
  15668. str (format-time-string
  15669. (concat
  15670. (if inactive "[" "<") (substring tf 1 -1)
  15671. (if inactive "]" ">"))
  15672. (apply 'encode-time time))
  15673. string (replace-match str t t string)
  15674. start (+ start (length str)))))))
  15675. string)
  15676. (defun org-fix-decoded-time (time)
  15677. "Set 0 instead of nil for the first 6 elements of time.
  15678. Don't touch the rest."
  15679. (let ((n 0))
  15680. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15681. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15682. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15683. "Difference between TIMESTAMP-STRING and now in days.
  15684. If SECONDS is non-nil, return the difference in seconds."
  15685. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15686. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15687. (funcall fdiff (current-time)))))
  15688. (defun org-deadline-close (timestamp-string &optional ndays)
  15689. "Is the time in TIMESTAMP-STRING close to the current date?"
  15690. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15691. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15692. (not (org-entry-is-done-p))))
  15693. (defun org-get-wdays (ts &optional delay zero-delay)
  15694. "Get the deadline lead time appropriate for timestring TS.
  15695. When DELAY is non-nil, get the delay time for scheduled items
  15696. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15697. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15698. don't try to find the delay cookie in the scheduled timestamp."
  15699. (let ((tv (if delay org-scheduled-delay-days
  15700. org-deadline-warning-days)))
  15701. (cond
  15702. ((or (and delay (< tv 0))
  15703. (and delay zero-delay (<= tv 0))
  15704. (and (not delay) (<= tv 0)))
  15705. ;; Enforce this value no matter what
  15706. (- tv))
  15707. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15708. ;; lead time is specified.
  15709. (floor (* (string-to-number (match-string 1 ts))
  15710. (cdr (assoc (match-string 2 ts)
  15711. '(("d" . 1) ("w" . 7)
  15712. ("m" . 30.4) ("y" . 365.25)
  15713. ("h" . 0.041667)))))))
  15714. ;; go for the default.
  15715. (t tv))))
  15716. (defun org-calendar-select-mouse (ev)
  15717. "Return to `org-read-date' with the date currently selected.
  15718. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15719. (interactive "e")
  15720. (mouse-set-point ev)
  15721. (when (calendar-cursor-to-date)
  15722. (let* ((date (calendar-cursor-to-date))
  15723. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15724. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15725. (if (active-minibuffer-window) (exit-minibuffer))))
  15726. (defun org-check-deadlines (ndays)
  15727. "Check if there are any deadlines due or past due.
  15728. A deadline is considered due if it happens within `org-deadline-warning-days'
  15729. days from today's date. If the deadline appears in an entry marked DONE,
  15730. it is not shown. The prefix arg NDAYS can be used to test that many
  15731. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15732. (interactive "P")
  15733. (let* ((org-warn-days
  15734. (cond
  15735. ((equal ndays '(4)) 100000)
  15736. (ndays (prefix-numeric-value ndays))
  15737. (t (abs org-deadline-warning-days))))
  15738. (case-fold-search nil)
  15739. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15740. (callback
  15741. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15742. (message "%d deadlines past-due or due within %d days"
  15743. (org-occur regexp nil callback)
  15744. org-warn-days)))
  15745. (defsubst org-re-timestamp (type)
  15746. "Return a regexp for timestamp TYPE.
  15747. Allowed values for TYPE are:
  15748. all: all timestamps
  15749. active: only active timestamps (<...>)
  15750. inactive: only inactive timestamps ([...])
  15751. scheduled: only scheduled timestamps
  15752. deadline: only deadline timestamps
  15753. closed: only closed time-stamps
  15754. When TYPE is nil, fall back on returning a regexp that matches
  15755. both scheduled and deadline timestamps."
  15756. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15757. ((eq type 'active) org-ts-regexp)
  15758. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15759. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15760. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15761. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15762. ((eq type 'scheduled-or-deadline)
  15763. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15764. (defun org-check-before-date (date)
  15765. "Check if there are deadlines or scheduled entries before DATE."
  15766. (interactive (list (org-read-date)))
  15767. (let ((case-fold-search nil)
  15768. (regexp (org-re-timestamp org-ts-type))
  15769. (callback
  15770. (lambda () (time-less-p
  15771. (org-time-string-to-time (match-string 1))
  15772. (org-time-string-to-time date)))))
  15773. (message "%d entries before %s"
  15774. (org-occur regexp nil callback) date)))
  15775. (defun org-check-after-date (date)
  15776. "Check if there are deadlines or scheduled entries after DATE."
  15777. (interactive (list (org-read-date)))
  15778. (let ((case-fold-search nil)
  15779. (regexp (org-re-timestamp org-ts-type))
  15780. (callback
  15781. (lambda () (not
  15782. (time-less-p
  15783. (org-time-string-to-time (match-string 1))
  15784. (org-time-string-to-time date))))))
  15785. (message "%d entries after %s"
  15786. (org-occur regexp nil callback) date)))
  15787. (defun org-check-dates-range (start-date end-date)
  15788. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15789. (interactive (list (org-read-date nil nil nil "Range starts")
  15790. (org-read-date nil nil nil "Range end")))
  15791. (let ((case-fold-search nil)
  15792. (regexp (org-re-timestamp org-ts-type))
  15793. (callback
  15794. (lambda ()
  15795. (let ((match (match-string 1)))
  15796. (and
  15797. (not (time-less-p
  15798. (org-time-string-to-time match)
  15799. (org-time-string-to-time start-date)))
  15800. (time-less-p
  15801. (org-time-string-to-time match)
  15802. (org-time-string-to-time end-date)))))))
  15803. (message "%d entries between %s and %s"
  15804. (org-occur regexp nil callback) start-date end-date)))
  15805. (defun org-evaluate-time-range (&optional to-buffer)
  15806. "Evaluate a time range by computing the difference between start and end.
  15807. Normally the result is just printed in the echo area, but with prefix arg
  15808. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15809. If the time range is actually in a table, the result is inserted into the
  15810. next column.
  15811. For time difference computation, a year is assumed to be exactly 365
  15812. days in order to avoid rounding problems."
  15813. (interactive "P")
  15814. (or
  15815. (org-clock-update-time-maybe)
  15816. (save-excursion
  15817. (unless (org-at-date-range-p t)
  15818. (goto-char (point-at-bol))
  15819. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15820. (if (not (org-at-date-range-p t))
  15821. (user-error "Not at a time-stamp range, and none found in current line")))
  15822. (let* ((ts1 (match-string 1))
  15823. (ts2 (match-string 2))
  15824. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15825. (match-end (match-end 0))
  15826. (time1 (org-time-string-to-time ts1))
  15827. (time2 (org-time-string-to-time ts2))
  15828. (t1 (org-float-time time1))
  15829. (t2 (org-float-time time2))
  15830. (diff (abs (- t2 t1)))
  15831. (negative (< (- t2 t1) 0))
  15832. ;; (ys (floor (* 365 24 60 60)))
  15833. (ds (* 24 60 60))
  15834. (hs (* 60 60))
  15835. (fy "%dy %dd %02d:%02d")
  15836. (fy1 "%dy %dd")
  15837. (fd "%dd %02d:%02d")
  15838. (fd1 "%dd")
  15839. (fh "%02d:%02d")
  15840. y d h m align)
  15841. (if havetime
  15842. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15843. y 0
  15844. d (floor (/ diff ds)) diff (mod diff ds)
  15845. h (floor (/ diff hs)) diff (mod diff hs)
  15846. m (floor (/ diff 60)))
  15847. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15848. y 0
  15849. d (floor (+ (/ diff ds) 0.5))
  15850. h 0 m 0))
  15851. (if (not to-buffer)
  15852. (message "%s" (org-make-tdiff-string y d h m))
  15853. (if (org-at-table-p)
  15854. (progn
  15855. (goto-char match-end)
  15856. (setq align t)
  15857. (and (looking-at " *|") (goto-char (match-end 0))))
  15858. (goto-char match-end))
  15859. (if (looking-at
  15860. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15861. (replace-match ""))
  15862. (if negative (insert " -"))
  15863. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15864. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15865. (insert " " (format fh h m))))
  15866. (if align (org-table-align))
  15867. (message "Time difference inserted")))))
  15868. (defun org-make-tdiff-string (y d h m)
  15869. (let ((fmt "")
  15870. (l nil))
  15871. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15872. l (push y l)))
  15873. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15874. l (push d l)))
  15875. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15876. l (push h l)))
  15877. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15878. l (push m l)))
  15879. (apply 'format fmt (nreverse l))))
  15880. (defun org-time-string-to-time (s &optional buffer pos)
  15881. "Convert a timestamp string into internal time."
  15882. (condition-case errdata
  15883. (apply 'encode-time (org-parse-time-string s))
  15884. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15885. s (if (not (and buffer pos))
  15886. ""
  15887. (format " at %d in buffer `%s'" pos buffer))
  15888. (cdr errdata)))))
  15889. (defun org-time-string-to-seconds (s)
  15890. "Convert a timestamp string to a number of seconds."
  15891. (org-float-time (org-time-string-to-time s)))
  15892. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15893. "Convert a time stamp to an absolute day number.
  15894. If there is a specifier for a cyclic time stamp, get the closest
  15895. date to DAYNR.
  15896. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15897. The variable `date' is bound by the calendar when this is called."
  15898. (cond
  15899. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15900. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15901. daynr
  15902. (+ daynr 1000)))
  15903. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15904. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15905. (time-to-days (current-time))) (match-string 0 s)
  15906. prefer show-all))
  15907. (t (time-to-days
  15908. (condition-case errdata
  15909. (apply 'encode-time (org-parse-time-string s))
  15910. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15911. s (if (not (and buffer pos))
  15912. ""
  15913. (format " at %d in buffer `%s'" pos buffer))
  15914. (cdr errdata))))))))
  15915. (defun org-days-to-iso-week (days)
  15916. "Return the iso week number."
  15917. (require 'cal-iso)
  15918. (car (calendar-iso-from-absolute days)))
  15919. (defun org-small-year-to-year (year)
  15920. "Convert 2-digit years into 4-digit years.
  15921. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15922. The year 2000 cannot be abbreviated. Any year larger than 99
  15923. is returned unchanged."
  15924. (if (< year 38)
  15925. (setq year (+ 2000 year))
  15926. (if (< year 100)
  15927. (setq year (+ 1900 year))))
  15928. year)
  15929. (defun org-time-from-absolute (d)
  15930. "Return the time corresponding to date D.
  15931. D may be an absolute day number, or a calendar-type list (month day year)."
  15932. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15933. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15934. (defun org-calendar-holiday ()
  15935. "List of holidays, for Diary display in Org-mode."
  15936. (require 'holidays)
  15937. (let ((hl (funcall
  15938. (if (fboundp 'calendar-check-holidays)
  15939. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15940. (if hl (mapconcat 'identity hl "; "))))
  15941. (defun org-diary-sexp-entry (sexp entry date)
  15942. "Process a SEXP diary ENTRY for DATE."
  15943. (require 'diary-lib)
  15944. (let ((result (if calendar-debug-sexp
  15945. (let ((stack-trace-on-error t))
  15946. (eval (car (read-from-string sexp))))
  15947. (condition-case nil
  15948. (eval (car (read-from-string sexp)))
  15949. (error
  15950. (beep)
  15951. (message "Bad sexp at line %d in %s: %s"
  15952. (org-current-line)
  15953. (buffer-file-name) sexp)
  15954. (sleep-for 2))))))
  15955. (cond ((stringp result) (split-string result "; "))
  15956. ((and (consp result)
  15957. (not (consp (cdr result)))
  15958. (stringp (cdr result))) (cdr result))
  15959. ((and (consp result)
  15960. (stringp (car result))) result)
  15961. (result entry))))
  15962. (defun org-diary-to-ical-string (frombuf)
  15963. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15964. This uses the icalendar.el library."
  15965. (let* ((tmpdir (if (featurep 'xemacs)
  15966. (temp-directory)
  15967. temporary-file-directory))
  15968. (tmpfile (make-temp-name
  15969. (expand-file-name "orgics" tmpdir)))
  15970. buf rtn b e)
  15971. (with-current-buffer frombuf
  15972. (icalendar-export-region (point-min) (point-max) tmpfile)
  15973. (setq buf (find-buffer-visiting tmpfile))
  15974. (set-buffer buf)
  15975. (goto-char (point-min))
  15976. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15977. (setq b (match-beginning 0)))
  15978. (goto-char (point-max))
  15979. (if (re-search-backward "^END:VEVENT" nil t)
  15980. (setq e (match-end 0)))
  15981. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15982. (kill-buffer buf)
  15983. (delete-file tmpfile)
  15984. rtn))
  15985. (defun org-closest-date (start current change prefer show-all)
  15986. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15987. When PREFER is `past', return a date that is either CURRENT or past.
  15988. When PREFER is `future', return a date that is either CURRENT or future.
  15989. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15990. ;; Make the proper lists from the dates
  15991. (catch 'exit
  15992. (let ((a1 '(("h" . hour)
  15993. ("d" . day)
  15994. ("w" . week)
  15995. ("m" . month)
  15996. ("y" . year)))
  15997. (shour (nth 2 (org-parse-time-string start)))
  15998. dn dw sday cday n1 n2 n0
  15999. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16000. (setq start (org-date-to-gregorian start)
  16001. current (org-date-to-gregorian
  16002. (if show-all
  16003. current
  16004. (time-to-days (current-time))))
  16005. sday (calendar-absolute-from-gregorian start)
  16006. cday (calendar-absolute-from-gregorian current))
  16007. (if (<= cday sday) (throw 'exit sday))
  16008. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16009. (setq dn (string-to-number (match-string 1 change))
  16010. dw (cdr (assoc (match-string 2 change) a1)))
  16011. (user-error "Invalid change specifier: %s" change))
  16012. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16013. (cond
  16014. ((eq dw 'hour)
  16015. (let ((missing-hours
  16016. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16017. dn)))
  16018. (setq n1 (if (zerop missing-hours) cday
  16019. (- cday (1+ (floor (/ missing-hours 24)))))
  16020. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16021. ((eq dw 'day)
  16022. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16023. n2 (+ n1 dn)))
  16024. ((eq dw 'year)
  16025. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16026. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16027. (setq date1 (list m d y1)
  16028. n1 (calendar-absolute-from-gregorian date1)
  16029. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16030. n2 (calendar-absolute-from-gregorian date2)))
  16031. ((eq dw 'month)
  16032. ;; approx number of month between the two dates
  16033. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16034. ;; How often does dn fit in there?
  16035. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16036. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16037. m (+ m nm)
  16038. ny (floor (/ m 12))
  16039. y (+ y ny)
  16040. m (- m (* ny 12)))
  16041. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16042. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16043. (setq m2 (+ m dn) y2 y)
  16044. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16045. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16046. (while (<= n2 cday)
  16047. (setq n1 n2 m m2 y y2)
  16048. (setq m2 (+ m dn) y2 y)
  16049. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16050. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16051. ;; Make sure n1 is the earlier date
  16052. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16053. (if show-all
  16054. (cond
  16055. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16056. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16057. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16058. (cond
  16059. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16060. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16061. (t (if (= cday n1) n1 n2)))))))
  16062. (defun org-date-to-gregorian (date)
  16063. "Turn any specification of DATE into a Gregorian date for the calendar."
  16064. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16065. ((and (listp date) (= (length date) 3)) date)
  16066. ((stringp date)
  16067. (setq date (org-parse-time-string date))
  16068. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16069. ((listp date)
  16070. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16071. (defun org-parse-time-string (s &optional nodefault)
  16072. "Parse the standard Org-mode time string.
  16073. This should be a lot faster than the normal `parse-time-string'.
  16074. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16075. hour and minute fields will be nil if not given."
  16076. (cond ((string-match org-ts-regexp0 s)
  16077. (list 0
  16078. (if (or (match-beginning 8) (not nodefault))
  16079. (string-to-number (or (match-string 8 s) "0")))
  16080. (if (or (match-beginning 7) (not nodefault))
  16081. (string-to-number (or (match-string 7 s) "0")))
  16082. (string-to-number (match-string 4 s))
  16083. (string-to-number (match-string 3 s))
  16084. (string-to-number (match-string 2 s))
  16085. nil nil nil))
  16086. ((string-match "^<[^>]+>$" s)
  16087. (decode-time (seconds-to-time (org-matcher-time s))))
  16088. (t (error "Not a standard Org-mode time string: %s" s))))
  16089. (defun org-timestamp-up (&optional arg)
  16090. "Increase the date item at the cursor by one.
  16091. If the cursor is on the year, change the year. If it is on the month,
  16092. the day or the time, change that.
  16093. With prefix ARG, change by that many units."
  16094. (interactive "p")
  16095. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16096. (defun org-timestamp-down (&optional arg)
  16097. "Decrease the date item at the cursor by one.
  16098. If the cursor is on the year, change the year. If it is on the month,
  16099. the day or the time, change that.
  16100. With prefix ARG, change by that many units."
  16101. (interactive "p")
  16102. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16103. (defun org-timestamp-up-day (&optional arg)
  16104. "Increase the date in the time stamp by one day.
  16105. With prefix ARG, change that many days."
  16106. (interactive "p")
  16107. (if (and (not (org-at-timestamp-p t))
  16108. (org-at-heading-p))
  16109. (org-todo 'up)
  16110. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16111. (defun org-timestamp-down-day (&optional arg)
  16112. "Decrease the date in the time stamp by one day.
  16113. With prefix ARG, change that many days."
  16114. (interactive "p")
  16115. (if (and (not (org-at-timestamp-p t))
  16116. (org-at-heading-p))
  16117. (org-todo 'down)
  16118. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16119. (defun org-at-timestamp-p (&optional inactive-ok)
  16120. "Determine if the cursor is in or at a timestamp."
  16121. (interactive)
  16122. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16123. (pos (point))
  16124. (ans (or (looking-at tsr)
  16125. (save-excursion
  16126. (skip-chars-backward "^[<\n\r\t")
  16127. (if (> (point) (point-min)) (backward-char 1))
  16128. (and (looking-at tsr)
  16129. (> (- (match-end 0) pos) -1))))))
  16130. (and ans
  16131. (boundp 'org-ts-what)
  16132. (setq org-ts-what
  16133. (cond
  16134. ((= pos (match-beginning 0)) 'bracket)
  16135. ;; Point is considered to be "on the bracket" whether
  16136. ;; it's really on it or right after it.
  16137. ((= pos (1- (match-end 0))) 'bracket)
  16138. ((= pos (match-end 0)) 'after)
  16139. ((org-pos-in-match-range pos 2) 'year)
  16140. ((org-pos-in-match-range pos 3) 'month)
  16141. ((org-pos-in-match-range pos 7) 'hour)
  16142. ((org-pos-in-match-range pos 8) 'minute)
  16143. ((or (org-pos-in-match-range pos 4)
  16144. (org-pos-in-match-range pos 5)) 'day)
  16145. ((and (> pos (or (match-end 8) (match-end 5)))
  16146. (< pos (match-end 0)))
  16147. (- pos (or (match-end 8) (match-end 5))))
  16148. (t 'day))))
  16149. ans))
  16150. (defun org-toggle-timestamp-type ()
  16151. "Toggle the type (<active> or [inactive]) of a time stamp."
  16152. (interactive)
  16153. (when (org-at-timestamp-p t)
  16154. (let ((beg (match-beginning 0)) (end (match-end 0))
  16155. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16156. (save-excursion
  16157. (goto-char beg)
  16158. (while (re-search-forward "[][<>]" end t)
  16159. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16160. t t)))
  16161. (message "Timestamp is now %sactive"
  16162. (if (equal (char-after beg) ?<) "" "in")))))
  16163. (defun org-at-clock-log-p nil
  16164. "Is the cursor on the clock log line?"
  16165. (save-excursion
  16166. (move-beginning-of-line 1)
  16167. (looking-at org-clock-line-re)))
  16168. (defvar org-clock-history) ; defined in org-clock.el
  16169. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16170. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16171. "Change the date in the time stamp at point.
  16172. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16173. `year', `minute', `second'. If WHAT is not given, the cursor position
  16174. in the timestamp determines what will be changed.
  16175. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16176. (let ((origin (point)) origin-cat
  16177. with-hm inactive
  16178. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16179. org-ts-what
  16180. extra rem
  16181. ts time time0 fixnext clrgx)
  16182. (if (not (org-at-timestamp-p t))
  16183. (user-error "Not at a timestamp"))
  16184. (if (and (not what) (eq org-ts-what 'bracket))
  16185. (org-toggle-timestamp-type)
  16186. ;; Point isn't on brackets. Remember the part of the time-stamp
  16187. ;; the point was in. Indeed, size of time-stamps may change,
  16188. ;; but point must be kept in the same category nonetheless.
  16189. (setq origin-cat org-ts-what)
  16190. (if (and (not what) (not (eq org-ts-what 'day))
  16191. org-display-custom-times
  16192. (get-text-property (point) 'display)
  16193. (not (get-text-property (1- (point)) 'display)))
  16194. (setq org-ts-what 'day))
  16195. (setq org-ts-what (or what org-ts-what)
  16196. inactive (= (char-after (match-beginning 0)) ?\[)
  16197. ts (match-string 0))
  16198. (replace-match "")
  16199. (when (string-match
  16200. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16201. ts)
  16202. (setq extra (match-string 1 ts))
  16203. (if suppress-tmp-delay
  16204. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16205. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16206. (setq with-hm t))
  16207. (setq time0 (org-parse-time-string ts))
  16208. (when (and updown
  16209. (eq org-ts-what 'minute)
  16210. (not current-prefix-arg))
  16211. ;; This looks like s-up and s-down. Change by one rounding step.
  16212. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16213. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16214. (setcar (cdr time0) (+ (nth 1 time0)
  16215. (if (> n 0) (- rem) (- dm rem))))))
  16216. (setq time
  16217. (encode-time (or (car time0) 0)
  16218. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16219. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16220. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16221. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16222. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16223. (nthcdr 6 time0)))
  16224. (when (and (member org-ts-what '(hour minute))
  16225. extra
  16226. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16227. (setq extra (org-modify-ts-extra
  16228. extra
  16229. (if (eq org-ts-what 'hour) 2 5)
  16230. n dm)))
  16231. (when (integerp org-ts-what)
  16232. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16233. (if (eq what 'calendar)
  16234. (let ((cal-date (org-get-date-from-calendar)))
  16235. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16236. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16237. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16238. (setcar time0 (or (car time0) 0))
  16239. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16240. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16241. (setq time (apply 'encode-time time0))))
  16242. ;; Insert the new time-stamp, and ensure point stays in the same
  16243. ;; category as before (i.e. not after the last position in that
  16244. ;; category).
  16245. (let ((pos (point)))
  16246. ;; Stay before inserted string. `save-excursion' is of no use.
  16247. (setq org-last-changed-timestamp
  16248. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16249. (goto-char pos))
  16250. (save-match-data
  16251. (looking-at org-ts-regexp3)
  16252. (goto-char (cond
  16253. ;; `day' category ends before `hour' if any, or at
  16254. ;; the end of the day name.
  16255. ((eq origin-cat 'day)
  16256. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16257. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16258. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16259. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16260. ;; `year' and `month' have both fixed size: point
  16261. ;; couldn't have moved into another part.
  16262. (t origin))))
  16263. ;; Update clock if on a CLOCK line.
  16264. (org-clock-update-time-maybe)
  16265. ;; Maybe adjust the closest clock in `org-clock-history'
  16266. (when org-clock-adjust-closest
  16267. (if (not (and (org-at-clock-log-p)
  16268. (< 1 (length (delq nil (mapcar 'marker-position
  16269. org-clock-history))))))
  16270. (message "No clock to adjust")
  16271. (cond ((save-excursion ; fix previous clock?
  16272. (re-search-backward org-ts-regexp0 nil t)
  16273. (org-looking-back (concat org-clock-string " \\[")))
  16274. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16275. ((save-excursion ; fix next clock?
  16276. (re-search-backward org-ts-regexp0 nil t)
  16277. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16278. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16279. (save-window-excursion
  16280. ;; Find closest clock to point, adjust the previous/next one in history
  16281. (let* ((p (save-excursion (org-back-to-heading t)))
  16282. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16283. (clfixnth
  16284. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16285. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16286. (if (not clfixpos)
  16287. (message "No clock to adjust")
  16288. (save-excursion
  16289. (org-goto-marker-or-bmk clfixpos)
  16290. (org-show-subtree)
  16291. (when (re-search-forward clrgx nil t)
  16292. (goto-char (match-beginning 1))
  16293. (let (org-clock-adjust-closest)
  16294. (org-timestamp-change n org-ts-what updown))
  16295. (message "Clock adjusted in %s for heading: %s"
  16296. (file-name-nondirectory (buffer-file-name))
  16297. (org-get-heading t t)))))))))
  16298. ;; Try to recenter the calendar window, if any.
  16299. (if (and org-calendar-follow-timestamp-change
  16300. (get-buffer-window "*Calendar*" t)
  16301. (memq org-ts-what '(day month year)))
  16302. (org-recenter-calendar (time-to-days time))))))
  16303. (defun org-modify-ts-extra (s pos n dm)
  16304. "Change the different parts of the lead-time and repeat fields in timestamp."
  16305. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16306. ng h m new rem)
  16307. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16308. (cond
  16309. ((or (org-pos-in-match-range pos 2)
  16310. (org-pos-in-match-range pos 3))
  16311. (setq m (string-to-number (match-string 3 s))
  16312. h (string-to-number (match-string 2 s)))
  16313. (if (org-pos-in-match-range pos 2)
  16314. (setq h (+ h n))
  16315. (setq n (* dm (org-no-warnings (signum n))))
  16316. (when (not (= 0 (setq rem (% m dm))))
  16317. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16318. (setq m (+ m n)))
  16319. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16320. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16321. (setq h (min 24 (max 0 h)))
  16322. (setq ng 1 new (format "-%02d:%02d" h m)))
  16323. ((org-pos-in-match-range pos 6)
  16324. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16325. ((org-pos-in-match-range pos 5)
  16326. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16327. ((org-pos-in-match-range pos 9)
  16328. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16329. ((org-pos-in-match-range pos 8)
  16330. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16331. (when ng
  16332. (setq s (concat
  16333. (substring s 0 (match-beginning ng))
  16334. new
  16335. (substring s (match-end ng))))))
  16336. s))
  16337. (defun org-recenter-calendar (date)
  16338. "If the calendar is visible, recenter it to DATE."
  16339. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16340. (when cwin
  16341. (let ((calendar-move-hook nil))
  16342. (with-selected-window cwin
  16343. (calendar-goto-date (if (listp date) date
  16344. (calendar-gregorian-from-absolute date))))))))
  16345. (defun org-goto-calendar (&optional arg)
  16346. "Go to the Emacs calendar at the current date.
  16347. If there is a time stamp in the current line, go to that date.
  16348. A prefix ARG can be used to force the current date."
  16349. (interactive "P")
  16350. (let ((tsr org-ts-regexp) diff
  16351. (calendar-move-hook nil)
  16352. (calendar-view-holidays-initially-flag nil)
  16353. (calendar-view-diary-initially-flag nil))
  16354. (if (or (org-at-timestamp-p)
  16355. (save-excursion
  16356. (beginning-of-line 1)
  16357. (looking-at (concat ".*" tsr))))
  16358. (let ((d1 (time-to-days (current-time)))
  16359. (d2 (time-to-days
  16360. (org-time-string-to-time (match-string 1)))))
  16361. (setq diff (- d2 d1))))
  16362. (calendar)
  16363. (calendar-goto-today)
  16364. (if (and diff (not arg)) (calendar-forward-day diff))))
  16365. (defun org-get-date-from-calendar ()
  16366. "Return a list (month day year) of date at point in calendar."
  16367. (with-current-buffer "*Calendar*"
  16368. (save-match-data
  16369. (calendar-cursor-to-date))))
  16370. (defun org-date-from-calendar ()
  16371. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16372. If there is already a time stamp at the cursor position, update it."
  16373. (interactive)
  16374. (if (org-at-timestamp-p t)
  16375. (org-timestamp-change 0 'calendar)
  16376. (let ((cal-date (org-get-date-from-calendar)))
  16377. (org-insert-time-stamp
  16378. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16379. (defcustom org-effort-durations
  16380. `(("h" . 60)
  16381. ("d" . ,(* 60 8))
  16382. ("w" . ,(* 60 8 5))
  16383. ("m" . ,(* 60 8 5 4))
  16384. ("y" . ,(* 60 8 5 40)))
  16385. "Conversion factor to minutes for an effort modifier.
  16386. Each entry has the form (MODIFIER . MINUTES).
  16387. In an effort string, a number followed by MODIFIER is multiplied
  16388. by the specified number of MINUTES to obtain an effort in
  16389. minutes.
  16390. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16391. effort string \"2hours\" is equivalent to 120 minutes."
  16392. :group 'org-agenda
  16393. :version "24.1"
  16394. :type '(alist :key-type (string :tag "Modifier")
  16395. :value-type (number :tag "Minutes")))
  16396. (defun org-minutes-to-clocksum-string (m)
  16397. "Format number of minutes as a clocksum string.
  16398. The format is determined by `org-time-clocksum-format',
  16399. `org-time-clocksum-use-fractional' and
  16400. `org-time-clocksum-fractional-format' and
  16401. `org-time-clocksum-use-effort-durations'."
  16402. (let ((clocksum "")
  16403. (m (round m)) ; Don't allow fractions of minutes
  16404. h d w mo y fmt n)
  16405. (setq h (if org-time-clocksum-use-effort-durations
  16406. (cdr (assoc "h" org-effort-durations)) 60)
  16407. d (if org-time-clocksum-use-effort-durations
  16408. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16409. w (if org-time-clocksum-use-effort-durations
  16410. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16411. mo (if org-time-clocksum-use-effort-durations
  16412. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16413. y (if org-time-clocksum-use-effort-durations
  16414. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16415. ;; fractional format
  16416. (if org-time-clocksum-use-fractional
  16417. (cond
  16418. ;; single format string
  16419. ((stringp org-time-clocksum-fractional-format)
  16420. (format org-time-clocksum-fractional-format (/ m (float h))))
  16421. ;; choice of fractional formats for different time units
  16422. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16423. (> (/ (truncate m) (* y d h)) 0))
  16424. (format fmt (/ m (* y d (float h)))))
  16425. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16426. (> (/ (truncate m) (* mo d h)) 0))
  16427. (format fmt (/ m (* mo d (float h)))))
  16428. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16429. (> (/ (truncate m) (* w d h)) 0))
  16430. (format fmt (/ m (* w d (float h)))))
  16431. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16432. (> (/ (truncate m) (* d h)) 0))
  16433. (format fmt (/ m (* d (float h)))))
  16434. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16435. (> (/ (truncate m) h) 0))
  16436. (format fmt (/ m (float h))))
  16437. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16438. (format fmt m))
  16439. ;; fall back to smallest time unit with a format
  16440. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16441. (format fmt (/ m (float h))))
  16442. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16443. (format fmt (/ m (* d (float h)))))
  16444. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16445. (format fmt (/ m (* w d (float h)))))
  16446. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16447. (format fmt (/ m (* mo d (float h)))))
  16448. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16449. (format fmt (/ m (* y d (float h))))))
  16450. ;; standard (non-fractional) format, with single format string
  16451. (if (stringp org-time-clocksum-format)
  16452. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16453. ;; separate formats components
  16454. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16455. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16456. (plist-get org-time-clocksum-format :require-years))
  16457. (setq clocksum (concat clocksum (format fmt n))
  16458. m (- m (* n y d h))))
  16459. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16460. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16461. (plist-get org-time-clocksum-format :require-months))
  16462. (setq clocksum (concat clocksum (format fmt n))
  16463. m (- m (* n mo d h))))
  16464. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16465. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16466. (plist-get org-time-clocksum-format :require-weeks))
  16467. (setq clocksum (concat clocksum (format fmt n))
  16468. m (- m (* n w d h))))
  16469. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16470. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16471. (plist-get org-time-clocksum-format :require-days))
  16472. (setq clocksum (concat clocksum (format fmt n))
  16473. m (- m (* n d h))))
  16474. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16475. (or (> (setq n (/ (truncate m) h)) 0)
  16476. (plist-get org-time-clocksum-format :require-hours))
  16477. (setq clocksum (concat clocksum (format fmt n))
  16478. m (- m (* n h))))
  16479. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16480. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16481. (setq clocksum (concat clocksum (format fmt m))))
  16482. ;; return formatted time duration
  16483. clocksum))))
  16484. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16485. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16486. "Org mode version 8.0")
  16487. (defun org-hours-to-clocksum-string (n)
  16488. (org-minutes-to-clocksum-string (* n 60)))
  16489. (defun org-hh:mm-string-to-minutes (s)
  16490. "Convert a string H:MM to a number of minutes.
  16491. If the string is just a number, interpret it as minutes.
  16492. In fact, the first hh:mm or number in the string will be taken,
  16493. there can be extra stuff in the string.
  16494. If no number is found, the return value is 0."
  16495. (cond
  16496. ((integerp s) s)
  16497. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16498. (+ (* (string-to-number (match-string 1 s)) 60)
  16499. (string-to-number (match-string 2 s))))
  16500. ((string-match "\\([0-9]+\\)" s)
  16501. (string-to-number (match-string 1 s)))
  16502. (t 0)))
  16503. (defcustom org-image-actual-width t
  16504. "Should we use the actual width of images when inlining them?
  16505. When set to `t', always use the image width.
  16506. When set to a number, use imagemagick (when available) to set
  16507. the image's width to this value.
  16508. When set to a number in a list, try to get the width from any
  16509. #+ATTR.* keyword if it matches a width specification like
  16510. #+ATTR_HTML: :width 300px
  16511. and fall back on that number if none is found.
  16512. When set to nil, try to get the width from an #+ATTR.* keyword
  16513. and fall back on the original width if none is found.
  16514. This requires Emacs >= 24.1, build with imagemagick support."
  16515. :group 'org-appearance
  16516. :version "24.4"
  16517. :package-version '(Org . "8.0")
  16518. :type '(choice
  16519. (const :tag "Use the image width" t)
  16520. (integer :tag "Use a number of pixels")
  16521. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16522. (const :tag "Use #+ATTR* or don't resize" nil)))
  16523. (defcustom org-agenda-inhibit-startup nil
  16524. "Inhibit startup when preparing agenda buffers.
  16525. When this variable is `t', the initialization of the Org agenda
  16526. buffers is inhibited: e.g. the visibility state is not set, the
  16527. tables are not re-aligned, etc."
  16528. :type 'boolean
  16529. :version "24.3"
  16530. :group 'org-agenda)
  16531. (define-obsolete-variable-alias
  16532. 'org-agenda-ignore-drawer-properties
  16533. 'org-agenda-ignore-properties "25.1")
  16534. (defcustom org-agenda-ignore-properties nil
  16535. "Avoid updating text properties when building the agenda.
  16536. Properties are used to prepare buffers for effort estimates,
  16537. appointments, statistics and subtree-local categories.
  16538. If you don't use these in the agenda, you can add them to this
  16539. list and agenda building will be a bit faster.
  16540. The value is a list, with zero or more of the symbols `effort', `appt',
  16541. `stats' or `category'."
  16542. :type '(set :greedy t
  16543. (const effort)
  16544. (const appt)
  16545. (const stats)
  16546. (const category))
  16547. :version "25.1"
  16548. :package-version '(Org . "8.3")
  16549. :group 'org-agenda)
  16550. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16551. "Convert a duration string S to minutes.
  16552. A bare number is interpreted as minutes, modifiers can be set by
  16553. customizing `org-effort-durations' (which see).
  16554. Entries containing a colon are interpreted as H:MM by
  16555. `org-hh:mm-string-to-minutes'."
  16556. (let ((result 0)
  16557. (re (concat "\\([0-9.]+\\) *\\("
  16558. (regexp-opt (mapcar 'car org-effort-durations))
  16559. "\\)")))
  16560. (while (string-match re s)
  16561. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16562. (string-to-number (match-string 1 s))))
  16563. (setq s (replace-match "" nil t s)))
  16564. (setq result (floor result))
  16565. (incf result (org-hh:mm-string-to-minutes s))
  16566. (if output-to-string (number-to-string result) result)))
  16567. ;;;; Files
  16568. (defun org-save-all-org-buffers ()
  16569. "Save all Org-mode buffers without user confirmation."
  16570. (interactive)
  16571. (message "Saving all Org-mode buffers...")
  16572. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16573. (when (featurep 'org-id) (org-id-locations-save))
  16574. (message "Saving all Org-mode buffers... done"))
  16575. (defun org-revert-all-org-buffers ()
  16576. "Revert all Org-mode buffers.
  16577. Prompt for confirmation when there are unsaved changes.
  16578. Be sure you know what you are doing before letting this function
  16579. overwrite your changes.
  16580. This function is useful in a setup where one tracks org files
  16581. with a version control system, to revert on one machine after pulling
  16582. changes from another. I believe the procedure must be like this:
  16583. 1. M-x org-save-all-org-buffers
  16584. 2. Pull changes from the other machine, resolve conflicts
  16585. 3. M-x org-revert-all-org-buffers"
  16586. (interactive)
  16587. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16588. (user-error "Abort"))
  16589. (save-excursion
  16590. (save-window-excursion
  16591. (mapc
  16592. (lambda (b)
  16593. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16594. (with-current-buffer b buffer-file-name))
  16595. (org-pop-to-buffer-same-window b)
  16596. (revert-buffer t 'no-confirm)))
  16597. (buffer-list))
  16598. (when (and (featurep 'org-id) org-id-track-globally)
  16599. (org-id-locations-load)))))
  16600. ;;;; Agenda files
  16601. ;;;###autoload
  16602. (defun org-switchb (&optional arg)
  16603. "Switch between Org buffers.
  16604. With one prefix argument, restrict available buffers to files.
  16605. With two prefix arguments, restrict available buffers to agenda files.
  16606. Defaults to `iswitchb' for buffer name completion.
  16607. Set `org-completion-use-ido' to make it use ido instead."
  16608. (interactive "P")
  16609. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16610. ((equal arg '(16)) (org-buffer-list 'agenda))
  16611. (t (org-buffer-list))))
  16612. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16613. (org-completion-use-ido org-completion-use-ido))
  16614. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16615. (setq org-completion-use-iswitchb t))
  16616. (org-pop-to-buffer-same-window
  16617. (org-icompleting-read "Org buffer: "
  16618. (mapcar 'list (mapcar 'buffer-name blist))
  16619. nil t))))
  16620. ;;; Define some older names previously used for this functionality
  16621. ;;;###autoload
  16622. (defalias 'org-ido-switchb 'org-switchb)
  16623. ;;;###autoload
  16624. (defalias 'org-iswitchb 'org-switchb)
  16625. (defun org-buffer-list (&optional predicate exclude-tmp)
  16626. "Return a list of Org buffers.
  16627. PREDICATE can be `export', `files' or `agenda'.
  16628. export restrict the list to Export buffers.
  16629. files restrict the list to buffers visiting Org files.
  16630. agenda restrict the list to buffers visiting agenda files.
  16631. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16632. (let* ((bfn nil)
  16633. (agenda-files (and (eq predicate 'agenda)
  16634. (mapcar 'file-truename (org-agenda-files t))))
  16635. (filter
  16636. (cond
  16637. ((eq predicate 'files)
  16638. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16639. ((eq predicate 'export)
  16640. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16641. ((eq predicate 'agenda)
  16642. (lambda (b)
  16643. (with-current-buffer b
  16644. (and (derived-mode-p 'org-mode)
  16645. (setq bfn (buffer-file-name b))
  16646. (member (file-truename bfn) agenda-files)))))
  16647. (t (lambda (b) (with-current-buffer b
  16648. (or (derived-mode-p 'org-mode)
  16649. (string-match "\*Org .*Export"
  16650. (buffer-name b)))))))))
  16651. (delq nil
  16652. (mapcar
  16653. (lambda(b)
  16654. (if (and (funcall filter b)
  16655. (or (not exclude-tmp)
  16656. (not (string-match "tmp" (buffer-name b)))))
  16657. b
  16658. nil))
  16659. (buffer-list)))))
  16660. (defun org-agenda-files (&optional unrestricted archives)
  16661. "Get the list of agenda files.
  16662. Optional UNRESTRICTED means return the full list even if a restriction
  16663. is currently in place.
  16664. When ARCHIVES is t, include all archive files that are really being
  16665. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16666. `org-agenda-archives-mode' is t."
  16667. (let ((files
  16668. (cond
  16669. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16670. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16671. ((listp org-agenda-files) org-agenda-files)
  16672. (t (error "Invalid value of `org-agenda-files'")))))
  16673. (setq files (apply 'append
  16674. (mapcar (lambda (f)
  16675. (if (file-directory-p f)
  16676. (directory-files
  16677. f t org-agenda-file-regexp)
  16678. (list f)))
  16679. files)))
  16680. (when org-agenda-skip-unavailable-files
  16681. (setq files (delq nil
  16682. (mapcar (function
  16683. (lambda (file)
  16684. (and (file-readable-p file) file)))
  16685. files))))
  16686. (when (or (eq archives t)
  16687. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16688. (setq files (org-add-archive-files files)))
  16689. files))
  16690. (defun org-agenda-file-p (&optional file)
  16691. "Return non-nil, if FILE is an agenda file.
  16692. If FILE is omitted, use the file associated with the current
  16693. buffer."
  16694. (let ((fname (or file (buffer-file-name))))
  16695. (and fname
  16696. (member (file-truename fname)
  16697. (mapcar #'file-truename (org-agenda-files t))))))
  16698. (defun org-edit-agenda-file-list ()
  16699. "Edit the list of agenda files.
  16700. Depending on setup, this either uses customize to edit the variable
  16701. `org-agenda-files', or it visits the file that is holding the list. In the
  16702. latter case, the buffer is set up in a way that saving it automatically kills
  16703. the buffer and restores the previous window configuration."
  16704. (interactive)
  16705. (if (stringp org-agenda-files)
  16706. (let ((cw (current-window-configuration)))
  16707. (find-file org-agenda-files)
  16708. (org-set-local 'org-window-configuration cw)
  16709. (org-add-hook 'after-save-hook
  16710. (lambda ()
  16711. (set-window-configuration
  16712. (prog1 org-window-configuration
  16713. (kill-buffer (current-buffer))))
  16714. (org-install-agenda-files-menu)
  16715. (message "New agenda file list installed"))
  16716. nil 'local)
  16717. (message "%s" (substitute-command-keys
  16718. "Edit list and finish with \\[save-buffer]")))
  16719. (customize-variable 'org-agenda-files)))
  16720. (defun org-store-new-agenda-file-list (list)
  16721. "Set new value for the agenda file list and save it correctly."
  16722. (if (stringp org-agenda-files)
  16723. (let ((fe (org-read-agenda-file-list t)) b u)
  16724. (while (setq b (find-buffer-visiting org-agenda-files))
  16725. (kill-buffer b))
  16726. (with-temp-file org-agenda-files
  16727. (insert
  16728. (mapconcat
  16729. (lambda (f) ;; Keep un-expanded entries.
  16730. (if (setq u (assoc f fe))
  16731. (cdr u)
  16732. f))
  16733. list "\n")
  16734. "\n")))
  16735. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16736. (org-insert-mode-line-in-empty-file nil))
  16737. (setq org-agenda-files list)
  16738. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16739. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16740. "Read the list of agenda files from a file.
  16741. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16742. filenames, used by `org-store-new-agenda-file-list' to write back
  16743. un-expanded file names."
  16744. (when (file-directory-p org-agenda-files)
  16745. (error "`org-agenda-files' cannot be a single directory"))
  16746. (when (stringp org-agenda-files)
  16747. (with-temp-buffer
  16748. (insert-file-contents org-agenda-files)
  16749. (mapcar
  16750. (lambda (f)
  16751. (let ((e (expand-file-name (substitute-in-file-name f)
  16752. org-directory)))
  16753. (if pair-with-expansion
  16754. (cons e f)
  16755. e)))
  16756. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16757. ;;;###autoload
  16758. (defun org-cycle-agenda-files ()
  16759. "Cycle through the files in `org-agenda-files'.
  16760. If the current buffer visits an agenda file, find the next one in the list.
  16761. If the current buffer does not, find the first agenda file."
  16762. (interactive)
  16763. (let* ((fs (org-agenda-files t))
  16764. (files (append fs (list (car fs))))
  16765. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16766. file)
  16767. (unless files (user-error "No agenda files"))
  16768. (catch 'exit
  16769. (while (setq file (pop files))
  16770. (if (equal (file-truename file) tcf)
  16771. (when (car files)
  16772. (find-file (car files))
  16773. (throw 'exit t))))
  16774. (find-file (car fs)))
  16775. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16776. (defun org-agenda-file-to-front (&optional to-end)
  16777. "Move/add the current file to the top of the agenda file list.
  16778. If the file is not present in the list, it is added to the front. If it is
  16779. present, it is moved there. With optional argument TO-END, add/move to the
  16780. end of the list."
  16781. (interactive "P")
  16782. (let ((org-agenda-skip-unavailable-files nil)
  16783. (file-alist (mapcar (lambda (x)
  16784. (cons (file-truename x) x))
  16785. (org-agenda-files t)))
  16786. (ctf (file-truename
  16787. (or buffer-file-name
  16788. (user-error "Please save the current buffer to a file"))))
  16789. x had)
  16790. (setq x (assoc ctf file-alist) had x)
  16791. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16792. (if to-end
  16793. (setq file-alist (append (delq x file-alist) (list x)))
  16794. (setq file-alist (cons x (delq x file-alist))))
  16795. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16796. (org-install-agenda-files-menu)
  16797. (message "File %s to %s of agenda file list"
  16798. (if had "moved" "added") (if to-end "end" "front"))))
  16799. (defun org-remove-file (&optional file)
  16800. "Remove current file from the list of files in variable `org-agenda-files'.
  16801. These are the files which are being checked for agenda entries.
  16802. Optional argument FILE means use this file instead of the current."
  16803. (interactive)
  16804. (let* ((org-agenda-skip-unavailable-files nil)
  16805. (file (or file buffer-file-name
  16806. (user-error "Current buffer does not visit a file")))
  16807. (true-file (file-truename file))
  16808. (afile (abbreviate-file-name file))
  16809. (files (delq nil (mapcar
  16810. (lambda (x)
  16811. (if (equal true-file
  16812. (file-truename x))
  16813. nil x))
  16814. (org-agenda-files t)))))
  16815. (if (not (= (length files) (length (org-agenda-files t))))
  16816. (progn
  16817. (org-store-new-agenda-file-list files)
  16818. (org-install-agenda-files-menu)
  16819. (message "Removed file: %s" afile))
  16820. (message "File was not in list: %s (not removed)" afile))))
  16821. (defun org-file-menu-entry (file)
  16822. (vector file (list 'find-file file) t))
  16823. (defun org-check-agenda-file (file)
  16824. "Make sure FILE exists. If not, ask user what to do."
  16825. (when (not (file-exists-p file))
  16826. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16827. (abbreviate-file-name file))
  16828. (let ((r (downcase (read-char-exclusive))))
  16829. (cond
  16830. ((equal r ?r)
  16831. (org-remove-file file)
  16832. (throw 'nextfile t))
  16833. (t (user-error "Abort"))))))
  16834. (defun org-get-agenda-file-buffer (file)
  16835. "Get an agenda buffer visiting FILE.
  16836. If the buffer needs to be created, add it to the list of buffers
  16837. which might be released later."
  16838. (let ((buf (org-find-base-buffer-visiting file)))
  16839. (if buf
  16840. buf ; just return it
  16841. ;; Make a new buffer and remember it
  16842. (setq buf (find-file-noselect file))
  16843. (if buf (push buf org-agenda-new-buffers))
  16844. buf)))
  16845. (defun org-release-buffers (blist)
  16846. "Release all buffers in list, asking the user for confirmation when needed.
  16847. When a buffer is unmodified, it is just killed. When modified, it is saved
  16848. \(if the user agrees) and then killed."
  16849. (let (buf file)
  16850. (while (setq buf (pop blist))
  16851. (setq file (buffer-file-name buf))
  16852. (when (and (buffer-modified-p buf)
  16853. file
  16854. (y-or-n-p (format "Save file %s? " file)))
  16855. (with-current-buffer buf (save-buffer)))
  16856. (kill-buffer buf))))
  16857. (defun org-agenda-prepare-buffers (files)
  16858. "Create buffers for all agenda files, protect archived trees and comments."
  16859. (interactive)
  16860. (let ((pa '(:org-archived t))
  16861. (pc '(:org-comment t))
  16862. (pall '(:org-archived t :org-comment t))
  16863. (inhibit-read-only t)
  16864. (org-inhibit-startup org-agenda-inhibit-startup)
  16865. (rea (concat ":" org-archive-tag ":"))
  16866. file re pos)
  16867. (setq org-tag-alist-for-agenda nil
  16868. org-tag-groups-alist-for-agenda nil)
  16869. (save-excursion
  16870. (save-restriction
  16871. (while (setq file (pop files))
  16872. (catch 'nextfile
  16873. (if (bufferp file)
  16874. (set-buffer file)
  16875. (org-check-agenda-file file)
  16876. (set-buffer (org-get-agenda-file-buffer file)))
  16877. (widen)
  16878. (org-set-regexps-and-options 'tags-only)
  16879. (setq pos (point))
  16880. (or (memq 'category org-agenda-ignore-properties)
  16881. (org-refresh-category-properties))
  16882. (or (memq 'stats org-agenda-ignore-properties)
  16883. (org-refresh-stats-properties))
  16884. (or (memq 'effort org-agenda-ignore-properties)
  16885. (org-refresh-effort-properties))
  16886. (or (memq 'appt org-agenda-ignore-properties)
  16887. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16888. (setq org-todo-keywords-for-agenda
  16889. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16890. (setq org-done-keywords-for-agenda
  16891. (append org-done-keywords-for-agenda org-done-keywords))
  16892. (setq org-todo-keyword-alist-for-agenda
  16893. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16894. (setq org-tag-alist-for-agenda
  16895. (org-uniquify
  16896. (append org-tag-alist-for-agenda
  16897. org-tag-alist
  16898. org-tag-persistent-alist)))
  16899. (if org-group-tags
  16900. (setq org-tag-groups-alist-for-agenda
  16901. (org-uniquify-alist
  16902. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16903. (org-with-silent-modifications
  16904. (save-excursion
  16905. (remove-text-properties (point-min) (point-max) pall)
  16906. (when org-agenda-skip-archived-trees
  16907. (goto-char (point-min))
  16908. (while (re-search-forward rea nil t)
  16909. (if (org-at-heading-p t)
  16910. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16911. (goto-char (point-min))
  16912. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16913. (while (re-search-forward re nil t)
  16914. (when (save-match-data (org-in-commented-heading-p t))
  16915. (add-text-properties
  16916. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16917. (goto-char pos)))))
  16918. (setq org-todo-keywords-for-agenda
  16919. (org-uniquify org-todo-keywords-for-agenda))
  16920. (setq org-todo-keyword-alist-for-agenda
  16921. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16922. ;;;; CDLaTeX minor mode
  16923. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16924. "Keymap for the minor `org-cdlatex-mode'.")
  16925. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16926. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16927. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16928. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16929. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  16930. (defvar org-cdlatex-texmathp-advice-is-done nil
  16931. "Flag remembering if we have applied the advice to texmathp already.")
  16932. (define-minor-mode org-cdlatex-mode
  16933. "Toggle the minor `org-cdlatex-mode'.
  16934. This mode supports entering LaTeX environment and math in LaTeX fragments
  16935. in Org-mode.
  16936. \\{org-cdlatex-mode-map}"
  16937. nil " OCDL" nil
  16938. (when org-cdlatex-mode
  16939. (require 'cdlatex)
  16940. (run-hooks 'cdlatex-mode-hook)
  16941. (cdlatex-compute-tables))
  16942. (unless org-cdlatex-texmathp-advice-is-done
  16943. (setq org-cdlatex-texmathp-advice-is-done t)
  16944. (defadvice texmathp (around org-math-always-on activate)
  16945. "Always return t in org-mode buffers.
  16946. This is because we want to insert math symbols without dollars even outside
  16947. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16948. an embedded LaTeX fragment, let texmathp do its job.
  16949. \\[org-cdlatex-mode-map]"
  16950. (interactive)
  16951. (let (p)
  16952. (cond
  16953. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16954. ((eq this-command 'cdlatex-math-symbol)
  16955. (setq ad-return-value t
  16956. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16957. (t
  16958. (let ((p (org-inside-LaTeX-fragment-p)))
  16959. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16960. (setq ad-return-value t
  16961. texmathp-why '("Org-mode embedded math" . 0))
  16962. (if p ad-do-it)))))))))
  16963. (defun turn-on-org-cdlatex ()
  16964. "Unconditionally turn on `org-cdlatex-mode'."
  16965. (org-cdlatex-mode 1))
  16966. (defun org-try-cdlatex-tab ()
  16967. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16968. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16969. - inside a LaTeX fragment, or
  16970. - after the first word in a line, where an abbreviation expansion could
  16971. insert a LaTeX environment."
  16972. (when org-cdlatex-mode
  16973. (cond
  16974. ;; Before any word on the line: No expansion possible.
  16975. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16976. ;; Just after first word on the line: Expand it. Make sure it
  16977. ;; cannot happen on headlines, though.
  16978. ((save-excursion
  16979. (skip-chars-backward "a-zA-Z0-9*")
  16980. (skip-chars-backward " \t")
  16981. (and (bolp) (not (org-at-heading-p))))
  16982. (cdlatex-tab) t)
  16983. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16984. (defun org-cdlatex-underscore-caret (&optional arg)
  16985. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16986. Revert to the normal definition outside of these fragments."
  16987. (interactive "P")
  16988. (if (org-inside-LaTeX-fragment-p)
  16989. (call-interactively 'cdlatex-sub-superscript)
  16990. (let (org-cdlatex-mode)
  16991. (call-interactively (key-binding (vector last-input-event))))))
  16992. (defun org-cdlatex-math-modify (&optional arg)
  16993. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16994. Revert to the normal definition outside of these fragments."
  16995. (interactive "P")
  16996. (if (org-inside-LaTeX-fragment-p)
  16997. (call-interactively 'cdlatex-math-modify)
  16998. (let (org-cdlatex-mode)
  16999. (call-interactively (key-binding (vector last-input-event))))))
  17000. (defun org-cdlatex-environment-indent (&optional environment item)
  17001. "Execute `cdlatex-environment' and indent the inserted environment."
  17002. (interactive)
  17003. (cdlatex-environment environment item)
  17004. (let ((element (org-element-at-point)))
  17005. (org-indent-region (org-element-property :begin element)
  17006. (org-element-property :end element))))
  17007. ;;;; LaTeX fragments
  17008. (defun org-inside-LaTeX-fragment-p ()
  17009. "Test if point is inside a LaTeX fragment.
  17010. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17011. sequence appearing also before point.
  17012. Even though the matchers for math are configurable, this function assumes
  17013. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17014. delimiters are skipped when they have been removed by customization.
  17015. The return value is nil, or a cons cell with the delimiter and the
  17016. position of this delimiter.
  17017. This function does a reasonably good job, but can locally be fooled by
  17018. for example currency specifications. For example it will assume being in
  17019. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17020. fragments that are properly closed, but during editing, we have to live
  17021. with the uncertainty caused by missing closing delimiters. This function
  17022. looks only before point, not after."
  17023. (catch 'exit
  17024. (let ((pos (point))
  17025. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17026. (lim (progn
  17027. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17028. (point)))
  17029. dd-on str (start 0) m re)
  17030. (goto-char pos)
  17031. (when dodollar
  17032. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17033. re (nth 1 (assoc "$" org-latex-regexps)))
  17034. (while (string-match re str start)
  17035. (cond
  17036. ((= (match-end 0) (length str))
  17037. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17038. ((= (match-end 0) (- (length str) 5))
  17039. (throw 'exit nil))
  17040. (t (setq start (match-end 0))))))
  17041. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17042. (goto-char pos)
  17043. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17044. (and (match-beginning 2) (throw 'exit nil))
  17045. ;; count $$
  17046. (while (re-search-backward "\\$\\$" lim t)
  17047. (setq dd-on (not dd-on)))
  17048. (goto-char pos)
  17049. (if dd-on (cons "$$" m))))))
  17050. (defun org-inside-latex-macro-p ()
  17051. "Is point inside a LaTeX macro or its arguments?"
  17052. (save-match-data
  17053. (org-in-regexp
  17054. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17055. (defvar org-latex-fragment-image-overlays nil
  17056. "List of overlays carrying the images of latex fragments.")
  17057. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17058. (defun org-remove-latex-fragment-image-overlays ()
  17059. "Remove all overlays with LaTeX fragment images in current buffer."
  17060. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17061. (setq org-latex-fragment-image-overlays nil))
  17062. (define-obsolete-function-alias
  17063. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17064. (defun org-toggle-latex-fragment (&optional subtree)
  17065. "Preview the LaTeX fragment at point, or all locally or globally.
  17066. If the cursor is in a LaTeX fragment, create the image and overlay
  17067. it over the source code. If there is no fragment at point, display
  17068. all fragments in the current text, from one headline to the next. With
  17069. prefix SUBTREE, display all fragments in the current subtree. With a
  17070. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17071. the cursor is before the first headline,
  17072. display all fragments in the buffer.
  17073. The images can be removed again with \\[org-toggle-latex-fragment]."
  17074. (interactive "P")
  17075. (unless (buffer-file-name (buffer-base-buffer))
  17076. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17077. (if org-latex-fragment-image-overlays
  17078. (progn (org-remove-latex-fragment-image-overlays)
  17079. (message "LaTeX fragments images removed"))
  17080. (when (display-graphic-p)
  17081. (org-remove-latex-fragment-image-overlays)
  17082. (org-with-wide-buffer
  17083. (let (beg end msg)
  17084. (cond
  17085. ((equal subtree '(16))
  17086. (setq beg (point-min) end (point-max)
  17087. msg "Creating images for buffer...%s"))
  17088. ((equal subtree '(4))
  17089. (org-back-to-heading)
  17090. (setq beg (point) end (org-end-of-subtree t)
  17091. msg "Creating images for subtree...%s"))
  17092. ((let ((context (org-element-context)))
  17093. (when (memq (org-element-type context)
  17094. '(latex-environment latex-fragment))
  17095. (setq beg (org-element-property :begin context)
  17096. end (org-element-property :end context)
  17097. msg "Creating image...%s"))))
  17098. ((org-before-first-heading-p)
  17099. (setq beg (point-min) end (point-max)
  17100. msg "Creating images for buffer...%s"))
  17101. (t
  17102. (org-back-to-heading)
  17103. (setq beg (point) end (progn (outline-next-heading) (point))
  17104. msg "Creating images for entry...%s")))
  17105. (message msg "")
  17106. (narrow-to-region beg end)
  17107. (goto-char beg)
  17108. (org-format-latex
  17109. (concat org-latex-preview-ltxpng-directory
  17110. (file-name-sans-extension
  17111. (file-name-nondirectory
  17112. (buffer-file-name (buffer-base-buffer)))))
  17113. default-directory 'overlays msg 'forbuffer
  17114. org-latex-create-formula-image-program)
  17115. (message msg "done. Use `C-c C-x C-l' to remove images."))))))
  17116. (defun org-format-latex
  17117. (prefix &optional dir overlays msg forbuffer processing-type)
  17118. "Replace LaTeX fragments with links to an image, and produce images.
  17119. Some of the options can be changed using the variable
  17120. `org-format-latex-options'."
  17121. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17122. (unless (eq processing-type 'verbatim)
  17123. (let* ((math-regexp "\\$\\|\\\\[(]]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17124. (cnt 0)
  17125. checkdir-flag)
  17126. (goto-char (point-min))
  17127. (while (re-search-forward math-regexp nil t)
  17128. (unless (and overlays
  17129. (eq (get-char-property (point) 'org-overlay-type)
  17130. 'org-latex-overlay))
  17131. (let* ((context (org-element-context))
  17132. (type (org-element-type context)))
  17133. (when (memq type '(latex-environment latex-fragment))
  17134. (let ((block-type (eq type 'latex-environment))
  17135. (value (org-element-property :value context))
  17136. (beg (org-element-property :begin context))
  17137. (end (save-excursion
  17138. (goto-char (org-element-property :end context))
  17139. (skip-chars-backward " \r\t\n")
  17140. (point))))
  17141. (case processing-type
  17142. (mathjax
  17143. ;; Prepare for MathJax processing.
  17144. (if (eq (char-after beg) ?$)
  17145. (save-excursion
  17146. (delete-region beg end)
  17147. (insert "\\(" (substring value 1 -1) "\\)"))
  17148. (goto-char end)))
  17149. ((dvipng imagemagick)
  17150. ;; Process to an image.
  17151. (incf cnt)
  17152. (goto-char beg)
  17153. (let* ((face (face-at-point))
  17154. ;; Get the colors from the face at point.
  17155. (fg
  17156. (let ((color (plist-get org-format-latex-options
  17157. :foreground)))
  17158. (if (and forbuffer (eq color 'auto))
  17159. (face-attribute face :foreground nil 'default)
  17160. color)))
  17161. (bg
  17162. (let ((color (plist-get org-format-latex-options
  17163. :background)))
  17164. (if (and forbuffer (eq color 'auto))
  17165. (face-attribute face :background nil 'default)
  17166. color)))
  17167. (hash (sha1 (prin1-to-string
  17168. (list org-format-latex-header
  17169. org-latex-default-packages-alist
  17170. org-latex-packages-alist
  17171. org-format-latex-options
  17172. forbuffer value fg bg))))
  17173. (absprefix (expand-file-name prefix dir))
  17174. (linkfile (format "%s_%s.png" prefix hash))
  17175. (movefile (format "%s_%s.png" absprefix hash))
  17176. (sep (and block-type "\n\n"))
  17177. (link (concat sep "[[file:" linkfile "]]" sep))
  17178. (options
  17179. (org-combine-plists
  17180. org-format-latex-options
  17181. `(:foreground ,fg :background ,bg))))
  17182. (when msg (message msg cnt))
  17183. (unless checkdir-flag ; Ensure the directory exists.
  17184. (setq checkdir-flag t)
  17185. (let ((todir (file-name-directory absprefix)))
  17186. (unless (file-directory-p todir)
  17187. (make-directory todir t))))
  17188. (unless (file-exists-p movefile)
  17189. (org-create-formula-image
  17190. value movefile options forbuffer processing-type))
  17191. (if overlays
  17192. (progn
  17193. (dolist (o (overlays-in beg end))
  17194. (when (eq (overlay-get o 'org-overlay-type)
  17195. 'org-latex-overlay)
  17196. (delete-overlay o)))
  17197. (let ((ov (make-overlay beg end)))
  17198. (overlay-put ov
  17199. 'org-overlay-type
  17200. 'org-latex-overlay)
  17201. (if (featurep 'xemacs)
  17202. (progn
  17203. (overlay-put ov 'invisible t)
  17204. (overlay-put
  17205. ov 'end-glyph
  17206. (make-glyph
  17207. (vector 'png :file movefile))))
  17208. (overlay-put
  17209. ov 'display
  17210. (list 'image
  17211. :type 'png
  17212. :file movefile
  17213. :ascent 'center)))
  17214. (push ov org-latex-fragment-image-overlays))
  17215. (goto-char end))
  17216. (delete-region beg end)
  17217. (insert
  17218. (org-add-props link
  17219. (list 'org-latex-src
  17220. (replace-regexp-in-string "\"" "" value)
  17221. 'org-latex-src-embed-type
  17222. (if block-type 'paragraph 'character)))))))
  17223. (mathml
  17224. ;; Process to MathML.
  17225. (unless (org-format-latex-mathml-available-p)
  17226. (user-error "LaTeX to MathML converter not configured"))
  17227. (incf cnt)
  17228. (when msg (message msg cnt))
  17229. (goto-char beg)
  17230. (delete-region beg end)
  17231. (insert (org-format-latex-as-mathml
  17232. value block-type prefix dir)))
  17233. (otherwise
  17234. (error "Unknown conversion type %s for LaTeX fragments"
  17235. processing-type)))))))))))
  17236. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17237. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17238. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17239. sucessful, return the portion between \"<math...> </math>\"
  17240. elements otherwise return nil. When MATHML-FILE is specified,
  17241. write the results in to that file. When invoked as an
  17242. interactive command, prompt for LATEX-FRAG, with initial value
  17243. set to the current active region and echo the results for user
  17244. inspection."
  17245. (interactive (list (let ((frag (when (org-region-active-p)
  17246. (buffer-substring-no-properties
  17247. (region-beginning) (region-end)))))
  17248. (read-string "LaTeX Fragment: " frag nil frag))))
  17249. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17250. (let* ((tmp-in-file (file-relative-name
  17251. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17252. (ignore (write-region latex-frag nil tmp-in-file))
  17253. (tmp-out-file (file-relative-name
  17254. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17255. (cmd (format-spec
  17256. org-latex-to-mathml-convert-command
  17257. `((?j . ,(shell-quote-argument
  17258. (expand-file-name org-latex-to-mathml-jar-file)))
  17259. (?I . ,(shell-quote-argument tmp-in-file))
  17260. (?o . ,(shell-quote-argument tmp-out-file)))))
  17261. mathml shell-command-output)
  17262. (when (org-called-interactively-p 'any)
  17263. (unless (org-format-latex-mathml-available-p)
  17264. (user-error "LaTeX to MathML converter not configured")))
  17265. (message "Running %s" cmd)
  17266. (setq shell-command-output (shell-command-to-string cmd))
  17267. (setq mathml
  17268. (when (file-readable-p tmp-out-file)
  17269. (with-current-buffer (find-file-noselect tmp-out-file t)
  17270. (goto-char (point-min))
  17271. (when (re-search-forward
  17272. (concat
  17273. (regexp-quote
  17274. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17275. "\\(.\\|\n\\)*"
  17276. (regexp-quote "</math>")) nil t)
  17277. (prog1 (match-string 0) (kill-buffer))))))
  17278. (cond
  17279. (mathml
  17280. (setq mathml
  17281. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17282. (when mathml-file
  17283. (write-region mathml nil mathml-file))
  17284. (when (org-called-interactively-p 'any)
  17285. (message mathml)))
  17286. ((message "LaTeX to MathML conversion failed")
  17287. (message shell-command-output)))
  17288. (delete-file tmp-in-file)
  17289. (when (file-exists-p tmp-out-file)
  17290. (delete-file tmp-out-file))
  17291. mathml))
  17292. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17293. prefix &optional dir)
  17294. "Use `org-create-math-formula' but check local cache first."
  17295. (let* ((absprefix (expand-file-name prefix dir))
  17296. (print-length nil) (print-level nil)
  17297. (formula-id (concat
  17298. "formula-"
  17299. (sha1
  17300. (prin1-to-string
  17301. (list latex-frag
  17302. org-latex-to-mathml-convert-command)))))
  17303. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17304. (formula-cache-dir (file-name-directory formula-cache)))
  17305. (unless (file-directory-p formula-cache-dir)
  17306. (make-directory formula-cache-dir t))
  17307. (unless (file-exists-p formula-cache)
  17308. (org-create-math-formula latex-frag formula-cache))
  17309. (if (file-exists-p formula-cache)
  17310. ;; Successful conversion. Return the link to MathML file.
  17311. (org-add-props
  17312. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17313. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17314. 'org-latex-src-embed-type (if latex-frag-type
  17315. 'paragraph 'character)))
  17316. ;; Failed conversion. Return the LaTeX fragment verbatim
  17317. latex-frag)))
  17318. (defun org-create-formula-image (string tofile options buffer &optional type)
  17319. "Create an image from LaTeX source using dvipng or convert.
  17320. This function calls either `org-create-formula-image-with-dvipng'
  17321. or `org-create-formula-image-with-imagemagick' depending on the
  17322. value of `org-latex-create-formula-image-program' or on the value
  17323. of the optional TYPE variable.
  17324. Note: ultimately these two function should be combined as they
  17325. share a good deal of logic."
  17326. (org-check-external-command
  17327. "latex" "needed to convert LaTeX fragments to images")
  17328. (funcall
  17329. (case (or type org-latex-create-formula-image-program)
  17330. ('dvipng
  17331. (org-check-external-command
  17332. "dvipng" "needed to convert LaTeX fragments to images")
  17333. 'org-create-formula-image-with-dvipng)
  17334. ('imagemagick
  17335. (org-check-external-command
  17336. "convert" "you need to install imagemagick")
  17337. 'org-create-formula-image-with-imagemagick)
  17338. (t (error
  17339. "Invalid value of `org-latex-create-formula-image-program'")))
  17340. string tofile options buffer))
  17341. (declare-function org-export-get-backend "ox" (name))
  17342. (declare-function org-export--get-global-options "ox" (&optional backend))
  17343. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17344. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17345. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17346. (defun org-create-formula--latex-header ()
  17347. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17348. (let ((info (org-combine-plists (org-export--get-global-options
  17349. (org-export-get-backend 'latex))
  17350. (org-export--get-inbuffer-options
  17351. (org-export-get-backend 'latex)))))
  17352. (org-latex-guess-babel-language
  17353. (org-latex-guess-inputenc
  17354. (org-splice-latex-header
  17355. org-format-latex-header
  17356. org-latex-default-packages-alist
  17357. org-latex-packages-alist t
  17358. (plist-get info :latex-header)))
  17359. info)))
  17360. ;; This function borrows from Ganesh Swami's latex2png.el
  17361. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17362. "This calls dvipng."
  17363. (require 'ox-latex)
  17364. (let* ((tmpdir (if (featurep 'xemacs)
  17365. (temp-directory)
  17366. temporary-file-directory))
  17367. (texfilebase (make-temp-name
  17368. (expand-file-name "orgtex" tmpdir)))
  17369. (texfile (concat texfilebase ".tex"))
  17370. (dvifile (concat texfilebase ".dvi"))
  17371. (pngfile (concat texfilebase ".png"))
  17372. (fnh (if (featurep 'xemacs)
  17373. (font-height (face-font 'default))
  17374. (face-attribute 'default :height nil)))
  17375. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17376. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17377. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17378. "Black"))
  17379. (bg (or (plist-get options (if buffer :background :html-background))
  17380. "Transparent")))
  17381. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17382. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17383. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17384. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17385. (let ((latex-header (org-create-formula--latex-header)))
  17386. (with-temp-file texfile
  17387. (insert latex-header)
  17388. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17389. (let ((dir default-directory))
  17390. (ignore-errors
  17391. (cd tmpdir)
  17392. (call-process "latex" nil nil nil texfile))
  17393. (cd dir))
  17394. (if (not (file-exists-p dvifile))
  17395. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17396. (ignore-errors
  17397. (if (featurep 'xemacs)
  17398. (call-process "dvipng" nil nil nil
  17399. "-fg" fg "-bg" bg
  17400. "-T" "tight"
  17401. "-o" pngfile
  17402. dvifile)
  17403. (call-process "dvipng" nil nil nil
  17404. "-fg" fg "-bg" bg
  17405. "-D" dpi
  17406. ;;"-x" scale "-y" scale
  17407. "-T" "tight"
  17408. "-o" pngfile
  17409. dvifile)))
  17410. (if (not (file-exists-p pngfile))
  17411. (if org-format-latex-signal-error
  17412. (error "Failed to create png file from %s" texfile)
  17413. (message "Failed to create png file from %s" texfile)
  17414. nil)
  17415. ;; Use the requested file name and clean up
  17416. (copy-file pngfile tofile 'replace)
  17417. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17418. (if (file-exists-p (concat texfilebase e))
  17419. (delete-file (concat texfilebase e))))
  17420. pngfile))))
  17421. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17422. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17423. "This calls convert, which is included into imagemagick."
  17424. (require 'ox-latex)
  17425. (let* ((tmpdir (if (featurep 'xemacs)
  17426. (temp-directory)
  17427. temporary-file-directory))
  17428. (texfilebase (make-temp-name
  17429. (expand-file-name "orgtex" tmpdir)))
  17430. (texfile (concat texfilebase ".tex"))
  17431. (pdffile (concat texfilebase ".pdf"))
  17432. (pngfile (concat texfilebase ".png"))
  17433. (fnh (if (featurep 'xemacs)
  17434. (font-height (face-font 'default))
  17435. (face-attribute 'default :height nil)))
  17436. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17437. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17438. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17439. "black"))
  17440. (bg (or (plist-get options (if buffer :background :html-background))
  17441. "white")))
  17442. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17443. (setq fg (org-latex-color-format fg)))
  17444. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17445. (setq bg (org-latex-color-format
  17446. (if (string= bg "Transparent") "white" bg))))
  17447. (let ((latex-header (org-create-formula--latex-header)))
  17448. (with-temp-file texfile
  17449. (insert latex-header)
  17450. (insert "\n\\begin{document}\n"
  17451. "\\definecolor{fg}{rgb}{" fg "}\n"
  17452. "\\definecolor{bg}{rgb}{" bg "}\n"
  17453. "\n\\pagecolor{bg}\n"
  17454. "\n{\\color{fg}\n"
  17455. string
  17456. "\n}\n"
  17457. "\n\\end{document}\n")))
  17458. (org-latex-compile texfile t)
  17459. (if (not (file-exists-p pdffile))
  17460. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17461. (ignore-errors
  17462. (if (featurep 'xemacs)
  17463. (call-process "convert" nil nil nil
  17464. "-density" "96"
  17465. "-trim"
  17466. "-antialias"
  17467. pdffile
  17468. "-quality" "100"
  17469. ;; "-sharpen" "0x1.0"
  17470. pngfile)
  17471. (call-process "convert" nil nil nil
  17472. "-density" dpi
  17473. "-trim"
  17474. "-antialias"
  17475. pdffile
  17476. "-quality" "100"
  17477. ;; "-sharpen" "0x1.0"
  17478. pngfile)))
  17479. (if (not (file-exists-p pngfile))
  17480. (if org-format-latex-signal-error
  17481. (error "Failed to create png file from %s" texfile)
  17482. (message "Failed to create png file from %s" texfile)
  17483. nil)
  17484. ;; Use the requested file name and clean up
  17485. (copy-file pngfile tofile 'replace)
  17486. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17487. (if (file-exists-p (concat texfilebase e))
  17488. (delete-file (concat texfilebase e))))
  17489. pngfile))))
  17490. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17491. "Fill a LaTeX header template TPL.
  17492. In the template, the following place holders will be recognized:
  17493. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17494. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17495. [PACKAGES] \\usepackage statements for PKG
  17496. [NO-PACKAGES] do not include PKG
  17497. [EXTRA] the string EXTRA
  17498. [NO-EXTRA] do not include EXTRA
  17499. For backward compatibility, if both the positive and the negative place
  17500. holder is missing, the positive one (without the \"NO-\") will be
  17501. assumed to be present at the end of the template.
  17502. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17503. EXTRA is a string.
  17504. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17505. (let (rpl (end ""))
  17506. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17507. (setq rpl (if (or (match-end 1) (not def-pkg))
  17508. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17509. tpl (replace-match rpl t t tpl))
  17510. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17511. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17512. (setq rpl (if (or (match-end 1) (not pkg))
  17513. "" (org-latex-packages-to-string pkg snippets-p t))
  17514. tpl (replace-match rpl t t tpl))
  17515. (if pkg (setq end
  17516. (concat end "\n"
  17517. (org-latex-packages-to-string pkg snippets-p)))))
  17518. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17519. (setq rpl (if (or (match-end 1) (not extra))
  17520. "" (concat extra "\n"))
  17521. tpl (replace-match rpl t t tpl))
  17522. (if (and extra (string-match "\\S-" extra))
  17523. (setq end (concat end "\n" extra))))
  17524. (if (string-match "\\S-" end)
  17525. (concat tpl "\n" end)
  17526. tpl)))
  17527. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17528. "Turn an alist of packages into a string with the \\usepackage macros."
  17529. (setq pkg (mapconcat (lambda(p)
  17530. (cond
  17531. ((stringp p) p)
  17532. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17533. (format "%% Package %s omitted" (cadr p)))
  17534. ((equal "" (car p))
  17535. (format "\\usepackage{%s}" (cadr p)))
  17536. (t
  17537. (format "\\usepackage[%s]{%s}"
  17538. (car p) (cadr p)))))
  17539. pkg
  17540. "\n"))
  17541. (if newline (concat pkg "\n") pkg))
  17542. (defun org-dvipng-color (attr)
  17543. "Return a RGB color specification for dvipng."
  17544. (apply 'format "rgb %s %s %s"
  17545. (mapcar 'org-normalize-color
  17546. (if (featurep 'xemacs)
  17547. (color-rgb-components
  17548. (face-property 'default
  17549. (cond ((eq attr :foreground) 'foreground)
  17550. ((eq attr :background) 'background))))
  17551. (color-values (face-attribute 'default attr nil))))))
  17552. (defun org-dvipng-color-format (color-name)
  17553. "Convert COLOR-NAME to a RGB color value for dvipng."
  17554. (apply 'format "rgb %s %s %s"
  17555. (mapcar 'org-normalize-color
  17556. (color-values color-name))))
  17557. (defun org-latex-color (attr)
  17558. "Return a RGB color for the LaTeX color package."
  17559. (apply 'format "%s,%s,%s"
  17560. (mapcar 'org-normalize-color
  17561. (if (featurep 'xemacs)
  17562. (color-rgb-components
  17563. (face-property 'default
  17564. (cond ((eq attr :foreground) 'foreground)
  17565. ((eq attr :background) 'background))))
  17566. (color-values (face-attribute 'default attr nil))))))
  17567. (defun org-latex-color-format (color-name)
  17568. "Convert COLOR-NAME to a RGB color value."
  17569. (apply 'format "%s,%s,%s"
  17570. (mapcar 'org-normalize-color
  17571. (color-values color-name))))
  17572. (defun org-normalize-color (value)
  17573. "Return string to be used as color value for an RGB component."
  17574. (format "%g" (/ value 65535.0)))
  17575. ;; Image display
  17576. (defvar org-inline-image-overlays nil)
  17577. (make-variable-buffer-local 'org-inline-image-overlays)
  17578. (defun org-toggle-inline-images (&optional include-linked)
  17579. "Toggle the display of inline images.
  17580. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17581. (interactive "P")
  17582. (if org-inline-image-overlays
  17583. (progn
  17584. (org-remove-inline-images)
  17585. (message "Inline image display turned off"))
  17586. (org-display-inline-images include-linked)
  17587. (if (and (org-called-interactively-p)
  17588. org-inline-image-overlays)
  17589. (message "%d images displayed inline"
  17590. (length org-inline-image-overlays))
  17591. (message "No images to display inline"))))
  17592. (defun org-redisplay-inline-images ()
  17593. "Refresh the display of inline images."
  17594. (interactive)
  17595. (if (not org-inline-image-overlays)
  17596. (org-toggle-inline-images)
  17597. (org-toggle-inline-images)
  17598. (org-toggle-inline-images)))
  17599. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17600. "Display inline images.
  17601. An inline image is a link which follows either of these
  17602. conventions:
  17603. 1. Its path is a file with an extension matching return value
  17604. from `image-file-name-regexp' and it has no contents.
  17605. 2. Its description consists in a single link of the previous
  17606. type.
  17607. When optional argument INCLUDE-LINKED is non-nil, also links with
  17608. a text description part will be inlined. This can be nice for
  17609. a quick look at those images, but it does not reflect what
  17610. exported files will look like.
  17611. When optional argument REFRESH is non-nil, refresh existing
  17612. images between BEG and END. This will create new image displays
  17613. only if necessary. BEG and END default to the buffer
  17614. boundaries."
  17615. (interactive "P")
  17616. (when (display-graphic-p)
  17617. (unless refresh
  17618. (org-remove-inline-images)
  17619. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17620. (org-with-wide-buffer
  17621. (goto-char (or beg (point-min)))
  17622. (let ((case-fold-search t)
  17623. (file-extension-re (org-image-file-name-regexp)))
  17624. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17625. (let ((link (save-match-data (org-element-context))))
  17626. ;; Check if we're at an inline image.
  17627. (when (and (equal (org-element-property :type link) "file")
  17628. (or include-linked
  17629. (not (org-element-property :contents-begin link)))
  17630. (let ((parent (org-element-property :parent link)))
  17631. (or (not (eq (org-element-type parent) 'link))
  17632. (not (cdr (org-element-contents parent)))))
  17633. (org-string-match-p file-extension-re
  17634. (org-element-property :path link)))
  17635. (let ((file (expand-file-name (org-element-property :path link))))
  17636. (when (file-exists-p file)
  17637. (let ((width
  17638. ;; Apply `org-image-actual-width' specifications.
  17639. (cond
  17640. ((not (image-type-available-p 'imagemagick)) nil)
  17641. ((eq org-image-actual-width t) nil)
  17642. ((listp org-image-actual-width)
  17643. (or
  17644. ;; First try to find a width among
  17645. ;; attributes associated to the paragraph
  17646. ;; containing link.
  17647. (let ((paragraph
  17648. (let ((e link))
  17649. (while (and (setq e (org-element-property
  17650. :parent e))
  17651. (not (eq (org-element-type e)
  17652. 'paragraph))))
  17653. e)))
  17654. (when paragraph
  17655. (save-excursion
  17656. (goto-char (org-element-property :begin paragraph))
  17657. (when
  17658. (re-search-forward
  17659. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17660. (org-element-property
  17661. :post-affiliated paragraph)
  17662. t)
  17663. (string-to-number (match-string 1))))))
  17664. ;; Otherwise, fall-back to provided number.
  17665. (car org-image-actual-width)))
  17666. ((numberp org-image-actual-width)
  17667. org-image-actual-width)))
  17668. (old (get-char-property-and-overlay
  17669. (org-element-property :begin link)
  17670. 'org-image-overlay)))
  17671. (if (and (car-safe old) refresh)
  17672. (image-refresh (overlay-get (cdr old) 'display))
  17673. (let ((image (create-image file
  17674. (and width 'imagemagick)
  17675. nil
  17676. :width width)))
  17677. (when image
  17678. (let* ((link
  17679. ;; If inline image is the description
  17680. ;; of another link, be sure to
  17681. ;; consider the latter as the one to
  17682. ;; apply the overlay on.
  17683. (let ((parent
  17684. (org-element-property :parent link)))
  17685. (if (eq (org-element-type parent) 'link)
  17686. parent
  17687. link)))
  17688. (ov (make-overlay
  17689. (org-element-property :begin link)
  17690. (progn
  17691. (goto-char
  17692. (org-element-property :end link))
  17693. (skip-chars-backward " \t")
  17694. (point)))))
  17695. (overlay-put ov 'display image)
  17696. (overlay-put ov 'face 'default)
  17697. (overlay-put ov 'org-image-overlay t)
  17698. (overlay-put
  17699. ov 'modification-hooks
  17700. (list 'org-display-inline-remove-overlay))
  17701. (push ov org-inline-image-overlays)))))))))))))))
  17702. (define-obsolete-function-alias
  17703. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17704. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17705. "Remove inline-display overlay if a corresponding region is modified."
  17706. (let ((inhibit-modification-hooks t))
  17707. (when (and ov after)
  17708. (delete ov org-inline-image-overlays)
  17709. (delete-overlay ov))))
  17710. (defun org-remove-inline-images ()
  17711. "Remove inline display of images."
  17712. (interactive)
  17713. (mapc 'delete-overlay org-inline-image-overlays)
  17714. (setq org-inline-image-overlays nil))
  17715. ;;;; Key bindings
  17716. ;; Outline functions from `outline-mode-prefix-map'
  17717. ;; that can be remapped in Org:
  17718. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17719. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17720. (define-key org-mode-map [remap outline-forward-same-level]
  17721. 'org-forward-heading-same-level)
  17722. (define-key org-mode-map [remap outline-backward-same-level]
  17723. 'org-backward-heading-same-level)
  17724. (define-key org-mode-map [remap show-branches]
  17725. 'org-kill-note-or-show-branches)
  17726. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17727. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17728. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17729. ;; Outline functions from `outline-mode-prefix-map' that can not
  17730. ;; be remapped in Org:
  17731. ;;
  17732. ;; - the column "key binding" shows whether the Outline function is still
  17733. ;; available in Org mode on the same key that it has been bound to in
  17734. ;; Outline mode:
  17735. ;; - "overridden": key used for a different functionality in Org mode
  17736. ;; - else: key still bound to the same Outline function in Org mode
  17737. ;;
  17738. ;; | Outline function | key binding | Org replacement |
  17739. ;; |------------------------------------+-------------+-----------------------|
  17740. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17741. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17742. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17743. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17744. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17745. ;; | `show-entry' | overridden | no replacement |
  17746. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17747. ;; | `show-branches' | `C-c C-k' | still same function |
  17748. ;; | `show-subtree' | overridden | visibility cycling |
  17749. ;; | `show-all' | overridden | no replacement |
  17750. ;; | `hide-subtree' | overridden | visibility cycling |
  17751. ;; | `hide-body' | overridden | no replacement |
  17752. ;; | `hide-entry' | overridden | visibility cycling |
  17753. ;; | `hide-leaves' | overridden | no replacement |
  17754. ;; | `hide-sublevels' | overridden | no replacement |
  17755. ;; | `hide-other' | overridden | no replacement |
  17756. ;; Make `C-c C-x' a prefix key
  17757. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17758. ;; TAB key with modifiers
  17759. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17760. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17761. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17762. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17763. ;; The following line is necessary under Suse GNU/Linux
  17764. (unless (featurep 'xemacs)
  17765. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17766. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17767. (define-key org-mode-map [backtab] 'org-shifttab)
  17768. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17769. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17770. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17771. ;; Cursor keys with modifiers
  17772. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17773. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17774. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17775. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17776. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17777. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17778. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17779. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17780. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17781. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17782. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17783. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17784. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17785. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17786. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17787. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17788. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17789. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17790. ;; Babel keys
  17791. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17792. (mapc (lambda (pair)
  17793. (define-key org-babel-map (car pair) (cdr pair)))
  17794. org-babel-key-bindings)
  17795. ;;; Extra keys for tty access.
  17796. ;; We only set them when really needed because otherwise the
  17797. ;; menus don't show the simple keys
  17798. (when (or org-use-extra-keys
  17799. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17800. (not window-system))
  17801. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17802. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17803. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17804. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17805. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17806. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17807. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17808. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17809. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17810. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17811. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17812. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17813. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17814. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17815. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17816. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17817. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17818. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17819. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17820. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17821. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17822. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17823. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17824. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17825. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17826. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17827. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17828. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17829. ;; All the other keys
  17830. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17831. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17832. (if (boundp 'narrow-map)
  17833. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17834. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17835. (if (boundp 'narrow-map)
  17836. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17837. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17838. (if (boundp 'narrow-map)
  17839. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17840. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17841. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17842. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17843. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17844. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17845. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17846. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17847. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17848. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17849. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17850. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17851. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17852. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17853. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17854. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17855. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17856. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17857. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17858. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17859. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17860. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17861. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17862. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17863. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17864. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17865. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17866. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17867. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17868. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17869. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17870. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17871. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17872. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17873. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17874. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17875. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17876. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17877. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17878. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17879. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17880. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17881. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17882. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17883. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17884. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17885. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17886. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17887. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17888. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17889. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17890. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17891. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17892. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17893. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17894. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17895. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17896. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17897. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17898. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17899. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17900. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17901. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17902. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17903. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17904. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17905. (org-defkey org-mode-map "\C-m" 'org-return)
  17906. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17907. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17908. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17909. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17910. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17911. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17912. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17913. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17914. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  17915. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17916. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17917. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17918. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17919. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17920. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17921. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17922. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17923. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17924. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17925. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17926. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17927. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17928. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17929. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17930. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17931. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17932. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17933. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17934. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17935. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17936. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17937. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17938. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17939. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17940. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17941. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17942. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17943. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17944. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  17945. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17946. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17947. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17948. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17949. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17950. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17951. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17952. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17953. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17954. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17955. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17956. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17957. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17958. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17959. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17960. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17961. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17962. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17963. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17964. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17965. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17966. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17967. (when (featurep 'xemacs)
  17968. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17969. (defconst org-speed-commands-default
  17970. '(
  17971. ("Outline Navigation")
  17972. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17973. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17974. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17975. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17976. ("F" . org-next-block)
  17977. ("B" . org-previous-block)
  17978. ("u" . (org-speed-move-safe 'outline-up-heading))
  17979. ("j" . org-goto)
  17980. ("g" . (org-refile t))
  17981. ("Outline Visibility")
  17982. ("c" . org-cycle)
  17983. ("C" . org-shifttab)
  17984. (" " . org-display-outline-path)
  17985. ("s" . org-narrow-to-subtree)
  17986. ("=" . org-columns)
  17987. ("Outline Structure Editing")
  17988. ("U" . org-metaup)
  17989. ("D" . org-metadown)
  17990. ("r" . org-metaright)
  17991. ("l" . org-metaleft)
  17992. ("R" . org-shiftmetaright)
  17993. ("L" . org-shiftmetaleft)
  17994. ("i" . (progn (forward-char 1) (call-interactively
  17995. 'org-insert-heading-respect-content)))
  17996. ("^" . org-sort)
  17997. ("w" . org-refile)
  17998. ("a" . org-archive-subtree-default-with-confirmation)
  17999. ("@" . org-mark-subtree)
  18000. ("#" . org-toggle-comment)
  18001. ("Clock Commands")
  18002. ("I" . org-clock-in)
  18003. ("O" . org-clock-out)
  18004. ("Meta Data Editing")
  18005. ("t" . org-todo)
  18006. ("," . (org-priority))
  18007. ("0" . (org-priority ?\ ))
  18008. ("1" . (org-priority ?A))
  18009. ("2" . (org-priority ?B))
  18010. ("3" . (org-priority ?C))
  18011. (":" . org-set-tags-command)
  18012. ("e" . org-set-effort)
  18013. ("E" . org-inc-effort)
  18014. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18015. (org-entry-put (point) "APPT_WARNTIME" m)))
  18016. ("Agenda Views etc")
  18017. ("v" . org-agenda)
  18018. ("/" . org-sparse-tree)
  18019. ("Misc")
  18020. ("o" . org-open-at-point)
  18021. ("?" . org-speed-command-help)
  18022. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18023. (">" . (org-agenda-remove-restriction-lock))
  18024. )
  18025. "The default speed commands.")
  18026. (defun org-print-speed-command (e)
  18027. (if (> (length (car e)) 1)
  18028. (progn
  18029. (princ "\n")
  18030. (princ (car e))
  18031. (princ "\n")
  18032. (princ (make-string (length (car e)) ?-))
  18033. (princ "\n"))
  18034. (princ (car e))
  18035. (princ " ")
  18036. (if (symbolp (cdr e))
  18037. (princ (symbol-name (cdr e)))
  18038. (prin1 (cdr e)))
  18039. (princ "\n")))
  18040. (defun org-speed-command-help ()
  18041. "Show the available speed commands."
  18042. (interactive)
  18043. (if (not org-use-speed-commands)
  18044. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18045. (with-output-to-temp-buffer "*Help*"
  18046. (princ "User-defined Speed commands\n===========================\n")
  18047. (mapc 'org-print-speed-command org-speed-commands-user)
  18048. (princ "\n")
  18049. (princ "Built-in Speed commands\n=======================\n")
  18050. (mapc 'org-print-speed-command org-speed-commands-default))
  18051. (with-current-buffer "*Help*"
  18052. (setq truncate-lines t))))
  18053. (defun org-speed-move-safe (cmd)
  18054. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18055. If not, return to the original position and throw an error."
  18056. (interactive)
  18057. (let ((pos (point)))
  18058. (call-interactively cmd)
  18059. (unless (and (bolp) (org-at-heading-p))
  18060. (goto-char pos)
  18061. (error "Boundary reached while executing %s" cmd))))
  18062. (defvar org-self-insert-command-undo-counter 0)
  18063. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18064. (defvar org-speed-command nil)
  18065. (define-obsolete-function-alias
  18066. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18067. (defun org-speed-command-activate (keys)
  18068. "Hook for activating single-letter speed commands.
  18069. `org-speed-commands-default' specifies a minimal command set.
  18070. Use `org-speed-commands-user' for further customization."
  18071. (when (or (and (bolp) (looking-at org-outline-regexp))
  18072. (and (functionp org-use-speed-commands)
  18073. (funcall org-use-speed-commands)))
  18074. (cdr (assoc keys (append org-speed-commands-user
  18075. org-speed-commands-default)))))
  18076. (define-obsolete-function-alias
  18077. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18078. (defun org-babel-speed-command-activate (keys)
  18079. "Hook for activating single-letter code block commands."
  18080. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18081. (cdr (assoc keys org-babel-key-bindings))))
  18082. (defcustom org-speed-command-hook
  18083. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18084. "Hook for activating speed commands at strategic locations.
  18085. Hook functions are called in sequence until a valid handler is
  18086. found.
  18087. Each hook takes a single argument, a user-pressed command key
  18088. which is also a `self-insert-command' from the global map.
  18089. Within the hook, examine the cursor position and the command key
  18090. and return nil or a valid handler as appropriate. Handler could
  18091. be one of an interactive command, a function, or a form.
  18092. Set `org-use-speed-commands' to non-nil value to enable this
  18093. hook. The default setting is `org-speed-command-activate'."
  18094. :group 'org-structure
  18095. :version "24.1"
  18096. :type 'hook)
  18097. (defun org-self-insert-command (N)
  18098. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18099. If the cursor is in a table looking at whitespace, the whitespace is
  18100. overwritten, and the table is not marked as requiring realignment."
  18101. (interactive "p")
  18102. (org-check-before-invisible-edit 'insert)
  18103. (cond
  18104. ((and org-use-speed-commands
  18105. (let ((kv (this-command-keys-vector)))
  18106. (setq org-speed-command
  18107. (run-hook-with-args-until-success
  18108. 'org-speed-command-hook
  18109. (make-string 1 (aref kv (1- (length kv))))))))
  18110. (cond
  18111. ((commandp org-speed-command)
  18112. (setq this-command org-speed-command)
  18113. (call-interactively org-speed-command))
  18114. ((functionp org-speed-command)
  18115. (funcall org-speed-command))
  18116. ((and org-speed-command (listp org-speed-command))
  18117. (eval org-speed-command))
  18118. (t (let (org-use-speed-commands)
  18119. (call-interactively 'org-self-insert-command)))))
  18120. ((and
  18121. (org-table-p)
  18122. (progn
  18123. ;; check if we blank the field, and if that triggers align
  18124. (and (featurep 'org-table) org-table-auto-blank-field
  18125. (member last-command
  18126. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas-expand))
  18127. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18128. ;; got extra space, this field does not determine column width
  18129. (let (org-table-may-need-update) (org-table-blank-field))
  18130. ;; no extra space, this field may determine column width
  18131. (org-table-blank-field)))
  18132. t)
  18133. (eq N 1)
  18134. (looking-at "[^|\n]* |"))
  18135. (let (org-table-may-need-update)
  18136. (goto-char (1- (match-end 0)))
  18137. (backward-delete-char 1)
  18138. (goto-char (match-beginning 0))
  18139. (self-insert-command N)))
  18140. (t
  18141. (setq org-table-may-need-update t)
  18142. (self-insert-command N)
  18143. (org-fix-tags-on-the-fly)
  18144. (if org-self-insert-cluster-for-undo
  18145. (if (not (eq last-command 'org-self-insert-command))
  18146. (setq org-self-insert-command-undo-counter 1)
  18147. (if (>= org-self-insert-command-undo-counter 20)
  18148. (setq org-self-insert-command-undo-counter 1)
  18149. (and (> org-self-insert-command-undo-counter 0)
  18150. buffer-undo-list (listp buffer-undo-list)
  18151. (not (cadr buffer-undo-list)) ; remove nil entry
  18152. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18153. (setq org-self-insert-command-undo-counter
  18154. (1+ org-self-insert-command-undo-counter))))))))
  18155. (defun org-check-before-invisible-edit (kind)
  18156. "Check is editing if kind KIND would be dangerous with invisible text around.
  18157. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18158. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18159. (if (and org-catch-invisible-edits
  18160. (or (not (boundp 'visible-mode)) (not visible-mode))
  18161. (or (get-char-property (point) 'invisible)
  18162. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18163. ;; OK, we need to take a closer look
  18164. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18165. (invisible-before-point (if (bobp) nil (get-char-property
  18166. (1- (point)) 'invisible)))
  18167. (border-and-ok-direction
  18168. (or
  18169. ;; Check if we are acting predictably before invisible text
  18170. (and invisible-at-point (not invisible-before-point)
  18171. (memq kind '(insert delete-backward)))
  18172. ;; Check if we are acting predictably after invisible text
  18173. ;; This works not well, and I have turned it off. It seems
  18174. ;; better to always show and stop after invisible text.
  18175. ;; (and (not invisible-at-point) invisible-before-point
  18176. ;; (memq kind '(insert delete)))
  18177. )))
  18178. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18179. (memq invisible-before-point '(outline org-hide-block t)))
  18180. (if (eq org-catch-invisible-edits 'error)
  18181. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18182. (if (and org-custom-properties-overlays
  18183. (y-or-n-p "Display invisible properties in this buffer? "))
  18184. (org-toggle-custom-properties-visibility)
  18185. ;; Make the area visible
  18186. (save-excursion
  18187. (if invisible-before-point
  18188. (goto-char (previous-single-char-property-change
  18189. (point) 'invisible)))
  18190. (show-subtree))
  18191. (cond
  18192. ((eq org-catch-invisible-edits 'show)
  18193. ;; That's it, we do the edit after showing
  18194. (message
  18195. "Unfolding invisible region around point before editing")
  18196. (sit-for 1))
  18197. ((and (eq org-catch-invisible-edits 'smart)
  18198. border-and-ok-direction)
  18199. (message "Unfolding invisible region around point before editing"))
  18200. (t
  18201. ;; Don't do the edit, make the user repeat it in full visibility
  18202. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18203. (defun org-fix-tags-on-the-fly ()
  18204. (when (and (equal (char-after (point-at-bol)) ?*)
  18205. (org-at-heading-p))
  18206. (org-align-tags-here org-tags-column)))
  18207. (defun org-delete-backward-char (N)
  18208. "Like `delete-backward-char', insert whitespace at field end in tables.
  18209. When deleting backwards, in tables this function will insert whitespace in
  18210. front of the next \"|\" separator, to keep the table aligned. The table will
  18211. still be marked for re-alignment if the field did fill the entire column,
  18212. because, in this case the deletion might narrow the column."
  18213. (interactive "p")
  18214. (save-match-data
  18215. (org-check-before-invisible-edit 'delete-backward)
  18216. (if (and (org-table-p)
  18217. (eq N 1)
  18218. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18219. (looking-at ".*?|"))
  18220. (let ((pos (point))
  18221. (noalign (looking-at "[^|\n\r]* |"))
  18222. (c org-table-may-need-update))
  18223. (backward-delete-char N)
  18224. (if (not overwrite-mode)
  18225. (progn
  18226. (skip-chars-forward "^|")
  18227. (insert " ")
  18228. (goto-char (1- pos))))
  18229. ;; noalign: if there were two spaces at the end, this field
  18230. ;; does not determine the width of the column.
  18231. (if noalign (setq org-table-may-need-update c)))
  18232. (backward-delete-char N)
  18233. (org-fix-tags-on-the-fly))))
  18234. (defun org-delete-char (N)
  18235. "Like `delete-char', but insert whitespace at field end in tables.
  18236. When deleting characters, in tables this function will insert whitespace in
  18237. front of the next \"|\" separator, to keep the table aligned. The table will
  18238. still be marked for re-alignment if the field did fill the entire column,
  18239. because, in this case the deletion might narrow the column."
  18240. (interactive "p")
  18241. (save-match-data
  18242. (org-check-before-invisible-edit 'delete)
  18243. (if (and (org-table-p)
  18244. (not (bolp))
  18245. (not (= (char-after) ?|))
  18246. (eq N 1))
  18247. (if (looking-at ".*?|")
  18248. (let ((pos (point))
  18249. (noalign (looking-at "[^|\n\r]* |"))
  18250. (c org-table-may-need-update))
  18251. (replace-match
  18252. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18253. (goto-char pos)
  18254. ;; noalign: if there were two spaces at the end, this field
  18255. ;; does not determine the width of the column.
  18256. (if noalign (setq org-table-may-need-update c)))
  18257. (delete-char N))
  18258. (delete-char N)
  18259. (org-fix-tags-on-the-fly))))
  18260. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18261. (put 'org-self-insert-command 'delete-selection
  18262. (lambda ()
  18263. (not (run-hook-with-args-until-success
  18264. 'self-insert-uses-region-functions))))
  18265. (put 'orgtbl-self-insert-command 'delete-selection
  18266. (lambda ()
  18267. (not (run-hook-with-args-until-success
  18268. 'self-insert-uses-region-functions))))
  18269. (put 'org-delete-char 'delete-selection 'supersede)
  18270. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18271. (put 'org-yank 'delete-selection 'yank)
  18272. ;; Make `flyspell-mode' delay after some commands
  18273. (put 'org-self-insert-command 'flyspell-delayed t)
  18274. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18275. (put 'org-delete-char 'flyspell-delayed t)
  18276. (put 'org-delete-backward-char 'flyspell-delayed t)
  18277. ;; Make pabbrev-mode expand after org-mode commands
  18278. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18279. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18280. (defun org-remap (map &rest commands)
  18281. "In MAP, remap the functions given in COMMANDS.
  18282. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18283. (let (new old)
  18284. (while commands
  18285. (setq old (pop commands) new (pop commands))
  18286. (if (fboundp 'command-remapping)
  18287. (org-defkey map (vector 'remap old) new)
  18288. (substitute-key-definition old new map global-map)))))
  18289. (defun org-transpose-words ()
  18290. "Transpose words for Org.
  18291. This uses the `org-mode-transpose-word-syntax-table' syntax
  18292. table, which interprets characters in `org-emphasis-alist' as
  18293. word constituents."
  18294. (interactive)
  18295. (with-syntax-table org-mode-transpose-word-syntax-table
  18296. (call-interactively 'transpose-words)))
  18297. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18298. (when (eq org-enable-table-editor 'optimized)
  18299. ;; If the user wants maximum table support, we need to hijack
  18300. ;; some standard editing functions
  18301. (org-remap org-mode-map
  18302. 'self-insert-command 'org-self-insert-command
  18303. 'delete-char 'org-delete-char
  18304. 'delete-backward-char 'org-delete-backward-char)
  18305. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18306. (defvar org-ctrl-c-ctrl-c-hook nil
  18307. "Hook for functions attaching themselves to `C-c C-c'.
  18308. This can be used to add additional functionality to the C-c C-c
  18309. key which executes context-dependent commands. This hook is run
  18310. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18311. run after the last test.
  18312. Each function will be called with no arguments. The function
  18313. must check if the context is appropriate for it to act. If yes,
  18314. it should do its thing and then return a non-nil value. If the
  18315. context is wrong, just do nothing and return nil.")
  18316. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18317. "Hook for functions attaching themselves to `C-c C-c'.
  18318. This can be used to add additional functionality to the C-c C-c
  18319. key which executes context-dependent commands. This hook is run
  18320. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18321. before the first test.
  18322. Each function will be called with no arguments. The function
  18323. must check if the context is appropriate for it to act. If yes,
  18324. it should do its thing and then return a non-nil value. If the
  18325. context is wrong, just do nothing and return nil.")
  18326. (defvar org-tab-first-hook nil
  18327. "Hook for functions to attach themselves to TAB.
  18328. See `org-ctrl-c-ctrl-c-hook' for more information.
  18329. This hook runs as the first action when TAB is pressed, even before
  18330. `org-cycle' messes around with the `outline-regexp' to cater for
  18331. inline tasks and plain list item folding.
  18332. If any function in this hook returns t, any other actions that
  18333. would have been caused by TAB (such as table field motion or visibility
  18334. cycling) will not occur.")
  18335. (defvar org-tab-after-check-for-table-hook nil
  18336. "Hook for functions to attach themselves to TAB.
  18337. See `org-ctrl-c-ctrl-c-hook' for more information.
  18338. This hook runs after it has been established that the cursor is not in a
  18339. table, but before checking if the cursor is in a headline or if global cycling
  18340. should be done.
  18341. If any function in this hook returns t, not other actions like visibility
  18342. cycling will be done.")
  18343. (defvar org-tab-after-check-for-cycling-hook nil
  18344. "Hook for functions to attach themselves to TAB.
  18345. See `org-ctrl-c-ctrl-c-hook' for more information.
  18346. This hook runs after it has been established that not table field motion and
  18347. not visibility should be done because of current context. This is probably
  18348. the place where a package like yasnippets can hook in.")
  18349. (defvar org-tab-before-tab-emulation-hook nil
  18350. "Hook for functions to attach themselves to TAB.
  18351. See `org-ctrl-c-ctrl-c-hook' for more information.
  18352. This hook runs after every other options for TAB have been exhausted, but
  18353. before indentation and \t insertion takes place.")
  18354. (defvar org-metaleft-hook nil
  18355. "Hook for functions attaching themselves to `M-left'.
  18356. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18357. (defvar org-metaright-hook nil
  18358. "Hook for functions attaching themselves to `M-right'.
  18359. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18360. (defvar org-metaup-hook nil
  18361. "Hook for functions attaching themselves to `M-up'.
  18362. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18363. (defvar org-metadown-hook nil
  18364. "Hook for functions attaching themselves to `M-down'.
  18365. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18366. (defvar org-shiftmetaleft-hook nil
  18367. "Hook for functions attaching themselves to `M-S-left'.
  18368. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18369. (defvar org-shiftmetaright-hook nil
  18370. "Hook for functions attaching themselves to `M-S-right'.
  18371. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18372. (defvar org-shiftmetaup-hook nil
  18373. "Hook for functions attaching themselves to `M-S-up'.
  18374. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18375. (defvar org-shiftmetadown-hook nil
  18376. "Hook for functions attaching themselves to `M-S-down'.
  18377. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18378. (defvar org-metareturn-hook nil
  18379. "Hook for functions attaching themselves to `M-RET'.
  18380. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18381. (defvar org-shiftup-hook nil
  18382. "Hook for functions attaching themselves to `S-up'.
  18383. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18384. (defvar org-shiftup-final-hook nil
  18385. "Hook for functions attaching themselves to `S-up'.
  18386. This one runs after all other options except shift-select have been excluded.
  18387. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18388. (defvar org-shiftdown-hook nil
  18389. "Hook for functions attaching themselves to `S-down'.
  18390. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18391. (defvar org-shiftdown-final-hook nil
  18392. "Hook for functions attaching themselves to `S-down'.
  18393. This one runs after all other options except shift-select have been excluded.
  18394. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18395. (defvar org-shiftleft-hook nil
  18396. "Hook for functions attaching themselves to `S-left'.
  18397. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18398. (defvar org-shiftleft-final-hook nil
  18399. "Hook for functions attaching themselves to `S-left'.
  18400. This one runs after all other options except shift-select have been excluded.
  18401. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18402. (defvar org-shiftright-hook nil
  18403. "Hook for functions attaching themselves to `S-right'.
  18404. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18405. (defvar org-shiftright-final-hook nil
  18406. "Hook for functions attaching themselves to `S-right'.
  18407. This one runs after all other options except shift-select have been excluded.
  18408. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18409. (defun org-modifier-cursor-error ()
  18410. "Throw an error, a modified cursor command was applied in wrong context."
  18411. (user-error "This command is active in special context like tables, headlines or items"))
  18412. (defun org-shiftselect-error ()
  18413. "Throw an error because Shift-Cursor command was applied in wrong context."
  18414. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18415. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18416. (user-error "This command works only in special context like headlines or timestamps")))
  18417. (defun org-call-for-shift-select (cmd)
  18418. (let ((this-command-keys-shift-translated t))
  18419. (call-interactively cmd)))
  18420. (defun org-shifttab (&optional arg)
  18421. "Global visibility cycling or move to previous table field.
  18422. Call `org-table-previous-field' within a table.
  18423. When ARG is nil, cycle globally through visibility states.
  18424. When ARG is a numeric prefix, show contents of this level."
  18425. (interactive "P")
  18426. (cond
  18427. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18428. ((integerp arg)
  18429. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18430. (message "Content view to level: %d" arg)
  18431. (org-content (prefix-numeric-value arg2))
  18432. (org-cycle-show-empty-lines t)
  18433. (setq org-cycle-global-status 'overview)))
  18434. (t (call-interactively 'org-global-cycle))))
  18435. (defun org-shiftmetaleft ()
  18436. "Promote subtree or delete table column.
  18437. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18438. `org-table-delete-column', depending on context. See the
  18439. individual commands for more information."
  18440. (interactive)
  18441. (cond
  18442. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18443. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18444. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18445. ((if (not (org-region-active-p)) (org-at-item-p)
  18446. (save-excursion (goto-char (region-beginning))
  18447. (org-at-item-p)))
  18448. (call-interactively 'org-outdent-item-tree))
  18449. (t (org-modifier-cursor-error))))
  18450. (defun org-shiftmetaright ()
  18451. "Demote subtree or insert table column.
  18452. Calls `org-demote-subtree', `org-indent-item-tree', or
  18453. `org-table-insert-column', depending on context. See the
  18454. individual commands for more information."
  18455. (interactive)
  18456. (cond
  18457. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18458. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18459. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18460. ((if (not (org-region-active-p)) (org-at-item-p)
  18461. (save-excursion (goto-char (region-beginning))
  18462. (org-at-item-p)))
  18463. (call-interactively 'org-indent-item-tree))
  18464. (t (org-modifier-cursor-error))))
  18465. (defun org-shiftmetaup (&optional arg)
  18466. "Drag the line at point up.
  18467. In a table, kill the current row.
  18468. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18469. but also adjust the previous clocked item in the clock history.
  18470. Everywhere else, drag the line at point up."
  18471. (interactive "P")
  18472. (cond
  18473. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18474. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18475. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18476. (call-interactively 'org-timestamp-up)))
  18477. (t (call-interactively 'org-drag-line-backward))))
  18478. (defun org-shiftmetadown (&optional arg)
  18479. "Drag the line at point down.
  18480. In a table, insert an empty row at the current line.
  18481. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18482. but also adjust the previous clocked item in the clock history.
  18483. Everywhere else, drag the line at point down."
  18484. (interactive "P")
  18485. (cond
  18486. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18487. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18488. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18489. (call-interactively 'org-timestamp-down)))
  18490. (t (call-interactively 'org-drag-line-forward))))
  18491. (defsubst org-hidden-tree-error ()
  18492. (user-error
  18493. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18494. (defun org-metaleft (&optional arg)
  18495. "Promote heading or move table column to left.
  18496. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18497. With no specific context, calls the Emacs default `backward-word'.
  18498. See the individual commands for more information."
  18499. (interactive "P")
  18500. (cond
  18501. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18502. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18503. ((org-with-limited-levels
  18504. (or (org-at-heading-p)
  18505. (and (org-region-active-p)
  18506. (save-excursion
  18507. (goto-char (region-beginning))
  18508. (org-at-heading-p)))))
  18509. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18510. (call-interactively 'org-do-promote))
  18511. ;; At an inline task.
  18512. ((org-at-heading-p)
  18513. (call-interactively 'org-inlinetask-promote))
  18514. ((or (org-at-item-p)
  18515. (and (org-region-active-p)
  18516. (save-excursion
  18517. (goto-char (region-beginning))
  18518. (org-at-item-p))))
  18519. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18520. (call-interactively 'org-outdent-item))
  18521. (t (call-interactively 'backward-word))))
  18522. (defun org-metaright (&optional arg)
  18523. "Demote a subtree, a list item or move table column to right.
  18524. In front of a drawer or a block keyword, indent it correctly.
  18525. With no specific context, calls the Emacs default `forward-word'.
  18526. See the individual commands for more information."
  18527. (interactive "P")
  18528. (cond
  18529. ((run-hook-with-args-until-success 'org-metaright-hook))
  18530. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18531. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18532. ((org-at-block-p) (call-interactively 'org-indent-block))
  18533. ((org-with-limited-levels
  18534. (or (org-at-heading-p)
  18535. (and (org-region-active-p)
  18536. (save-excursion
  18537. (goto-char (region-beginning))
  18538. (org-at-heading-p)))))
  18539. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18540. (call-interactively 'org-do-demote))
  18541. ;; At an inline task.
  18542. ((org-at-heading-p)
  18543. (call-interactively 'org-inlinetask-demote))
  18544. ((or (org-at-item-p)
  18545. (and (org-region-active-p)
  18546. (save-excursion
  18547. (goto-char (region-beginning))
  18548. (org-at-item-p))))
  18549. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18550. (call-interactively 'org-indent-item))
  18551. (t (call-interactively 'forward-word))))
  18552. (defun org-check-for-hidden (what)
  18553. "Check if there are hidden headlines/items in the current visual line.
  18554. WHAT can be either `headlines' or `items'. If the current line is
  18555. an outline or item heading and it has a folded subtree below it,
  18556. this function returns t, nil otherwise."
  18557. (let ((re (cond
  18558. ((eq what 'headlines) org-outline-regexp-bol)
  18559. ((eq what 'items) (org-item-beginning-re))
  18560. (t (error "This should not happen"))))
  18561. beg end)
  18562. (save-excursion
  18563. (catch 'exit
  18564. (unless (org-region-active-p)
  18565. (setq beg (point-at-bol))
  18566. (beginning-of-line 2)
  18567. (while (and (not (eobp)) ;; this is like `next-line'
  18568. (get-char-property (1- (point)) 'invisible))
  18569. (beginning-of-line 2))
  18570. (setq end (point))
  18571. (goto-char beg)
  18572. (goto-char (point-at-eol))
  18573. (setq end (max end (point)))
  18574. (while (re-search-forward re end t)
  18575. (if (get-char-property (match-beginning 0) 'invisible)
  18576. (throw 'exit t))))
  18577. nil))))
  18578. (defun org-metaup (&optional arg)
  18579. "Move subtree up or move table row up.
  18580. Calls `org-move-subtree-up' or `org-table-move-row' or
  18581. `org-move-item-up', depending on context. See the individual commands
  18582. for more information."
  18583. (interactive "P")
  18584. (cond
  18585. ((run-hook-with-args-until-success 'org-metaup-hook))
  18586. ((org-region-active-p)
  18587. (let* ((a (min (region-beginning) (region-end)))
  18588. (b (1- (max (region-beginning) (region-end))))
  18589. (c (save-excursion (goto-char a)
  18590. (move-beginning-of-line 0)))
  18591. (d (save-excursion (goto-char a)
  18592. (move-end-of-line 0) (point))))
  18593. (transpose-regions a b c d)
  18594. (goto-char c)))
  18595. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18596. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18597. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18598. (t (org-drag-element-backward))))
  18599. (defun org-metadown (&optional arg)
  18600. "Move subtree down or move table row down.
  18601. Calls `org-move-subtree-down' or `org-table-move-row' or
  18602. `org-move-item-down', depending on context. See the individual
  18603. commands for more information."
  18604. (interactive "P")
  18605. (cond
  18606. ((run-hook-with-args-until-success 'org-metadown-hook))
  18607. ((org-region-active-p)
  18608. (let* ((a (min (region-beginning) (region-end)))
  18609. (b (max (region-beginning) (region-end)))
  18610. (c (save-excursion (goto-char b)
  18611. (move-beginning-of-line 1)))
  18612. (d (save-excursion (goto-char b)
  18613. (move-end-of-line 1) (1+ (point)))))
  18614. (transpose-regions a b c d)
  18615. (goto-char d)))
  18616. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18617. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18618. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18619. (t (org-drag-element-forward))))
  18620. (defun org-shiftup (&optional arg)
  18621. "Increase item in timestamp or increase priority of current headline.
  18622. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18623. depending on context. See the individual commands for more information."
  18624. (interactive "P")
  18625. (cond
  18626. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18627. ((and org-support-shift-select (org-region-active-p))
  18628. (org-call-for-shift-select 'previous-line))
  18629. ((org-at-timestamp-p t)
  18630. (call-interactively (if org-edit-timestamp-down-means-later
  18631. 'org-timestamp-down 'org-timestamp-up)))
  18632. ((and (not (eq org-support-shift-select 'always))
  18633. org-enable-priority-commands
  18634. (org-at-heading-p))
  18635. (call-interactively 'org-priority-up))
  18636. ((and (not org-support-shift-select) (org-at-item-p))
  18637. (call-interactively 'org-previous-item))
  18638. ((org-clocktable-try-shift 'up arg))
  18639. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18640. (org-support-shift-select
  18641. (org-call-for-shift-select 'previous-line))
  18642. (t (org-shiftselect-error))))
  18643. (defun org-shiftdown (&optional arg)
  18644. "Decrease item in timestamp or decrease priority of current headline.
  18645. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18646. depending on context. See the individual commands for more information."
  18647. (interactive "P")
  18648. (cond
  18649. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18650. ((and org-support-shift-select (org-region-active-p))
  18651. (org-call-for-shift-select 'next-line))
  18652. ((org-at-timestamp-p t)
  18653. (call-interactively (if org-edit-timestamp-down-means-later
  18654. 'org-timestamp-up 'org-timestamp-down)))
  18655. ((and (not (eq org-support-shift-select 'always))
  18656. org-enable-priority-commands
  18657. (org-at-heading-p))
  18658. (call-interactively 'org-priority-down))
  18659. ((and (not org-support-shift-select) (org-at-item-p))
  18660. (call-interactively 'org-next-item))
  18661. ((org-clocktable-try-shift 'down arg))
  18662. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18663. (org-support-shift-select
  18664. (org-call-for-shift-select 'next-line))
  18665. (t (org-shiftselect-error))))
  18666. (defun org-shiftright (&optional arg)
  18667. "Cycle the thing at point or in the current line, depending on context.
  18668. Depending on context, this does one of the following:
  18669. - switch a timestamp at point one day into the future
  18670. - on a headline, switch to the next TODO keyword.
  18671. - on an item, switch entire list to the next bullet type
  18672. - on a property line, switch to the next allowed value
  18673. - on a clocktable definition line, move time block into the future"
  18674. (interactive "P")
  18675. (cond
  18676. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18677. ((and org-support-shift-select (org-region-active-p))
  18678. (org-call-for-shift-select 'forward-char))
  18679. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18680. ((and (not (eq org-support-shift-select 'always))
  18681. (org-at-heading-p))
  18682. (let ((org-inhibit-logging
  18683. (not org-treat-S-cursor-todo-selection-as-state-change))
  18684. (org-inhibit-blocking
  18685. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18686. (org-call-with-arg 'org-todo 'right)))
  18687. ((or (and org-support-shift-select
  18688. (not (eq org-support-shift-select 'always))
  18689. (org-at-item-bullet-p))
  18690. (and (not org-support-shift-select) (org-at-item-p)))
  18691. (org-call-with-arg 'org-cycle-list-bullet nil))
  18692. ((and (not (eq org-support-shift-select 'always))
  18693. (org-at-property-p))
  18694. (call-interactively 'org-property-next-allowed-value))
  18695. ((org-clocktable-try-shift 'right arg))
  18696. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18697. (org-support-shift-select
  18698. (org-call-for-shift-select 'forward-char))
  18699. (t (org-shiftselect-error))))
  18700. (defun org-shiftleft (&optional arg)
  18701. "Cycle the thing at point or in the current line, depending on context.
  18702. Depending on context, this does one of the following:
  18703. - switch a timestamp at point one day into the past
  18704. - on a headline, switch to the previous TODO keyword.
  18705. - on an item, switch entire list to the previous bullet type
  18706. - on a property line, switch to the previous allowed value
  18707. - on a clocktable definition line, move time block into the past"
  18708. (interactive "P")
  18709. (cond
  18710. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18711. ((and org-support-shift-select (org-region-active-p))
  18712. (org-call-for-shift-select 'backward-char))
  18713. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18714. ((and (not (eq org-support-shift-select 'always))
  18715. (org-at-heading-p))
  18716. (let ((org-inhibit-logging
  18717. (not org-treat-S-cursor-todo-selection-as-state-change))
  18718. (org-inhibit-blocking
  18719. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18720. (org-call-with-arg 'org-todo 'left)))
  18721. ((or (and org-support-shift-select
  18722. (not (eq org-support-shift-select 'always))
  18723. (org-at-item-bullet-p))
  18724. (and (not org-support-shift-select) (org-at-item-p)))
  18725. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18726. ((and (not (eq org-support-shift-select 'always))
  18727. (org-at-property-p))
  18728. (call-interactively 'org-property-previous-allowed-value))
  18729. ((org-clocktable-try-shift 'left arg))
  18730. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18731. (org-support-shift-select
  18732. (org-call-for-shift-select 'backward-char))
  18733. (t (org-shiftselect-error))))
  18734. (defun org-shiftcontrolright ()
  18735. "Switch to next TODO set."
  18736. (interactive)
  18737. (cond
  18738. ((and org-support-shift-select (org-region-active-p))
  18739. (org-call-for-shift-select 'forward-word))
  18740. ((and (not (eq org-support-shift-select 'always))
  18741. (org-at-heading-p))
  18742. (org-call-with-arg 'org-todo 'nextset))
  18743. (org-support-shift-select
  18744. (org-call-for-shift-select 'forward-word))
  18745. (t (org-shiftselect-error))))
  18746. (defun org-shiftcontrolleft ()
  18747. "Switch to previous TODO set."
  18748. (interactive)
  18749. (cond
  18750. ((and org-support-shift-select (org-region-active-p))
  18751. (org-call-for-shift-select 'backward-word))
  18752. ((and (not (eq org-support-shift-select 'always))
  18753. (org-at-heading-p))
  18754. (org-call-with-arg 'org-todo 'previousset))
  18755. (org-support-shift-select
  18756. (org-call-for-shift-select 'backward-word))
  18757. (t (org-shiftselect-error))))
  18758. (defun org-shiftcontrolup (&optional n)
  18759. "Change timestamps synchronously up in CLOCK log lines.
  18760. Optional argument N tells to change by that many units."
  18761. (interactive "P")
  18762. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18763. (let (org-support-shift-select)
  18764. (org-clock-timestamps-up n))
  18765. (user-error "Not at a clock log")))
  18766. (defun org-shiftcontroldown (&optional n)
  18767. "Change timestamps synchronously down in CLOCK log lines.
  18768. Optional argument N tells to change by that many units."
  18769. (interactive "P")
  18770. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18771. (let (org-support-shift-select)
  18772. (org-clock-timestamps-down n))
  18773. (user-error "Not at a clock log")))
  18774. (defun org-increase-number-at-point (&optional inc)
  18775. "Increment the number at point.
  18776. With an optional prefix numeric argument INC, increment using
  18777. this numeric value."
  18778. (interactive "p")
  18779. (if (not (number-at-point))
  18780. (user-error "Not on a number")
  18781. (unless inc (setq inc 1))
  18782. (let ((pos (point))
  18783. (beg (skip-chars-backward "-+^/*0-9eE."))
  18784. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18785. (setq nap (buffer-substring-no-properties
  18786. (+ pos beg) (+ pos beg end)))
  18787. (delete-region (+ pos beg) (+ pos beg end))
  18788. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18789. (when (org-at-table-p)
  18790. (org-table-align)
  18791. (org-table-end-of-field 1))))
  18792. (defun org-decrease-number-at-point (&optional inc)
  18793. "Decrement the number at point.
  18794. With an optional prefix numeric argument INC, decrement using
  18795. this numeric value."
  18796. (interactive "p")
  18797. (org-increase-number-at-point (- inc)))
  18798. (defun org-ctrl-c-ret ()
  18799. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18800. (interactive)
  18801. (cond
  18802. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18803. (t (call-interactively 'org-insert-heading))))
  18804. (defun org-find-visible ()
  18805. (let ((s (point)))
  18806. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18807. (get-char-property s 'invisible)))
  18808. s))
  18809. (defun org-find-invisible ()
  18810. (let ((s (point)))
  18811. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18812. (not (get-char-property s 'invisible))))
  18813. s))
  18814. (defun org-copy-visible (beg end)
  18815. "Copy the visible parts of the region."
  18816. (interactive "r")
  18817. (let (snippets s)
  18818. (save-excursion
  18819. (save-restriction
  18820. (narrow-to-region beg end)
  18821. (setq s (goto-char (point-min)))
  18822. (while (not (= (point) (point-max)))
  18823. (goto-char (org-find-invisible))
  18824. (push (buffer-substring s (point)) snippets)
  18825. (setq s (goto-char (org-find-visible))))))
  18826. (kill-new (apply 'concat (nreverse snippets)))))
  18827. (defun org-copy-special ()
  18828. "Copy region in table or copy current subtree.
  18829. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18830. See the individual commands for more information."
  18831. (interactive)
  18832. (call-interactively
  18833. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18834. (defun org-cut-special ()
  18835. "Cut region in table or cut current subtree.
  18836. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18837. See the individual commands for more information."
  18838. (interactive)
  18839. (call-interactively
  18840. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18841. (defun org-paste-special (arg)
  18842. "Paste rectangular region into table, or past subtree relative to level.
  18843. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18844. See the individual commands for more information."
  18845. (interactive "P")
  18846. (if (org-at-table-p)
  18847. (org-table-paste-rectangle)
  18848. (org-paste-subtree arg)))
  18849. (defsubst org-in-fixed-width-region-p ()
  18850. "Is point in a fixed-width region?"
  18851. (save-match-data
  18852. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18853. (defun org-edit-special (&optional arg)
  18854. "Call a special editor for the element at point.
  18855. When at a table, call the formula editor with `org-table-edit-formulas'.
  18856. When in a source code block, call `org-edit-src-code'.
  18857. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18858. When at an #+INCLUDE keyword, visit the included file.
  18859. On a link, call `ffap' to visit the link at point.
  18860. Otherwise, return a user error."
  18861. (interactive "P")
  18862. (let ((element (org-element-at-point)))
  18863. (assert (not buffer-read-only) nil
  18864. "Buffer is read-only: %s" (buffer-name))
  18865. (case (org-element-type element)
  18866. (src-block
  18867. (if (not arg) (org-edit-src-code)
  18868. (let* ((info (org-babel-get-src-block-info))
  18869. (lang (nth 0 info))
  18870. (params (nth 2 info))
  18871. (session (cdr (assq :session params))))
  18872. (if (not session) (org-edit-src-code)
  18873. ;; At a src-block with a session and function called with
  18874. ;; an ARG: switch to the buffer related to the inferior
  18875. ;; process.
  18876. (switch-to-buffer
  18877. (funcall (intern (concat "org-babel-prep-session:" lang))
  18878. session params))))))
  18879. (keyword
  18880. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18881. (org-open-link-from-string
  18882. (format "[[%s]]"
  18883. (expand-file-name
  18884. (let ((value (org-element-property :value element)))
  18885. (cond ((not (org-string-nw-p value))
  18886. (user-error "No file to edit"))
  18887. ((string-match "\\`\"\\(.*?\\)\"" value)
  18888. (match-string 1 value))
  18889. ((string-match "\\`[^ \t\"]\\S-*" value)
  18890. (match-string 0 value))
  18891. (t (user-error "No valid file specified")))))))
  18892. (user-error "No special environment to edit here")))
  18893. (table
  18894. (if (eq (org-element-property :type element) 'table.el)
  18895. (org-edit-src-code)
  18896. (call-interactively 'org-table-edit-formulas)))
  18897. ;; Only Org tables contain `table-row' type elements.
  18898. (table-row (call-interactively 'org-table-edit-formulas))
  18899. ((example-block export-block) (org-edit-src-code))
  18900. (fixed-width (org-edit-fixed-width-region))
  18901. (otherwise
  18902. ;; No notable element at point. Though, we may be at a link,
  18903. ;; which is an object. Thus, scan deeper.
  18904. (if (eq (org-element-type (org-element-context element)) 'link)
  18905. (call-interactively 'ffap)
  18906. (user-error "No special environment to edit here"))))))
  18907. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18908. (defun org-ctrl-c-ctrl-c (&optional arg)
  18909. "Set tags in headline, or update according to changed information at point.
  18910. This command does many different things, depending on context:
  18911. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18912. this is what we do.
  18913. - If the cursor is on a statistics cookie, update it.
  18914. - If the cursor is in a headline, prompt for tags and insert them
  18915. into the current line, aligned to `org-tags-column'. When called
  18916. with prefix arg, realign all tags in the current buffer.
  18917. - If the cursor is in one of the special #+KEYWORD lines, this
  18918. triggers scanning the buffer for these lines and updating the
  18919. information.
  18920. - If the cursor is inside a table, realign the table. This command
  18921. works even if the automatic table editor has been turned off.
  18922. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18923. the entire table.
  18924. - If the cursor is at a footnote reference or definition, jump to
  18925. the corresponding definition or references, respectively.
  18926. - If the cursor is a the beginning of a dynamic block, update it.
  18927. - If the current buffer is a capture buffer, close note and file it.
  18928. - If the cursor is on a <<<target>>>, update radio targets and
  18929. corresponding links in this buffer.
  18930. - If the cursor is on a numbered item in a plain list, renumber the
  18931. ordered list.
  18932. - If the cursor is on a checkbox, toggle it.
  18933. - If the cursor is on a code block, evaluate it. The variable
  18934. `org-confirm-babel-evaluate' can be used to control prompting
  18935. before code block evaluation, by default every code block
  18936. evaluation requires confirmation. Code block evaluation can be
  18937. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18938. (interactive "P")
  18939. (cond
  18940. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18941. org-occur-highlights)
  18942. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18943. (org-remove-occur-highlights)
  18944. (message "Temporary highlights/overlays removed from current buffer"))
  18945. ((and (local-variable-p 'org-finish-function (current-buffer))
  18946. (fboundp org-finish-function))
  18947. (funcall org-finish-function))
  18948. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18949. (t
  18950. (let* ((context (org-element-context)) (type (org-element-type context)))
  18951. ;; Test if point is within a blank line.
  18952. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18953. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18954. (user-error "C-c C-c can do nothing useful at this location"))
  18955. (case type
  18956. ;; When at a link, act according to the parent instead.
  18957. (link (setq context (org-element-property :parent context))
  18958. (setq type (org-element-type context)))
  18959. ;; Unsupported object types: refer to the first supported
  18960. ;; element or object containing it.
  18961. ((bold code entity export-snippet inline-babel-call inline-src-block
  18962. italic latex-fragment line-break macro strike-through subscript
  18963. superscript underline verbatim)
  18964. (while (and (setq context (org-element-property :parent context))
  18965. (not (memq (setq type (org-element-type context))
  18966. '(radio-target paragraph verse-block
  18967. table-cell)))))))
  18968. ;; For convenience: at the first line of a paragraph on the
  18969. ;; same line as an item, apply function on that item instead.
  18970. (when (eq type 'paragraph)
  18971. (let ((parent (org-element-property :parent context)))
  18972. (when (and (eq (org-element-type parent) 'item)
  18973. (= (point-at-bol) (org-element-property :begin parent)))
  18974. (setq context parent type 'item))))
  18975. ;; Act according to type of element or object at point.
  18976. (case type
  18977. (clock (org-clock-update-time-maybe))
  18978. (dynamic-block
  18979. (save-excursion
  18980. (goto-char (org-element-property :post-affiliated context))
  18981. (org-update-dblock)))
  18982. (footnote-definition
  18983. (goto-char (org-element-property :post-affiliated context))
  18984. (call-interactively 'org-footnote-action))
  18985. (footnote-reference (call-interactively 'org-footnote-action))
  18986. ((headline inlinetask)
  18987. (save-excursion (goto-char (org-element-property :begin context))
  18988. (call-interactively 'org-set-tags)))
  18989. (item
  18990. ;; At an item: a double C-u set checkbox to "[-]"
  18991. ;; unconditionally, whereas a single one will toggle its
  18992. ;; presence. Without an universal argument, if the item
  18993. ;; has a checkbox, toggle it. Otherwise repair the list.
  18994. (let* ((box (org-element-property :checkbox context))
  18995. (struct (org-element-property :structure context))
  18996. (old-struct (copy-tree struct))
  18997. (parents (org-list-parents-alist struct))
  18998. (prevs (org-list-prevs-alist struct))
  18999. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19000. (org-list-set-checkbox
  19001. (org-element-property :begin context) struct
  19002. (cond ((equal arg '(16)) "[-]")
  19003. ((and (not box) (equal arg '(4))) "[ ]")
  19004. ((or (not box) (equal arg '(4))) nil)
  19005. ((eq box 'on) "[ ]")
  19006. (t "[X]")))
  19007. ;; Mimic `org-list-write-struct' but with grabbing
  19008. ;; a return value from `org-list-struct-fix-box'.
  19009. (org-list-struct-fix-ind struct parents 2)
  19010. (org-list-struct-fix-item-end struct)
  19011. (org-list-struct-fix-bul struct prevs)
  19012. (org-list-struct-fix-ind struct parents)
  19013. (let ((block-item
  19014. (org-list-struct-fix-box struct parents prevs orderedp)))
  19015. (if (and box (equal struct old-struct))
  19016. (if (equal arg '(16))
  19017. (message "Checkboxes already reset")
  19018. (user-error "Cannot toggle this checkbox: %s"
  19019. (if (eq box 'on)
  19020. "all subitems checked"
  19021. "unchecked subitems")))
  19022. (org-list-struct-apply-struct struct old-struct)
  19023. (org-update-checkbox-count-maybe))
  19024. (when block-item
  19025. (message "Checkboxes were removed due to empty box at line %d"
  19026. (org-current-line block-item))))))
  19027. (keyword
  19028. (let ((org-inhibit-startup-visibility-stuff t)
  19029. (org-startup-align-all-tables nil))
  19030. (when (boundp 'org-table-coordinate-overlays)
  19031. (mapc 'delete-overlay org-table-coordinate-overlays)
  19032. (setq org-table-coordinate-overlays nil))
  19033. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19034. (message "Local setup has been refreshed"))
  19035. (plain-list
  19036. ;; At a plain list, with a double C-u argument, set
  19037. ;; checkboxes of each item to "[-]", whereas a single one
  19038. ;; will toggle their presence according to the state of the
  19039. ;; first item in the list. Without an argument, repair the
  19040. ;; list.
  19041. (let* ((begin (org-element-property :contents-begin context))
  19042. (beginm (move-marker (make-marker) begin))
  19043. (struct (org-element-property :structure context))
  19044. (old-struct (copy-tree struct))
  19045. (first-box (save-excursion
  19046. (goto-char begin)
  19047. (looking-at org-list-full-item-re)
  19048. (match-string-no-properties 3)))
  19049. (new-box (cond ((equal arg '(16)) "[-]")
  19050. ((equal arg '(4)) (unless first-box "[ ]"))
  19051. ((equal first-box "[X]") "[ ]")
  19052. (t "[X]"))))
  19053. (cond
  19054. (arg
  19055. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19056. (org-list-get-all-items
  19057. begin struct (org-list-prevs-alist struct))))
  19058. ((and first-box (eq (point) begin))
  19059. ;; For convenience, when point is at bol on the first
  19060. ;; item of the list and no argument is provided, simply
  19061. ;; toggle checkbox of that item, if any.
  19062. (org-list-set-checkbox begin struct new-box)))
  19063. (org-list-write-struct
  19064. struct (org-list-parents-alist struct) old-struct)
  19065. (org-update-checkbox-count-maybe)
  19066. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19067. ((property-drawer node-property)
  19068. (call-interactively 'org-property-action))
  19069. ((radio-target target)
  19070. (call-interactively 'org-update-radio-target-regexp))
  19071. (statistics-cookie
  19072. (call-interactively 'org-update-statistics-cookies))
  19073. ((table table-cell table-row)
  19074. ;; At a table, recalculate every field and align it. Also
  19075. ;; send the table if necessary. If the table has
  19076. ;; a `table.el' type, just give up. At a table row or
  19077. ;; cell, maybe recalculate line but always align table.
  19078. (if (eq (org-element-property :type context) 'table.el)
  19079. (message "Use C-c ' to edit table.el tables")
  19080. (let ((org-enable-table-editor t))
  19081. (if (or (eq type 'table)
  19082. ;; Check if point is at a TBLFM line.
  19083. (and (eq type 'table-row)
  19084. (= (point) (org-element-property :end context))))
  19085. (save-excursion
  19086. (if (org-at-TBLFM-p)
  19087. (progn (require 'org-table)
  19088. (org-table-calc-current-TBLFM))
  19089. (goto-char (org-element-property :contents-begin context))
  19090. (org-call-with-arg 'org-table-recalculate (or arg t))
  19091. (orgtbl-send-table 'maybe)))
  19092. (org-table-maybe-eval-formula)
  19093. (cond (arg (call-interactively 'org-table-recalculate))
  19094. ((org-table-maybe-recalculate-line))
  19095. (t (org-table-align)))))))
  19096. (timestamp (org-timestamp-change 0 'day))
  19097. (otherwise
  19098. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19099. (user-error
  19100. "C-c C-c can do nothing useful at this location")))))))))
  19101. (defun org-mode-restart ()
  19102. (interactive)
  19103. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19104. (funcall major-mode)
  19105. (hack-local-variables)
  19106. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19107. (org-indent-mode -1)))
  19108. (message "%s restarted" major-mode))
  19109. (defun org-kill-note-or-show-branches ()
  19110. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19111. (interactive)
  19112. (if (not org-finish-function)
  19113. (progn
  19114. (hide-subtree)
  19115. (call-interactively 'show-branches))
  19116. (let ((org-note-abort t))
  19117. (funcall org-finish-function))))
  19118. (defun org-open-line (n)
  19119. "Insert a new row in tables, call `open-line' elsewhere.
  19120. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19121. (interactive "*p")
  19122. (cond
  19123. ((not org-special-ctrl-o)
  19124. (open-line n))
  19125. ((org-at-table-p)
  19126. (org-table-insert-row))
  19127. (t
  19128. (open-line n))))
  19129. (defun org-return (&optional indent)
  19130. "Goto next table row or insert a newline.
  19131. Calls `org-table-next-row' or `newline', depending on context.
  19132. See the individual commands for more information."
  19133. (interactive)
  19134. (let (org-ts-what)
  19135. (cond
  19136. ((or (bobp) (org-in-src-block-p))
  19137. (if indent (newline-and-indent) (newline)))
  19138. ((org-at-table-p)
  19139. (org-table-justify-field-maybe)
  19140. (call-interactively 'org-table-next-row))
  19141. ;; when `newline-and-indent' is called within a list, make sure
  19142. ;; text moved stays inside the item.
  19143. ((and (org-in-item-p) indent)
  19144. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  19145. (progn
  19146. (save-match-data (newline))
  19147. (org-indent-line-to (length (match-string 0))))
  19148. (let ((ind (org-get-indentation)))
  19149. (newline)
  19150. (if (org-looking-back org-list-end-re)
  19151. (org-indent-line)
  19152. (org-indent-line-to ind)))))
  19153. ((and org-return-follows-link
  19154. (org-at-timestamp-p t)
  19155. (not (eq org-ts-what 'after)))
  19156. (org-follow-timestamp-link))
  19157. ((and org-return-follows-link
  19158. (let ((tprop (get-text-property (point) 'face)))
  19159. (or (eq tprop 'org-link)
  19160. (and (listp tprop) (memq 'org-link tprop)))))
  19161. (call-interactively 'org-open-at-point))
  19162. ((and (org-at-heading-p)
  19163. (looking-at
  19164. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  19165. (org-show-entry)
  19166. (end-of-line 1)
  19167. (newline))
  19168. (t (if indent (newline-and-indent) (newline))))))
  19169. (defun org-return-indent ()
  19170. "Goto next table row or insert a newline and indent.
  19171. Calls `org-table-next-row' or `newline-and-indent', depending on
  19172. context. See the individual commands for more information."
  19173. (interactive)
  19174. (org-return t))
  19175. (defun org-ctrl-c-star ()
  19176. "Compute table, or change heading status of lines.
  19177. Calls `org-table-recalculate' or `org-toggle-heading',
  19178. depending on context."
  19179. (interactive)
  19180. (cond
  19181. ((org-at-table-p)
  19182. (call-interactively 'org-table-recalculate))
  19183. (t
  19184. ;; Convert all lines in region to list items
  19185. (call-interactively 'org-toggle-heading))))
  19186. (defun org-ctrl-c-minus ()
  19187. "Insert separator line in table or modify bullet status of line.
  19188. Also turns a plain line or a region of lines into list items.
  19189. Calls `org-table-insert-hline', `org-toggle-item', or
  19190. `org-cycle-list-bullet', depending on context."
  19191. (interactive)
  19192. (cond
  19193. ((org-at-table-p)
  19194. (call-interactively 'org-table-insert-hline))
  19195. ((org-region-active-p)
  19196. (call-interactively 'org-toggle-item))
  19197. ((org-in-item-p)
  19198. (call-interactively 'org-cycle-list-bullet))
  19199. (t
  19200. (call-interactively 'org-toggle-item))))
  19201. (defun org-toggle-item (arg)
  19202. "Convert headings or normal lines to items, items to normal lines.
  19203. If there is no active region, only the current line is considered.
  19204. If the first non blank line in the region is a headline, convert
  19205. all headlines to items, shifting text accordingly.
  19206. If it is an item, convert all items to normal lines.
  19207. If it is normal text, change region into a list of items.
  19208. With a prefix argument ARG, change the region in a single item."
  19209. (interactive "P")
  19210. (let ((shift-text
  19211. (function
  19212. ;; Shift text in current section to IND, from point to END.
  19213. ;; The function leaves point to END line.
  19214. (lambda (ind end)
  19215. (let ((min-i 1000) (end (copy-marker end)))
  19216. ;; First determine the minimum indentation (MIN-I) of
  19217. ;; the text.
  19218. (save-excursion
  19219. (catch 'exit
  19220. (while (< (point) end)
  19221. (let ((i (org-get-indentation)))
  19222. (cond
  19223. ;; Skip blank lines and inline tasks.
  19224. ((looking-at "^[ \t]*$"))
  19225. ((looking-at org-outline-regexp-bol))
  19226. ;; We can't find less than 0 indentation.
  19227. ((zerop i) (throw 'exit (setq min-i 0)))
  19228. ((< i min-i) (setq min-i i))))
  19229. (forward-line))))
  19230. ;; Then indent each line so that a line indented to
  19231. ;; MIN-I becomes indented to IND. Ignore blank lines
  19232. ;; and inline tasks in the process.
  19233. (let ((delta (- ind min-i)))
  19234. (while (< (point) end)
  19235. (unless (or (looking-at "^[ \t]*$")
  19236. (looking-at org-outline-regexp-bol))
  19237. (org-indent-line-to (+ (org-get-indentation) delta)))
  19238. (forward-line)))))))
  19239. (skip-blanks
  19240. (function
  19241. ;; Return beginning of first non-blank line, starting from
  19242. ;; line at POS.
  19243. (lambda (pos)
  19244. (save-excursion
  19245. (goto-char pos)
  19246. (skip-chars-forward " \r\t\n")
  19247. (point-at-bol)))))
  19248. beg end)
  19249. ;; Determine boundaries of changes.
  19250. (if (org-region-active-p)
  19251. (setq beg (funcall skip-blanks (region-beginning))
  19252. end (copy-marker (region-end)))
  19253. (setq beg (funcall skip-blanks (point-at-bol))
  19254. end (copy-marker (point-at-eol))))
  19255. ;; Depending on the starting line, choose an action on the text
  19256. ;; between BEG and END.
  19257. (org-with-limited-levels
  19258. (save-excursion
  19259. (goto-char beg)
  19260. (cond
  19261. ;; Case 1. Start at an item: de-itemize. Note that it only
  19262. ;; happens when a region is active: `org-ctrl-c-minus'
  19263. ;; would call `org-cycle-list-bullet' otherwise.
  19264. ((org-at-item-p)
  19265. (while (< (point) end)
  19266. (when (org-at-item-p)
  19267. (skip-chars-forward " \t")
  19268. (delete-region (point) (match-end 0)))
  19269. (forward-line)))
  19270. ;; Case 2. Start at an heading: convert to items.
  19271. ((org-at-heading-p)
  19272. (let* ((bul (org-list-bullet-string "-"))
  19273. (bul-len (length bul))
  19274. (done (org-entry-is-done-p))
  19275. (todo (org-entry-is-todo-p))
  19276. ;; Indentation of the first heading. It should be
  19277. ;; relative to the indentation of its parent, if any.
  19278. (start-ind (save-excursion
  19279. (cond
  19280. ((not org-adapt-indentation) 0)
  19281. ((not (outline-previous-heading)) 0)
  19282. (t (length (match-string 0))))))
  19283. ;; Level of first heading. Further headings will be
  19284. ;; compared to it to determine hierarchy in the list.
  19285. (ref-level (org-reduced-level (org-outline-level))))
  19286. (when (or done todo) (org-todo ""))
  19287. (while (< (point) end)
  19288. (let* ((level (org-reduced-level (org-outline-level)))
  19289. (delta (max 0 (- level ref-level))))
  19290. ;; If current headline is less indented than the first
  19291. ;; one, set it as reference, in order to preserve
  19292. ;; subtrees.
  19293. (when (< level ref-level) (setq ref-level level))
  19294. (replace-match bul t t)
  19295. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19296. (when (or done todo)
  19297. (let* ((struct (org-list-struct))
  19298. (old (copy-tree struct)))
  19299. (org-list-set-checkbox (line-beginning-position)
  19300. struct
  19301. (if done "[X]" "[ ]"))
  19302. (org-list-write-struct struct
  19303. (org-list-parents-alist struct)
  19304. old)))
  19305. ;; Ensure all text down to END (or SECTION-END) belongs
  19306. ;; to the newly created item.
  19307. (let ((section-end (save-excursion
  19308. (or (outline-next-heading) (point)))))
  19309. (forward-line)
  19310. (funcall shift-text
  19311. (+ start-ind (* (1+ delta) bul-len))
  19312. (min end section-end)))))))
  19313. ;; Case 3. Normal line with ARG: make the first line of region
  19314. ;; an item, and shift indentation of others lines to
  19315. ;; set them as item's body.
  19316. (arg (let* ((bul (org-list-bullet-string "-"))
  19317. (bul-len (length bul))
  19318. (ref-ind (org-get-indentation)))
  19319. (skip-chars-forward " \t")
  19320. (insert bul)
  19321. (forward-line)
  19322. (while (< (point) end)
  19323. ;; Ensure that lines less indented than first one
  19324. ;; still get included in item body.
  19325. (funcall shift-text
  19326. (+ ref-ind bul-len)
  19327. (min end (save-excursion (or (outline-next-heading)
  19328. (point)))))
  19329. (forward-line))))
  19330. ;; Case 4. Normal line without ARG: turn each non-item line
  19331. ;; into an item.
  19332. (t
  19333. (while (< (point) end)
  19334. (unless (or (org-at-heading-p) (org-at-item-p))
  19335. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19336. (replace-match
  19337. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19338. (forward-line))))))))
  19339. (defun org-toggle-heading (&optional nstars)
  19340. "Convert headings to normal text, or items or text to headings.
  19341. If there is no active region, only convert the current line.
  19342. With a \\[universal-argument] prefix, convert the whole list at
  19343. point into heading.
  19344. In a region:
  19345. - If the first non blank line is a headline, remove the stars
  19346. from all headlines in the region.
  19347. - If it is a normal line, turn each and every normal line (i.e.,
  19348. not an heading or an item) in the region into headings. If you
  19349. want to convert only the first line of this region, use one
  19350. universal prefix argument.
  19351. - If it is a plain list item, turn all plain list items into headings.
  19352. When converting a line into a heading, the number of stars is chosen
  19353. such that the lines become children of the current entry. However,
  19354. when a numeric prefix argument is given, its value determines the
  19355. number of stars to add."
  19356. (interactive "P")
  19357. (let ((skip-blanks
  19358. (function
  19359. ;; Return beginning of first non-blank line, starting from
  19360. ;; line at POS.
  19361. (lambda (pos)
  19362. (save-excursion
  19363. (goto-char pos)
  19364. (while (org-at-comment-p) (forward-line))
  19365. (skip-chars-forward " \r\t\n")
  19366. (point-at-bol)))))
  19367. beg end toggled)
  19368. ;; Determine boundaries of changes. If a universal prefix has
  19369. ;; been given, put the list in a region. If region ends at a bol,
  19370. ;; do not consider the last line to be in the region.
  19371. (when (and current-prefix-arg (org-at-item-p))
  19372. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19373. (org-mark-element))
  19374. (if (org-region-active-p)
  19375. (setq beg (funcall skip-blanks (region-beginning))
  19376. end (copy-marker (save-excursion
  19377. (goto-char (region-end))
  19378. (if (bolp) (point) (point-at-eol)))))
  19379. (setq beg (funcall skip-blanks (point-at-bol))
  19380. end (copy-marker (point-at-eol))))
  19381. ;; Ensure inline tasks don't count as headings.
  19382. (org-with-limited-levels
  19383. (save-excursion
  19384. (goto-char beg)
  19385. (cond
  19386. ;; Case 1. Started at an heading: de-star headings.
  19387. ((org-at-heading-p)
  19388. (while (< (point) end)
  19389. (when (org-at-heading-p t)
  19390. (looking-at org-outline-regexp) (replace-match "")
  19391. (setq toggled t))
  19392. (forward-line)))
  19393. ;; Case 2. Started at an item: change items into headlines.
  19394. ;; One star will be added by `org-list-to-subtree'.
  19395. ((org-at-item-p)
  19396. (let* ((stars (make-string
  19397. ;; subtract the star that will be added again by
  19398. ;; `org-list-to-subtree'
  19399. (if (numberp nstars) (1- nstars)
  19400. (or (org-current-level) 0))
  19401. ?*))
  19402. (add-stars
  19403. (cond (nstars "") ; stars from prefix only
  19404. ((equal stars "") "") ; before first heading
  19405. (org-odd-levels-only "*") ; inside heading, odd
  19406. (t "")))) ; inside heading, oddeven
  19407. (while (< (point) end)
  19408. (when (org-at-item-p)
  19409. ;; Pay attention to cases when region ends before list.
  19410. (let* ((struct (org-list-struct))
  19411. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19412. (save-restriction
  19413. (narrow-to-region (point) list-end)
  19414. (insert
  19415. (org-list-to-subtree
  19416. (org-list-parse-list t)
  19417. '(:istart (concat stars add-stars (funcall get-stars depth))
  19418. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19419. (setq toggled t))
  19420. (forward-line))))
  19421. ;; Case 3. Started at normal text: make every line an heading,
  19422. ;; skipping headlines and items.
  19423. (t (let* ((stars
  19424. (make-string
  19425. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19426. (add-stars
  19427. (cond (nstars "") ; stars from prefix only
  19428. ((equal stars "") "*") ; before first heading
  19429. (org-odd-levels-only "**") ; inside heading, odd
  19430. (t "*"))) ; inside heading, oddeven
  19431. (rpl (concat stars add-stars " "))
  19432. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19433. (while (< (point) (if (equal nstars '(4)) lend end))
  19434. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19435. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19436. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19437. (forward-line)))))))
  19438. (unless toggled (message "Cannot toggle heading from here"))))
  19439. (defun org-meta-return (&optional arg)
  19440. "Insert a new heading or wrap a region in a table.
  19441. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19442. on context. See the individual commands for more information."
  19443. (interactive "P")
  19444. (org-check-before-invisible-edit 'insert)
  19445. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19446. (let* ((element (org-element-at-point))
  19447. (type (org-element-type element)))
  19448. (when (eq type 'table-row)
  19449. (setq element (org-element-property :parent element))
  19450. (setq type 'table))
  19451. (if (and (eq type 'table)
  19452. (eq (org-element-property :type element) 'org)
  19453. (>= (point) (org-element-property :contents-begin element))
  19454. (< (point) (org-element-property :contents-end element)))
  19455. (call-interactively 'org-table-wrap-region)
  19456. (call-interactively 'org-insert-heading)))))
  19457. ;;; Menu entries
  19458. (defsubst org-in-subtree-not-table-p ()
  19459. "Are we in a subtree and not in a table?"
  19460. (and (not (org-before-first-heading-p))
  19461. (not (org-at-table-p))))
  19462. ;; Define the Org-mode menus
  19463. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19464. '("Tbl"
  19465. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19466. ["Next Field" org-cycle (org-at-table-p)]
  19467. ["Previous Field" org-shifttab (org-at-table-p)]
  19468. ["Next Row" org-return (org-at-table-p)]
  19469. "--"
  19470. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19471. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19472. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19473. "--"
  19474. ("Column"
  19475. ["Move Column Left" org-metaleft (org-at-table-p)]
  19476. ["Move Column Right" org-metaright (org-at-table-p)]
  19477. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19478. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19479. ("Row"
  19480. ["Move Row Up" org-metaup (org-at-table-p)]
  19481. ["Move Row Down" org-metadown (org-at-table-p)]
  19482. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19483. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19484. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19485. "--"
  19486. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19487. ("Rectangle"
  19488. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19489. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19490. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19491. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19492. "--"
  19493. ("Calculate"
  19494. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19495. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19496. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19497. "--"
  19498. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19499. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19500. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19501. "--"
  19502. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19503. "--"
  19504. ["Sum Column/Rectangle" org-table-sum
  19505. (or (org-at-table-p) (org-region-active-p))]
  19506. ["Which Column?" org-table-current-column (org-at-table-p)])
  19507. ["Debug Formulas"
  19508. org-table-toggle-formula-debugger
  19509. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19510. ["Show Col/Row Numbers"
  19511. org-table-toggle-coordinate-overlays
  19512. :style toggle
  19513. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19514. "--"
  19515. ["Create" org-table-create (and (not (org-at-table-p))
  19516. org-enable-table-editor)]
  19517. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19518. ["Import from File" org-table-import (not (org-at-table-p))]
  19519. ["Export to File" org-table-export (org-at-table-p)]
  19520. "--"
  19521. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19522. "--"
  19523. ("Plot"
  19524. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19525. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19526. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19527. '("Org"
  19528. ("Show/Hide"
  19529. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19530. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19531. ["Sparse Tree..." org-sparse-tree t]
  19532. ["Reveal Context" org-reveal t]
  19533. ["Show All" show-all t]
  19534. "--"
  19535. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19536. "--"
  19537. ["New Heading" org-insert-heading t]
  19538. ("Navigate Headings"
  19539. ["Up" outline-up-heading t]
  19540. ["Next" outline-next-visible-heading t]
  19541. ["Previous" outline-previous-visible-heading t]
  19542. ["Next Same Level" outline-forward-same-level t]
  19543. ["Previous Same Level" outline-backward-same-level t]
  19544. "--"
  19545. ["Jump" org-goto t])
  19546. ("Edit Structure"
  19547. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19548. "--"
  19549. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19550. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19551. "--"
  19552. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19553. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19554. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19555. "--"
  19556. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19557. "--"
  19558. ["Copy visible text" org-copy-visible t]
  19559. "--"
  19560. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19561. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19562. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19563. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19564. "--"
  19565. ["Sort Region/Children" org-sort t]
  19566. "--"
  19567. ["Convert to odd levels" org-convert-to-odd-levels t]
  19568. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19569. ("Editing"
  19570. ["Emphasis..." org-emphasize t]
  19571. ["Edit Source Example" org-edit-special t]
  19572. "--"
  19573. ["Footnote new/jump" org-footnote-action t]
  19574. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19575. ("Archive"
  19576. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19577. "--"
  19578. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19579. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19580. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19581. )
  19582. "--"
  19583. ("Hyperlinks"
  19584. ["Store Link (Global)" org-store-link t]
  19585. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19586. ["Insert Link" org-insert-link t]
  19587. ["Follow Link" org-open-at-point t]
  19588. "--"
  19589. ["Next link" org-next-link t]
  19590. ["Previous link" org-previous-link t]
  19591. "--"
  19592. ["Descriptive Links"
  19593. org-toggle-link-display
  19594. :style radio
  19595. :selected org-descriptive-links
  19596. ]
  19597. ["Literal Links"
  19598. org-toggle-link-display
  19599. :style radio
  19600. :selected (not org-descriptive-links)])
  19601. "--"
  19602. ("TODO Lists"
  19603. ["TODO/DONE/-" org-todo t]
  19604. ("Select keyword"
  19605. ["Next keyword" org-shiftright (org-at-heading-p)]
  19606. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19607. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19608. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19609. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19610. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19611. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19612. "--"
  19613. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19614. :selected org-enforce-todo-dependencies :style toggle :active t]
  19615. "Settings for tree at point"
  19616. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19617. :selected (org-entry-get nil "ORDERED")
  19618. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19619. ["Do Children parallel" org-toggle-ordered-property :style radio
  19620. :selected (not (org-entry-get nil "ORDERED"))
  19621. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19622. "--"
  19623. ["Set Priority" org-priority t]
  19624. ["Priority Up" org-shiftup t]
  19625. ["Priority Down" org-shiftdown t]
  19626. "--"
  19627. ["Get news from all feeds" org-feed-update-all t]
  19628. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19629. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19630. ("TAGS and Properties"
  19631. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19632. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19633. "--"
  19634. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19635. ["Column view of properties" org-columns t]
  19636. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19637. ("Dates and Scheduling"
  19638. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19639. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19640. ("Change Date"
  19641. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19642. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19643. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19644. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19645. ["Compute Time Range" org-evaluate-time-range t]
  19646. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19647. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19648. "--"
  19649. ["Custom time format" org-toggle-time-stamp-overlays
  19650. :style radio :selected org-display-custom-times]
  19651. "--"
  19652. ["Goto Calendar" org-goto-calendar t]
  19653. ["Date from Calendar" org-date-from-calendar t]
  19654. "--"
  19655. ["Start/Restart Timer" org-timer-start t]
  19656. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19657. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19658. ["Insert Timer String" org-timer t]
  19659. ["Insert Timer Item" org-timer-item t])
  19660. ("Logging work"
  19661. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19662. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19663. ["Clock out" org-clock-out t]
  19664. ["Clock cancel" org-clock-cancel t]
  19665. "--"
  19666. ["Mark as default task" org-clock-mark-default-task t]
  19667. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19668. ["Goto running clock" org-clock-goto t]
  19669. "--"
  19670. ["Display times" org-clock-display t]
  19671. ["Create clock table" org-clock-report t]
  19672. "--"
  19673. ["Record DONE time"
  19674. (progn (setq org-log-done (not org-log-done))
  19675. (message "Switching to %s will %s record a timestamp"
  19676. (car org-done-keywords)
  19677. (if org-log-done "automatically" "not")))
  19678. :style toggle :selected org-log-done])
  19679. "--"
  19680. ["Agenda Command..." org-agenda t]
  19681. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19682. ("File List for Agenda")
  19683. ("Special views current file"
  19684. ["TODO Tree" org-show-todo-tree t]
  19685. ["Check Deadlines" org-check-deadlines t]
  19686. ["Timeline" org-timeline t]
  19687. ["Tags/Property tree" org-match-sparse-tree t])
  19688. "--"
  19689. ["Export/Publish..." org-export-dispatch t]
  19690. ("LaTeX"
  19691. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19692. :selected org-cdlatex-mode]
  19693. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19694. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19695. ["Modify math symbol" org-cdlatex-math-modify
  19696. (org-inside-LaTeX-fragment-p)]
  19697. ["Insert citation" org-reftex-citation t]
  19698. "--"
  19699. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19700. "--"
  19701. ("MobileOrg"
  19702. ["Push Files and Views" org-mobile-push t]
  19703. ["Get Captured and Flagged" org-mobile-pull t]
  19704. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19705. "--"
  19706. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19707. "--"
  19708. ("Documentation"
  19709. ["Show Version" org-version t]
  19710. ["Info Documentation" org-info t])
  19711. ("Customize"
  19712. ["Browse Org Group" org-customize t]
  19713. "--"
  19714. ["Expand This Menu" org-create-customize-menu
  19715. (fboundp 'customize-menu-create)])
  19716. ["Send bug report" org-submit-bug-report t]
  19717. "--"
  19718. ("Refresh/Reload"
  19719. ["Refresh setup current buffer" org-mode-restart t]
  19720. ["Reload Org (after update)" org-reload t]
  19721. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19722. ))
  19723. (defun org-info (&optional node)
  19724. "Read documentation for Org-mode in the info system.
  19725. With optional NODE, go directly to that node."
  19726. (interactive)
  19727. (info (format "(org)%s" (or node ""))))
  19728. ;;;###autoload
  19729. (defun org-submit-bug-report ()
  19730. "Submit a bug report on Org-mode via mail.
  19731. Don't hesitate to report any problems or inaccurate documentation.
  19732. If you don't have setup sending mail from (X)Emacs, please copy the
  19733. output buffer into your mail program, as it gives us important
  19734. information about your Org-mode version and configuration."
  19735. (interactive)
  19736. (require 'reporter)
  19737. (org-load-modules-maybe)
  19738. (org-require-autoloaded-modules)
  19739. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19740. (reporter-submit-bug-report
  19741. "emacs-orgmode@gnu.org"
  19742. (org-version nil 'full)
  19743. (let (list)
  19744. (save-window-excursion
  19745. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19746. (delete-other-windows)
  19747. (erase-buffer)
  19748. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19749. We would like to add your full Org-mode and Outline configuration to the
  19750. bug report. This greatly simplifies the work of the maintainer and
  19751. other experts on the mailing list.
  19752. HOWEVER, some variables you have customized may contain private
  19753. information. The names of customers, colleagues, or friends, might
  19754. appear in the form of file names, tags, todo states, or search strings.
  19755. If you answer yes to the prompt, you might want to check and remove
  19756. such private information before sending the email.")
  19757. (add-text-properties (point-min) (point-max) '(face org-warning))
  19758. (when (yes-or-no-p "Include your Org-mode configuration ")
  19759. (mapatoms
  19760. (lambda (v)
  19761. (and (boundp v)
  19762. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19763. (or (and (symbol-value v)
  19764. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19765. (and
  19766. (get v 'custom-type) (get v 'standard-value)
  19767. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19768. (push v list)))))
  19769. (kill-buffer (get-buffer "*Warn about privacy*"))
  19770. list))
  19771. nil nil
  19772. "Remember to cover the basics, that is, what you expected to happen and
  19773. what in fact did happen. You don't know how to make a good report? See
  19774. http://orgmode.org/manual/Feedback.html#Feedback
  19775. Your bug report will be posted to the Org-mode mailing list.
  19776. ------------------------------------------------------------------------")
  19777. (save-excursion
  19778. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19779. (replace-match "\\1Bug: \\3 [\\2]")))))
  19780. (defun org-install-agenda-files-menu ()
  19781. (let ((bl (buffer-list)))
  19782. (save-excursion
  19783. (while bl
  19784. (set-buffer (pop bl))
  19785. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19786. (when (derived-mode-p 'org-mode)
  19787. (easy-menu-change
  19788. '("Org") "File List for Agenda"
  19789. (append
  19790. (list
  19791. ["Edit File List" (org-edit-agenda-file-list) t]
  19792. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19793. ["Remove Current File from List" org-remove-file t]
  19794. ["Cycle through agenda files" org-cycle-agenda-files t]
  19795. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19796. "--")
  19797. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19798. ;;;; Documentation
  19799. (defun org-require-autoloaded-modules ()
  19800. (interactive)
  19801. (mapc 'require
  19802. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19803. org-table org-timer)))
  19804. ;;;###autoload
  19805. (defun org-reload (&optional uncompiled)
  19806. "Reload all org lisp files.
  19807. With prefix arg UNCOMPILED, load the uncompiled versions."
  19808. (interactive "P")
  19809. (require 'loadhist)
  19810. (let* ((org-dir (org-find-library-dir "org"))
  19811. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19812. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19813. (remove-re (mapconcat 'identity
  19814. (mapcar (lambda (f) (concat "^" f "$"))
  19815. (list (if (featurep 'xemacs)
  19816. "org-colview"
  19817. "org-colview-xemacs")
  19818. "org" "org-loaddefs" "org-version"))
  19819. "\\|"))
  19820. (feats (delete-dups
  19821. (mapcar 'file-name-sans-extension
  19822. (mapcar 'file-name-nondirectory
  19823. (delq nil
  19824. (mapcar 'feature-file
  19825. features))))))
  19826. (lfeat (append
  19827. (sort
  19828. (setq feats
  19829. (delq nil (mapcar
  19830. (lambda (f)
  19831. (if (and (string-match feature-re f)
  19832. (not (string-match remove-re f)))
  19833. f nil))
  19834. feats)))
  19835. 'string-lessp)
  19836. (list "org-version" "org")))
  19837. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19838. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19839. load-uncore load-misses)
  19840. (setq load-misses
  19841. (delq 't
  19842. (mapcar (lambda (f)
  19843. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19844. (and (string= org-dir contrib-dir)
  19845. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19846. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19847. (add-to-list 'load-uncore f 'append)
  19848. 't)
  19849. f))
  19850. lfeat)))
  19851. (if load-uncore
  19852. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19853. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19854. (if load-misses
  19855. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19856. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19857. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19858. ;;;###autoload
  19859. (defun org-customize ()
  19860. "Call the customize function with org as argument."
  19861. (interactive)
  19862. (org-load-modules-maybe)
  19863. (org-require-autoloaded-modules)
  19864. (customize-browse 'org))
  19865. (defun org-create-customize-menu ()
  19866. "Create a full customization menu for Org-mode, insert it into the menu."
  19867. (interactive)
  19868. (org-load-modules-maybe)
  19869. (org-require-autoloaded-modules)
  19870. (if (fboundp 'customize-menu-create)
  19871. (progn
  19872. (easy-menu-change
  19873. '("Org") "Customize"
  19874. `(["Browse Org group" org-customize t]
  19875. "--"
  19876. ,(customize-menu-create 'org)
  19877. ["Set" Custom-set t]
  19878. ["Save" Custom-save t]
  19879. ["Reset to Current" Custom-reset-current t]
  19880. ["Reset to Saved" Custom-reset-saved t]
  19881. ["Reset to Standard Settings" Custom-reset-standard t]))
  19882. (message "\"Org\"-menu now contains full customization menu"))
  19883. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19884. ;;;; Miscellaneous stuff
  19885. ;;; Generally useful functions
  19886. (defsubst org-get-at-eol (property n)
  19887. "Get text property PROPERTY at the end of line less N characters."
  19888. (get-text-property (- (point-at-eol) n) property))
  19889. (defun org-find-text-property-in-string (prop s)
  19890. "Return the first non-nil value of property PROP in string S."
  19891. (or (get-text-property 0 prop s)
  19892. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19893. prop s)))
  19894. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19895. "Display the given MESSAGE as a warning."
  19896. (if (fboundp 'display-warning)
  19897. (display-warning 'org message
  19898. (if (featurep 'xemacs) 'warning :warning))
  19899. (let ((buf (get-buffer-create "*Org warnings*")))
  19900. (with-current-buffer buf
  19901. (goto-char (point-max))
  19902. (insert "Warning (Org): " message)
  19903. (unless (bolp)
  19904. (newline)))
  19905. (display-buffer buf)
  19906. (sit-for 0))))
  19907. (defun org-eval (form)
  19908. "Eval FORM and return result."
  19909. (condition-case error
  19910. (eval form)
  19911. (error (format "%%![Error: %s]" error))))
  19912. (defun org-in-clocktable-p ()
  19913. "Check if the cursor is in a clocktable."
  19914. (let ((pos (point)) start)
  19915. (save-excursion
  19916. (end-of-line 1)
  19917. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19918. (setq start (match-beginning 0))
  19919. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19920. (>= (match-end 0) pos)
  19921. start))))
  19922. (defun org-in-verbatim-emphasis ()
  19923. (save-match-data
  19924. (and (org-in-regexp org-emph-re 2)
  19925. (>= (point) (match-beginning 3))
  19926. (<= (point) (match-end 4))
  19927. (member (match-string 3) '("=" "~")))))
  19928. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19929. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19930. (if (and marker (marker-buffer marker)
  19931. (buffer-live-p (marker-buffer marker)))
  19932. (progn
  19933. (org-pop-to-buffer-same-window (marker-buffer marker))
  19934. (if (or (> marker (point-max)) (< marker (point-min)))
  19935. (widen))
  19936. (goto-char marker)
  19937. (org-show-context 'org-goto))
  19938. (if bookmark
  19939. (bookmark-jump bookmark)
  19940. (error "Cannot find location"))))
  19941. (defun org-quote-csv-field (s)
  19942. "Quote field for inclusion in CSV material."
  19943. (if (string-match "[\",]" s)
  19944. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19945. s))
  19946. (defun org-force-self-insert (N)
  19947. "Needed to enforce self-insert under remapping."
  19948. (interactive "p")
  19949. (self-insert-command N))
  19950. (defun org-string-width (s)
  19951. "Compute width of string, ignoring invisible characters.
  19952. This ignores character with invisibility property `org-link', and also
  19953. characters with property `org-cwidth', because these will become invisible
  19954. upon the next fontification round."
  19955. (let (b l)
  19956. (when (or (eq t buffer-invisibility-spec)
  19957. (assq 'org-link buffer-invisibility-spec))
  19958. (while (setq b (text-property-any 0 (length s)
  19959. 'invisible 'org-link s))
  19960. (setq s (concat (substring s 0 b)
  19961. (substring s (or (next-single-property-change
  19962. b 'invisible s) (length s)))))))
  19963. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19964. (setq s (concat (substring s 0 b)
  19965. (substring s (or (next-single-property-change
  19966. b 'org-cwidth s) (length s))))))
  19967. (setq l (string-width s) b -1)
  19968. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19969. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19970. l))
  19971. (defun org-shorten-string (s maxlength)
  19972. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19973. If the string is shorter or has length MAXLENGTH, just return the
  19974. original string. If it is longer, the functions finds a space in the
  19975. string, breaks this string off at that locations and adds three dots
  19976. as ellipsis. Including the ellipsis, the string will not be longer
  19977. than MAXLENGTH. If finding a good breaking point in the string does
  19978. not work, the string is just chopped off in the middle of a word
  19979. if necessary."
  19980. (if (<= (length s) maxlength)
  19981. s
  19982. (let* ((n (max (- maxlength 4) 1))
  19983. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19984. (if (string-match re s)
  19985. (concat (match-string 1 s) "...")
  19986. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19987. (defun org-get-indentation (&optional line)
  19988. "Get the indentation of the current line, interpreting tabs.
  19989. When LINE is given, assume it represents a line and compute its indentation."
  19990. (if line
  19991. (if (string-match "^ *" (org-remove-tabs line))
  19992. (match-end 0))
  19993. (save-excursion
  19994. (beginning-of-line 1)
  19995. (skip-chars-forward " \t")
  19996. (current-column))))
  19997. (defun org-get-string-indentation (s)
  19998. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19999. (let ((n -1) (i 0) (w tab-width) c)
  20000. (catch 'exit
  20001. (while (< (setq n (1+ n)) (length s))
  20002. (setq c (aref s n))
  20003. (cond ((= c ?\ ) (setq i (1+ i)))
  20004. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20005. (t (throw 'exit t)))))
  20006. i))
  20007. (defun org-remove-tabs (s &optional width)
  20008. "Replace tabulators in S with spaces.
  20009. Assumes that s is a single line, starting in column 0."
  20010. (setq width (or width tab-width))
  20011. (while (string-match "\t" s)
  20012. (setq s (replace-match
  20013. (make-string
  20014. (- (* width (/ (+ (match-beginning 0) width) width))
  20015. (match-beginning 0)) ?\ )
  20016. t t s)))
  20017. s)
  20018. (defun org-fix-indentation (line ind)
  20019. "Fix indentation in LINE.
  20020. IND is a cons cell with target and minimum indentation.
  20021. If the current indentation in LINE is smaller than the minimum,
  20022. leave it alone. If it is larger than ind, set it to the target."
  20023. (let* ((l (org-remove-tabs line))
  20024. (i (org-get-indentation l))
  20025. (i1 (car ind)) (i2 (cdr ind)))
  20026. (if (>= i i2) (setq l (substring line i2)))
  20027. (if (> i1 0)
  20028. (concat (make-string i1 ?\ ) l)
  20029. l)))
  20030. (defun org-remove-indentation (code &optional n)
  20031. "Remove the maximum common indentation from the lines in CODE.
  20032. N may optionally be the number of spaces to remove."
  20033. (with-temp-buffer
  20034. (insert code)
  20035. (org-do-remove-indentation n)
  20036. (buffer-string)))
  20037. (defun org-do-remove-indentation (&optional n)
  20038. "Remove the maximum common indentation from the buffer."
  20039. (untabify (point-min) (point-max))
  20040. (let ((min 10000) re)
  20041. (if n
  20042. (setq min n)
  20043. (goto-char (point-min))
  20044. (while (re-search-forward "^ *[^ \n]" nil t)
  20045. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20046. (unless (or (= min 0) (= min 10000))
  20047. (setq re (format "^ \\{%d\\}" min))
  20048. (goto-char (point-min))
  20049. (while (re-search-forward re nil t)
  20050. (replace-match "")
  20051. (end-of-line 1))
  20052. min)))
  20053. (defun org-fill-template (template alist)
  20054. "Find each %key of ALIST in TEMPLATE and replace it."
  20055. (let ((case-fold-search nil)
  20056. entry key value)
  20057. (setq alist (sort (copy-sequence alist)
  20058. (lambda (a b) (< (length (car a)) (length (car b))))))
  20059. (while (setq entry (pop alist))
  20060. (setq template
  20061. (replace-regexp-in-string
  20062. (concat "%" (regexp-quote (car entry)))
  20063. (or (cdr entry) "") template t t)))
  20064. template))
  20065. (defun org-base-buffer (buffer)
  20066. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20067. (if (not buffer)
  20068. buffer
  20069. (or (buffer-base-buffer buffer)
  20070. buffer)))
  20071. (defun org-wrap (string &optional width lines)
  20072. "Wrap string to either a number of lines, or a width in characters.
  20073. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20074. that costs. If there is a word longer than WIDTH, the text is actually
  20075. wrapped to the length of that word.
  20076. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20077. many lines, whatever width that takes.
  20078. The return value is a list of lines, without newlines at the end."
  20079. (let* ((words (org-split-string string "[ \t\n]+"))
  20080. (maxword (apply 'max (mapcar 'org-string-width words)))
  20081. w ll)
  20082. (cond (width
  20083. (org-do-wrap words (max maxword width)))
  20084. (lines
  20085. (setq w maxword)
  20086. (setq ll (org-do-wrap words maxword))
  20087. (if (<= (length ll) lines)
  20088. ll
  20089. (setq ll words)
  20090. (while (> (length ll) lines)
  20091. (setq w (1+ w))
  20092. (setq ll (org-do-wrap words w)))
  20093. ll))
  20094. (t (error "Cannot wrap this")))))
  20095. (defun org-do-wrap (words width)
  20096. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20097. (let (lines line)
  20098. (while words
  20099. (setq line (pop words))
  20100. (while (and words (< (+ (length line) (length (car words))) width))
  20101. (setq line (concat line " " (pop words))))
  20102. (setq lines (push line lines)))
  20103. (nreverse lines)))
  20104. (defun org-split-string (string &optional separators)
  20105. "Splits STRING into substrings at SEPARATORS.
  20106. No empty strings are returned if there are matches at the beginning
  20107. and end of string."
  20108. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20109. (start 0)
  20110. notfirst
  20111. (list nil))
  20112. (while (and (string-match rexp string
  20113. (if (and notfirst
  20114. (= start (match-beginning 0))
  20115. (< start (length string)))
  20116. (1+ start) start))
  20117. (< (match-beginning 0) (length string)))
  20118. (setq notfirst t)
  20119. (or (eq (match-beginning 0) 0)
  20120. (and (eq (match-beginning 0) (match-end 0))
  20121. (eq (match-beginning 0) start))
  20122. (setq list
  20123. (cons (substring string start (match-beginning 0))
  20124. list)))
  20125. (setq start (match-end 0)))
  20126. (or (eq start (length string))
  20127. (setq list
  20128. (cons (substring string start)
  20129. list)))
  20130. (nreverse list)))
  20131. (defun org-quote-vert (s)
  20132. "Replace \"|\" with \"\\vert\"."
  20133. (while (string-match "|" s)
  20134. (setq s (replace-match "\\vert" t t s)))
  20135. s)
  20136. (defun org-uuidgen-p (s)
  20137. "Is S an ID created by UUIDGEN?"
  20138. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20139. (defun org-in-src-block-p (&optional inside)
  20140. "Whether point is in a code source block.
  20141. When INSIDE is non-nil, don't consider we are within a src block
  20142. when point is at #+BEGIN_SRC or #+END_SRC."
  20143. (let ((case-fold-search t) ov)
  20144. (or (and (eq (get-char-property (point) 'src-block) t))
  20145. (and (not inside)
  20146. (save-match-data
  20147. (save-excursion
  20148. (beginning-of-line)
  20149. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20150. (defun org-context ()
  20151. "Return a list of contexts of the current cursor position.
  20152. If several contexts apply, all are returned.
  20153. Each context entry is a list with a symbol naming the context, and
  20154. two positions indicating start and end of the context. Possible
  20155. contexts are:
  20156. :headline anywhere in a headline
  20157. :headline-stars on the leading stars in a headline
  20158. :todo-keyword on a TODO keyword (including DONE) in a headline
  20159. :tags on the TAGS in a headline
  20160. :priority on the priority cookie in a headline
  20161. :item on the first line of a plain list item
  20162. :item-bullet on the bullet/number of a plain list item
  20163. :checkbox on the checkbox in a plain list item
  20164. :table in an org-mode table
  20165. :table-special on a special filed in a table
  20166. :table-table in a table.el table
  20167. :clocktable in a clocktable
  20168. :src-block in a source block
  20169. :link on a hyperlink
  20170. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20171. :target on a <<target>>
  20172. :radio-target on a <<<radio-target>>>
  20173. :latex-fragment on a LaTeX fragment
  20174. :latex-preview on a LaTeX fragment with overlaid preview image
  20175. This function expects the position to be visible because it uses font-lock
  20176. faces as a help to recognize the following contexts: :table-special, :link,
  20177. and :keyword."
  20178. (let* ((f (get-text-property (point) 'face))
  20179. (faces (if (listp f) f (list f)))
  20180. (case-fold-search t)
  20181. (p (point)) clist o)
  20182. ;; First the large context
  20183. (cond
  20184. ((org-at-heading-p t)
  20185. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20186. (when (progn
  20187. (beginning-of-line 1)
  20188. (looking-at org-todo-line-tags-regexp))
  20189. (push (org-point-in-group p 1 :headline-stars) clist)
  20190. (push (org-point-in-group p 2 :todo-keyword) clist)
  20191. (push (org-point-in-group p 4 :tags) clist))
  20192. (goto-char p)
  20193. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20194. (if (looking-at "\\[#[A-Z0-9]\\]")
  20195. (push (org-point-in-group p 0 :priority) clist)))
  20196. ((org-at-item-p)
  20197. (push (org-point-in-group p 2 :item-bullet) clist)
  20198. (push (list :item (point-at-bol)
  20199. (save-excursion (org-end-of-item) (point)))
  20200. clist)
  20201. (and (org-at-item-checkbox-p)
  20202. (push (org-point-in-group p 0 :checkbox) clist)))
  20203. ((org-at-table-p)
  20204. (push (list :table (org-table-begin) (org-table-end)) clist)
  20205. (if (memq 'org-formula faces)
  20206. (push (list :table-special
  20207. (previous-single-property-change p 'face)
  20208. (next-single-property-change p 'face)) clist)))
  20209. ((org-at-table-p 'any)
  20210. (push (list :table-table) clist)))
  20211. (goto-char p)
  20212. (let ((case-fold-search t))
  20213. ;; New the "medium" contexts: clocktables, source blocks
  20214. (cond ((org-in-clocktable-p)
  20215. (push (list :clocktable
  20216. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20217. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20218. (match-beginning 1))
  20219. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20220. (match-end 0))) clist))
  20221. ((org-in-src-block-p)
  20222. (push (list :src-block
  20223. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20224. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20225. (match-beginning 1))
  20226. (and (search-forward "#+END_SRC" nil t)
  20227. (match-beginning 0))) clist))))
  20228. (goto-char p)
  20229. ;; Now the small context
  20230. (cond
  20231. ((org-at-timestamp-p)
  20232. (push (org-point-in-group p 0 :timestamp) clist))
  20233. ((memq 'org-link faces)
  20234. (push (list :link
  20235. (previous-single-property-change p 'face)
  20236. (next-single-property-change p 'face)) clist))
  20237. ((memq 'org-special-keyword faces)
  20238. (push (list :keyword
  20239. (previous-single-property-change p 'face)
  20240. (next-single-property-change p 'face)) clist))
  20241. ((org-at-target-p)
  20242. (push (org-point-in-group p 0 :target) clist)
  20243. (goto-char (1- (match-beginning 0)))
  20244. (if (looking-at org-radio-target-regexp)
  20245. (push (org-point-in-group p 0 :radio-target) clist))
  20246. (goto-char p))
  20247. ((setq o (car (delq nil
  20248. (mapcar
  20249. (lambda (x)
  20250. (if (memq x org-latex-fragment-image-overlays) x))
  20251. (overlays-at (point))))))
  20252. (push (list :latex-fragment
  20253. (overlay-start o) (overlay-end o)) clist)
  20254. (push (list :latex-preview
  20255. (overlay-start o) (overlay-end o)) clist))
  20256. ((org-inside-LaTeX-fragment-p)
  20257. ;; FIXME: positions wrong.
  20258. (push (list :latex-fragment (point) (point)) clist)))
  20259. (setq clist (nreverse (delq nil clist)))
  20260. clist))
  20261. ;; FIXME: Compare with at-regexp-p Do we need both?
  20262. (defun org-in-regexp (re &optional nlines visually)
  20263. "Check if point is inside a match of regexp.
  20264. Normally only the current line is checked, but you can include NLINES extra
  20265. lines both before and after point into the search.
  20266. If VISUALLY is set, require that the cursor is not after the match but
  20267. really on, so that the block visually is on the match."
  20268. (catch 'exit
  20269. (let ((pos (point))
  20270. (eol (point-at-eol (+ 1 (or nlines 0))))
  20271. (inc (if visually 1 0)))
  20272. (save-excursion
  20273. (beginning-of-line (- 1 (or nlines 0)))
  20274. (while (re-search-forward re eol t)
  20275. (if (and (<= (match-beginning 0) pos)
  20276. (>= (+ inc (match-end 0)) pos))
  20277. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20278. (defun org-at-regexp-p (regexp)
  20279. "Is point inside a match of REGEXP in the current line?"
  20280. (catch 'exit
  20281. (save-excursion
  20282. (let ((pos (point)) (end (point-at-eol)))
  20283. (beginning-of-line 1)
  20284. (while (re-search-forward regexp end t)
  20285. (if (and (<= (match-beginning 0) pos)
  20286. (>= (match-end 0) pos))
  20287. (throw 'exit t)))
  20288. nil))))
  20289. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20290. "Non-nil when point is between matches of START-RE and END-RE.
  20291. Also return a non-nil value when point is on one of the matches.
  20292. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20293. buffer positions. Default values are the positions of headlines
  20294. surrounding the point.
  20295. The functions returns a cons cell whose car (resp. cdr) is the
  20296. position before START-RE (resp. after END-RE)."
  20297. (save-match-data
  20298. (let ((pos (point))
  20299. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20300. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20301. beg end)
  20302. (save-excursion
  20303. ;; Point is on a block when on START-RE or if START-RE can be
  20304. ;; found before it...
  20305. (and (or (org-at-regexp-p start-re)
  20306. (re-search-backward start-re limit-up t))
  20307. (setq beg (match-beginning 0))
  20308. ;; ... and END-RE after it...
  20309. (goto-char (match-end 0))
  20310. (re-search-forward end-re limit-down t)
  20311. (> (setq end (match-end 0)) pos)
  20312. ;; ... without another START-RE in-between.
  20313. (goto-char (match-beginning 0))
  20314. (not (re-search-backward start-re (1+ beg) t))
  20315. ;; Return value.
  20316. (cons beg end))))))
  20317. (defun org-in-block-p (names)
  20318. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20319. NAMES is a list of strings containing names of blocks.
  20320. Return first block name matched, or nil. Beware that in case of
  20321. nested blocks, the returned name may not belong to the closest
  20322. block from point."
  20323. (save-match-data
  20324. (catch 'exit
  20325. (let ((case-fold-search t)
  20326. (lim-up (save-excursion (outline-previous-heading)))
  20327. (lim-down (save-excursion (outline-next-heading))))
  20328. (mapc (lambda (name)
  20329. (let ((n (regexp-quote name)))
  20330. (when (org-between-regexps-p
  20331. (concat "^[ \t]*#\\+begin_" n)
  20332. (concat "^[ \t]*#\\+end_" n)
  20333. lim-up lim-down)
  20334. (throw 'exit n))))
  20335. names))
  20336. nil)))
  20337. (defun org-in-drawer-p ()
  20338. "Non-nil if point is within a drawer.
  20339. If point is within a drawer, return it, as parsed data."
  20340. (let ((element (save-match-data (org-element-at-point))))
  20341. (while (and element (not (memq (org-element-type element)
  20342. '(drawer property-drawer))))
  20343. (setq element (org-element-property :parent element)))
  20344. element))
  20345. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20346. "Call `multi-occur' with buffers for all agenda files."
  20347. (interactive "sOrg-files matching: \np")
  20348. (let* ((files (org-agenda-files))
  20349. (tnames (mapcar 'file-truename files))
  20350. (extra org-agenda-text-search-extra-files)
  20351. f)
  20352. (when (eq (car extra) 'agenda-archives)
  20353. (setq extra (cdr extra))
  20354. (setq files (org-add-archive-files files)))
  20355. (while (setq f (pop extra))
  20356. (unless (member (file-truename f) tnames)
  20357. (add-to-list 'files f 'append)
  20358. (add-to-list 'tnames (file-truename f) 'append)))
  20359. (multi-occur
  20360. (mapcar (lambda (x)
  20361. (with-current-buffer
  20362. (or (get-file-buffer x) (find-file-noselect x))
  20363. (widen)
  20364. (current-buffer)))
  20365. files)
  20366. regexp)))
  20367. (if (boundp 'occur-mode-find-occurrence-hook)
  20368. ;; Emacs 23
  20369. (add-hook 'occur-mode-find-occurrence-hook
  20370. (lambda ()
  20371. (when (derived-mode-p 'org-mode)
  20372. (org-reveal))))
  20373. ;; Emacs 22
  20374. (defadvice occur-mode-goto-occurrence
  20375. (after org-occur-reveal activate)
  20376. (and (derived-mode-p 'org-mode) (org-reveal)))
  20377. (defadvice occur-mode-goto-occurrence-other-window
  20378. (after org-occur-reveal activate)
  20379. (and (derived-mode-p 'org-mode) (org-reveal)))
  20380. (defadvice occur-mode-display-occurrence
  20381. (after org-occur-reveal activate)
  20382. (when (derived-mode-p 'org-mode)
  20383. (let ((pos (occur-mode-find-occurrence)))
  20384. (with-current-buffer (marker-buffer pos)
  20385. (save-excursion
  20386. (goto-char pos)
  20387. (org-reveal)))))))
  20388. (defun org-occur-link-in-agenda-files ()
  20389. "Create a link and search for it in the agendas.
  20390. The link is not stored in `org-stored-links', it is just created
  20391. for the search purpose."
  20392. (interactive)
  20393. (let ((link (condition-case nil
  20394. (org-store-link nil)
  20395. (error "Unable to create a link to here"))))
  20396. (org-occur-in-agenda-files (regexp-quote link))))
  20397. (defun org-reverse-string (string)
  20398. "Return the reverse of STRING."
  20399. (apply 'string (reverse (string-to-list string))))
  20400. ;; defsubst org-uniquify must be defined before first use
  20401. (defun org-uniquify-alist (alist)
  20402. "Merge elements of ALIST with the same key.
  20403. For example, in this alist:
  20404. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20405. => '((a 1 3) (b 2))
  20406. merge (a 1) and (a 3) into (a 1 3).
  20407. The function returns the new ALIST."
  20408. (let (rtn)
  20409. (mapc
  20410. (lambda (e)
  20411. (let (n)
  20412. (if (not (assoc (car e) rtn))
  20413. (push e rtn)
  20414. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20415. (setq rtn (assq-delete-all (car e) rtn))
  20416. (push n rtn))))
  20417. alist)
  20418. rtn))
  20419. (defun org-delete-all (elts list)
  20420. "Remove all elements in ELTS from LIST."
  20421. (while elts
  20422. (setq list (delete (pop elts) list)))
  20423. list)
  20424. (defun org-count (cl-item cl-seq)
  20425. "Count the number of occurrences of ITEM in SEQ.
  20426. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20427. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20428. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20429. (while (< cl-start cl-end)
  20430. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20431. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20432. (setq cl-start (1+ cl-start)))
  20433. cl-count))
  20434. (defun org-remove-if (predicate seq)
  20435. "Remove everything from SEQ that fulfills PREDICATE."
  20436. (let (res e)
  20437. (while seq
  20438. (setq e (pop seq))
  20439. (if (not (funcall predicate e)) (push e res)))
  20440. (nreverse res)))
  20441. (defun org-remove-if-not (predicate seq)
  20442. "Remove everything from SEQ that does not fulfill PREDICATE."
  20443. (let (res e)
  20444. (while seq
  20445. (setq e (pop seq))
  20446. (if (funcall predicate e) (push e res)))
  20447. (nreverse res)))
  20448. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20449. "Reduce two-argument FUNCTION across SEQ.
  20450. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20451. \":initial-value\" removed."
  20452. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20453. (cadr (memq :initial-value cl-keys)))
  20454. (cl-seq (pop cl-seq))
  20455. (t (funcall cl-func)))))
  20456. (while cl-seq
  20457. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20458. cl-accum))
  20459. (defun org-every (pred seq)
  20460. "Return true if PREDICATE is true of every element of SEQ.
  20461. Adapted from `every' in cl.el."
  20462. (catch 'org-every
  20463. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20464. t))
  20465. (defun org-some (pred seq)
  20466. "Return true if PREDICATE is true of any element of SEQ.
  20467. Adapted from `some' in cl.el."
  20468. (catch 'org-some
  20469. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20470. nil))
  20471. (defun org-back-over-empty-lines ()
  20472. "Move backwards over whitespace, to the beginning of the first empty line.
  20473. Returns the number of empty lines passed."
  20474. (let ((pos (point)))
  20475. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20476. (skip-chars-backward " \t\n\r")
  20477. (unless (eobp)
  20478. (forward-line -1)))
  20479. (beginning-of-line 2)
  20480. (goto-char (min (point) pos))
  20481. (count-lines (point) pos)))
  20482. (defun org-skip-whitespace ()
  20483. (skip-chars-forward " \t\n\r"))
  20484. (defun org-point-in-group (point group &optional context)
  20485. "Check if POINT is in match-group GROUP.
  20486. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20487. match. If the match group does not exist or point is not inside it,
  20488. return nil."
  20489. (and (match-beginning group)
  20490. (>= point (match-beginning group))
  20491. (<= point (match-end group))
  20492. (if context
  20493. (list context (match-beginning group) (match-end group))
  20494. t)))
  20495. (defun org-switch-to-buffer-other-window (&rest args)
  20496. "Switch to buffer in a second window on the current frame.
  20497. In particular, do not allow pop-up frames.
  20498. Returns the newly created buffer."
  20499. (org-no-popups
  20500. (apply 'switch-to-buffer-other-window args)))
  20501. (defun org-combine-plists (&rest plists)
  20502. "Create a single property list from all plists in PLISTS.
  20503. The process starts by copying the first list, and then setting properties
  20504. from the other lists. Settings in the last list are the most significant
  20505. ones and overrule settings in the other lists."
  20506. (let ((rtn (copy-sequence (pop plists)))
  20507. p v ls)
  20508. (while plists
  20509. (setq ls (pop plists))
  20510. (while ls
  20511. (setq p (pop ls) v (pop ls))
  20512. (setq rtn (plist-put rtn p v))))
  20513. rtn))
  20514. (defun org-replace-escapes (string table)
  20515. "Replace %-escapes in STRING with values in TABLE.
  20516. TABLE is an association list with keys like \"%a\" and string values.
  20517. The sequences in STRING may contain normal field width and padding information,
  20518. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20519. so values can contain further %-escapes if they are define later in TABLE."
  20520. (let ((tbl (copy-alist table))
  20521. (case-fold-search nil)
  20522. (pchg 0)
  20523. e re rpl)
  20524. (while (setq e (pop tbl))
  20525. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20526. (when (and (cdr e) (string-match re (cdr e)))
  20527. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20528. (safe "SREF"))
  20529. (add-text-properties 0 3 (list 'sref sref) safe)
  20530. (setcdr e (replace-match safe t t (cdr e)))))
  20531. (while (string-match re string)
  20532. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20533. (cdr e)))
  20534. (setq string (replace-match rpl t t string))))
  20535. (while (setq pchg (next-property-change pchg string))
  20536. (let ((sref (get-text-property pchg 'sref string)))
  20537. (when (and sref (string-match "SREF" string pchg))
  20538. (setq string (replace-match sref t t string)))))
  20539. string))
  20540. (defun org-sublist (list start end)
  20541. "Return a section of LIST, from START to END.
  20542. Counting starts at 1."
  20543. (let (rtn (c start))
  20544. (setq list (nthcdr (1- start) list))
  20545. (while (and list (<= c end))
  20546. (push (pop list) rtn)
  20547. (setq c (1+ c)))
  20548. (nreverse rtn)))
  20549. (defun org-find-base-buffer-visiting (file)
  20550. "Like `find-buffer-visiting' but always return the base buffer and
  20551. not an indirect buffer."
  20552. (let ((buf (or (get-file-buffer file)
  20553. (find-buffer-visiting file))))
  20554. (if buf
  20555. (or (buffer-base-buffer buf) buf)
  20556. nil)))
  20557. (defun org-image-file-name-regexp (&optional extensions)
  20558. "Return regexp matching the file names of images.
  20559. If EXTENSIONS is given, only match these."
  20560. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20561. (image-file-name-regexp)
  20562. (let ((image-file-name-extensions
  20563. (or extensions
  20564. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20565. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20566. (concat "\\."
  20567. (regexp-opt (nconc (mapcar 'upcase
  20568. image-file-name-extensions)
  20569. image-file-name-extensions)
  20570. t)
  20571. "\\'"))))
  20572. (defun org-file-image-p (file &optional extensions)
  20573. "Return non-nil if FILE is an image."
  20574. (save-match-data
  20575. (string-match (org-image-file-name-regexp extensions) file)))
  20576. (defun org-get-cursor-date (&optional with-time)
  20577. "Return the date at cursor in as a time.
  20578. This works in the calendar and in the agenda, anywhere else it just
  20579. returns the current time.
  20580. If WITH-TIME is non-nil, returns the time of the event at point (in
  20581. the agenda) or the current time of the day."
  20582. (let (date day defd tp tm hod mod)
  20583. (when with-time
  20584. (setq tp (get-text-property (point) 'time))
  20585. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20586. (setq hod (string-to-number (match-string 1 tp))
  20587. mod (string-to-number (match-string 2 tp))))
  20588. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20589. mod (nth 1 (decode-time (current-time))))))
  20590. (cond
  20591. ((eq major-mode 'calendar-mode)
  20592. (setq date (calendar-cursor-to-date)
  20593. defd (encode-time 0 (or mod 0) (or hod 0)
  20594. (nth 1 date) (nth 0 date) (nth 2 date))))
  20595. ((eq major-mode 'org-agenda-mode)
  20596. (setq day (get-text-property (point) 'day))
  20597. (if day
  20598. (setq date (calendar-gregorian-from-absolute day)
  20599. defd (encode-time 0 (or mod 0) (or hod 0)
  20600. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20601. (or defd (current-time))))
  20602. (defun org-mark-subtree (&optional up)
  20603. "Mark the current subtree.
  20604. This puts point at the start of the current subtree, and mark at
  20605. the end. If a numeric prefix UP is given, move up into the
  20606. hierarchy of headlines by UP levels before marking the subtree."
  20607. (interactive "P")
  20608. (org-with-limited-levels
  20609. (cond ((org-at-heading-p) (beginning-of-line))
  20610. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20611. (t (outline-previous-visible-heading 1))))
  20612. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20613. (if (org-called-interactively-p 'any)
  20614. (call-interactively 'org-mark-element)
  20615. (org-mark-element)))
  20616. ;;; Indentation
  20617. (defun org--get-expected-indentation (element contentsp)
  20618. "Expected indentation column for current line, according to ELEMENT.
  20619. ELEMENT is an element containing point. CONTENTSP is non-nil
  20620. when indentation is to be computed according to contents of
  20621. ELEMENT."
  20622. (let ((type (org-element-type element))
  20623. (start (org-element-property :begin element)))
  20624. (org-with-wide-buffer
  20625. (cond
  20626. (contentsp
  20627. (case type
  20628. (footnote-definition 0)
  20629. ((headline inlinetask nil)
  20630. (if (not org-adapt-indentation) 0
  20631. (let ((level (org-current-level)))
  20632. (if level (1+ level) 0))))
  20633. (item
  20634. (org-list-item-body-column
  20635. (org-element-property :post-affiliated element)))
  20636. (plain-list
  20637. (save-excursion
  20638. (goto-char (org-element-property :post-affiliated element))
  20639. (org-get-indentation)))
  20640. (otherwise
  20641. (goto-char start)
  20642. (org-get-indentation))))
  20643. ((memq type '(headline inlinetask nil))
  20644. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20645. (org--get-expected-indentation element t)
  20646. 0))
  20647. ((eq type 'footnote-definition) 0)
  20648. ;; First paragraph of a footnote definition or an item.
  20649. ;; Indent like parent.
  20650. ((< (line-beginning-position) start)
  20651. (org--get-expected-indentation
  20652. (org-element-property :parent element) t))
  20653. ;; At first line: indent according to previous sibling, if any,
  20654. ;; ignoring footnote definitions and inline tasks, or parent's
  20655. ;; contents.
  20656. ((= (line-beginning-position) start)
  20657. (catch 'exit
  20658. (while t
  20659. (if (= (point-min) start) (throw 'exit 0)
  20660. (goto-char (1- start))
  20661. (let* ((previous (org-element-at-point))
  20662. (parent previous))
  20663. (while (and parent (<= (org-element-property :end parent) start))
  20664. (setq previous parent
  20665. parent (org-element-property :parent parent)))
  20666. (cond
  20667. ((not previous) (throw 'exit 0))
  20668. ((> (org-element-property :end previous) start)
  20669. (throw 'exit (org--get-expected-indentation previous t)))
  20670. ((memq (org-element-type previous)
  20671. '(footnote-definition inlinetask))
  20672. (setq start (org-element-property :begin previous)))
  20673. (t (goto-char (org-element-property :begin previous))
  20674. (throw 'exit
  20675. (if (bolp) (org-get-indentation)
  20676. ;; At first paragraph in an item or
  20677. ;; a footnote definition.
  20678. (org--get-expected-indentation
  20679. (org-element-property :parent previous) t))))))))))
  20680. ;; Otherwise, move to the first non-blank line above.
  20681. (t
  20682. (beginning-of-line)
  20683. (let ((pos (point)))
  20684. (skip-chars-backward " \r\t\n")
  20685. (cond
  20686. ;; Two blank lines end a footnote definition or a plain
  20687. ;; list. When we indent an empty line after them, the
  20688. ;; containing list or footnote definition is over, so it
  20689. ;; qualifies as a previous sibling. Therefore, we indent
  20690. ;; like its first line.
  20691. ((and (memq type '(footnote-definition plain-list))
  20692. (> (count-lines (point) pos) 2))
  20693. (goto-char start)
  20694. (org-get-indentation))
  20695. ;; Line above is the first one of a paragraph at the
  20696. ;; beginning of an item or a footnote definition. Indent
  20697. ;; like parent.
  20698. ((< (line-beginning-position) start)
  20699. (org--get-expected-indentation
  20700. (org-element-property :parent element) t))
  20701. ;; POS is after contents in a greater element. Indent like
  20702. ;; the beginning of the element.
  20703. ;;
  20704. ;; As a special case, if point is at the end of a footnote
  20705. ;; definition or an item, indent like the very last element
  20706. ;; within.
  20707. ((let ((cend (org-element-property :contents-end element)))
  20708. (and cend (<= cend pos)))
  20709. (if (memq type '(footnote-definition item plain-list))
  20710. (org--get-expected-indentation (org-element-at-point) nil)
  20711. (goto-char start)
  20712. (org-get-indentation)))
  20713. ;; In any other case, indent like the current line.
  20714. (t (org-get-indentation)))))))))
  20715. (defun org--align-node-property ()
  20716. "Align node property at point.
  20717. Alignment is done according to `org-property-format', which see."
  20718. (when (save-excursion
  20719. (beginning-of-line)
  20720. (looking-at org-property-re))
  20721. (replace-match
  20722. (concat (match-string 4)
  20723. (org-trim
  20724. (format org-property-format (match-string 1) (match-string 3))))
  20725. t t)))
  20726. (defun org-indent-line ()
  20727. "Indent line depending on context.
  20728. Indentation is done according to the following rules:
  20729. - Footnote definitions, headlines and inline tasks have to
  20730. start at column 0.
  20731. - On the very first line of an element, consider, in order, the
  20732. next rules until one matches:
  20733. 1. If there's a sibling element before, ignoring footnote
  20734. definitions and inline tasks, indent like its first line.
  20735. 2. If element has a parent, indent like its contents. More
  20736. precisely, if parent is an item, indent after the
  20737. description part, if any, or the bullet (see
  20738. ``org-list-description-max-indent'). Else, indent like
  20739. parent's first line.
  20740. 3. Otherwise, indent relatively to current level, if
  20741. `org-adapt-indentation' is non-nil, or to left margin.
  20742. - On a blank line at the end of a plain list, an item, or
  20743. a footnote definition, indent like the very last element
  20744. within.
  20745. - In the code part of a source block, use language major mode
  20746. to indent current line if `org-src-tab-acts-natively' is
  20747. non-nil. If it is nil, do nothing.
  20748. - Otherwise, indent like the first non-blank line above.
  20749. The function doesn't indent an item as it could break the whole
  20750. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20751. \\[org-shiftmetaright].
  20752. Also align node properties according to `org-property-format'."
  20753. (interactive)
  20754. (cond
  20755. (orgstruct-is-++
  20756. (let ((indent-line-function
  20757. (cadadr (assq 'indent-line-function org-fb-vars))))
  20758. (indent-according-to-mode)))
  20759. ((org-at-heading-p) 'noindent)
  20760. (t
  20761. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20762. (type (org-element-type element)))
  20763. (cond ((and (memq type '(plain-list item))
  20764. (= (line-beginning-position)
  20765. (org-element-property :post-affiliated element)))
  20766. 'noindent)
  20767. ((and (eq type 'src-block)
  20768. org-src-tab-acts-natively
  20769. (> (line-beginning-position)
  20770. (org-element-property :post-affiliated element))
  20771. (< (line-beginning-position)
  20772. (org-with-wide-buffer
  20773. (goto-char (org-element-property :end element))
  20774. (skip-chars-backward " \r\t\n")
  20775. (line-beginning-position))))
  20776. (let ((org-src-strip-leading-and-trailing-blank-lines nil))
  20777. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB"))))
  20778. (t
  20779. (let ((column (org--get-expected-indentation element nil)))
  20780. ;; Preserve current column.
  20781. (if (<= (current-column) (current-indentation))
  20782. (org-indent-line-to column)
  20783. (save-excursion (org-indent-line-to column))))
  20784. ;; Align node property. Also preserve current column.
  20785. (when (eq type 'node-property)
  20786. (let ((column (current-column)))
  20787. (org--align-node-property)
  20788. (org-move-to-column column)))))))))
  20789. (defun org-indent-region (start end)
  20790. "Indent each non-blank line in the region.
  20791. Called from a program, START and END specify the region to
  20792. indent. The function will not indent contents of example blocks,
  20793. verse blocks and export blocks as leading white spaces are
  20794. assumed to be significant there."
  20795. (interactive "r")
  20796. (save-excursion
  20797. (goto-char start)
  20798. (skip-chars-forward " \r\t\n")
  20799. (unless (eobp) (beginning-of-line))
  20800. (let ((indent-to
  20801. (lambda (ind pos)
  20802. ;; Set IND as indentation for all lines between point and
  20803. ;; POS or END, whichever comes first. Blank lines are
  20804. ;; ignored. Leave point after POS once done.
  20805. (let ((limit (copy-marker (min end pos))))
  20806. (while (< (point) limit)
  20807. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20808. (forward-line))
  20809. (set-marker limit nil))))
  20810. (end (copy-marker end)))
  20811. (while (< (point) end)
  20812. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20813. (let* ((element (org-element-at-point))
  20814. (type (org-element-type element))
  20815. (element-end (copy-marker (org-element-property :end element)))
  20816. (ind (org--get-expected-indentation element nil)))
  20817. (cond
  20818. ((or (memq type '(paragraph table table-row))
  20819. (not (or (org-element-property :contents-begin element)
  20820. (memq type
  20821. '(example-block export-block src-block)))))
  20822. ;; Elements here are indented as a single block. Also
  20823. ;; align node properties.
  20824. (when (eq type 'node-property)
  20825. (org--align-node-property)
  20826. (beginning-of-line))
  20827. (funcall indent-to ind element-end))
  20828. (t
  20829. ;; Elements in this category consist of three parts:
  20830. ;; before the contents, the contents, and after the
  20831. ;; contents. The contents are treated specially,
  20832. ;; according to the element type, or not indented at
  20833. ;; all. Other parts are indented as a single block.
  20834. (let* ((post (copy-marker
  20835. (org-element-property :post-affiliated element)))
  20836. (cbeg
  20837. (copy-marker
  20838. (cond
  20839. ((not (org-element-property :contents-begin element))
  20840. ;; Fake contents for source blocks.
  20841. (org-with-wide-buffer
  20842. (goto-char post)
  20843. (forward-line)
  20844. (point)))
  20845. ((memq type '(footnote-definition item plain-list))
  20846. ;; Contents in these elements could start on
  20847. ;; the same line as the beginning of the
  20848. ;; element. Make sure we start indenting
  20849. ;; from the second line.
  20850. (org-with-wide-buffer
  20851. (goto-char post)
  20852. (end-of-line)
  20853. (skip-chars-forward " \r\t\n")
  20854. (if (eobp) (point) (line-beginning-position))))
  20855. (t (org-element-property :contents-begin element)))))
  20856. (cend (copy-marker
  20857. (or (org-element-property :contents-end element)
  20858. ;; Fake contents for source blocks.
  20859. (org-with-wide-buffer
  20860. (goto-char element-end)
  20861. (skip-chars-backward " \r\t\n")
  20862. (line-beginning-position))))))
  20863. ;; Do not change items indentation individually as it
  20864. ;; might break the list as a whole. On the other
  20865. ;; hand, when at a plain list, indent it as a whole.
  20866. (cond ((eq type 'plain-list)
  20867. (let ((offset (- ind (org-get-indentation))))
  20868. (unless (zerop offset)
  20869. (indent-rigidly (org-element-property :begin element)
  20870. (org-element-property :end element)
  20871. offset))
  20872. (goto-char cbeg)))
  20873. ((eq type 'item) (goto-char cbeg))
  20874. (t (funcall indent-to ind cbeg)))
  20875. (when (< (point) end)
  20876. (case type
  20877. ((example-block export-block verse-block))
  20878. (src-block
  20879. ;; In a source block, indent source code
  20880. ;; according to language major mode, but only if
  20881. ;; `org-src-tab-acts-natively' is non-nil.
  20882. (when (and (< (point) end) org-src-tab-acts-natively)
  20883. (ignore-errors
  20884. (let (org-src-strip-leading-and-trailing-blank-lines
  20885. ;; Region boundaries in edit buffer.
  20886. (start (1+ (- (point) cbeg)))
  20887. (end (- (min cend end) cbeg)))
  20888. (org-babel-do-in-edit-buffer
  20889. (indent-region start end))))))
  20890. (t (org-indent-region (point) (min cend end))))
  20891. (goto-char (min cend end))
  20892. (when (< (point) end) (funcall indent-to ind element-end)))
  20893. (set-marker post nil)
  20894. (set-marker cbeg nil)
  20895. (set-marker cend nil))))
  20896. (set-marker element-end nil))))
  20897. (set-marker end nil))))
  20898. (defun org-indent-drawer ()
  20899. "Indent the drawer at point."
  20900. (interactive)
  20901. (unless (save-excursion
  20902. (beginning-of-line)
  20903. (org-looking-at-p org-drawer-regexp))
  20904. (user-error "Not at a drawer"))
  20905. (let ((element (org-element-at-point)))
  20906. (unless (memq (org-element-type element) '(drawer property-drawer))
  20907. (user-error "Not at a drawer"))
  20908. (org-with-wide-buffer
  20909. (org-indent-region (org-element-property :begin element)
  20910. (org-element-property :end element))))
  20911. (message "Drawer at point indented"))
  20912. (defun org-indent-block ()
  20913. "Indent the block at point."
  20914. (interactive)
  20915. (unless (save-excursion
  20916. (beginning-of-line)
  20917. (let ((case-fold-search t))
  20918. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20919. (user-error "Not at a block"))
  20920. (let ((element (org-element-at-point)))
  20921. (unless (memq (org-element-type element)
  20922. '(comment-block center-block dynamic-block example-block
  20923. export-block quote-block special-block
  20924. src-block verse-block))
  20925. (user-error "Not at a block"))
  20926. (org-with-wide-buffer
  20927. (org-indent-region (org-element-property :begin element)
  20928. (org-element-property :end element))))
  20929. (message "Block at point indented"))
  20930. ;;; Filling
  20931. ;; We use our own fill-paragraph and auto-fill functions.
  20932. ;; `org-fill-paragraph' relies on adaptive filling and context
  20933. ;; checking. Appropriate `fill-prefix' is computed with
  20934. ;; `org-adaptive-fill-function'.
  20935. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20936. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20937. ;; `org-adaptive-fill-function' value. Internally,
  20938. ;; `org-comment-line-break-function' breaks the line.
  20939. ;; `org-setup-filling' installs filling and auto-filling related
  20940. ;; variables during `org-mode' initialization.
  20941. (defvar org-element-paragraph-separate) ; org-element.el
  20942. (defun org-setup-filling ()
  20943. (require 'org-element)
  20944. ;; Prevent auto-fill from inserting unwanted new items.
  20945. (when (boundp 'fill-nobreak-predicate)
  20946. (org-set-local
  20947. 'fill-nobreak-predicate
  20948. (org-uniquify
  20949. (append fill-nobreak-predicate
  20950. '(org-fill-line-break-nobreak-p
  20951. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20952. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20953. (org-set-local 'paragraph-start paragraph-ending)
  20954. (org-set-local 'paragraph-separate paragraph-ending))
  20955. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20956. (org-set-local 'auto-fill-inhibit-regexp nil)
  20957. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20958. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20959. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20960. (defun org-fill-line-break-nobreak-p ()
  20961. "Non-nil when a new line at point would create an Org line break."
  20962. (save-excursion
  20963. (skip-chars-backward "[ \t]")
  20964. (skip-chars-backward "\\\\")
  20965. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20966. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20967. "Non-nil when a new line at point would split a timestamp."
  20968. (and (org-at-timestamp-p t)
  20969. (not (looking-at org-ts-regexp-both))))
  20970. (declare-function message-in-body-p "message" ())
  20971. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20972. (defun org-adaptive-fill-function ()
  20973. "Compute a fill prefix for the current line.
  20974. Return fill prefix, as a string, or nil if current line isn't
  20975. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20976. matches in paragraphs or comments, use it."
  20977. (catch 'exit
  20978. (when (derived-mode-p 'message-mode)
  20979. (save-excursion
  20980. (beginning-of-line)
  20981. (cond ((or (not (message-in-body-p))
  20982. (looking-at orgtbl-line-start-regexp))
  20983. (throw 'exit nil))
  20984. ((looking-at message-cite-prefix-regexp)
  20985. (throw 'exit (match-string-no-properties 0)))
  20986. ((looking-at org-outline-regexp)
  20987. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  20988. (org-with-wide-buffer
  20989. (unless (org-at-heading-p)
  20990. (let* ((p (line-beginning-position))
  20991. (element (save-excursion
  20992. (beginning-of-line)
  20993. (org-element-at-point)))
  20994. (type (org-element-type element))
  20995. (post-affiliated (org-element-property :post-affiliated element)))
  20996. (unless (< p post-affiliated)
  20997. (case type
  20998. (comment
  20999. (save-excursion
  21000. (beginning-of-line)
  21001. (looking-at "[ \t]*")
  21002. (concat (match-string 0) "# ")))
  21003. (footnote-definition "")
  21004. ((item plain-list)
  21005. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21006. (paragraph
  21007. ;; Fill prefix is usually the same as the current line,
  21008. ;; unless the paragraph is at the beginning of an item.
  21009. (let ((parent (org-element-property :parent element)))
  21010. (save-excursion
  21011. (beginning-of-line)
  21012. (cond ((eq (org-element-type parent) 'item)
  21013. (make-string (org-list-item-body-column
  21014. (org-element-property :begin parent))
  21015. ?\s))
  21016. ((and adaptive-fill-regexp
  21017. ;; Locally disable
  21018. ;; `adaptive-fill-function' to let
  21019. ;; `fill-context-prefix' handle
  21020. ;; `adaptive-fill-regexp' variable.
  21021. (let (adaptive-fill-function)
  21022. (fill-context-prefix
  21023. post-affiliated
  21024. (org-element-property :end element)))))
  21025. ((looking-at "[ \t]+") (match-string 0))
  21026. (t "")))))
  21027. (comment-block
  21028. ;; Only fill contents if P is within block boundaries.
  21029. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21030. (forward-line)
  21031. (point)))
  21032. (cend (save-excursion
  21033. (goto-char (org-element-property :end element))
  21034. (skip-chars-backward " \r\t\n")
  21035. (line-beginning-position))))
  21036. (when (and (>= p cbeg) (< p cend))
  21037. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21038. (match-string 0)
  21039. "")))))))))))
  21040. (declare-function message-goto-body "message" ())
  21041. (defvar message-cite-prefix-regexp) ; From message.el
  21042. (defun org-fill-paragraph (&optional justify)
  21043. "Fill element at point, when applicable.
  21044. This function only applies to comment blocks, comments, example
  21045. blocks and paragraphs. Also, as a special case, re-align table
  21046. when point is at one.
  21047. If JUSTIFY is non-nil (interactively, with prefix argument),
  21048. justify as well. If `sentence-end-double-space' is non-nil, then
  21049. period followed by one space does not end a sentence, so don't
  21050. break a line there. The variable `fill-column' controls the
  21051. width for filling.
  21052. For convenience, when point is at a plain list, an item or
  21053. a footnote definition, try to fill the first paragraph within."
  21054. (interactive)
  21055. (if (and (derived-mode-p 'message-mode)
  21056. (or (not (message-in-body-p))
  21057. (save-excursion (move-beginning-of-line 1)
  21058. (looking-at message-cite-prefix-regexp))))
  21059. ;; First ensure filling is correct in message-mode.
  21060. (let ((fill-paragraph-function
  21061. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21062. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21063. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21064. (paragraph-separate
  21065. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21066. (fill-paragraph nil))
  21067. (with-syntax-table org-mode-transpose-word-syntax-table
  21068. ;; Move to end of line in order to get the first paragraph
  21069. ;; within a plain list or a footnote definition.
  21070. (let ((element (save-excursion
  21071. (end-of-line)
  21072. (or (ignore-errors (org-element-at-point))
  21073. (user-error "An element cannot be parsed line %d"
  21074. (line-number-at-pos (point)))))))
  21075. ;; First check if point is in a blank line at the beginning of
  21076. ;; the buffer. In that case, ignore filling.
  21077. (case (org-element-type element)
  21078. ;; Use major mode filling function is src blocks.
  21079. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21080. ;; Align Org tables, leave table.el tables as-is.
  21081. (table-row (org-table-align) t)
  21082. (table
  21083. (when (eq (org-element-property :type element) 'org)
  21084. (save-excursion
  21085. (goto-char (org-element-property :post-affiliated element))
  21086. (org-table-align)))
  21087. t)
  21088. (paragraph
  21089. ;; Paragraphs may contain `line-break' type objects.
  21090. (let ((beg (max (point-min)
  21091. (org-element-property :contents-begin element)))
  21092. (end (min (point-max)
  21093. (org-element-property :contents-end element))))
  21094. ;; Do nothing if point is at an affiliated keyword.
  21095. (if (< (line-end-position) beg) t
  21096. (when (derived-mode-p 'message-mode)
  21097. ;; In `message-mode', do not fill following citation
  21098. ;; in current paragraph nor text before message body.
  21099. (let ((body-start (save-excursion (message-goto-body))))
  21100. (when body-start (setq beg (max body-start beg))))
  21101. (when (save-excursion
  21102. (re-search-forward
  21103. (concat "^" message-cite-prefix-regexp) end t))
  21104. (setq end (match-beginning 0))))
  21105. ;; Fill paragraph, taking line breaks into account.
  21106. (save-excursion
  21107. (goto-char beg)
  21108. (let ((cuts (list beg)))
  21109. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21110. (when (eq 'line-break
  21111. (org-element-type
  21112. (save-excursion (backward-char)
  21113. (org-element-context))))
  21114. (push (point) cuts)))
  21115. (dolist (c (delq end cuts))
  21116. (fill-region-as-paragraph c end justify)
  21117. (setq end c))))
  21118. t)))
  21119. ;; Contents of `comment-block' type elements should be
  21120. ;; filled as plain text, but only if point is within block
  21121. ;; markers.
  21122. (comment-block
  21123. (let* ((case-fold-search t)
  21124. (beg (save-excursion
  21125. (goto-char (org-element-property :begin element))
  21126. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21127. (forward-line)
  21128. (point)))
  21129. (end (save-excursion
  21130. (goto-char (org-element-property :end element))
  21131. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21132. (line-beginning-position))))
  21133. (if (or (< (point) beg) (> (point) end)) t
  21134. (fill-region-as-paragraph
  21135. (save-excursion (end-of-line)
  21136. (re-search-backward "^[ \t]*$" beg 'move)
  21137. (line-beginning-position))
  21138. (save-excursion (beginning-of-line)
  21139. (re-search-forward "^[ \t]*$" end 'move)
  21140. (line-beginning-position))
  21141. justify))))
  21142. ;; Fill comments.
  21143. (comment
  21144. (let ((begin (org-element-property :post-affiliated element))
  21145. (end (org-element-property :end element)))
  21146. (when (and (>= (point) begin) (<= (point) end))
  21147. (let ((begin (save-excursion
  21148. (end-of-line)
  21149. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21150. (progn (forward-line) (point))
  21151. begin)))
  21152. (end (save-excursion
  21153. (end-of-line)
  21154. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21155. (1- (line-beginning-position))
  21156. (skip-chars-backward " \r\t\n")
  21157. (line-end-position)))))
  21158. ;; Do not fill comments when at a blank line.
  21159. (when (> end begin)
  21160. (let ((fill-prefix
  21161. (save-excursion
  21162. (beginning-of-line)
  21163. (looking-at "[ \t]*#")
  21164. (let ((comment-prefix (match-string 0)))
  21165. (goto-char (match-end 0))
  21166. (if (looking-at adaptive-fill-regexp)
  21167. (concat comment-prefix (match-string 0))
  21168. (concat comment-prefix " "))))))
  21169. (save-excursion
  21170. (fill-region-as-paragraph begin end justify))))))
  21171. t))
  21172. ;; Ignore every other element.
  21173. (otherwise t))))))
  21174. (defun org-auto-fill-function ()
  21175. "Auto-fill function."
  21176. ;; Check if auto-filling is meaningful.
  21177. (let ((fc (current-fill-column)))
  21178. (when (and fc (> (current-column) fc))
  21179. (let* ((fill-prefix (org-adaptive-fill-function))
  21180. ;; Enforce empty fill prefix, if required. Otherwise, it
  21181. ;; will be computed again.
  21182. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21183. (when fill-prefix (do-auto-fill))))))
  21184. (defun org-comment-line-break-function (&optional soft)
  21185. "Break line at point and indent, continuing comment if within one.
  21186. The inserted newline is marked hard if variable
  21187. `use-hard-newlines' is true, unless optional argument SOFT is
  21188. non-nil."
  21189. (if soft (insert-and-inherit ?\n) (newline 1))
  21190. (save-excursion (forward-char -1) (delete-horizontal-space))
  21191. (delete-horizontal-space)
  21192. (indent-to-left-margin)
  21193. (insert-before-markers-and-inherit fill-prefix))
  21194. ;;; Fixed Width Areas
  21195. (defun org-toggle-fixed-width ()
  21196. "Toggle fixed-width markup.
  21197. Add or remove fixed-width markup on current line, whenever it
  21198. makes sense. Return an error otherwise.
  21199. If a region is active and if it contains only fixed-width areas
  21200. or blank lines, remove all fixed-width markup in it. If the
  21201. region contains anything else, convert all non-fixed-width lines
  21202. to fixed-width ones.
  21203. Blank lines at the end of the region are ignored unless the
  21204. region only contains such lines."
  21205. (interactive)
  21206. (if (not (org-region-active-p))
  21207. ;; No region:
  21208. ;;
  21209. ;; Remove fixed width marker only in a fixed-with element.
  21210. ;;
  21211. ;; Add fixed width maker in paragraphs, in blank lines after
  21212. ;; elements or at the beginning of a headline or an inlinetask,
  21213. ;; and before any one-line elements (e.g., a clock).
  21214. (progn
  21215. (beginning-of-line)
  21216. (let* ((element (org-element-at-point))
  21217. (type (org-element-type element)))
  21218. (cond
  21219. ((and (eq type 'fixed-width)
  21220. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21221. (replace-match
  21222. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21223. ((and (memq type '(babel-call clock comment diary-sexp headline
  21224. horizontal-rule keyword paragraph
  21225. planning))
  21226. (<= (org-element-property :post-affiliated element) (point)))
  21227. (skip-chars-forward " \t")
  21228. (insert ": "))
  21229. ((and (org-looking-at-p "[ \t]*$")
  21230. (or (eq type 'inlinetask)
  21231. (save-excursion
  21232. (skip-chars-forward " \r\t\n")
  21233. (<= (org-element-property :end element) (point)))))
  21234. (delete-region (point) (line-end-position))
  21235. (org-indent-line)
  21236. (insert ": "))
  21237. (t (user-error "Cannot insert a fixed-width line here")))))
  21238. ;; Region active.
  21239. (let* ((begin (save-excursion
  21240. (goto-char (region-beginning))
  21241. (line-beginning-position)))
  21242. (end (copy-marker
  21243. (save-excursion
  21244. (goto-char (region-end))
  21245. (unless (eolp) (beginning-of-line))
  21246. (if (save-excursion (re-search-backward "\\S-" begin t))
  21247. (progn (skip-chars-backward " \r\t\n") (point))
  21248. (point)))))
  21249. (all-fixed-width-p
  21250. (catch 'not-all-p
  21251. (save-excursion
  21252. (goto-char begin)
  21253. (skip-chars-forward " \r\t\n")
  21254. (when (eobp) (throw 'not-all-p nil))
  21255. (while (< (point) end)
  21256. (let ((element (org-element-at-point)))
  21257. (if (eq (org-element-type element) 'fixed-width)
  21258. (goto-char (org-element-property :end element))
  21259. (throw 'not-all-p nil))))
  21260. t))))
  21261. (if all-fixed-width-p
  21262. (save-excursion
  21263. (goto-char begin)
  21264. (while (< (point) end)
  21265. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21266. (replace-match
  21267. "" nil nil nil
  21268. (if (= (line-end-position) (match-end 0)) 0 1)))
  21269. (forward-line)))
  21270. (let ((min-ind (point-max)))
  21271. ;; Find minimum indentation across all lines.
  21272. (save-excursion
  21273. (goto-char begin)
  21274. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21275. (setq min-ind 0)
  21276. (catch 'zerop
  21277. (while (< (point) end)
  21278. (unless (org-looking-at-p "[ \t]*$")
  21279. (let ((ind (org-get-indentation)))
  21280. (setq min-ind (min min-ind ind))
  21281. (when (zerop ind) (throw 'zerop t))))
  21282. (forward-line)))))
  21283. ;; Loop over all lines and add fixed-width markup everywhere
  21284. ;; but in fixed-width lines.
  21285. (save-excursion
  21286. (goto-char begin)
  21287. (while (< (point) end)
  21288. (cond
  21289. ((org-at-heading-p)
  21290. (insert ": ")
  21291. (forward-line)
  21292. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21293. (insert ":")
  21294. (forward-line)))
  21295. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21296. (let* ((element (org-element-at-point))
  21297. (element-end (org-element-property :end element)))
  21298. (if (eq (org-element-type element) 'fixed-width)
  21299. (progn (goto-char element-end)
  21300. (skip-chars-backward " \r\t\n")
  21301. (forward-line))
  21302. (let ((limit (min end element-end)))
  21303. (while (< (point) limit)
  21304. (org-move-to-column min-ind t)
  21305. (insert ": ")
  21306. (forward-line))))))
  21307. (t
  21308. (org-move-to-column min-ind t)
  21309. (insert ": ")
  21310. (forward-line)))))))
  21311. (set-marker end nil))))
  21312. ;;; Comments
  21313. ;; Org comments syntax is quite complex. It requires the entire line
  21314. ;; to be just a comment. Also, even with the right syntax at the
  21315. ;; beginning of line, some some elements (i.e. verse-block or
  21316. ;; example-block) don't accept comments. Usual Emacs comment commands
  21317. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21318. ;; Org still relies on `comment-dwim', but cannot trust
  21319. ;; `comment-only-p'. So, `comment-region-function' and
  21320. ;; `uncomment-region-function' both point
  21321. ;; to`org-comment-or-uncomment-region'. Eventually,
  21322. ;; `org-insert-comment' takes care of insertion of comments at the
  21323. ;; beginning of line.
  21324. ;; `org-setup-comments-handling' install comments related variables
  21325. ;; during `org-mode' initialization.
  21326. (defun org-setup-comments-handling ()
  21327. (interactive)
  21328. (org-set-local 'comment-use-syntax nil)
  21329. (org-set-local 'comment-start "# ")
  21330. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21331. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21332. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21333. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21334. (defun org-insert-comment ()
  21335. "Insert an empty comment above current line.
  21336. If the line is empty, insert comment at its beginning. When
  21337. point is within a source block, comment according to the related
  21338. major mode."
  21339. (if (let ((element (org-element-at-point)))
  21340. (and (eq (org-element-type element) 'src-block)
  21341. (< (save-excursion
  21342. (goto-char (org-element-property :post-affiliated element))
  21343. (line-end-position))
  21344. (point))
  21345. (> (save-excursion
  21346. (goto-char (org-element-property :end element))
  21347. (skip-chars-backward " \r\t\n")
  21348. (line-beginning-position))
  21349. (point))))
  21350. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21351. (beginning-of-line)
  21352. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21353. (open-line 1))
  21354. (org-indent-line)
  21355. (insert "# ")))
  21356. (defvar comment-empty-lines) ; From newcomment.el.
  21357. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21358. "Comment or uncomment each non-blank line in the region.
  21359. Uncomment each non-blank line between BEG and END if it only
  21360. contains commented lines. Otherwise, comment them. If region is
  21361. strictly within a source block, use appropriate comment syntax."
  21362. (if (let ((element (org-element-at-point)))
  21363. (and (eq (org-element-type element) 'src-block)
  21364. (< (save-excursion
  21365. (goto-char (org-element-property :post-affiliated element))
  21366. (line-end-position))
  21367. beg)
  21368. (>= (save-excursion
  21369. (goto-char (org-element-property :end element))
  21370. (skip-chars-backward " \r\t\n")
  21371. (line-beginning-position))
  21372. end)))
  21373. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21374. (save-restriction
  21375. ;; Restrict region
  21376. (narrow-to-region (save-excursion (goto-char beg)
  21377. (skip-chars-forward " \r\t\n" end)
  21378. (line-beginning-position))
  21379. (save-excursion (goto-char end)
  21380. (skip-chars-backward " \r\t\n" beg)
  21381. (line-end-position)))
  21382. (let ((uncommentp
  21383. ;; UNCOMMENTP is non-nil when every non blank line between
  21384. ;; BEG and END is a comment.
  21385. (save-excursion
  21386. (goto-char (point-min))
  21387. (while (and (not (eobp))
  21388. (let ((element (org-element-at-point)))
  21389. (and (eq (org-element-type element) 'comment)
  21390. (goto-char (min (point-max)
  21391. (org-element-property
  21392. :end element)))))))
  21393. (eobp))))
  21394. (if uncommentp
  21395. ;; Only blank lines and comments in region: uncomment it.
  21396. (save-excursion
  21397. (goto-char (point-min))
  21398. (while (not (eobp))
  21399. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21400. (replace-match "" nil nil nil 1))
  21401. (forward-line)))
  21402. ;; Comment each line in region.
  21403. (let ((min-indent (point-max)))
  21404. ;; First find the minimum indentation across all lines.
  21405. (save-excursion
  21406. (goto-char (point-min))
  21407. (while (and (not (eobp)) (not (zerop min-indent)))
  21408. (unless (looking-at "[ \t]*$")
  21409. (setq min-indent (min min-indent (current-indentation))))
  21410. (forward-line)))
  21411. ;; Then loop over all lines.
  21412. (save-excursion
  21413. (goto-char (point-min))
  21414. (while (not (eobp))
  21415. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21416. ;; Don't get fooled by invisible text (e.g. link path)
  21417. ;; when moving to column MIN-INDENT.
  21418. (let ((buffer-invisibility-spec nil))
  21419. (org-move-to-column min-indent t))
  21420. (insert comment-start))
  21421. (forward-line)))))))))
  21422. (defun org-comment-dwim (arg)
  21423. "Call `comment-dwim' within a source edit buffer if needed."
  21424. (interactive "*P")
  21425. (if (org-in-src-block-p)
  21426. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21427. (call-interactively 'comment-dwim)))
  21428. ;;; Planning
  21429. ;; This section contains tools to operate on timestamp objects, as
  21430. ;; returned by, e.g. `org-element-context'.
  21431. (defun org-timestamp-has-time-p (timestamp)
  21432. "Non-nil when TIMESTAMP has a time specified."
  21433. (org-element-property :hour-start timestamp))
  21434. (defun org-timestamp-format (timestamp format &optional end utc)
  21435. "Format a TIMESTAMP element into a string.
  21436. FORMAT is a format specifier to be passed to
  21437. `format-time-string'.
  21438. When optional argument END is non-nil, use end of date-range or
  21439. time-range, if possible.
  21440. When optional argument UTC is non-nil, time will be expressed as
  21441. Universal Time."
  21442. (format-time-string
  21443. format
  21444. (apply 'encode-time
  21445. (cons 0
  21446. (mapcar
  21447. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21448. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21449. '(:minute-start :hour-start :day-start :month-start
  21450. :year-start)))))
  21451. utc))
  21452. (defun org-timestamp-split-range (timestamp &optional end)
  21453. "Extract a timestamp object from a date or time range.
  21454. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21455. the end of the range. Otherwise, extract its start.
  21456. Return a new timestamp object sharing the same parent as
  21457. TIMESTAMP."
  21458. (let ((type (org-element-property :type timestamp)))
  21459. (if (memq type '(active inactive diary)) timestamp
  21460. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21461. ;; Set new type.
  21462. (org-element-put-property
  21463. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21464. ;; Copy start properties over end properties if END is
  21465. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21466. (let ((p-alist '((:minute-start . :minute-end)
  21467. (:hour-start . :hour-end)
  21468. (:day-start . :day-end)
  21469. (:month-start . :month-end)
  21470. (:year-start . :year-end))))
  21471. (dolist (p-cell p-alist)
  21472. (org-element-put-property
  21473. split-ts
  21474. (funcall (if end 'car 'cdr) p-cell)
  21475. (org-element-property
  21476. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21477. ;; Eventually refresh `:raw-value'.
  21478. (org-element-put-property split-ts :raw-value nil)
  21479. (org-element-put-property
  21480. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21481. (defun org-timestamp-translate (timestamp &optional boundary)
  21482. "Apply `org-translate-time' on a TIMESTAMP object.
  21483. When optional argument BOUNDARY is non-nil, it is either the
  21484. symbol `start' or `end'. In this case, only translate the
  21485. starting or ending part of TIMESTAMP if it is a date or time
  21486. range. Otherwise, translate both parts."
  21487. (if (and (not boundary)
  21488. (memq (org-element-property :type timestamp)
  21489. '(active-range inactive-range)))
  21490. (concat
  21491. (org-translate-time
  21492. (org-element-property :raw-value
  21493. (org-timestamp-split-range timestamp)))
  21494. "--"
  21495. (org-translate-time
  21496. (org-element-property :raw-value
  21497. (org-timestamp-split-range timestamp t))))
  21498. (org-translate-time
  21499. (org-element-property
  21500. :raw-value
  21501. (if (not boundary) timestamp
  21502. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21503. ;;; Other stuff.
  21504. (defun org-reftex-citation ()
  21505. "Use reftex-citation to insert a citation into the buffer.
  21506. This looks for a line like
  21507. #+BIBLIOGRAPHY: foo plain option:-d
  21508. and derives from it that foo.bib is the bibliography file relevant
  21509. for this document. It then installs the necessary environment for RefTeX
  21510. to work in this buffer and calls `reftex-citation' to insert a citation
  21511. into the buffer.
  21512. Export of such citations to both LaTeX and HTML is handled by the contributed
  21513. package ox-bibtex by Taru Karttunen."
  21514. (interactive)
  21515. (let ((reftex-docstruct-symbol 'rds)
  21516. (reftex-cite-format "\\cite{%l}")
  21517. rds bib)
  21518. (save-excursion
  21519. (save-restriction
  21520. (widen)
  21521. (let ((case-fold-search t)
  21522. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21523. (if (not (save-excursion
  21524. (or (re-search-forward re nil t)
  21525. (re-search-backward re nil t))))
  21526. (user-error "No bibliography defined in file")
  21527. (setq bib (concat (match-string 1) ".bib")
  21528. rds (list (list 'bib bib)))))))
  21529. (call-interactively 'reftex-citation)))
  21530. ;;;; Functions extending outline functionality
  21531. (defun org-beginning-of-line (&optional arg)
  21532. "Go to the beginning of the current line. If that is invisible, continue
  21533. to a visible line beginning. This makes the function of C-a more intuitive.
  21534. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21535. first attempt, and only move to after the tags when the cursor is already
  21536. beyond the end of the headline."
  21537. (interactive "P")
  21538. (let ((pos (point))
  21539. (special (if (consp org-special-ctrl-a/e)
  21540. (car org-special-ctrl-a/e)
  21541. org-special-ctrl-a/e))
  21542. deactivate-mark refpos)
  21543. (if (org-bound-and-true-p visual-line-mode)
  21544. (beginning-of-visual-line 1)
  21545. (beginning-of-line 1))
  21546. (if (and arg (fboundp 'move-beginning-of-line))
  21547. (call-interactively 'move-beginning-of-line)
  21548. (if (bobp)
  21549. nil
  21550. (backward-char 1)
  21551. (if (org-truely-invisible-p)
  21552. (while (and (not (bobp)) (org-truely-invisible-p))
  21553. (backward-char 1)
  21554. (beginning-of-line 1))
  21555. (forward-char 1))))
  21556. (when special
  21557. (cond
  21558. ((and (looking-at org-complex-heading-regexp)
  21559. (= (char-after (match-end 1)) ?\ ))
  21560. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21561. (point-at-eol)))
  21562. (goto-char
  21563. (if (eq special t)
  21564. (cond ((> pos refpos) refpos)
  21565. ((= pos (point)) refpos)
  21566. (t (point)))
  21567. (cond ((> pos (point)) (point))
  21568. ((not (eq last-command this-command)) (point))
  21569. (t refpos)))))
  21570. ((org-at-item-p)
  21571. ;; Being at an item and not looking at an the item means point
  21572. ;; was previously moved to beginning of a visual line, which
  21573. ;; doesn't contain the item. Therefore, do nothing special,
  21574. ;; just stay here.
  21575. (when (looking-at org-list-full-item-re)
  21576. ;; Set special position at first white space character after
  21577. ;; bullet, and check-box, if any.
  21578. (let ((after-bullet
  21579. (let ((box (match-end 3)))
  21580. (if (not box) (match-end 1)
  21581. (let ((after (char-after box)))
  21582. (if (and after (= after ? )) (1+ box) box))))))
  21583. ;; Special case: Move point to special position when
  21584. ;; currently after it or at beginning of line.
  21585. (if (eq special t)
  21586. (when (or (> pos after-bullet) (= (point) pos))
  21587. (goto-char after-bullet))
  21588. ;; Reversed case: Move point to special position when
  21589. ;; point was already at beginning of line and command is
  21590. ;; repeated.
  21591. (when (and (= (point) pos) (eq last-command this-command))
  21592. (goto-char after-bullet))))))))
  21593. (org-no-warnings
  21594. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21595. (setq disable-point-adjustment
  21596. (or (not (invisible-p (point)))
  21597. (not (invisible-p (max (point-min) (1- (point))))))))
  21598. (defun org-end-of-line (&optional arg)
  21599. "Go to the end of the line.
  21600. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21601. tags on the first attempt, and only move to after the tags when
  21602. the cursor is already beyond the end of the headline."
  21603. (interactive "P")
  21604. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21605. org-special-ctrl-a/e))
  21606. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21607. 'end-of-visual-line)
  21608. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21609. (t 'end-of-line)))
  21610. deactivate-mark)
  21611. (if (or (not special) arg) (call-interactively move-fun)
  21612. (let* ((element (save-excursion (beginning-of-line)
  21613. (org-element-at-point)))
  21614. (type (org-element-type element)))
  21615. (cond
  21616. ((memq type '(headline inlinetask))
  21617. (let ((pos (point)))
  21618. (beginning-of-line 1)
  21619. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21620. (if (eq special t)
  21621. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21622. (goto-char (match-beginning 1))
  21623. (goto-char (match-end 0)))
  21624. (if (or (< pos (match-end 0))
  21625. (not (eq this-command last-command)))
  21626. (goto-char (match-end 0))
  21627. (goto-char (match-beginning 1))))
  21628. (call-interactively move-fun))))
  21629. ((outline-invisible-p (line-end-position))
  21630. ;; If element is hidden, `move-end-of-line' would put point
  21631. ;; after it. Use `end-of-line' to stay on current line.
  21632. (call-interactively 'end-of-line))
  21633. (t (call-interactively move-fun)))))
  21634. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21635. (setq disable-point-adjustment
  21636. (or (not (invisible-p (point)))
  21637. (not (invisible-p (max (point-min) (1- (point))))))))
  21638. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21639. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21640. (defun org-backward-sentence (&optional arg)
  21641. "Go to beginning of sentence, or beginning of table field.
  21642. This will call `backward-sentence' or `org-table-beginning-of-field',
  21643. depending on context."
  21644. (interactive "P")
  21645. (cond
  21646. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21647. (t (call-interactively 'backward-sentence))))
  21648. (defun org-forward-sentence (&optional arg)
  21649. "Go to end of sentence, or end of table field.
  21650. This will call `forward-sentence' or `org-table-end-of-field',
  21651. depending on context."
  21652. (interactive "P")
  21653. (cond
  21654. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21655. (t (call-interactively 'forward-sentence))))
  21656. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21657. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21658. (defun org-kill-line (&optional arg)
  21659. "Kill line, to tags or end of line."
  21660. (interactive "P")
  21661. (cond
  21662. ((or (not org-special-ctrl-k)
  21663. (bolp)
  21664. (not (org-at-heading-p)))
  21665. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21666. org-ctrl-k-protect-subtree)
  21667. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21668. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21669. (user-error "C-k aborted as it would kill a hidden subtree")))
  21670. (call-interactively
  21671. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21672. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21673. (kill-region (point) (match-beginning 1))
  21674. (org-set-tags nil t))
  21675. (t (kill-region (point) (point-at-eol)))))
  21676. (define-key org-mode-map "\C-k" 'org-kill-line)
  21677. (defun org-yank (&optional arg)
  21678. "Yank. If the kill is a subtree, treat it specially.
  21679. This command will look at the current kill and check if is a single
  21680. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21681. cursor is at the beginning of a line or after the stars of a currently
  21682. empty headline, then the yank is handled specially. How exactly depends
  21683. on the value of the following variables, both set by default.
  21684. `org-yank-folded-subtrees'
  21685. When set, the subtree(s) will be folded after insertion, but only
  21686. if doing so would now swallow text after the yanked text.
  21687. `org-yank-adjusted-subtrees'
  21688. When set, the subtree will be promoted or demoted in order to
  21689. fit into the local outline tree structure, which means that the
  21690. level will be adjusted so that it becomes the smaller one of the
  21691. two *visible* surrounding headings.
  21692. Any prefix to this command will cause `yank' to be called directly with
  21693. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21694. will just
  21695. plainly yank the text as it is.
  21696. \[1] The test checks if the first non-white line is a heading
  21697. and if there are no other headings with fewer stars."
  21698. (interactive "P")
  21699. (org-yank-generic 'yank arg))
  21700. (defun org-yank-generic (command arg)
  21701. "Perform some yank-like command.
  21702. This function implements the behavior described in the `org-yank'
  21703. documentation. However, it has been generalized to work for any
  21704. interactive command with similar behavior."
  21705. ;; pretend to be command COMMAND
  21706. (setq this-command command)
  21707. (if arg
  21708. (call-interactively command)
  21709. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21710. (and (org-kill-is-subtree-p)
  21711. (or (bolp)
  21712. (and (looking-at "[ \t]*$")
  21713. (string-match
  21714. "\\`\\*+\\'"
  21715. (buffer-substring (point-at-bol) (point)))))))
  21716. swallowp)
  21717. (cond
  21718. ((and subtreep org-yank-folded-subtrees)
  21719. (let ((beg (point))
  21720. end)
  21721. (if (and subtreep org-yank-adjusted-subtrees)
  21722. (org-paste-subtree nil nil 'for-yank)
  21723. (call-interactively command))
  21724. (setq end (point))
  21725. (goto-char beg)
  21726. (when (and (bolp) subtreep
  21727. (not (setq swallowp
  21728. (org-yank-folding-would-swallow-text beg end))))
  21729. (org-with-limited-levels
  21730. (or (looking-at org-outline-regexp)
  21731. (re-search-forward org-outline-regexp-bol end t))
  21732. (while (and (< (point) end) (looking-at org-outline-regexp))
  21733. (hide-subtree)
  21734. (org-cycle-show-empty-lines 'folded)
  21735. (condition-case nil
  21736. (outline-forward-same-level 1)
  21737. (error (goto-char end))))))
  21738. (when swallowp
  21739. (message
  21740. "Inserted text not folded because that would swallow text"))
  21741. (goto-char end)
  21742. (skip-chars-forward " \t\n\r")
  21743. (beginning-of-line 1)
  21744. (push-mark beg 'nomsg)))
  21745. ((and subtreep org-yank-adjusted-subtrees)
  21746. (let ((beg (point-at-bol)))
  21747. (org-paste-subtree nil nil 'for-yank)
  21748. (push-mark beg 'nomsg)))
  21749. (t
  21750. (call-interactively command))))))
  21751. (defun org-yank-folding-would-swallow-text (beg end)
  21752. "Would hide-subtree at BEG swallow any text after END?"
  21753. (let (level)
  21754. (org-with-limited-levels
  21755. (save-excursion
  21756. (goto-char beg)
  21757. (when (or (looking-at org-outline-regexp)
  21758. (re-search-forward org-outline-regexp-bol end t))
  21759. (setq level (org-outline-level)))
  21760. (goto-char end)
  21761. (skip-chars-forward " \t\r\n\v\f")
  21762. (if (or (eobp)
  21763. (and (bolp) (looking-at org-outline-regexp)
  21764. (<= (org-outline-level) level)))
  21765. nil ; Nothing would be swallowed
  21766. t))))) ; something would swallow
  21767. (define-key org-mode-map "\C-y" 'org-yank)
  21768. (defun org-truely-invisible-p ()
  21769. "Check if point is at a character currently not visible.
  21770. This version does not only check the character property, but also
  21771. `visible-mode'."
  21772. ;; Early versions of noutline don't have `outline-invisible-p'.
  21773. (if (org-bound-and-true-p visible-mode)
  21774. nil
  21775. (outline-invisible-p)))
  21776. (defun org-invisible-p2 ()
  21777. "Check if point is at a character currently not visible."
  21778. (save-excursion
  21779. (if (and (eolp) (not (bobp))) (backward-char 1))
  21780. ;; Early versions of noutline don't have `outline-invisible-p'.
  21781. (outline-invisible-p)))
  21782. (defun org-back-to-heading (&optional invisible-ok)
  21783. "Call `outline-back-to-heading', but provide a better error message."
  21784. (condition-case nil
  21785. (outline-back-to-heading invisible-ok)
  21786. (error (error "Before first headline at position %d in buffer %s"
  21787. (point) (current-buffer)))))
  21788. (defun org-before-first-heading-p ()
  21789. "Before first heading?"
  21790. (save-excursion
  21791. (end-of-line)
  21792. (null (re-search-backward org-outline-regexp-bol nil t))))
  21793. (defun org-at-heading-p (&optional ignored)
  21794. (outline-on-heading-p t))
  21795. ;; Compatibility alias with Org versions < 7.8.03
  21796. (defalias 'org-on-heading-p 'org-at-heading-p)
  21797. (defun org-in-commented-heading-p (&optional no-inheritance)
  21798. "Non-nil if point is under a commented heading.
  21799. This function also checks ancestors of the current headline,
  21800. unless optional argument NO-INHERITANCE is non-nil."
  21801. (cond
  21802. ((org-before-first-heading-p) nil)
  21803. ((let ((headline (nth 4 (org-heading-components))))
  21804. (and headline
  21805. (let ((case-fold-search nil))
  21806. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21807. headline)))))
  21808. (no-inheritance nil)
  21809. (t
  21810. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21811. (defun org-at-comment-p nil
  21812. "Is cursor in a commented line?"
  21813. (save-excursion
  21814. (save-match-data
  21815. (beginning-of-line)
  21816. (looking-at "^[ \t]*# "))))
  21817. (defun org-at-drawer-p nil
  21818. "Is cursor at a drawer keyword?"
  21819. (save-excursion
  21820. (move-beginning-of-line 1)
  21821. (looking-at org-drawer-regexp)))
  21822. (defun org-at-block-p nil
  21823. "Is cursor at a block keyword?"
  21824. (save-excursion
  21825. (move-beginning-of-line 1)
  21826. (looking-at org-block-regexp)))
  21827. (defun org-point-at-end-of-empty-headline ()
  21828. "If point is at the end of an empty headline, return t, else nil.
  21829. If the heading only contains a TODO keyword, it is still still considered
  21830. empty."
  21831. (and (looking-at "[ \t]*$")
  21832. (when org-todo-line-regexp
  21833. (save-excursion
  21834. (beginning-of-line 1)
  21835. (let ((case-fold-search nil))
  21836. (looking-at org-todo-line-regexp)
  21837. (string= (match-string 3) ""))))))
  21838. (defun org-at-heading-or-item-p ()
  21839. (or (org-at-heading-p) (org-at-item-p)))
  21840. (defun org-at-target-p ()
  21841. (or (org-in-regexp org-radio-target-regexp)
  21842. (org-in-regexp org-target-regexp)))
  21843. ;; Compatibility alias with Org versions < 7.8.03
  21844. (defalias 'org-on-target-p 'org-at-target-p)
  21845. (defun org-up-heading-all (arg)
  21846. "Move to the heading line of which the present line is a subheading.
  21847. This function considers both visible and invisible heading lines.
  21848. With argument, move up ARG levels."
  21849. (if (fboundp 'outline-up-heading-all)
  21850. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21851. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21852. (defun org-up-heading-safe ()
  21853. "Move to the heading line of which the present line is a subheading.
  21854. This version will not throw an error. It will return the level of the
  21855. headline found, or nil if no higher level is found.
  21856. Also, this function will be a lot faster than `outline-up-heading',
  21857. because it relies on stars being the outline starters. This can really
  21858. make a significant difference in outlines with very many siblings."
  21859. (when (ignore-errors (org-back-to-heading t))
  21860. (let ((level-up (1- (funcall outline-level))))
  21861. (and (> level-up 0)
  21862. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21863. (funcall outline-level)))))
  21864. (defun org-first-sibling-p ()
  21865. "Is this heading the first child of its parents?"
  21866. (interactive)
  21867. (let ((re org-outline-regexp-bol)
  21868. level l)
  21869. (unless (org-at-heading-p t)
  21870. (user-error "Not at a heading"))
  21871. (setq level (funcall outline-level))
  21872. (save-excursion
  21873. (if (not (re-search-backward re nil t))
  21874. t
  21875. (setq l (funcall outline-level))
  21876. (< l level)))))
  21877. (defun org-goto-sibling (&optional previous)
  21878. "Goto the next sibling, even if it is invisible.
  21879. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21880. when a sibling was found. When none is found, return nil and don't
  21881. move point."
  21882. (let ((fun (if previous 're-search-backward 're-search-forward))
  21883. (pos (point))
  21884. (re org-outline-regexp-bol)
  21885. level l)
  21886. (when (ignore-errors (org-back-to-heading t))
  21887. (setq level (funcall outline-level))
  21888. (catch 'exit
  21889. (or previous (forward-char 1))
  21890. (while (funcall fun re nil t)
  21891. (setq l (funcall outline-level))
  21892. (when (< l level) (goto-char pos) (throw 'exit nil))
  21893. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21894. (goto-char pos)
  21895. nil))))
  21896. (defun org-show-siblings ()
  21897. "Show all siblings of the current headline."
  21898. (save-excursion
  21899. (while (org-goto-sibling) (org-flag-heading nil)))
  21900. (save-excursion
  21901. (while (org-goto-sibling 'previous)
  21902. (org-flag-heading nil))))
  21903. (defun org-goto-first-child ()
  21904. "Goto the first child, even if it is invisible.
  21905. Return t when a child was found. Otherwise don't move point and
  21906. return nil."
  21907. (let (level (pos (point)) (re org-outline-regexp-bol))
  21908. (when (ignore-errors (org-back-to-heading t))
  21909. (setq level (outline-level))
  21910. (forward-char 1)
  21911. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21912. (progn (goto-char (match-beginning 0)) t)
  21913. (goto-char pos) nil))))
  21914. (defun org-show-hidden-entry ()
  21915. "Show an entry where even the heading is hidden."
  21916. (save-excursion
  21917. (org-show-entry)))
  21918. (defun org-flag-heading (flag &optional entry)
  21919. "Flag the current heading. FLAG non-nil means make invisible.
  21920. When ENTRY is non-nil, show the entire entry."
  21921. (save-excursion
  21922. (org-back-to-heading t)
  21923. ;; Check if we should show the entire entry
  21924. (if entry
  21925. (progn
  21926. (org-show-entry)
  21927. (save-excursion
  21928. (and (outline-next-heading)
  21929. (org-flag-heading nil))))
  21930. (outline-flag-region (max (point-min) (1- (point)))
  21931. (save-excursion (outline-end-of-heading) (point))
  21932. flag))))
  21933. (defun org-get-next-sibling ()
  21934. "Move to next heading of the same level, and return point.
  21935. If there is no such heading, return nil.
  21936. This is like outline-next-sibling, but invisible headings are ok."
  21937. (let ((level (funcall outline-level)))
  21938. (outline-next-heading)
  21939. (while (and (not (eobp)) (> (funcall outline-level) level))
  21940. (outline-next-heading))
  21941. (if (or (eobp) (< (funcall outline-level) level))
  21942. nil
  21943. (point))))
  21944. (defun org-get-last-sibling ()
  21945. "Move to previous heading of the same level, and return point.
  21946. If there is no such heading, return nil."
  21947. (let ((opoint (point))
  21948. (level (funcall outline-level)))
  21949. (outline-previous-heading)
  21950. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21951. (while (and (> (funcall outline-level) level)
  21952. (not (bobp)))
  21953. (outline-previous-heading))
  21954. (if (< (funcall outline-level) level)
  21955. nil
  21956. (point)))))
  21957. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21958. "Goto to the end of a subtree."
  21959. ;; This contains an exact copy of the original function, but it uses
  21960. ;; `org-back-to-heading', to make it work also in invisible
  21961. ;; trees. And is uses an invisible-ok argument.
  21962. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21963. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21964. ;; with many children and grandchildren etc, this can be much faster
  21965. ;; than the outline version.
  21966. (org-back-to-heading invisible-ok)
  21967. (let ((first t)
  21968. (level (funcall outline-level)))
  21969. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21970. ;; A true heading (not a plain list item), in Org-mode
  21971. ;; This means we can easily find the end by looking
  21972. ;; only for the right number of stars. Using a regexp to do
  21973. ;; this is so much faster than using a Lisp loop.
  21974. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21975. (forward-char 1)
  21976. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21977. ;; something else, do it the slow way
  21978. (while (and (not (eobp))
  21979. (or first (> (funcall outline-level) level)))
  21980. (setq first nil)
  21981. (outline-next-heading)))
  21982. (unless to-heading
  21983. (if (memq (preceding-char) '(?\n ?\^M))
  21984. (progn
  21985. ;; Go to end of line before heading
  21986. (forward-char -1)
  21987. (if (memq (preceding-char) '(?\n ?\^M))
  21988. ;; leave blank line before heading
  21989. (forward-char -1))))))
  21990. (point))
  21991. (defun org-end-of-meta-data-and-drawers ()
  21992. "Jump to the first text after meta data and drawers in the current entry.
  21993. This will move over empty lines, lines with planning time stamps,
  21994. clocking lines, and drawers."
  21995. (org-back-to-heading t)
  21996. (let ((end (save-excursion (outline-next-heading) (point)))
  21997. (re (concat "\\(" org-drawer-regexp "\\)"
  21998. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21999. (forward-line 1)
  22000. (while (re-search-forward re end t)
  22001. (if (not (match-end 1))
  22002. ;; empty or planning line
  22003. (forward-line 1)
  22004. ;; a drawer, find the end
  22005. (re-search-forward "^[ \t]*:END:" end 'move)
  22006. (forward-line 1)))
  22007. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  22008. (point)))
  22009. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22010. "Move forward to the ARG'th subheading at same level as this one.
  22011. Stop at the first and last subheadings of a superior heading.
  22012. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22013. non-nil it will also look at invisible ones."
  22014. (interactive "p")
  22015. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22016. (if (and arg (< arg 0))
  22017. (goto-char (point-min))
  22018. (outline-next-heading))
  22019. (org-at-heading-p)
  22020. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22021. (f (if (and arg (< arg 0))
  22022. 're-search-backward
  22023. 're-search-forward))
  22024. (count (if arg (abs arg) 1))
  22025. (result (point)))
  22026. (while (and (prog1 (> count 0)
  22027. (forward-char (if (and arg (< arg 0)) -1 1)))
  22028. (funcall f org-outline-regexp-bol nil 'move))
  22029. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22030. (cond ((< l level) (setq count 0))
  22031. ((and (= l level)
  22032. (or invisible-ok
  22033. (progn
  22034. (goto-char (line-beginning-position))
  22035. (not (outline-invisible-p)))))
  22036. (setq count (1- count))
  22037. (when (eq l level)
  22038. (setq result (point)))))))
  22039. (goto-char result))
  22040. (beginning-of-line 1)))
  22041. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22042. "Move backward to the ARG'th subheading at same level as this one.
  22043. Stop at the first and last subheadings of a superior heading."
  22044. (interactive "p")
  22045. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22046. (defun org-next-block (arg &optional backward block-regexp)
  22047. "Jump to the next block.
  22048. With a prefix argument ARG, jump forward ARG many source blocks.
  22049. When BACKWARD is non-nil, jump to the previous block.
  22050. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22051. (interactive "p")
  22052. (let ((re (or block-regexp org-block-regexp))
  22053. (re-search-fn (or (and backward 're-search-backward)
  22054. 're-search-forward)))
  22055. (if (looking-at re) (forward-char 1))
  22056. (condition-case nil
  22057. (funcall re-search-fn re nil nil arg)
  22058. (error (user-error "No %s code blocks"
  22059. (if backward "previous" "further" ))))
  22060. (goto-char (match-beginning 0)) (org-show-context)))
  22061. (defun org-previous-block (arg &optional block-regexp)
  22062. "Jump to the previous block.
  22063. With a prefix argument ARG, jump backward ARG many source blocks.
  22064. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22065. (interactive "p")
  22066. (org-next-block arg t block-regexp))
  22067. (defun org-forward-paragraph ()
  22068. "Move forward to beginning of next paragraph or equivalent.
  22069. The function moves point to the beginning of the next visible
  22070. structural element, which can be a paragraph, a table, a list
  22071. item, etc. It also provides some special moves for convenience:
  22072. - On an affiliated keyword, jump to the beginning of the
  22073. relative element.
  22074. - On an item or a footnote definition, move to the second
  22075. element inside, if any.
  22076. - On a table or a property drawer, jump after it.
  22077. - On a verse or source block, stop after blank lines."
  22078. (interactive)
  22079. (when (eobp) (user-error "Cannot move further down"))
  22080. (let* ((deactivate-mark nil)
  22081. (element (org-element-at-point))
  22082. (type (org-element-type element))
  22083. (post-affiliated (org-element-property :post-affiliated element))
  22084. (contents-begin (org-element-property :contents-begin element))
  22085. (contents-end (org-element-property :contents-end element))
  22086. (end (let ((end (org-element-property :end element)) (parent element))
  22087. (while (and (setq parent (org-element-property :parent parent))
  22088. (= (org-element-property :contents-end parent) end))
  22089. (setq end (org-element-property :end parent)))
  22090. end)))
  22091. (cond ((not element)
  22092. (skip-chars-forward " \r\t\n")
  22093. (or (eobp) (beginning-of-line)))
  22094. ;; On affiliated keywords, move to element's beginning.
  22095. ((< (point) post-affiliated)
  22096. (goto-char post-affiliated))
  22097. ;; At a table row, move to the end of the table. Similarly,
  22098. ;; at a node property, move to the end of the property
  22099. ;; drawer.
  22100. ((memq type '(node-property table-row))
  22101. (goto-char (org-element-property
  22102. :end (org-element-property :parent element))))
  22103. ((memq type '(property-drawer table)) (goto-char end))
  22104. ;; Consider blank lines as separators in verse and source
  22105. ;; blocks to ease editing.
  22106. ((memq type '(src-block verse-block))
  22107. (when (eq type 'src-block)
  22108. (setq contents-end
  22109. (save-excursion (goto-char end)
  22110. (skip-chars-backward " \r\t\n")
  22111. (line-beginning-position))))
  22112. (beginning-of-line)
  22113. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22114. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22115. (goto-char end)
  22116. (skip-chars-forward " \r\t\n")
  22117. (if (= (point) contents-end) (goto-char end)
  22118. (beginning-of-line))))
  22119. ;; With no contents, just skip element.
  22120. ((not contents-begin) (goto-char end))
  22121. ;; If contents are invisible, skip the element altogether.
  22122. ((outline-invisible-p (line-end-position))
  22123. (case type
  22124. (headline
  22125. (org-with-limited-levels (outline-next-visible-heading 1)))
  22126. ;; At a plain list, make sure we move to the next item
  22127. ;; instead of skipping the whole list.
  22128. (plain-list (forward-char)
  22129. (org-forward-paragraph))
  22130. (otherwise (goto-char end))))
  22131. ((>= (point) contents-end) (goto-char end))
  22132. ((>= (point) contents-begin)
  22133. ;; This can only happen on paragraphs and plain lists.
  22134. (case type
  22135. (paragraph (goto-char end))
  22136. ;; At a plain list, try to move to second element in
  22137. ;; first item, if possible.
  22138. (plain-list (end-of-line)
  22139. (org-forward-paragraph))))
  22140. ;; When contents start on the middle of a line (e.g. in
  22141. ;; items and footnote definitions), try to reach first
  22142. ;; element starting after current line.
  22143. ((> (line-end-position) contents-begin)
  22144. (end-of-line)
  22145. (org-forward-paragraph))
  22146. (t (goto-char contents-begin)))))
  22147. (defun org-backward-paragraph ()
  22148. "Move backward to start of previous paragraph or equivalent.
  22149. The function moves point to the beginning of the current
  22150. structural element, which can be a paragraph, a table, a list
  22151. item, etc., or to the beginning of the previous visible one if
  22152. point is already there. It also provides some special moves for
  22153. convenience:
  22154. - On an affiliated keyword, jump to the first one.
  22155. - On a table or a property drawer, move to its beginning.
  22156. - On a verse or source block, stop before blank lines."
  22157. (interactive)
  22158. (when (bobp) (user-error "Cannot move further up"))
  22159. (let* ((deactivate-mark nil)
  22160. (element (org-element-at-point))
  22161. (type (org-element-type element))
  22162. (contents-begin (org-element-property :contents-begin element))
  22163. (contents-end (org-element-property :contents-end element))
  22164. (post-affiliated (org-element-property :post-affiliated element))
  22165. (begin (org-element-property :begin element)))
  22166. (cond
  22167. ((not element) (goto-char (point-min)))
  22168. ((= (point) begin)
  22169. (backward-char)
  22170. (org-backward-paragraph))
  22171. ((<= (point) post-affiliated) (goto-char begin))
  22172. ((memq type '(node-property table-row))
  22173. (goto-char (org-element-property
  22174. :post-affiliated (org-element-property :parent element))))
  22175. ((memq type '(property-drawer table)) (goto-char begin))
  22176. ((memq type '(src-block verse-block))
  22177. (when (eq type 'src-block)
  22178. (setq contents-begin
  22179. (save-excursion (goto-char begin) (forward-line) (point))))
  22180. (if (= (point) contents-begin) (goto-char post-affiliated)
  22181. ;; Inside a verse block, see blank lines as paragraph
  22182. ;; separators.
  22183. (let ((origin (point)))
  22184. (skip-chars-backward " \r\t\n" contents-begin)
  22185. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22186. (skip-chars-forward " \r\t\n" origin)
  22187. (if (= (point) origin) (goto-char contents-begin)
  22188. (beginning-of-line))))))
  22189. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22190. ((eq type 'paragraph)
  22191. (goto-char contents-begin)
  22192. ;; When at first paragraph in an item or a footnote definition,
  22193. ;; move directly to beginning of line.
  22194. (let ((parent-contents
  22195. (org-element-property
  22196. :contents-begin (org-element-property :parent element))))
  22197. (when (and parent-contents (= parent-contents contents-begin))
  22198. (beginning-of-line))))
  22199. ;; At the end of a greater element, move to the beginning of the
  22200. ;; last element within.
  22201. ((>= (point) contents-end)
  22202. (goto-char (1- contents-end))
  22203. (org-backward-paragraph))
  22204. (t (goto-char (or post-affiliated begin))))
  22205. ;; Ensure we never leave point invisible.
  22206. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22207. (defun org-forward-element ()
  22208. "Move forward by one element.
  22209. Move to the next element at the same level, when possible."
  22210. (interactive)
  22211. (cond ((eobp) (user-error "Cannot move further down"))
  22212. ((org-with-limited-levels (org-at-heading-p))
  22213. (let ((origin (point)))
  22214. (goto-char (org-end-of-subtree nil t))
  22215. (unless (org-with-limited-levels (org-at-heading-p))
  22216. (goto-char origin)
  22217. (user-error "Cannot move further down"))))
  22218. (t
  22219. (let* ((elem (org-element-at-point))
  22220. (end (org-element-property :end elem))
  22221. (parent (org-element-property :parent elem)))
  22222. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22223. (goto-char (org-element-property :end parent)))
  22224. ((integer-or-marker-p end) (goto-char end))
  22225. (t (message "No element at point")))))))
  22226. (defun org-backward-element ()
  22227. "Move backward by one element.
  22228. Move to the previous element at the same level, when possible."
  22229. (interactive)
  22230. (cond ((bobp) (user-error "Cannot move further up"))
  22231. ((org-with-limited-levels (org-at-heading-p))
  22232. ;; At a headline, move to the previous one, if any, or stay
  22233. ;; here.
  22234. (let ((origin (point)))
  22235. (org-with-limited-levels (org-backward-heading-same-level 1))
  22236. ;; When current headline has no sibling above, move to its
  22237. ;; parent.
  22238. (when (= (point) origin)
  22239. (or (org-with-limited-levels (org-up-heading-safe))
  22240. (progn (goto-char origin)
  22241. (user-error "Cannot move further up"))))))
  22242. (t
  22243. (let* ((elem (org-element-at-point))
  22244. (beg (org-element-property :begin elem)))
  22245. (cond
  22246. ;; Move to beginning of current element if point isn't
  22247. ;; there already.
  22248. ((null beg) (message "No element at point"))
  22249. ((/= (point) beg) (goto-char beg))
  22250. (t (goto-char beg)
  22251. (skip-chars-backward " \r\t\n")
  22252. (unless (bobp)
  22253. (let ((prev (org-element-at-point)))
  22254. (goto-char (org-element-property :begin prev))
  22255. (while (and (setq prev (org-element-property :parent prev))
  22256. (<= (org-element-property :end prev) beg))
  22257. (goto-char (org-element-property :begin prev)))))))))))
  22258. (defun org-up-element ()
  22259. "Move to upper element."
  22260. (interactive)
  22261. (if (org-with-limited-levels (org-at-heading-p))
  22262. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22263. (let* ((elem (org-element-at-point))
  22264. (parent (org-element-property :parent elem)))
  22265. (if parent (goto-char (org-element-property :begin parent))
  22266. (if (org-with-limited-levels (org-before-first-heading-p))
  22267. (user-error "No surrounding element")
  22268. (org-with-limited-levels (org-back-to-heading)))))))
  22269. (defvar org-element-greater-elements)
  22270. (defun org-down-element ()
  22271. "Move to inner element."
  22272. (interactive)
  22273. (let ((element (org-element-at-point)))
  22274. (cond
  22275. ((memq (org-element-type element) '(plain-list table))
  22276. (goto-char (org-element-property :contents-begin element))
  22277. (forward-char))
  22278. ((memq (org-element-type element) org-element-greater-elements)
  22279. ;; If contents are hidden, first disclose them.
  22280. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22281. (goto-char (or (org-element-property :contents-begin element)
  22282. (user-error "No content for this element"))))
  22283. (t (user-error "No inner element")))))
  22284. (defun org-drag-element-backward ()
  22285. "Move backward element at point."
  22286. (interactive)
  22287. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22288. (let* ((elem (org-element-at-point))
  22289. (prev-elem
  22290. (save-excursion
  22291. (goto-char (org-element-property :begin elem))
  22292. (skip-chars-backward " \r\t\n")
  22293. (unless (bobp)
  22294. (let* ((beg (org-element-property :begin elem))
  22295. (prev (org-element-at-point))
  22296. (up prev))
  22297. (while (and (setq up (org-element-property :parent up))
  22298. (<= (org-element-property :end up) beg))
  22299. (setq prev up))
  22300. prev)))))
  22301. ;; Error out if no previous element or previous element is
  22302. ;; a parent of the current one.
  22303. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22304. (user-error "Cannot drag element backward")
  22305. (let ((pos (point)))
  22306. (org-element-swap-A-B prev-elem elem)
  22307. (goto-char (+ (org-element-property :begin prev-elem)
  22308. (- pos (org-element-property :begin elem)))))))))
  22309. (defun org-drag-element-forward ()
  22310. "Move forward element at point."
  22311. (interactive)
  22312. (let* ((pos (point))
  22313. (elem (org-element-at-point)))
  22314. (when (= (point-max) (org-element-property :end elem))
  22315. (user-error "Cannot drag element forward"))
  22316. (goto-char (org-element-property :end elem))
  22317. (let ((next-elem (org-element-at-point)))
  22318. (when (or (org-element-nested-p elem next-elem)
  22319. (and (eq (org-element-type next-elem) 'headline)
  22320. (not (eq (org-element-type elem) 'headline))))
  22321. (goto-char pos)
  22322. (user-error "Cannot drag element forward"))
  22323. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22324. ;; body's length (without final blanks) and by the length of
  22325. ;; blanks between ELEM and NEXT-ELEM.
  22326. (let ((size-next (- (save-excursion
  22327. (goto-char (org-element-property :end next-elem))
  22328. (skip-chars-backward " \r\t\n")
  22329. (forward-line)
  22330. ;; Small correction if buffer doesn't end
  22331. ;; with a newline character.
  22332. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22333. (org-element-property :begin next-elem)))
  22334. (size-blank (- (org-element-property :end elem)
  22335. (save-excursion
  22336. (goto-char (org-element-property :end elem))
  22337. (skip-chars-backward " \r\t\n")
  22338. (forward-line)
  22339. (point)))))
  22340. (org-element-swap-A-B elem next-elem)
  22341. (goto-char (+ pos size-next size-blank))))))
  22342. (defun org-drag-line-forward (arg)
  22343. "Drag the line at point ARG lines forward."
  22344. (interactive "p")
  22345. (dotimes (n (abs arg))
  22346. (let ((c (current-column)))
  22347. (if (< 0 arg)
  22348. (progn
  22349. (beginning-of-line 2)
  22350. (transpose-lines 1)
  22351. (beginning-of-line 0))
  22352. (transpose-lines 1)
  22353. (beginning-of-line -1))
  22354. (org-move-to-column c))))
  22355. (defun org-drag-line-backward (arg)
  22356. "Drag the line at point ARG lines backward."
  22357. (interactive "p")
  22358. (org-drag-line-forward (- arg)))
  22359. (defun org-mark-element ()
  22360. "Put point at beginning of this element, mark at end.
  22361. Interactively, if this command is repeated or (in Transient Mark
  22362. mode) if the mark is active, it marks the next element after the
  22363. ones already marked."
  22364. (interactive)
  22365. (let (deactivate-mark)
  22366. (if (and (org-called-interactively-p 'any)
  22367. (or (and (eq last-command this-command) (mark t))
  22368. (and transient-mark-mode mark-active)))
  22369. (set-mark
  22370. (save-excursion
  22371. (goto-char (mark))
  22372. (goto-char (org-element-property :end (org-element-at-point)))))
  22373. (let ((element (org-element-at-point)))
  22374. (end-of-line)
  22375. (push-mark (org-element-property :end element) t t)
  22376. (goto-char (org-element-property :begin element))))))
  22377. (defun org-narrow-to-element ()
  22378. "Narrow buffer to current element."
  22379. (interactive)
  22380. (let ((elem (org-element-at-point)))
  22381. (cond
  22382. ((eq (car elem) 'headline)
  22383. (narrow-to-region
  22384. (org-element-property :begin elem)
  22385. (org-element-property :end elem)))
  22386. ((memq (car elem) org-element-greater-elements)
  22387. (narrow-to-region
  22388. (org-element-property :contents-begin elem)
  22389. (org-element-property :contents-end elem)))
  22390. (t
  22391. (narrow-to-region
  22392. (org-element-property :begin elem)
  22393. (org-element-property :end elem))))))
  22394. (defun org-transpose-element ()
  22395. "Transpose current and previous elements, keeping blank lines between.
  22396. Point is moved after both elements."
  22397. (interactive)
  22398. (org-skip-whitespace)
  22399. (let ((end (org-element-property :end (org-element-at-point))))
  22400. (org-drag-element-backward)
  22401. (goto-char end)))
  22402. (defun org-unindent-buffer ()
  22403. "Un-indent the visible part of the buffer.
  22404. Relative indentation (between items, inside blocks, etc.) isn't
  22405. modified."
  22406. (interactive)
  22407. (unless (eq major-mode 'org-mode)
  22408. (user-error "Cannot un-indent a buffer not in Org mode"))
  22409. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22410. unindent-tree ; For byte-compiler.
  22411. (unindent-tree
  22412. (function
  22413. (lambda (contents)
  22414. (mapc
  22415. (lambda (element)
  22416. (if (memq (org-element-type element) '(headline section))
  22417. (funcall unindent-tree (org-element-contents element))
  22418. (save-excursion
  22419. (save-restriction
  22420. (narrow-to-region
  22421. (org-element-property :begin element)
  22422. (org-element-property :end element))
  22423. (org-do-remove-indentation)))))
  22424. (reverse contents))))))
  22425. (funcall unindent-tree (org-element-contents parse-tree))))
  22426. (defun org-show-subtree ()
  22427. "Show everything after this heading at deeper levels."
  22428. (interactive)
  22429. (outline-flag-region
  22430. (point)
  22431. (save-excursion
  22432. (org-end-of-subtree t t))
  22433. nil))
  22434. (defun org-show-entry ()
  22435. "Show the body directly following this heading.
  22436. Show the heading too, if it is currently invisible."
  22437. (interactive)
  22438. (save-excursion
  22439. (ignore-errors
  22440. (org-back-to-heading t)
  22441. (outline-flag-region
  22442. (max (point-min) (1- (point)))
  22443. (save-excursion
  22444. (if (re-search-forward
  22445. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22446. (match-beginning 1)
  22447. (point-max)))
  22448. nil)
  22449. (org-cycle-hide-drawers 'children))))
  22450. (defun org-make-options-regexp (kwds &optional extra)
  22451. "Make a regular expression for keyword lines.
  22452. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22453. when non-nil, is a regexp matching keywords names."
  22454. (concat "^[ \t]*#\\+\\("
  22455. (regexp-opt kwds)
  22456. (and extra (concat (and kwds "\\|") extra))
  22457. "\\):[ \t]*\\(.*\\)"))
  22458. ;; Make isearch reveal the necessary context
  22459. (defun org-isearch-end ()
  22460. "Reveal context after isearch exits."
  22461. (when isearch-success ; only if search was successful
  22462. (if (featurep 'xemacs)
  22463. ;; Under XEmacs, the hook is run in the correct place,
  22464. ;; we directly show the context.
  22465. (org-show-context 'isearch)
  22466. ;; In Emacs the hook runs *before* restoring the overlays.
  22467. ;; So we have to use a one-time post-command-hook to do this.
  22468. ;; (Emacs 22 has a special variable, see function `org-mode')
  22469. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22470. isearch-mode-end-hook-quit)
  22471. ;; Only when the isearch was not quitted.
  22472. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22473. 'append 'local)))))
  22474. (defun org-isearch-post-command ()
  22475. "Remove self from hook, and show context."
  22476. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22477. (org-show-context 'isearch))
  22478. ;;;; Integration with and fixes for other packages
  22479. ;;; Imenu support
  22480. (defvar org-imenu-markers nil
  22481. "All markers currently used by Imenu.")
  22482. (make-variable-buffer-local 'org-imenu-markers)
  22483. (defun org-imenu-new-marker (&optional pos)
  22484. "Return a new marker for use by Imenu, and remember the marker."
  22485. (let ((m (make-marker)))
  22486. (move-marker m (or pos (point)))
  22487. (push m org-imenu-markers)
  22488. m))
  22489. (defun org-imenu-get-tree ()
  22490. "Produce the index for Imenu."
  22491. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22492. (setq org-imenu-markers nil)
  22493. (let* ((n org-imenu-depth)
  22494. (re (concat "^" (org-get-limited-outline-regexp)))
  22495. (subs (make-vector (1+ n) nil))
  22496. (last-level 0)
  22497. m level head0 head)
  22498. (save-excursion
  22499. (save-restriction
  22500. (widen)
  22501. (goto-char (point-max))
  22502. (while (re-search-backward re nil t)
  22503. (setq level (org-reduced-level (funcall outline-level)))
  22504. (when (and (<= level n)
  22505. (looking-at org-complex-heading-regexp)
  22506. (setq head0 (org-match-string-no-properties 4)))
  22507. (setq head (org-link-display-format head0)
  22508. m (org-imenu-new-marker))
  22509. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22510. (if (>= level last-level)
  22511. (push (cons head m) (aref subs level))
  22512. (push (cons head (aref subs (1+ level))) (aref subs level))
  22513. (loop for i from (1+ level) to n do (aset subs i nil)))
  22514. (setq last-level level)))))
  22515. (aref subs 1)))
  22516. (eval-after-load "imenu"
  22517. '(progn
  22518. (add-hook 'imenu-after-jump-hook
  22519. (lambda ()
  22520. (if (derived-mode-p 'org-mode)
  22521. (org-show-context 'org-goto))))))
  22522. (defun org-link-display-format (link)
  22523. "Replace a link with its the description.
  22524. If there is no description, use the link target."
  22525. (save-match-data
  22526. (if (string-match org-bracket-link-analytic-regexp link)
  22527. (replace-match (if (match-end 5)
  22528. (match-string 5 link)
  22529. (concat (match-string 1 link)
  22530. (match-string 3 link)))
  22531. nil t link)
  22532. link)))
  22533. (defun org-toggle-link-display ()
  22534. "Toggle the literal or descriptive display of links."
  22535. (interactive)
  22536. (if org-descriptive-links
  22537. (progn (org-remove-from-invisibility-spec '(org-link))
  22538. (org-restart-font-lock)
  22539. (setq org-descriptive-links nil))
  22540. (progn (add-to-invisibility-spec '(org-link))
  22541. (org-restart-font-lock)
  22542. (setq org-descriptive-links t))))
  22543. ;; Speedbar support
  22544. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22545. "Overlay marking the agenda restriction line in speedbar.")
  22546. (overlay-put org-speedbar-restriction-lock-overlay
  22547. 'face 'org-agenda-restriction-lock)
  22548. (overlay-put org-speedbar-restriction-lock-overlay
  22549. 'help-echo "Agendas are currently limited to this item.")
  22550. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22551. (defun org-speedbar-set-agenda-restriction ()
  22552. "Restrict future agenda commands to the location at point in speedbar.
  22553. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22554. (interactive)
  22555. (require 'org-agenda)
  22556. (let (p m tp np dir txt)
  22557. (cond
  22558. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22559. 'org-imenu t))
  22560. (setq m (get-text-property p 'org-imenu-marker))
  22561. (with-current-buffer (marker-buffer m)
  22562. (goto-char m)
  22563. (org-agenda-set-restriction-lock 'subtree)))
  22564. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22565. 'speedbar-function 'speedbar-find-file))
  22566. (setq tp (previous-single-property-change
  22567. (1+ p) 'speedbar-function)
  22568. np (next-single-property-change
  22569. tp 'speedbar-function)
  22570. dir (speedbar-line-directory)
  22571. txt (buffer-substring-no-properties (or tp (point-min))
  22572. (or np (point-max))))
  22573. (with-current-buffer (find-file-noselect
  22574. (let ((default-directory dir))
  22575. (expand-file-name txt)))
  22576. (unless (derived-mode-p 'org-mode)
  22577. (user-error "Cannot restrict to non-Org-mode file"))
  22578. (org-agenda-set-restriction-lock 'file)))
  22579. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22580. (move-overlay org-speedbar-restriction-lock-overlay
  22581. (point-at-bol) (point-at-eol))
  22582. (setq current-prefix-arg nil)
  22583. (org-agenda-maybe-redo)))
  22584. (defvar speedbar-file-key-map)
  22585. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22586. (eval-after-load "speedbar"
  22587. '(progn
  22588. (speedbar-add-supported-extension ".org")
  22589. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22590. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22591. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22592. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22593. (add-hook 'speedbar-visiting-tag-hook
  22594. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22595. ;;; Fixes and Hacks for problems with other packages
  22596. (defun org--flyspell-object-check-p (element)
  22597. "Non-nil when Flyspell can check object at point.
  22598. ELEMENT is the element at point."
  22599. (let ((object (save-excursion
  22600. (when (org-looking-at-p "\\>") (backward-char))
  22601. (org-element-context element))))
  22602. (case (org-element-type object)
  22603. ;; Prevent checks in links due to keybinding conflict with
  22604. ;; Flyspell.
  22605. ((code entity export-snippet inline-babel-call
  22606. inline-src-block line-break latex-fragment link macro
  22607. statistics-cookie target timestamp verbatim)
  22608. nil)
  22609. (footnote-reference
  22610. ;; Only in inline footnotes, within the definition.
  22611. (and (eq (org-element-property :type object) 'inline)
  22612. (< (save-excursion
  22613. (goto-char (org-element-property :begin object))
  22614. (search-forward ":" nil t 2))
  22615. (point))))
  22616. (otherwise t))))
  22617. (defun org-mode-flyspell-verify ()
  22618. "Function used for `flyspell-generic-check-word-predicate'."
  22619. (if (org-at-heading-p)
  22620. ;; At a headline or an inlinetask, check title only. This is
  22621. ;; faster than relying on `org-element-at-point'.
  22622. (and (save-excursion (beginning-of-line)
  22623. (and (let ((case-fold-search t))
  22624. (not (looking-at "\\*+ END[ \t]*$")))
  22625. (looking-at org-complex-heading-regexp)))
  22626. (match-beginning 4)
  22627. (>= (point) (match-beginning 4))
  22628. (or (not (match-beginning 5))
  22629. (< (point) (match-beginning 5))))
  22630. (let* ((element (org-element-at-point))
  22631. (post-affiliated (org-element-property :post-affiliated element)))
  22632. (cond
  22633. ;; Ignore checks in all affiliated keywords but captions.
  22634. ((< (point) post-affiliated)
  22635. (and (save-excursion
  22636. (beginning-of-line)
  22637. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22638. (> (point) (match-end 0))
  22639. (org--flyspell-object-check-p element)))
  22640. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22641. ((and org-log-into-drawer
  22642. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22643. (parent element))
  22644. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22645. (setq parent (org-element-property :parent parent)))
  22646. (and parent
  22647. (eq (compare-strings
  22648. log nil nil
  22649. (org-element-property :drawer-name parent) nil nil t)
  22650. t))))
  22651. nil)
  22652. (t
  22653. (case (org-element-type element)
  22654. ((comment quote-section) t)
  22655. (comment-block
  22656. ;; Allow checks between block markers, not on them.
  22657. (and (> (line-beginning-position) post-affiliated)
  22658. (save-excursion
  22659. (end-of-line)
  22660. (skip-chars-forward " \r\t\n")
  22661. (< (point) (org-element-property :end element)))))
  22662. ;; Arbitrary list of keywords where checks are meaningful.
  22663. ;; Make sure point is on the value part of the element.
  22664. (keyword
  22665. (and (member (org-element-property :key element)
  22666. '("DESCRIPTION" "TITLE"))
  22667. (< (save-excursion
  22668. (beginning-of-line) (search-forward ":") (point))
  22669. (point))))
  22670. ;; Check is globally allowed in paragraphs verse blocks and
  22671. ;; table rows (after affiliated keywords) but some objects
  22672. ;; must not be affected.
  22673. ((paragraph table-row verse-block)
  22674. (let ((cbeg (org-element-property :contents-begin element))
  22675. (cend (org-element-property :contents-end element)))
  22676. (and cbeg (>= (point) cbeg) (< (point) cend)
  22677. (org--flyspell-object-check-p element))))))))))
  22678. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22679. (defun org-remove-flyspell-overlays-in (beg end)
  22680. "Remove flyspell overlays in region."
  22681. (and (org-bound-and-true-p flyspell-mode)
  22682. (fboundp 'flyspell-delete-region-overlays)
  22683. (flyspell-delete-region-overlays beg end)))
  22684. (defvar flyspell-delayed-commands)
  22685. (eval-after-load "flyspell"
  22686. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22687. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22688. (eval-after-load "bookmark"
  22689. '(if (boundp 'bookmark-after-jump-hook)
  22690. ;; We can use the hook
  22691. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22692. ;; Hook not available, use advice
  22693. (defadvice bookmark-jump (after org-make-visible activate)
  22694. "Make the position visible."
  22695. (org-bookmark-jump-unhide))))
  22696. ;; Make sure saveplace shows the location if it was hidden
  22697. (eval-after-load "saveplace"
  22698. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22699. "Make the position visible."
  22700. (org-bookmark-jump-unhide)))
  22701. ;; Make sure ecb shows the location if it was hidden
  22702. (eval-after-load "ecb"
  22703. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22704. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22705. (if (derived-mode-p 'org-mode)
  22706. (org-show-context))))
  22707. (defun org-bookmark-jump-unhide ()
  22708. "Unhide the current position, to show the bookmark location."
  22709. (and (derived-mode-p 'org-mode)
  22710. (or (outline-invisible-p)
  22711. (save-excursion (goto-char (max (point-min) (1- (point))))
  22712. (outline-invisible-p)))
  22713. (org-show-context 'bookmark-jump)))
  22714. (defun org-mark-jump-unhide ()
  22715. "Make the point visible with `org-show-context' after jumping to the mark."
  22716. (when (and (derived-mode-p 'org-mode)
  22717. (outline-invisible-p))
  22718. (org-show-context 'mark-goto)))
  22719. (eval-after-load "simple"
  22720. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22721. "Make the point visible with `org-show-context'."
  22722. (org-mark-jump-unhide)))
  22723. (eval-after-load "simple"
  22724. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22725. "Make the point visible with `org-show-context'."
  22726. (org-mark-jump-unhide)))
  22727. (eval-after-load "simple"
  22728. '(defadvice pop-global-mark (after org-make-visible activate)
  22729. "Make the point visible with `org-show-context'."
  22730. (org-mark-jump-unhide)))
  22731. ;; Make session.el ignore our circular variable
  22732. (defvar session-globals-exclude)
  22733. (eval-after-load "session"
  22734. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22735. ;;;; Finish up
  22736. (provide 'org)
  22737. (run-hooks 'org-load-hook)
  22738. ;;; org.el ends here