org.el 941 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-add-archive-files "org-archive" (files))
  135. (declare-function org-id-find-id-file "org-id" (id))
  136. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  137. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  138. (declare-function org-agenda-redo "org-agenda" (&optional all))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-end "org-table" (&optional table-type))
  143. (declare-function org-table-end-of-field "org-table" (&optional n))
  144. (declare-function org-table-insert-row "org-table" (&optional arg))
  145. (declare-function org-table-paste-rectangle "org-table" ())
  146. (declare-function org-table-maybe-eval-formula "org-table" ())
  147. (declare-function org-table-maybe-recalculate-line "org-table" ())
  148. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  149. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  150. (declare-function org-element-at-point "org-element" ())
  151. (declare-function org-element-cache-reset "org-element" (&optional all))
  152. (declare-function org-element-cache-refresh "org-element" (pos))
  153. (declare-function org-element-contents "org-element" (element))
  154. (declare-function org-element-context "org-element" (&optional element))
  155. (declare-function org-element-interpret-data "org-element"
  156. (data &optional parent))
  157. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  158. (declare-function org-element-parse-buffer "org-element"
  159. (&optional granularity visible-only))
  160. (declare-function org-element-property "org-element" (property element))
  161. (declare-function org-element-put-property "org-element"
  162. (element property value))
  163. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  164. (declare-function org-element-parse-buffer "org-element"
  165. (&optional granularity visible-only))
  166. (declare-function org-element-type "org-element" (element))
  167. (defsubst org-uniquify (list)
  168. "Non-destructively remove duplicate elements from LIST."
  169. (let ((res (copy-sequence list))) (delete-dups res)))
  170. (defsubst org-get-at-bol (property)
  171. "Get text property PROPERTY at the beginning of line."
  172. (get-text-property (point-at-bol) property))
  173. (defsubst org-trim (s)
  174. "Remove whitespace at the beginning and the end of string S."
  175. (replace-regexp-in-string
  176. "\\`[ \t\n\r]+" ""
  177. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  178. ;; load languages based on value of `org-babel-load-languages'
  179. (defvar org-babel-load-languages)
  180. ;;;###autoload
  181. (defun org-babel-do-load-languages (sym value)
  182. "Load the languages defined in `org-babel-load-languages'."
  183. (set-default sym value)
  184. (mapc (lambda (pair)
  185. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  186. (if active
  187. (progn
  188. (require (intern (concat "ob-" lang))))
  189. (progn
  190. (funcall 'fmakunbound
  191. (intern (concat "org-babel-execute:" lang)))
  192. (funcall 'fmakunbound
  193. (intern (concat "org-babel-expand-body:" lang)))))))
  194. org-babel-load-languages))
  195. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  196. ;;;###autoload
  197. (defun org-babel-load-file (file &optional compile)
  198. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  199. This function exports the source code using `org-babel-tangle'
  200. and then loads the resulting file using `load-file'. With prefix
  201. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  202. file to byte-code before it is loaded."
  203. (interactive "fFile to load: \nP")
  204. (let* ((age (lambda (file)
  205. (float-time
  206. (time-subtract (current-time)
  207. (nth 5 (or (file-attributes (file-truename file))
  208. (file-attributes file)))))))
  209. (base-name (file-name-sans-extension file))
  210. (exported-file (concat base-name ".el")))
  211. ;; tangle if the org-mode file is newer than the elisp file
  212. (unless (and (file-exists-p exported-file)
  213. (> (funcall age file) (funcall age exported-file)))
  214. ;; Tangle-file traversal returns reversed list of tangled files
  215. ;; and we want to evaluate the first target.
  216. (setq exported-file
  217. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  218. (message "%s %s"
  219. (if compile
  220. (progn (byte-compile-file exported-file 'load)
  221. "Compiled and loaded")
  222. (progn (load-file exported-file) "Loaded"))
  223. exported-file)))
  224. (defcustom org-babel-load-languages '((emacs-lisp . t))
  225. "Languages which can be evaluated in Org-mode buffers.
  226. This list can be used to load support for any of the languages
  227. below, note that each language will depend on a different set of
  228. system executables and/or Emacs modes. When a language is
  229. \"loaded\", then code blocks in that language can be evaluated
  230. with `org-babel-execute-src-block' bound by default to C-c
  231. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  232. be set to remove code block evaluation from the C-c C-c
  233. keybinding. By default only Emacs Lisp (which has no
  234. requirements) is loaded."
  235. :group 'org-babel
  236. :set 'org-babel-do-load-languages
  237. :version "24.1"
  238. :type '(alist :tag "Babel Languages"
  239. :key-type
  240. (choice
  241. (const :tag "Awk" awk)
  242. (const :tag "C" C)
  243. (const :tag "R" R)
  244. (const :tag "Asymptote" asymptote)
  245. (const :tag "Calc" calc)
  246. (const :tag "Clojure" clojure)
  247. (const :tag "CSS" css)
  248. (const :tag "Ditaa" ditaa)
  249. (const :tag "Dot" dot)
  250. (const :tag "Emacs Lisp" emacs-lisp)
  251. (const :tag "Forth" forth)
  252. (const :tag "Fortran" fortran)
  253. (const :tag "Gnuplot" gnuplot)
  254. (const :tag "Haskell" haskell)
  255. (const :tag "IO" io)
  256. (const :tag "J" J)
  257. (const :tag "Java" java)
  258. (const :tag "Javascript" js)
  259. (const :tag "LaTeX" latex)
  260. (const :tag "Ledger" ledger)
  261. (const :tag "Lilypond" lilypond)
  262. (const :tag "Lisp" lisp)
  263. (const :tag "Makefile" makefile)
  264. (const :tag "Maxima" maxima)
  265. (const :tag "Matlab" matlab)
  266. (const :tag "Mscgen" mscgen)
  267. (const :tag "Ocaml" ocaml)
  268. (const :tag "Octave" octave)
  269. (const :tag "Org" org)
  270. (const :tag "Perl" perl)
  271. (const :tag "Pico Lisp" picolisp)
  272. (const :tag "PlantUML" plantuml)
  273. (const :tag "Python" python)
  274. (const :tag "Ruby" ruby)
  275. (const :tag "Sass" sass)
  276. (const :tag "Scala" scala)
  277. (const :tag "Scheme" scheme)
  278. (const :tag "Screen" screen)
  279. (const :tag "Shell Script" shell)
  280. (const :tag "Shen" shen)
  281. (const :tag "Sql" sql)
  282. (const :tag "Sqlite" sqlite)
  283. (const :tag "ebnf2ps" ebnf2ps))
  284. :value-type (boolean :tag "Activate" :value t)))
  285. ;;;; Customization variables
  286. (defcustom org-clone-delete-id nil
  287. "Remove ID property of clones of a subtree.
  288. When non-nil, clones of a subtree don't inherit the ID property.
  289. Otherwise they inherit the ID property with a new unique
  290. identifier."
  291. :type 'boolean
  292. :version "24.1"
  293. :group 'org-id)
  294. ;;; Version
  295. (org-check-version)
  296. ;;;###autoload
  297. (defun org-version (&optional here full message)
  298. "Show the org-mode version.
  299. Interactively, or when MESSAGE is non-nil, show it in echo area.
  300. With prefix argument, or when HERE is non-nil, insert it at point.
  301. In non-interactive uses, a reduced version string is output unless
  302. FULL is given."
  303. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  304. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  305. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  306. (load-suffixes (list ".el"))
  307. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  308. (org-trash (or
  309. (and (fboundp 'org-release) (fboundp 'org-git-version))
  310. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  311. (load-suffixes save-load-suffixes)
  312. (org-version (org-release))
  313. (git-version (org-git-version))
  314. (version (format "Org-mode version %s (%s @ %s)"
  315. org-version
  316. git-version
  317. (if org-install-dir
  318. (if (string= org-dir org-install-dir)
  319. org-install-dir
  320. (concat "mixed installation! " org-install-dir " and " org-dir))
  321. "org-loaddefs.el can not be found!")))
  322. (version1 (if full version org-version)))
  323. (when here (insert version1))
  324. (when message (message "%s" version1))
  325. version1))
  326. (defconst org-version (org-version))
  327. ;;; Syntax Constants
  328. ;;;; Block
  329. (defconst org-block-regexp
  330. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  331. "Regular expression for hiding blocks.")
  332. (defconst org-dblock-start-re
  333. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  334. "Matches the start line of a dynamic block, with parameters.")
  335. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  336. "Matches the end of a dynamic block.")
  337. ;;;; Clock and Planning
  338. (defconst org-clock-string "CLOCK:"
  339. "String used as prefix for timestamps clocking work hours on an item.")
  340. (defvar org-closed-string "CLOSED:"
  341. "String used as the prefix for timestamps logging closing a TODO entry.")
  342. (defvar org-deadline-string "DEADLINE:"
  343. "String to mark deadline entries.
  344. A deadline is this string, followed by a time stamp. Should be a word,
  345. terminated by a colon. You can insert a schedule keyword and
  346. a timestamp with \\[org-deadline].")
  347. (defvar org-scheduled-string "SCHEDULED:"
  348. "String to mark scheduled TODO entries.
  349. A schedule is this string, followed by a time stamp. Should be a word,
  350. terminated by a colon. You can insert a schedule keyword and
  351. a timestamp with \\[org-schedule].")
  352. (defconst org-ds-keyword-length
  353. (+ 2
  354. (apply #'max
  355. (mapcar #'length
  356. (list org-deadline-string org-scheduled-string
  357. org-clock-string org-closed-string))))
  358. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  359. (defconst org-planning-line-re
  360. (concat "^[ \t]*"
  361. (regexp-opt
  362. (list org-closed-string org-deadline-string org-scheduled-string)
  363. t))
  364. "Matches a line with planning info.
  365. Matched keyword is in group 1.")
  366. (defconst org-clock-line-re
  367. (concat "^[ \t]*" org-clock-string)
  368. "Matches a line with clock info.")
  369. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  370. "Matches the DEADLINE keyword.")
  371. (defconst org-deadline-time-regexp
  372. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  373. "Matches the DEADLINE keyword together with a time stamp.")
  374. (defconst org-deadline-time-hour-regexp
  375. (concat "\\<" org-deadline-string
  376. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  377. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  378. (defconst org-deadline-line-regexp
  379. (concat "\\<\\(" org-deadline-string "\\).*")
  380. "Matches the DEADLINE keyword and the rest of the line.")
  381. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  382. "Matches the SCHEDULED keyword.")
  383. (defconst org-scheduled-time-regexp
  384. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  385. "Matches the SCHEDULED keyword together with a time stamp.")
  386. (defconst org-scheduled-time-hour-regexp
  387. (concat "\\<" org-scheduled-string
  388. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  389. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  390. (defconst org-closed-time-regexp
  391. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  392. "Matches the CLOSED keyword together with a time stamp.")
  393. (defconst org-keyword-time-regexp
  394. (concat "\\<"
  395. (regexp-opt
  396. (list org-scheduled-string org-deadline-string org-closed-string
  397. org-clock-string)
  398. t)
  399. " *[[<]\\([^]>]+\\)[]>]")
  400. "Matches any of the 4 keywords, together with the time stamp.")
  401. (defconst org-keyword-time-not-clock-regexp
  402. (concat
  403. "\\<"
  404. (regexp-opt
  405. (list org-scheduled-string org-deadline-string org-closed-string) t)
  406. " *[[<]\\([^]>]+\\)[]>]")
  407. "Matches any of the 3 keywords, together with the time stamp.")
  408. (defconst org-maybe-keyword-time-regexp
  409. (concat "\\(\\<"
  410. (regexp-opt
  411. (list org-scheduled-string org-deadline-string org-closed-string
  412. org-clock-string)
  413. t)
  414. "\\)?"
  415. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  416. "\\|"
  417. "<%%([^\r\n>]*>\\)")
  418. "Matches a timestamp, possibly preceded by a keyword.")
  419. (defconst org-all-time-keywords
  420. (mapcar (lambda (w) (substring w 0 -1))
  421. (list org-scheduled-string org-deadline-string
  422. org-clock-string org-closed-string))
  423. "List of time keywords.")
  424. ;;;; Drawer
  425. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  426. "Matches first or last line of a hidden block.
  427. Group 1 contains drawer's name or \"END\".")
  428. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  429. "Regular expression matching the first line of a property drawer.")
  430. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  431. "Regular expression matching the last line of a property drawer.")
  432. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  433. "Regular expression matching the first line of a clock drawer.")
  434. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  435. "Regular expression matching the last line of a clock drawer.")
  436. (defconst org-property-drawer-re
  437. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  438. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  439. "[ \t]*:END:[ \t]*$")
  440. "Matches an entire property drawer.")
  441. (defconst org-clock-drawer-re
  442. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  443. org-clock-drawer-end-re "\\)\n?")
  444. "Matches an entire clock drawer.")
  445. ;;;; Headline
  446. (defconst org-heading-keyword-regexp-format
  447. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  448. "Printf format for a regexp matching a headline with some keyword.
  449. This regexp will match the headline of any node which has the
  450. exact keyword that is put into the format. The keyword isn't in
  451. any group by default, but the stars and the body are.")
  452. (defconst org-heading-keyword-maybe-regexp-format
  453. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  454. "Printf format for a regexp matching a headline, possibly with some keyword.
  455. This regexp can match any headline with the specified keyword, or
  456. without a keyword. The keyword isn't in any group by default,
  457. but the stars and the body are.")
  458. (defconst org-archive-tag "ARCHIVE"
  459. "The tag that marks a subtree as archived.
  460. An archived subtree does not open during visibility cycling, and does
  461. not contribute to the agenda listings.")
  462. (defconst org-comment-string "COMMENT"
  463. "Entries starting with this keyword will never be exported.
  464. An entry can be toggled between COMMENT and normal with
  465. \\[org-toggle-comment].")
  466. ;;;; LaTeX Environments and Fragments
  467. (defconst org-latex-regexps
  468. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  469. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  470. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  471. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  472. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  473. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  474. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  475. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  476. "Regular expressions for matching embedded LaTeX.")
  477. ;;;; Node Property
  478. (defconst org-effort-property "Effort"
  479. "The property that is being used to keep track of effort estimates.
  480. Effort estimates given in this property need to have the format H:MM.")
  481. ;;;; Table
  482. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  483. "Detect an org-type or table-type table.")
  484. (defconst org-table-line-regexp "^[ \t]*|"
  485. "Detect an org-type table line.")
  486. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  487. "Detect an org-type table line.")
  488. (defconst org-table-hline-regexp "^[ \t]*|-"
  489. "Detect an org-type table hline.")
  490. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  491. "Detect a table-type table hline.")
  492. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  493. "Detect the first line outside a table when searching from within it.
  494. This works for both table types.")
  495. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  496. "Detect a #+TBLFM line.")
  497. ;;;; Timestamp
  498. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  499. "Regular expression for fast time stamp matching.")
  500. (defconst org-ts-regexp-inactive
  501. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  502. "Regular expression for fast inactive time stamp matching.")
  503. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  504. "Regular expression for fast time stamp matching.")
  505. (defconst org-ts-regexp0
  506. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  507. "Regular expression matching time strings for analysis.
  508. This one does not require the space after the date, so it can be used
  509. on a string that terminates immediately after the date.")
  510. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  511. "Regular expression matching time strings for analysis.")
  512. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  513. "Regular expression matching time stamps, with groups.")
  514. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  515. "Regular expression matching time stamps (also [..]), with groups.")
  516. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  517. "Regular expression matching a time stamp range.")
  518. (defconst org-tr-regexp-both
  519. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  520. "Regular expression matching a time stamp range.")
  521. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  522. org-ts-regexp "\\)?")
  523. "Regular expression matching a time stamp or time stamp range.")
  524. (defconst org-tsr-regexp-both
  525. (concat org-ts-regexp-both "\\(--?-?"
  526. org-ts-regexp-both "\\)?")
  527. "Regular expression matching a time stamp or time stamp range.
  528. The time stamps may be either active or inactive.")
  529. (defconst org-repeat-re
  530. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  531. "Regular expression for specifying repeated events.
  532. After a match, group 1 contains the repeat expression.")
  533. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  534. "Formats for `format-time-string' which are used for time stamps.")
  535. ;;; The custom variables
  536. (defgroup org nil
  537. "Outline-based notes management and organizer."
  538. :tag "Org"
  539. :group 'outlines
  540. :group 'calendar)
  541. (defcustom org-mode-hook nil
  542. "Mode hook for Org-mode, run after the mode was turned on."
  543. :group 'org
  544. :type 'hook)
  545. (defcustom org-load-hook nil
  546. "Hook that is run after org.el has been loaded."
  547. :group 'org
  548. :type 'hook)
  549. (defcustom org-log-buffer-setup-hook nil
  550. "Hook that is run after an Org log buffer is created."
  551. :group 'org
  552. :version "24.1"
  553. :type 'hook)
  554. (defvar org-modules) ; defined below
  555. (defvar org-modules-loaded nil
  556. "Have the modules been loaded already?")
  557. (defun org-load-modules-maybe (&optional force)
  558. "Load all extensions listed in `org-modules'."
  559. (when (or force (not org-modules-loaded))
  560. (mapc (lambda (ext)
  561. (condition-case nil (require ext)
  562. (error (message "Problems while trying to load feature `%s'" ext))))
  563. org-modules)
  564. (setq org-modules-loaded t)))
  565. (defun org-set-modules (var value)
  566. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  567. (set var value)
  568. (when (featurep 'org)
  569. (org-load-modules-maybe 'force)
  570. (org-element-cache-reset 'all)))
  571. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  572. "Modules that should always be loaded together with org.el.
  573. If a description starts with <C>, the file is not part of Emacs
  574. and loading it will require that you have downloaded and properly
  575. installed the Org mode distribution.
  576. You can also use this system to load external packages (i.e. neither Org
  577. core modules, nor modules from the CONTRIB directory). Just add symbols
  578. to the end of the list. If the package is called org-xyz.el, then you need
  579. to add the symbol `xyz', and the package must have a call to:
  580. \(provide 'org-xyz)
  581. For export specific modules, see also `org-export-backends'."
  582. :group 'org
  583. :set 'org-set-modules
  584. :version "24.4"
  585. :package-version '(Org . "8.0")
  586. :type
  587. '(set :greedy t
  588. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  589. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  590. (const :tag " crypt: Encryption of subtrees" org-crypt)
  591. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  592. (const :tag " docview: Links to doc-view buffers" org-docview)
  593. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  594. (const :tag " habit: Track your consistency with habits" org-habit)
  595. (const :tag " id: Global IDs for identifying entries" org-id)
  596. (const :tag " info: Links to Info nodes" org-info)
  597. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  598. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  599. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  600. (const :tag " mouse: Additional mouse support" org-mouse)
  601. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  602. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  603. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  604. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  605. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  606. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  607. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  608. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  609. (const :tag "C collector: Collect properties into tables" org-collector)
  610. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  611. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  612. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  613. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  614. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  615. (const :tag "C eval: Include command output as text" org-eval)
  616. (const :tag "C eww: Store link to url of eww" org-eww)
  617. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  618. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  619. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  620. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  621. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  622. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  623. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  624. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  625. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  626. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  627. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  628. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  629. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  630. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  631. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  632. (const :tag "C registry: A registry for Org-mode links" org-registry)
  633. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  634. (const :tag "C secretary: Team management with org-mode" org-secretary)
  635. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  636. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  637. (const :tag "C track: Keep up with Org-mode development" org-track)
  638. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  639. (const :tag "C vm: Links to VM folders/messages" org-vm)
  640. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  641. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  642. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  643. (defvar org-export--registered-backends) ; From ox.el.
  644. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  645. (declare-function org-export-backend-name "ox" (backend))
  646. (defcustom org-export-backends '(ascii html icalendar latex)
  647. "List of export back-ends that should be always available.
  648. If a description starts with <C>, the file is not part of Emacs
  649. and loading it will require that you have downloaded and properly
  650. installed the Org mode distribution.
  651. Unlike to `org-modules', libraries in this list will not be
  652. loaded along with Org, but only once the export framework is
  653. needed.
  654. This variable needs to be set before org.el is loaded. If you
  655. need to make a change while Emacs is running, use the customize
  656. interface or run the following code, where VAL stands for the new
  657. value of the variable, after updating it:
  658. \(progn
  659. \(setq org-export--registered-backends
  660. \(org-remove-if-not
  661. \(lambda (backend)
  662. \(let ((name (org-export-backend-name backend)))
  663. \(or (memq name val)
  664. \(catch 'parentp
  665. \(dolist (b val)
  666. \(and (org-export-derived-backend-p b name)
  667. \(throw 'parentp t)))))))
  668. org-export--registered-backends))
  669. \(let ((new-list (mapcar 'org-export-backend-name
  670. org-export--registered-backends)))
  671. \(dolist (backend val)
  672. \(cond
  673. \((not (load (format \"ox-%s\" backend) t t))
  674. \(message \"Problems while trying to load export back-end `%s'\"
  675. backend))
  676. \((not (memq backend new-list)) (push backend new-list))))
  677. \(set-default 'org-export-backends new-list)))
  678. Adding a back-end to this list will also pull the back-end it
  679. depends on, if any."
  680. :group 'org
  681. :group 'org-export
  682. :version "24.4"
  683. :package-version '(Org . "8.0")
  684. :initialize 'custom-initialize-set
  685. :set (lambda (var val)
  686. (if (not (featurep 'ox)) (set-default var val)
  687. ;; Any back-end not required anymore (not present in VAL and not
  688. ;; a parent of any back-end in the new value) is removed from the
  689. ;; list of registered back-ends.
  690. (setq org-export--registered-backends
  691. (org-remove-if-not
  692. (lambda (backend)
  693. (let ((name (org-export-backend-name backend)))
  694. (or (memq name val)
  695. (catch 'parentp
  696. (dolist (b val)
  697. (and (org-export-derived-backend-p b name)
  698. (throw 'parentp t)))))))
  699. org-export--registered-backends))
  700. ;; Now build NEW-LIST of both new back-ends and required
  701. ;; parents.
  702. (let ((new-list (mapcar 'org-export-backend-name
  703. org-export--registered-backends)))
  704. (dolist (backend val)
  705. (cond
  706. ((not (load (format "ox-%s" backend) t t))
  707. (message "Problems while trying to load export back-end `%s'"
  708. backend))
  709. ((not (memq backend new-list)) (push backend new-list))))
  710. ;; Set VAR to that list with fixed dependencies.
  711. (set-default var new-list))))
  712. :type '(set :greedy t
  713. (const :tag " ascii Export buffer to ASCII format" ascii)
  714. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  715. (const :tag " html Export buffer to HTML format" html)
  716. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  717. (const :tag " latex Export buffer to LaTeX format" latex)
  718. (const :tag " man Export buffer to MAN format" man)
  719. (const :tag " md Export buffer to Markdown format" md)
  720. (const :tag " odt Export buffer to ODT format" odt)
  721. (const :tag " org Export buffer to Org format" org)
  722. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  723. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  724. (const :tag "C deck Export buffer to deck.js presentations" deck)
  725. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  726. (const :tag "C groff Export buffer to Groff format" groff)
  727. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  728. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  729. (const :tag "C s5 Export buffer to s5 presentations" s5)
  730. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  731. (eval-after-load 'ox
  732. '(mapc
  733. (lambda (backend)
  734. (condition-case nil (require (intern (format "ox-%s" backend)))
  735. (error (message "Problems while trying to load export back-end `%s'"
  736. backend))))
  737. org-export-backends))
  738. (defcustom org-support-shift-select nil
  739. "Non-nil means make shift-cursor commands select text when possible.
  740. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  741. start selecting a region, or enlarge regions started in this way.
  742. In Org-mode, in special contexts, these same keys are used for
  743. other purposes, important enough to compete with shift selection.
  744. Org tries to balance these needs by supporting `shift-select-mode'
  745. outside these special contexts, under control of this variable.
  746. The default of this variable is nil, to avoid confusing behavior. Shifted
  747. cursor keys will then execute Org commands in the following contexts:
  748. - on a headline, changing TODO state (left/right) and priority (up/down)
  749. - on a time stamp, changing the time
  750. - in a plain list item, changing the bullet type
  751. - in a property definition line, switching between allowed values
  752. - in the BEGIN line of a clock table (changing the time block).
  753. Outside these contexts, the commands will throw an error.
  754. When this variable is t and the cursor is not in a special
  755. context, Org-mode will support shift-selection for making and
  756. enlarging regions. To make this more effective, the bullet
  757. cycling will no longer happen anywhere in an item line, but only
  758. if the cursor is exactly on the bullet.
  759. If you set this variable to the symbol `always', then the keys
  760. will not be special in headlines, property lines, and item lines,
  761. to make shift selection work there as well. If this is what you
  762. want, you can use the following alternative commands: `C-c C-t'
  763. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  764. can be used to switch TODO sets, `C-c -' to cycle item bullet
  765. types, and properties can be edited by hand or in column view.
  766. However, when the cursor is on a timestamp, shift-cursor commands
  767. will still edit the time stamp - this is just too good to give up.
  768. XEmacs user should have this variable set to nil, because
  769. `shift-select-mode' is in Emacs 23 or later only."
  770. :group 'org
  771. :type '(choice
  772. (const :tag "Never" nil)
  773. (const :tag "When outside special context" t)
  774. (const :tag "Everywhere except timestamps" always)))
  775. (defcustom org-loop-over-headlines-in-active-region nil
  776. "Shall some commands act upon headlines in the active region?
  777. When set to `t', some commands will be performed in all headlines
  778. within the active region.
  779. When set to `start-level', some commands will be performed in all
  780. headlines within the active region, provided that these headlines
  781. are of the same level than the first one.
  782. When set to a string, those commands will be performed on the
  783. matching headlines within the active region. Such string must be
  784. a tags/property/todo match as it is used in the agenda tags view.
  785. The list of commands is: `org-schedule', `org-deadline',
  786. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  787. `org-archive-to-archive-sibling'. The archiving commands skip
  788. already archived entries."
  789. :type '(choice (const :tag "Don't loop" nil)
  790. (const :tag "All headlines in active region" t)
  791. (const :tag "In active region, headlines at the same level than the first one" start-level)
  792. (string :tag "Tags/Property/Todo matcher"))
  793. :version "24.1"
  794. :group 'org-todo
  795. :group 'org-archive)
  796. (defgroup org-startup nil
  797. "Options concerning startup of Org-mode."
  798. :tag "Org Startup"
  799. :group 'org)
  800. (defcustom org-startup-folded t
  801. "Non-nil means entering Org-mode will switch to OVERVIEW.
  802. This can also be configured on a per-file basis by adding one of
  803. the following lines anywhere in the buffer:
  804. #+STARTUP: fold (or `overview', this is equivalent)
  805. #+STARTUP: nofold (or `showall', this is equivalent)
  806. #+STARTUP: content
  807. #+STARTUP: showeverything
  808. By default, this option is ignored when Org opens agenda files
  809. for the first time. If you want the agenda to honor the startup
  810. option, set `org-agenda-inhibit-startup' to nil."
  811. :group 'org-startup
  812. :type '(choice
  813. (const :tag "nofold: show all" nil)
  814. (const :tag "fold: overview" t)
  815. (const :tag "content: all headlines" content)
  816. (const :tag "show everything, even drawers" showeverything)))
  817. (defcustom org-startup-truncated t
  818. "Non-nil means entering Org-mode will set `truncate-lines'.
  819. This is useful since some lines containing links can be very long and
  820. uninteresting. Also tables look terrible when wrapped."
  821. :group 'org-startup
  822. :type 'boolean)
  823. (defcustom org-startup-indented nil
  824. "Non-nil means turn on `org-indent-mode' on startup.
  825. This can also be configured on a per-file basis by adding one of
  826. the following lines anywhere in the buffer:
  827. #+STARTUP: indent
  828. #+STARTUP: noindent"
  829. :group 'org-structure
  830. :type '(choice
  831. (const :tag "Not" nil)
  832. (const :tag "Globally (slow on startup in large files)" t)))
  833. (defcustom org-use-sub-superscripts t
  834. "Non-nil means interpret \"_\" and \"^\" for display.
  835. If you want to control how Org exports those characters, see
  836. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  837. used to be an alias for `org-export-with-sub-superscripts' in
  838. Org <8.0, it is not anymore.
  839. When this option is turned on, you can use TeX-like syntax for
  840. sub- and superscripts within the buffer. Several characters after
  841. \"_\" or \"^\" will be considered as a single item - so grouping
  842. with {} is normally not needed. For example, the following things
  843. will be parsed as single sub- or superscripts:
  844. 10^24 or 10^tau several digits will be considered 1 item.
  845. 10^-12 or 10^-tau a leading sign with digits or a word
  846. x^2-y^3 will be read as x^2 - y^3, because items are
  847. terminated by almost any nonword/nondigit char.
  848. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  849. Still, ambiguity is possible. So when in doubt, use {} to enclose
  850. the sub/superscript. If you set this variable to the symbol `{}',
  851. the braces are *required* in order to trigger interpretations as
  852. sub/superscript. This can be helpful in documents that need \"_\"
  853. frequently in plain text."
  854. :group 'org-startup
  855. :version "24.4"
  856. :package-version '(Org . "8.0")
  857. :type '(choice
  858. (const :tag "Always interpret" t)
  859. (const :tag "Only with braces" {})
  860. (const :tag "Never interpret" nil)))
  861. (defcustom org-startup-with-beamer-mode nil
  862. "Non-nil means turn on `org-beamer-mode' on startup.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: beamer"
  866. :group 'org-startup
  867. :version "24.1"
  868. :type 'boolean)
  869. (defcustom org-startup-align-all-tables nil
  870. "Non-nil means align all tables when visiting a file.
  871. This is useful when the column width in tables is forced with <N> cookies
  872. in table fields. Such tables will look correct only after the first re-align.
  873. This can also be configured on a per-file basis by adding one of
  874. the following lines anywhere in the buffer:
  875. #+STARTUP: align
  876. #+STARTUP: noalign"
  877. :group 'org-startup
  878. :type 'boolean)
  879. (defcustom org-startup-with-inline-images nil
  880. "Non-nil means show inline images when loading a new Org file.
  881. This can also be configured on a per-file basis by adding one of
  882. the following lines anywhere in the buffer:
  883. #+STARTUP: inlineimages
  884. #+STARTUP: noinlineimages"
  885. :group 'org-startup
  886. :version "24.1"
  887. :type 'boolean)
  888. (defcustom org-startup-with-latex-preview nil
  889. "Non-nil means preview LaTeX fragments when loading a new Org file.
  890. This can also be configured on a per-file basis by adding one of
  891. the following lines anywhere in the buffer:
  892. #+STARTUP: latexpreview
  893. #+STARTUP: nolatexpreview"
  894. :group 'org-startup
  895. :version "24.4"
  896. :package-version '(Org . "8.0")
  897. :type 'boolean)
  898. (defcustom org-insert-mode-line-in-empty-file nil
  899. "Non-nil means insert the first line setting Org-mode in empty files.
  900. When the function `org-mode' is called interactively in an empty file, this
  901. normally means that the file name does not automatically trigger Org-mode.
  902. To ensure that the file will always be in Org-mode in the future, a
  903. line enforcing Org-mode will be inserted into the buffer, if this option
  904. has been set."
  905. :group 'org-startup
  906. :type 'boolean)
  907. (defcustom org-replace-disputed-keys nil
  908. "Non-nil means use alternative key bindings for some keys.
  909. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  910. These keys are also used by other packages like shift-selection-mode'
  911. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  912. If you want to use Org-mode together with one of these other modes,
  913. or more generally if you would like to move some Org-mode commands to
  914. other keys, set this variable and configure the keys with the variable
  915. `org-disputed-keys'.
  916. This option is only relevant at load-time of Org-mode, and must be set
  917. *before* org.el is loaded. Changing it requires a restart of Emacs to
  918. become effective."
  919. :group 'org-startup
  920. :type 'boolean)
  921. (defcustom org-use-extra-keys nil
  922. "Non-nil means use extra key sequence definitions for certain commands.
  923. This happens automatically if you run XEmacs or if `window-system'
  924. is nil. This variable lets you do the same manually. You must
  925. set it before loading org.
  926. Example: on Carbon Emacs 22 running graphically, with an external
  927. keyboard on a Powerbook, the default way of setting M-left might
  928. not work for either Alt or ESC. Setting this variable will make
  929. it work for ESC."
  930. :group 'org-startup
  931. :type 'boolean)
  932. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  933. (defcustom org-disputed-keys
  934. '(([(shift up)] . [(meta p)])
  935. ([(shift down)] . [(meta n)])
  936. ([(shift left)] . [(meta -)])
  937. ([(shift right)] . [(meta +)])
  938. ([(control shift right)] . [(meta shift +)])
  939. ([(control shift left)] . [(meta shift -)]))
  940. "Keys for which Org-mode and other modes compete.
  941. This is an alist, cars are the default keys, second element specifies
  942. the alternative to use when `org-replace-disputed-keys' is t.
  943. Keys can be specified in any syntax supported by `define-key'.
  944. The value of this option takes effect only at Org-mode's startup,
  945. therefore you'll have to restart Emacs to apply it after changing."
  946. :group 'org-startup
  947. :type 'alist)
  948. (defun org-key (key)
  949. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  950. Or return the original if not disputed.
  951. Also apply the translations defined in `org-xemacs-key-equivalents'."
  952. (when org-replace-disputed-keys
  953. (let* ((nkey (key-description key))
  954. (x (org-find-if (lambda (x)
  955. (equal (key-description (car x)) nkey))
  956. org-disputed-keys)))
  957. (setq key (if x (cdr x) key))))
  958. (when (featurep 'xemacs)
  959. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  960. key)
  961. (defun org-find-if (predicate seq)
  962. (catch 'exit
  963. (while seq
  964. (if (funcall predicate (car seq))
  965. (throw 'exit (car seq))
  966. (pop seq)))))
  967. (defun org-defkey (keymap key def)
  968. "Define a key, possibly translated, as returned by `org-key'."
  969. (define-key keymap (org-key key) def))
  970. (defcustom org-ellipsis nil
  971. "The ellipsis to use in the Org-mode outline.
  972. When nil, just use the standard three dots.
  973. When a string, use that string instead.
  974. When a face, use the standard 3 dots, but with the specified face.
  975. The change affects only Org-mode (which will then use its own display table).
  976. Changing this requires executing \\[org-mode] in a buffer to become
  977. effective."
  978. :group 'org-startup
  979. :type '(choice (const :tag "Default" nil)
  980. (face :tag "Face" :value org-warning)
  981. (string :tag "String" :value "...#")))
  982. (defvar org-display-table nil
  983. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  984. (defgroup org-keywords nil
  985. "Keywords in Org-mode."
  986. :tag "Org Keywords"
  987. :group 'org)
  988. (defcustom org-closed-keep-when-no-todo nil
  989. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  990. :group 'org-todo
  991. :group 'org-keywords
  992. :version "24.4"
  993. :package-version '(Org . "8.0")
  994. :type 'boolean)
  995. (defgroup org-structure nil
  996. "Options concerning the general structure of Org-mode files."
  997. :tag "Org Structure"
  998. :group 'org)
  999. (defgroup org-reveal-location nil
  1000. "Options about how to make context of a location visible."
  1001. :tag "Org Reveal Location"
  1002. :group 'org-structure)
  1003. (defconst org-context-choice
  1004. '(choice
  1005. (const :tag "Always" t)
  1006. (const :tag "Never" nil)
  1007. (repeat :greedy t :tag "Individual contexts"
  1008. (cons
  1009. (choice :tag "Context"
  1010. (const agenda)
  1011. (const org-goto)
  1012. (const occur-tree)
  1013. (const tags-tree)
  1014. (const link-search)
  1015. (const mark-goto)
  1016. (const bookmark-jump)
  1017. (const isearch)
  1018. (const default))
  1019. (boolean))))
  1020. "Contexts for the reveal options.")
  1021. (defcustom org-show-hierarchy-above '((default . t))
  1022. "Non-nil means show full hierarchy when revealing a location.
  1023. Org-mode often shows locations in an org-mode file which might have
  1024. been invisible before. When this is set, the hierarchy of headings
  1025. above the exposed location is shown.
  1026. Turning this off for example for sparse trees makes them very compact.
  1027. Instead of t, this can also be an alist specifying this option for different
  1028. contexts. Valid contexts are
  1029. agenda when exposing an entry from the agenda
  1030. org-goto when using the command `org-goto' on key C-c C-j
  1031. occur-tree when using the command `org-occur' on key C-c /
  1032. tags-tree when constructing a sparse tree based on tags matches
  1033. link-search when exposing search matches associated with a link
  1034. mark-goto when exposing the jump goal of a mark
  1035. bookmark-jump when exposing a bookmark location
  1036. isearch when exiting from an incremental search
  1037. default default for all contexts not set explicitly"
  1038. :group 'org-reveal-location
  1039. :type org-context-choice)
  1040. (defcustom org-show-following-heading '((default . nil))
  1041. "Non-nil means show following heading when revealing a location.
  1042. Org-mode often shows locations in an org-mode file which might have
  1043. been invisible before. When this is set, the heading following the
  1044. match is shown.
  1045. Turning this off for example for sparse trees makes them very compact,
  1046. but makes it harder to edit the location of the match. In such a case,
  1047. use the command \\[org-reveal] to show more context.
  1048. Instead of t, this can also be an alist specifying this option for different
  1049. contexts. See `org-show-hierarchy-above' for valid contexts."
  1050. :group 'org-reveal-location
  1051. :type org-context-choice)
  1052. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  1053. "Non-nil means show all sibling heading when revealing a location.
  1054. Org-mode often shows locations in an org-mode file which might have
  1055. been invisible before. When this is set, the sibling of the current entry
  1056. heading are all made visible. If `org-show-hierarchy-above' is t,
  1057. the same happens on each level of the hierarchy above the current entry.
  1058. By default this is on for the isearch context, off for all other contexts.
  1059. Turning this off for example for sparse trees makes them very compact,
  1060. but makes it harder to edit the location of the match. In such a case,
  1061. use the command \\[org-reveal] to show more context.
  1062. Instead of t, this can also be an alist specifying this option for different
  1063. contexts. See `org-show-hierarchy-above' for valid contexts."
  1064. :group 'org-reveal-location
  1065. :type org-context-choice
  1066. :version "24.4"
  1067. :package-version '(Org . "8.0"))
  1068. (defcustom org-show-entry-below '((default . nil))
  1069. "Non-nil means show the entry below a headline when revealing a location.
  1070. Org-mode often shows locations in an org-mode file which might have
  1071. been invisible before. When this is set, the text below the headline that is
  1072. exposed is also shown.
  1073. By default this is off for all contexts.
  1074. Instead of t, this can also be an alist specifying this option for different
  1075. contexts. See `org-show-hierarchy-above' for valid contexts."
  1076. :group 'org-reveal-location
  1077. :type org-context-choice)
  1078. (defcustom org-indirect-buffer-display 'other-window
  1079. "How should indirect tree buffers be displayed?
  1080. This applies to indirect buffers created with the commands
  1081. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1082. Valid values are:
  1083. current-window Display in the current window
  1084. other-window Just display in another window.
  1085. dedicated-frame Create one new frame, and re-use it each time.
  1086. new-frame Make a new frame each time. Note that in this case
  1087. previously-made indirect buffers are kept, and you need to
  1088. kill these buffers yourself."
  1089. :group 'org-structure
  1090. :group 'org-agenda-windows
  1091. :type '(choice
  1092. (const :tag "In current window" current-window)
  1093. (const :tag "In current frame, other window" other-window)
  1094. (const :tag "Each time a new frame" new-frame)
  1095. (const :tag "One dedicated frame" dedicated-frame)))
  1096. (defcustom org-use-speed-commands nil
  1097. "Non-nil means activate single letter commands at beginning of a headline.
  1098. This may also be a function to test for appropriate locations where speed
  1099. commands should be active.
  1100. For example, to activate speed commands when the point is on any
  1101. star at the beginning of the headline, you can do this:
  1102. (setq org-use-speed-commands
  1103. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1104. :group 'org-structure
  1105. :type '(choice
  1106. (const :tag "Never" nil)
  1107. (const :tag "At beginning of headline stars" t)
  1108. (function)))
  1109. (defcustom org-speed-commands-user nil
  1110. "Alist of additional speed commands.
  1111. This list will be checked before `org-speed-commands-default'
  1112. when the variable `org-use-speed-commands' is non-nil
  1113. and when the cursor is at the beginning of a headline.
  1114. The car if each entry is a string with a single letter, which must
  1115. be assigned to `self-insert-command' in the global map.
  1116. The cdr is either a command to be called interactively, a function
  1117. to be called, or a form to be evaluated.
  1118. An entry that is just a list with a single string will be interpreted
  1119. as a descriptive headline that will be added when listing the speed
  1120. commands in the Help buffer using the `?' speed command."
  1121. :group 'org-structure
  1122. :type '(repeat :value ("k" . ignore)
  1123. (choice :value ("k" . ignore)
  1124. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1125. (cons :tag "Letter and Command"
  1126. (string :tag "Command letter")
  1127. (choice
  1128. (function)
  1129. (sexp))))))
  1130. (defcustom org-bookmark-names-plist
  1131. '(:last-capture "org-capture-last-stored"
  1132. :last-refile "org-refile-last-stored"
  1133. :last-capture-marker "org-capture-last-stored-marker")
  1134. "Names for bookmarks automatically set by some Org commands.
  1135. This can provide strings as names for a number of bookmarks Org sets
  1136. automatically. The following keys are currently implemented:
  1137. :last-capture
  1138. :last-capture-marker
  1139. :last-refile
  1140. When a key does not show up in the property list, the corresponding bookmark
  1141. is not set."
  1142. :group 'org-structure
  1143. :type 'plist)
  1144. (defgroup org-cycle nil
  1145. "Options concerning visibility cycling in Org-mode."
  1146. :tag "Org Cycle"
  1147. :group 'org-structure)
  1148. (defcustom org-cycle-skip-children-state-if-no-children t
  1149. "Non-nil means skip CHILDREN state in entries that don't have any."
  1150. :group 'org-cycle
  1151. :type 'boolean)
  1152. (defcustom org-cycle-max-level nil
  1153. "Maximum level which should still be subject to visibility cycling.
  1154. Levels higher than this will, for cycling, be treated as text, not a headline.
  1155. When `org-odd-levels-only' is set, a value of N in this variable actually
  1156. means 2N-1 stars as the limiting headline.
  1157. When nil, cycle all levels.
  1158. Note that the limiting level of cycling is also influenced by
  1159. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1160. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1161. than its value."
  1162. :group 'org-cycle
  1163. :type '(choice
  1164. (const :tag "No limit" nil)
  1165. (integer :tag "Maximum level")))
  1166. (defcustom org-hide-block-startup nil
  1167. "Non-nil means entering Org-mode will fold all blocks.
  1168. This can also be set in on a per-file basis with
  1169. #+STARTUP: hideblocks
  1170. #+STARTUP: showblocks"
  1171. :group 'org-startup
  1172. :group 'org-cycle
  1173. :type 'boolean)
  1174. (defcustom org-cycle-global-at-bob nil
  1175. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1176. This makes it possible to do global cycling without having to use S-TAB or
  1177. \\[universal-argument] TAB. For this special case to work, the first line
  1178. of the buffer must not be a headline -- it may be empty or some other text.
  1179. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1180. sure the cursor stays at the beginning of the buffer. When this option is
  1181. nil, don't do anything special at the beginning of the buffer."
  1182. :group 'org-cycle
  1183. :type 'boolean)
  1184. (defcustom org-cycle-level-after-item/entry-creation t
  1185. "Non-nil means cycle entry level or item indentation in new empty entries.
  1186. When the cursor is at the end of an empty headline, i.e., with only stars
  1187. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1188. and then all possible ancestor states, before returning to the original state.
  1189. This makes data entry extremely fast: M-RET to create a new headline,
  1190. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1191. When the cursor is at the end of an empty plain list item, one TAB will
  1192. make it a subitem, two or more tabs will back up to make this an item
  1193. higher up in the item hierarchy."
  1194. :group 'org-cycle
  1195. :type 'boolean)
  1196. (defcustom org-cycle-emulate-tab t
  1197. "Where should `org-cycle' emulate TAB.
  1198. nil Never
  1199. white Only in completely white lines
  1200. whitestart Only at the beginning of lines, before the first non-white char
  1201. t Everywhere except in headlines
  1202. exc-hl-bol Everywhere except at the start of a headline
  1203. If TAB is used in a place where it does not emulate TAB, the current subtree
  1204. visibility is cycled."
  1205. :group 'org-cycle
  1206. :type '(choice (const :tag "Never" nil)
  1207. (const :tag "Only in completely white lines" white)
  1208. (const :tag "Before first char in a line" whitestart)
  1209. (const :tag "Everywhere except in headlines" t)
  1210. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1211. (defcustom org-cycle-separator-lines 2
  1212. "Number of empty lines needed to keep an empty line between collapsed trees.
  1213. If you leave an empty line between the end of a subtree and the following
  1214. headline, this empty line is hidden when the subtree is folded.
  1215. Org-mode will leave (exactly) one empty line visible if the number of
  1216. empty lines is equal or larger to the number given in this variable.
  1217. So the default 2 means at least 2 empty lines after the end of a subtree
  1218. are needed to produce free space between a collapsed subtree and the
  1219. following headline.
  1220. If the number is negative, and the number of empty lines is at least -N,
  1221. all empty lines are shown.
  1222. Special case: when 0, never leave empty lines in collapsed view."
  1223. :group 'org-cycle
  1224. :type 'integer)
  1225. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1226. (defcustom org-pre-cycle-hook nil
  1227. "Hook that is run before visibility cycling is happening.
  1228. The function(s) in this hook must accept a single argument which indicates
  1229. the new state that will be set right after running this hook. The
  1230. argument is a symbol. Before a global state change, it can have the values
  1231. `overview', `content', or `all'. Before a local state change, it can have
  1232. the values `folded', `children', or `subtree'."
  1233. :group 'org-cycle
  1234. :type 'hook)
  1235. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1236. org-cycle-hide-drawers
  1237. org-cycle-hide-inline-tasks
  1238. org-cycle-show-empty-lines
  1239. org-optimize-window-after-visibility-change)
  1240. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1241. The function(s) in this hook must accept a single argument which indicates
  1242. the new state that was set by the most recent `org-cycle' command. The
  1243. argument is a symbol. After a global state change, it can have the values
  1244. `overview', `contents', or `all'. After a local state change, it can have
  1245. the values `folded', `children', or `subtree'."
  1246. :group 'org-cycle
  1247. :type 'hook)
  1248. (defgroup org-edit-structure nil
  1249. "Options concerning structure editing in Org-mode."
  1250. :tag "Org Edit Structure"
  1251. :group 'org-structure)
  1252. (defcustom org-odd-levels-only nil
  1253. "Non-nil means skip even levels and only use odd levels for the outline.
  1254. This has the effect that two stars are being added/taken away in
  1255. promotion/demotion commands. It also influences how levels are
  1256. handled by the exporters.
  1257. Changing it requires restart of `font-lock-mode' to become effective
  1258. for fontification also in regions already fontified.
  1259. You may also set this on a per-file basis by adding one of the following
  1260. lines to the buffer:
  1261. #+STARTUP: odd
  1262. #+STARTUP: oddeven"
  1263. :group 'org-edit-structure
  1264. :group 'org-appearance
  1265. :type 'boolean)
  1266. (defcustom org-adapt-indentation t
  1267. "Non-nil means adapt indentation to outline node level.
  1268. When this variable is set, Org assumes that you write outlines by
  1269. indenting text in each node to align with the headline (after the
  1270. stars). The following issues are influenced by this variable:
  1271. - The indentation is increased by one space in a demotion
  1272. command, and decreased by one in a promotion command. However,
  1273. in the latter case, if shifting some line in the entry body
  1274. would alter document structure (e.g., insert a new headline),
  1275. indentation is not changed at all.
  1276. - Property drawers and planning information is inserted indented
  1277. when this variable is set. When nil, they will not be indented.
  1278. - TAB indents a line relative to current level. The lines below
  1279. a headline will be indented when this variable is set.
  1280. Note that this is all about true indentation, by adding and
  1281. removing space characters. See also `org-indent.el' which does
  1282. level-dependent indentation in a virtual way, i.e. at display
  1283. time in Emacs."
  1284. :group 'org-edit-structure
  1285. :type 'boolean)
  1286. (defcustom org-special-ctrl-a/e nil
  1287. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1288. When t, `C-a' will bring back the cursor to the beginning of the
  1289. headline text, i.e. after the stars and after a possible TODO
  1290. keyword. In an item, this will be the position after bullet and
  1291. check-box, if any. When the cursor is already at that position,
  1292. another `C-a' will bring it to the beginning of the line.
  1293. `C-e' will jump to the end of the headline, ignoring the presence
  1294. of tags in the headline. A second `C-e' will then jump to the
  1295. true end of the line, after any tags. This also means that, when
  1296. this variable is non-nil, `C-e' also will never jump beyond the
  1297. end of the heading of a folded section, i.e. not after the
  1298. ellipses.
  1299. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1300. normally, going to the true line boundary first. Only a directly
  1301. following, identical keypress will bring the cursor to the
  1302. special positions.
  1303. This may also be a cons cell where the behavior for `C-a' and
  1304. `C-e' is set separately."
  1305. :group 'org-edit-structure
  1306. :type '(choice
  1307. (const :tag "off" nil)
  1308. (const :tag "on: after stars/bullet and before tags first" t)
  1309. (const :tag "reversed: true line boundary first" reversed)
  1310. (cons :tag "Set C-a and C-e separately"
  1311. (choice :tag "Special C-a"
  1312. (const :tag "off" nil)
  1313. (const :tag "on: after stars/bullet first" t)
  1314. (const :tag "reversed: before stars/bullet first" reversed))
  1315. (choice :tag "Special C-e"
  1316. (const :tag "off" nil)
  1317. (const :tag "on: before tags first" t)
  1318. (const :tag "reversed: after tags first" reversed)))))
  1319. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1320. (defcustom org-special-ctrl-k nil
  1321. "Non-nil means `C-k' will behave specially in headlines.
  1322. When nil, `C-k' will call the default `kill-line' command.
  1323. When t, the following will happen while the cursor is in the headline:
  1324. - When the cursor is at the beginning of a headline, kill the entire
  1325. line and possible the folded subtree below the line.
  1326. - When in the middle of the headline text, kill the headline up to the tags.
  1327. - When after the headline text, kill the tags."
  1328. :group 'org-edit-structure
  1329. :type 'boolean)
  1330. (defcustom org-ctrl-k-protect-subtree nil
  1331. "Non-nil means, do not delete a hidden subtree with C-k.
  1332. When set to the symbol `error', simply throw an error when C-k is
  1333. used to kill (part-of) a headline that has hidden text behind it.
  1334. Any other non-nil value will result in a query to the user, if it is
  1335. OK to kill that hidden subtree. When nil, kill without remorse."
  1336. :group 'org-edit-structure
  1337. :version "24.1"
  1338. :type '(choice
  1339. (const :tag "Do not protect hidden subtrees" nil)
  1340. (const :tag "Protect hidden subtrees with a security query" t)
  1341. (const :tag "Never kill a hidden subtree with C-k" error)))
  1342. (defcustom org-special-ctrl-o t
  1343. "Non-nil means, make `C-o' insert a row in tables."
  1344. :group 'org-edit-structure
  1345. :type 'boolean)
  1346. (defcustom org-catch-invisible-edits nil
  1347. "Check if in invisible region before inserting or deleting a character.
  1348. Valid values are:
  1349. nil Do not check, so just do invisible edits.
  1350. error Throw an error and do nothing.
  1351. show Make point visible, and do the requested edit.
  1352. show-and-error Make point visible, then throw an error and abort the edit.
  1353. smart Make point visible, and do insertion/deletion if it is
  1354. adjacent to visible text and the change feels predictable.
  1355. Never delete a previously invisible character or add in the
  1356. middle or right after an invisible region. Basically, this
  1357. allows insertion and backward-delete right before ellipses.
  1358. FIXME: maybe in this case we should not even show?"
  1359. :group 'org-edit-structure
  1360. :version "24.1"
  1361. :type '(choice
  1362. (const :tag "Do not check" nil)
  1363. (const :tag "Throw error when trying to edit" error)
  1364. (const :tag "Unhide, but do not do the edit" show-and-error)
  1365. (const :tag "Show invisible part and do the edit" show)
  1366. (const :tag "Be smart and do the right thing" smart)))
  1367. (defcustom org-yank-folded-subtrees t
  1368. "Non-nil means when yanking subtrees, fold them.
  1369. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1370. it starts with a heading and all other headings in it are either children
  1371. or siblings, then fold all the subtrees. However, do this only if no
  1372. text after the yank would be swallowed into a folded tree by this action."
  1373. :group 'org-edit-structure
  1374. :type 'boolean)
  1375. (defcustom org-yank-adjusted-subtrees nil
  1376. "Non-nil means when yanking subtrees, adjust the level.
  1377. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1378. this function for details."
  1379. :group 'org-edit-structure
  1380. :type 'boolean)
  1381. (defcustom org-M-RET-may-split-line '((default . t))
  1382. "Non-nil means M-RET will split the line at the cursor position.
  1383. When nil, it will go to the end of the line before making a
  1384. new line.
  1385. You may also set this option in a different way for different
  1386. contexts. Valid contexts are:
  1387. headline when creating a new headline
  1388. item when creating a new item
  1389. table in a table field
  1390. default the value to be used for all contexts not explicitly
  1391. customized"
  1392. :group 'org-structure
  1393. :group 'org-table
  1394. :type '(choice
  1395. (const :tag "Always" t)
  1396. (const :tag "Never" nil)
  1397. (repeat :greedy t :tag "Individual contexts"
  1398. (cons
  1399. (choice :tag "Context"
  1400. (const headline)
  1401. (const item)
  1402. (const table)
  1403. (const default))
  1404. (boolean)))))
  1405. (defcustom org-insert-heading-respect-content nil
  1406. "Non-nil means insert new headings after the current subtree.
  1407. When nil, the new heading is created directly after the current line.
  1408. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1409. this variable on for the duration of the command."
  1410. :group 'org-structure
  1411. :type 'boolean)
  1412. (defcustom org-blank-before-new-entry '((heading . auto)
  1413. (plain-list-item . auto))
  1414. "Should `org-insert-heading' leave a blank line before new heading/item?
  1415. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1416. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1417. which case Org will look at the surrounding headings/items and try to
  1418. make an intelligent decision whether to insert a blank line or not.
  1419. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1420. the setting here is ignored and no empty line is inserted to avoid breaking
  1421. the list structure."
  1422. :group 'org-edit-structure
  1423. :type '(list
  1424. (cons (const heading)
  1425. (choice (const :tag "Never" nil)
  1426. (const :tag "Always" t)
  1427. (const :tag "Auto" auto)))
  1428. (cons (const plain-list-item)
  1429. (choice (const :tag "Never" nil)
  1430. (const :tag "Always" t)
  1431. (const :tag "Auto" auto)))))
  1432. (defcustom org-insert-heading-hook nil
  1433. "Hook being run after inserting a new heading."
  1434. :group 'org-edit-structure
  1435. :type 'hook)
  1436. (defcustom org-enable-fixed-width-editor t
  1437. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1438. This currently only means they are never auto-wrapped.
  1439. When nil, such lines will be treated like ordinary lines."
  1440. :group 'org-edit-structure
  1441. :type 'boolean)
  1442. (defcustom org-goto-auto-isearch t
  1443. "Non-nil means typing characters in `org-goto' starts incremental search.
  1444. When nil, you can use these keybindings to navigate the buffer:
  1445. q Quit the org-goto interface
  1446. n Go to the next visible heading
  1447. p Go to the previous visible heading
  1448. f Go one heading forward on same level
  1449. b Go one heading backward on same level
  1450. u Go one heading up"
  1451. :group 'org-edit-structure
  1452. :type 'boolean)
  1453. (defgroup org-sparse-trees nil
  1454. "Options concerning sparse trees in Org-mode."
  1455. :tag "Org Sparse Trees"
  1456. :group 'org-structure)
  1457. (defcustom org-highlight-sparse-tree-matches t
  1458. "Non-nil means highlight all matches that define a sparse tree.
  1459. The highlights will automatically disappear the next time the buffer is
  1460. changed by an edit command."
  1461. :group 'org-sparse-trees
  1462. :type 'boolean)
  1463. (defcustom org-remove-highlights-with-change t
  1464. "Non-nil means any change to the buffer will remove temporary highlights.
  1465. Such highlights are created by `org-occur' and `org-clock-display'.
  1466. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1467. The highlights created by `org-toggle-latex-fragment' always need
  1468. `C-c C-x C-l' to be removed."
  1469. :group 'org-sparse-trees
  1470. :group 'org-time
  1471. :type 'boolean)
  1472. (defcustom org-occur-hook '(org-first-headline-recenter)
  1473. "Hook that is run after `org-occur' has constructed a sparse tree.
  1474. This can be used to recenter the window to show as much of the structure
  1475. as possible."
  1476. :group 'org-sparse-trees
  1477. :type 'hook)
  1478. (defgroup org-imenu-and-speedbar nil
  1479. "Options concerning imenu and speedbar in Org-mode."
  1480. :tag "Org Imenu and Speedbar"
  1481. :group 'org-structure)
  1482. (defcustom org-imenu-depth 2
  1483. "The maximum level for Imenu access to Org-mode headlines.
  1484. This also applied for speedbar access."
  1485. :group 'org-imenu-and-speedbar
  1486. :type 'integer)
  1487. (defgroup org-table nil
  1488. "Options concerning tables in Org-mode."
  1489. :tag "Org Table"
  1490. :group 'org)
  1491. (defcustom org-enable-table-editor 'optimized
  1492. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1493. When nil, such lines will be treated like ordinary lines.
  1494. When equal to the symbol `optimized', the table editor will be optimized to
  1495. do the following:
  1496. - Automatic overwrite mode in front of whitespace in table fields.
  1497. This makes the structure of the table stay in tact as long as the edited
  1498. field does not exceed the column width.
  1499. - Minimize the number of realigns. Normally, the table is aligned each time
  1500. TAB or RET are pressed to move to another field. With optimization this
  1501. happens only if changes to a field might have changed the column width.
  1502. Optimization requires replacing the functions `self-insert-command',
  1503. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1504. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1505. very good at guessing when a re-align will be necessary, but you can always
  1506. force one with \\[org-ctrl-c-ctrl-c].
  1507. If you would like to use the optimized version in Org-mode, but the
  1508. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1509. This variable can be used to turn on and off the table editor during a session,
  1510. but in order to toggle optimization, a restart is required.
  1511. See also the variable `org-table-auto-blank-field'."
  1512. :group 'org-table
  1513. :type '(choice
  1514. (const :tag "off" nil)
  1515. (const :tag "on" t)
  1516. (const :tag "on, optimized" optimized)))
  1517. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1518. (version<= emacs-version "24.1"))
  1519. "Non-nil means cluster self-insert commands for undo when possible.
  1520. If this is set, then, like in the Emacs command loop, 20 consecutive
  1521. characters will be undone together.
  1522. This is configurable, because there is some impact on typing performance."
  1523. :group 'org-table
  1524. :type 'boolean)
  1525. (defcustom org-table-tab-recognizes-table.el t
  1526. "Non-nil means TAB will automatically notice a table.el table.
  1527. When it sees such a table, it moves point into it and - if necessary -
  1528. calls `table-recognize-table'."
  1529. :group 'org-table-editing
  1530. :type 'boolean)
  1531. (defgroup org-link nil
  1532. "Options concerning links in Org-mode."
  1533. :tag "Org Link"
  1534. :group 'org)
  1535. (defvar org-link-abbrev-alist-local nil
  1536. "Buffer-local version of `org-link-abbrev-alist', which see.
  1537. The value of this is taken from the #+LINK lines.")
  1538. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1539. (defcustom org-link-abbrev-alist nil
  1540. "Alist of link abbreviations.
  1541. The car of each element is a string, to be replaced at the start of a link.
  1542. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1543. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1544. [[linkkey:tag][description]]
  1545. The 'linkkey' must be a word word, starting with a letter, followed
  1546. by letters, numbers, '-' or '_'.
  1547. If REPLACE is a string, the tag will simply be appended to create the link.
  1548. If the string contains \"%s\", the tag will be inserted there. If the string
  1549. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1550. at that point (see the function `url-hexify-string'). If the string contains
  1551. the specifier \"%(my-function)\", then the custom function `my-function' will
  1552. be invoked: this function takes the tag as its only argument and must return
  1553. a string.
  1554. REPLACE may also be a function that will be called with the tag as the
  1555. only argument to create the link, which should be returned as a string.
  1556. See the manual for examples."
  1557. :group 'org-link
  1558. :type '(repeat
  1559. (cons
  1560. (string :tag "Protocol")
  1561. (choice
  1562. (string :tag "Format")
  1563. (function)))))
  1564. (defcustom org-descriptive-links t
  1565. "Non-nil means Org will display descriptive links.
  1566. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1567. \"Org Website\", hiding the link itself and just displaying its
  1568. description. When set to `nil', Org will display the full links
  1569. literally.
  1570. You can interactively set the value of this variable by calling
  1571. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1572. :group 'org-link
  1573. :type 'boolean)
  1574. (defcustom org-link-file-path-type 'adaptive
  1575. "How the path name in file links should be stored.
  1576. Valid values are:
  1577. relative Relative to the current directory, i.e. the directory of the file
  1578. into which the link is being inserted.
  1579. absolute Absolute path, if possible with ~ for home directory.
  1580. noabbrev Absolute path, no abbreviation of home directory.
  1581. adaptive Use relative path for files in the current directory and sub-
  1582. directories of it. For other files, use an absolute path."
  1583. :group 'org-link
  1584. :type '(choice
  1585. (const relative)
  1586. (const absolute)
  1587. (const noabbrev)
  1588. (const adaptive)))
  1589. (defvaralias 'org-activate-links 'org-highlight-links)
  1590. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1591. "Types of links that should be highlighted in Org-mode files.
  1592. This is a list of symbols, each one of them leading to the
  1593. highlighting of a certain link type.
  1594. You can still open links that are not highlighted.
  1595. In principle, it does not hurt to turn on highlighting for all
  1596. link types. There may be a small gain when turning off unused
  1597. link types. The types are:
  1598. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1599. angle Links in angular brackets that may contain whitespace like
  1600. <bbdb:Carsten Dominik>.
  1601. plain Plain links in normal text, no whitespace, like http://google.com.
  1602. radio Text that is matched by a radio target, see manual for details.
  1603. tag Tag settings in a headline (link to tag search).
  1604. date Time stamps (link to calendar).
  1605. footnote Footnote labels.
  1606. If you set this variable during an Emacs session, use `org-mode-restart'
  1607. in the Org buffer so that the change takes effect."
  1608. :group 'org-link
  1609. :group 'org-appearance
  1610. :type '(set :greedy t
  1611. (const :tag "Double bracket links" bracket)
  1612. (const :tag "Angular bracket links" angle)
  1613. (const :tag "Plain text links" plain)
  1614. (const :tag "Radio target matches" radio)
  1615. (const :tag "Tags" tag)
  1616. (const :tag "Timestamps" date)
  1617. (const :tag "Footnotes" footnote)))
  1618. (defcustom org-make-link-description-function nil
  1619. "Function to use for generating link descriptions from links.
  1620. When nil, the link location will be used. This function must take
  1621. two parameters: the first one is the link, the second one is the
  1622. description generated by `org-insert-link'. The function should
  1623. return the description to use."
  1624. :group 'org-link
  1625. :type '(choice (const nil) (function)))
  1626. (defgroup org-link-store nil
  1627. "Options concerning storing links in Org-mode."
  1628. :tag "Org Store Link"
  1629. :group 'org-link)
  1630. (defcustom org-url-hexify-p t
  1631. "When non-nil, hexify URL when creating a link."
  1632. :type 'boolean
  1633. :version "24.3"
  1634. :group 'org-link-store)
  1635. (defcustom org-email-link-description-format "Email %c: %.30s"
  1636. "Format of the description part of a link to an email or usenet message.
  1637. The following %-escapes will be replaced by corresponding information:
  1638. %F full \"From\" field
  1639. %f name, taken from \"From\" field, address if no name
  1640. %T full \"To\" field
  1641. %t first name in \"To\" field, address if no name
  1642. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1643. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1644. %s subject
  1645. %d date
  1646. %m message-id.
  1647. You may use normal field width specification between the % and the letter.
  1648. This is for example useful to limit the length of the subject.
  1649. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1650. :group 'org-link-store
  1651. :type 'string)
  1652. (defcustom org-from-is-user-regexp
  1653. (let (r1 r2)
  1654. (when (and user-mail-address (not (string= user-mail-address "")))
  1655. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1656. (when (and user-full-name (not (string= user-full-name "")))
  1657. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1658. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1659. "Regexp matched against the \"From:\" header of an email or usenet message.
  1660. It should match if the message is from the user him/herself."
  1661. :group 'org-link-store
  1662. :type 'regexp)
  1663. (defcustom org-context-in-file-links t
  1664. "Non-nil means file links from `org-store-link' contain context.
  1665. A search string will be added to the file name with :: as separator and
  1666. used to find the context when the link is activated by the command
  1667. `org-open-at-point'. When this option is t, the entire active region
  1668. will be placed in the search string of the file link. If set to a
  1669. positive integer, only the first n lines of context will be stored.
  1670. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1671. negates this setting for the duration of the command."
  1672. :group 'org-link-store
  1673. :type '(choice boolean integer))
  1674. (defcustom org-keep-stored-link-after-insertion nil
  1675. "Non-nil means keep link in list for entire session.
  1676. The command `org-store-link' adds a link pointing to the current
  1677. location to an internal list. These links accumulate during a session.
  1678. The command `org-insert-link' can be used to insert links into any
  1679. Org-mode file (offering completion for all stored links). When this
  1680. option is nil, every link which has been inserted once using \\[org-insert-link]
  1681. will be removed from the list, to make completing the unused links
  1682. more efficient."
  1683. :group 'org-link-store
  1684. :type 'boolean)
  1685. (defgroup org-link-follow nil
  1686. "Options concerning following links in Org-mode."
  1687. :tag "Org Follow Link"
  1688. :group 'org-link)
  1689. (defcustom org-link-translation-function nil
  1690. "Function to translate links with different syntax to Org syntax.
  1691. This can be used to translate links created for example by the Planner
  1692. or emacs-wiki packages to Org syntax.
  1693. The function must accept two parameters, a TYPE containing the link
  1694. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1695. which is everything after the link protocol. It should return a cons
  1696. with possibly modified values of type and path.
  1697. Org contains a function for this, so if you set this variable to
  1698. `org-translate-link-from-planner', you should be able follow many
  1699. links created by planner."
  1700. :group 'org-link-follow
  1701. :type '(choice (const nil) (function)))
  1702. (defcustom org-follow-link-hook nil
  1703. "Hook that is run after a link has been followed."
  1704. :group 'org-link-follow
  1705. :type 'hook)
  1706. (defcustom org-tab-follows-link nil
  1707. "Non-nil means on links TAB will follow the link.
  1708. Needs to be set before org.el is loaded.
  1709. This really should not be used, it does not make sense, and the
  1710. implementation is bad."
  1711. :group 'org-link-follow
  1712. :type 'boolean)
  1713. (defcustom org-return-follows-link nil
  1714. "Non-nil means on links RET will follow the link.
  1715. In tables, the special behavior of RET has precedence."
  1716. :group 'org-link-follow
  1717. :type 'boolean)
  1718. (defcustom org-mouse-1-follows-link
  1719. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1720. "Non-nil means mouse-1 on a link will follow the link.
  1721. A longer mouse click will still set point. Does not work on XEmacs.
  1722. Needs to be set before org.el is loaded."
  1723. :group 'org-link-follow
  1724. :version "24.4"
  1725. :package-version '(Org . "8.3")
  1726. :type '(choice
  1727. (const :tag "A double click follows the link" double)
  1728. (const :tag "Unconditionally follow the link with mouse-1" t)
  1729. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1730. (defcustom org-mark-ring-length 4
  1731. "Number of different positions to be recorded in the ring.
  1732. Changing this requires a restart of Emacs to work correctly."
  1733. :group 'org-link-follow
  1734. :type 'integer)
  1735. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1736. "Non-nil means internal links in Org files must exactly match a headline.
  1737. When nil, the link search tries to match a phrase with all words
  1738. in the search text."
  1739. :group 'org-link-follow
  1740. :version "24.1"
  1741. :type '(choice
  1742. (const :tag "Use fuzzy text search" nil)
  1743. (const :tag "Match only exact headline" t)
  1744. (const :tag "Match exact headline or query to create it"
  1745. query-to-create)))
  1746. (defcustom org-link-frame-setup
  1747. '((vm . vm-visit-folder-other-frame)
  1748. (vm-imap . vm-visit-imap-folder-other-frame)
  1749. (gnus . org-gnus-no-new-news)
  1750. (file . find-file-other-window)
  1751. (wl . wl-other-frame))
  1752. "Setup the frame configuration for following links.
  1753. When following a link with Emacs, it may often be useful to display
  1754. this link in another window or frame. This variable can be used to
  1755. set this up for the different types of links.
  1756. For VM, use any of
  1757. `vm-visit-folder'
  1758. `vm-visit-folder-other-window'
  1759. `vm-visit-folder-other-frame'
  1760. For Gnus, use any of
  1761. `gnus'
  1762. `gnus-other-frame'
  1763. `org-gnus-no-new-news'
  1764. For FILE, use any of
  1765. `find-file'
  1766. `find-file-other-window'
  1767. `find-file-other-frame'
  1768. For Wanderlust use any of
  1769. `wl'
  1770. `wl-other-frame'
  1771. For the calendar, use the variable `calendar-setup'.
  1772. For BBDB, it is currently only possible to display the matches in
  1773. another window."
  1774. :group 'org-link-follow
  1775. :type '(list
  1776. (cons (const vm)
  1777. (choice
  1778. (const vm-visit-folder)
  1779. (const vm-visit-folder-other-window)
  1780. (const vm-visit-folder-other-frame)))
  1781. (cons (const vm-imap)
  1782. (choice
  1783. (const vm-visit-imap-folder)
  1784. (const vm-visit-imap-folder-other-window)
  1785. (const vm-visit-imap-folder-other-frame)))
  1786. (cons (const gnus)
  1787. (choice
  1788. (const gnus)
  1789. (const gnus-other-frame)
  1790. (const org-gnus-no-new-news)))
  1791. (cons (const file)
  1792. (choice
  1793. (const find-file)
  1794. (const find-file-other-window)
  1795. (const find-file-other-frame)))
  1796. (cons (const wl)
  1797. (choice
  1798. (const wl)
  1799. (const wl-other-frame)))))
  1800. (defcustom org-display-internal-link-with-indirect-buffer nil
  1801. "Non-nil means use indirect buffer to display infile links.
  1802. Activating internal links (from one location in a file to another location
  1803. in the same file) normally just jumps to the location. When the link is
  1804. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1805. is displayed in
  1806. another window. When this option is set, the other window actually displays
  1807. an indirect buffer clone of the current buffer, to avoid any visibility
  1808. changes to the current buffer."
  1809. :group 'org-link-follow
  1810. :type 'boolean)
  1811. (defcustom org-open-non-existing-files nil
  1812. "Non-nil means `org-open-file' will open non-existing files.
  1813. When nil, an error will be generated.
  1814. This variable applies only to external applications because they
  1815. might choke on non-existing files. If the link is to a file that
  1816. will be opened in Emacs, the variable is ignored."
  1817. :group 'org-link-follow
  1818. :type 'boolean)
  1819. (defcustom org-open-directory-means-index-dot-org nil
  1820. "Non-nil means a link to a directory really means to index.org.
  1821. When nil, following a directory link will run dired or open a finder/explorer
  1822. window on that directory."
  1823. :group 'org-link-follow
  1824. :type 'boolean)
  1825. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1826. "Non-nil means ask for confirmation before executing shell links.
  1827. Shell links can be dangerous: just think about a link
  1828. [[shell:rm -rf ~/*][Google Search]]
  1829. This link would show up in your Org-mode document as \"Google Search\",
  1830. but really it would remove your entire home directory.
  1831. Therefore we advise against setting this variable to nil.
  1832. Just change it to `y-or-n-p' if you want to confirm with a
  1833. single keystroke rather than having to type \"yes\"."
  1834. :group 'org-link-follow
  1835. :type '(choice
  1836. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1837. (const :tag "with y-or-n (faster)" y-or-n-p)
  1838. (const :tag "no confirmation (dangerous)" nil)))
  1839. (put 'org-confirm-shell-link-function
  1840. 'safe-local-variable
  1841. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1842. (defcustom org-confirm-shell-link-not-regexp ""
  1843. "A regexp to skip confirmation for shell links."
  1844. :group 'org-link-follow
  1845. :version "24.1"
  1846. :type 'regexp)
  1847. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1848. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1849. Elisp links can be dangerous: just think about a link
  1850. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1851. This link would show up in your Org-mode document as \"Google Search\",
  1852. but really it would remove your entire home directory.
  1853. Therefore we advise against setting this variable to nil.
  1854. Just change it to `y-or-n-p' if you want to confirm with a
  1855. single keystroke rather than having to type \"yes\"."
  1856. :group 'org-link-follow
  1857. :type '(choice
  1858. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1859. (const :tag "with y-or-n (faster)" y-or-n-p)
  1860. (const :tag "no confirmation (dangerous)" nil)))
  1861. (put 'org-confirm-shell-link-function
  1862. 'safe-local-variable
  1863. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1864. (defcustom org-confirm-elisp-link-not-regexp ""
  1865. "A regexp to skip confirmation for Elisp links."
  1866. :group 'org-link-follow
  1867. :version "24.1"
  1868. :type 'regexp)
  1869. (defconst org-file-apps-defaults-gnu
  1870. '((remote . emacs)
  1871. (system . mailcap)
  1872. (t . mailcap))
  1873. "Default file applications on a UNIX or GNU/Linux system.
  1874. See `org-file-apps'.")
  1875. (defconst org-file-apps-defaults-macosx
  1876. '((remote . emacs)
  1877. (t . "open %s")
  1878. (system . "open %s")
  1879. ("ps.gz" . "gv %s")
  1880. ("eps.gz" . "gv %s")
  1881. ("dvi" . "xdvi %s")
  1882. ("fig" . "xfig %s"))
  1883. "Default file applications on a MacOS X system.
  1884. The system \"open\" is known as a default, but we use X11 applications
  1885. for some files for which the OS does not have a good default.
  1886. See `org-file-apps'.")
  1887. (defconst org-file-apps-defaults-windowsnt
  1888. (list
  1889. '(remote . emacs)
  1890. (cons t
  1891. (list (if (featurep 'xemacs)
  1892. 'mswindows-shell-execute
  1893. 'w32-shell-execute)
  1894. "open" 'file))
  1895. (cons 'system
  1896. (list (if (featurep 'xemacs)
  1897. 'mswindows-shell-execute
  1898. 'w32-shell-execute)
  1899. "open" 'file)))
  1900. "Default file applications on a Windows NT system.
  1901. The system \"open\" is used for most files.
  1902. See `org-file-apps'.")
  1903. (defcustom org-file-apps
  1904. '((auto-mode . emacs)
  1905. ("\\.mm\\'" . default)
  1906. ("\\.x?html?\\'" . default)
  1907. ("\\.pdf\\'" . default))
  1908. "External applications for opening `file:path' items in a document.
  1909. Org-mode uses system defaults for different file types, but
  1910. you can use this variable to set the application for a given file
  1911. extension. The entries in this list are cons cells where the car identifies
  1912. files and the cdr the corresponding command. Possible values for the
  1913. file identifier are
  1914. \"string\" A string as a file identifier can be interpreted in different
  1915. ways, depending on its contents:
  1916. - Alphanumeric characters only:
  1917. Match links with this file extension.
  1918. Example: (\"pdf\" . \"evince %s\")
  1919. to open PDFs with evince.
  1920. - Regular expression: Match links where the
  1921. filename matches the regexp. If you want to
  1922. use groups here, use shy groups.
  1923. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1924. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1925. to open *.html and *.xhtml with firefox.
  1926. - Regular expression which contains (non-shy) groups:
  1927. Match links where the whole link, including \"::\", and
  1928. anything after that, matches the regexp.
  1929. In a custom command string, %1, %2, etc. are replaced with
  1930. the parts of the link that were matched by the groups.
  1931. For backwards compatibility, if a command string is given
  1932. that does not use any of the group matches, this case is
  1933. handled identically to the second one (i.e. match against
  1934. file name only).
  1935. In a custom lisp form, you can access the group matches with
  1936. (match-string n link).
  1937. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1938. to open [[file:document.pdf::5]] with evince at page 5.
  1939. `directory' Matches a directory
  1940. `remote' Matches a remote file, accessible through tramp or efs.
  1941. Remote files most likely should be visited through Emacs
  1942. because external applications cannot handle such paths.
  1943. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1944. so all files Emacs knows how to handle. Using this with
  1945. command `emacs' will open most files in Emacs. Beware that this
  1946. will also open html files inside Emacs, unless you add
  1947. (\"html\" . default) to the list as well.
  1948. t Default for files not matched by any of the other options.
  1949. `system' The system command to open files, like `open' on Windows
  1950. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1951. that will be selected if you call `C-c C-o' with a double
  1952. \\[universal-argument] \\[universal-argument] prefix.
  1953. Possible values for the command are:
  1954. `emacs' The file will be visited by the current Emacs process.
  1955. `default' Use the default application for this file type, which is the
  1956. association for t in the list, most likely in the system-specific
  1957. part.
  1958. This can be used to overrule an unwanted setting in the
  1959. system-specific variable.
  1960. `system' Use the system command for opening files, like \"open\".
  1961. This command is specified by the entry whose car is `system'.
  1962. Most likely, the system-specific version of this variable
  1963. does define this command, but you can overrule/replace it
  1964. here.
  1965. string A command to be executed by a shell; %s will be replaced
  1966. by the path to the file.
  1967. sexp A Lisp form which will be evaluated. The file path will
  1968. be available in the Lisp variable `file'.
  1969. For more examples, see the system specific constants
  1970. `org-file-apps-defaults-macosx'
  1971. `org-file-apps-defaults-windowsnt'
  1972. `org-file-apps-defaults-gnu'."
  1973. :group 'org-link-follow
  1974. :type '(repeat
  1975. (cons (choice :value ""
  1976. (string :tag "Extension")
  1977. (const :tag "System command to open files" system)
  1978. (const :tag "Default for unrecognized files" t)
  1979. (const :tag "Remote file" remote)
  1980. (const :tag "Links to a directory" directory)
  1981. (const :tag "Any files that have Emacs modes"
  1982. auto-mode))
  1983. (choice :value ""
  1984. (const :tag "Visit with Emacs" emacs)
  1985. (const :tag "Use default" default)
  1986. (const :tag "Use the system command" system)
  1987. (string :tag "Command")
  1988. (sexp :tag "Lisp form")))))
  1989. (defcustom org-doi-server-url "http://dx.doi.org/"
  1990. "The URL of the DOI server."
  1991. :type 'string
  1992. :version "24.3"
  1993. :group 'org-link-follow)
  1994. (defgroup org-refile nil
  1995. "Options concerning refiling entries in Org-mode."
  1996. :tag "Org Refile"
  1997. :group 'org)
  1998. (defcustom org-directory "~/org"
  1999. "Directory with org files.
  2000. This is just a default location to look for Org files. There is no need
  2001. at all to put your files into this directory. It is only used in the
  2002. following situations:
  2003. 1. When a capture template specifies a target file that is not an
  2004. absolute path. The path will then be interpreted relative to
  2005. `org-directory'
  2006. 2. When a capture note is filed away in an interactive way (when exiting the
  2007. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  2008. with `org-directory' as the default path."
  2009. :group 'org-refile
  2010. :group 'org-capture
  2011. :type 'directory)
  2012. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2013. "Default target for storing notes.
  2014. Used as a fall back file for org-capture.el, for templates that
  2015. do not specify a target file."
  2016. :group 'org-refile
  2017. :group 'org-capture
  2018. :type '(choice
  2019. (const :tag "Default from remember-data-file" nil)
  2020. file))
  2021. (defcustom org-goto-interface 'outline
  2022. "The default interface to be used for `org-goto'.
  2023. Allowed values are:
  2024. outline The interface shows an outline of the relevant file
  2025. and the correct heading is found by moving through
  2026. the outline or by searching with incremental search.
  2027. outline-path-completion Headlines in the current buffer are offered via
  2028. completion. This is the interface also used by
  2029. the refile command."
  2030. :group 'org-refile
  2031. :type '(choice
  2032. (const :tag "Outline" outline)
  2033. (const :tag "Outline-path-completion" outline-path-completion)))
  2034. (defcustom org-goto-max-level 5
  2035. "Maximum target level when running `org-goto' with refile interface."
  2036. :group 'org-refile
  2037. :type 'integer)
  2038. (defcustom org-reverse-note-order nil
  2039. "Non-nil means store new notes at the beginning of a file or entry.
  2040. When nil, new notes will be filed to the end of a file or entry.
  2041. This can also be a list with cons cells of regular expressions that
  2042. are matched against file names, and values."
  2043. :group 'org-capture
  2044. :group 'org-refile
  2045. :type '(choice
  2046. (const :tag "Reverse always" t)
  2047. (const :tag "Reverse never" nil)
  2048. (repeat :tag "By file name regexp"
  2049. (cons regexp boolean))))
  2050. (defcustom org-log-refile nil
  2051. "Information to record when a task is refiled.
  2052. Possible values are:
  2053. nil Don't add anything
  2054. time Add a time stamp to the task
  2055. note Prompt for a note and add it with template `org-log-note-headings'
  2056. This option can also be set with on a per-file-basis with
  2057. #+STARTUP: nologrefile
  2058. #+STARTUP: logrefile
  2059. #+STARTUP: lognoterefile
  2060. You can have local logging settings for a subtree by setting the LOGGING
  2061. property to one or more of these keywords.
  2062. When bulk-refiling from the agenda, the value `note' is forbidden and
  2063. will temporarily be changed to `time'."
  2064. :group 'org-refile
  2065. :group 'org-progress
  2066. :version "24.1"
  2067. :type '(choice
  2068. (const :tag "No logging" nil)
  2069. (const :tag "Record timestamp" time)
  2070. (const :tag "Record timestamp with note." note)))
  2071. (defcustom org-refile-targets nil
  2072. "Targets for refiling entries with \\[org-refile].
  2073. This is a list of cons cells. Each cell contains:
  2074. - a specification of the files to be considered, either a list of files,
  2075. or a symbol whose function or variable value will be used to retrieve
  2076. a file name or a list of file names. If you use `org-agenda-files' for
  2077. that, all agenda files will be scanned for targets. Nil means consider
  2078. headings in the current buffer.
  2079. - A specification of how to find candidate refile targets. This may be
  2080. any of:
  2081. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2082. This tag has to be present in all target headlines, inheritance will
  2083. not be considered.
  2084. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2085. todo keyword.
  2086. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2087. headlines that are refiling targets.
  2088. - a cons cell (:level . N). Any headline of level N is considered 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. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2092. Note that, when `org-odd-levels-only' is set, level corresponds to
  2093. order in hierarchy, not to the number of stars.
  2094. Each element of this list generates a set of possible targets.
  2095. The union of these sets is presented (with completion) to
  2096. the user by `org-refile'.
  2097. You can set the variable `org-refile-target-verify-function' to a function
  2098. to verify each headline found by the simple criteria above.
  2099. When this variable is nil, all top-level headlines in the current buffer
  2100. are used, equivalent to the value `((nil . (:level . 1))'."
  2101. :group 'org-refile
  2102. :type '(repeat
  2103. (cons
  2104. (choice :value org-agenda-files
  2105. (const :tag "All agenda files" org-agenda-files)
  2106. (const :tag "Current buffer" nil)
  2107. (function) (variable) (file))
  2108. (choice :tag "Identify target headline by"
  2109. (cons :tag "Specific tag" (const :value :tag) (string))
  2110. (cons :tag "TODO keyword" (const :value :todo) (string))
  2111. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2112. (cons :tag "Level number" (const :value :level) (integer))
  2113. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2114. (defcustom org-refile-target-verify-function nil
  2115. "Function to verify if the headline at point should be a refile target.
  2116. The function will be called without arguments, with point at the
  2117. beginning of the headline. It should return t and leave point
  2118. where it is if the headline is a valid target for refiling.
  2119. If the target should not be selected, the function must return nil.
  2120. In addition to this, it may move point to a place from where the search
  2121. should be continued. For example, the function may decide that the entire
  2122. subtree of the current entry should be excluded and move point to the end
  2123. of the subtree."
  2124. :group 'org-refile
  2125. :type '(choice
  2126. (const nil)
  2127. (function)))
  2128. (defcustom org-refile-use-cache nil
  2129. "Non-nil means cache refile targets to speed up the process.
  2130. The cache for a particular file will be updated automatically when
  2131. the buffer has been killed, or when any of the marker used for flagging
  2132. refile targets no longer points at a live buffer.
  2133. If you have added new entries to a buffer that might themselves be targets,
  2134. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2135. find that easier, `C-u C-u C-u C-c C-w'."
  2136. :group 'org-refile
  2137. :version "24.1"
  2138. :type 'boolean)
  2139. (defcustom org-refile-use-outline-path nil
  2140. "Non-nil means provide refile targets as paths.
  2141. So a level 3 headline will be available as level1/level2/level3.
  2142. When the value is `file', also include the file name (without directory)
  2143. into the path. In this case, you can also stop the completion after
  2144. the file name, to get entries inserted as top level in the file.
  2145. When `full-file-path', include the full file path."
  2146. :group 'org-refile
  2147. :type '(choice
  2148. (const :tag "Not" nil)
  2149. (const :tag "Yes" t)
  2150. (const :tag "Start with file name" file)
  2151. (const :tag "Start with full file path" full-file-path)))
  2152. (defcustom org-outline-path-complete-in-steps t
  2153. "Non-nil means complete the outline path in hierarchical steps.
  2154. When Org-mode uses the refile interface to select an outline path
  2155. \(see variable `org-refile-use-outline-path'), the completion of
  2156. the path can be done is a single go, or if can be done in steps down
  2157. the headline hierarchy. Going in steps is probably the best if you
  2158. do not use a special completion package like `ido' or `icicles'.
  2159. However, when using these packages, going in one step can be very
  2160. fast, while still showing the whole path to the entry."
  2161. :group 'org-refile
  2162. :type 'boolean)
  2163. (defcustom org-refile-allow-creating-parent-nodes nil
  2164. "Non-nil means allow to create new nodes as refile targets.
  2165. New nodes are then created by adding \"/new node name\" to the completion
  2166. of an existing node. When the value of this variable is `confirm',
  2167. new node creation must be confirmed by the user (recommended).
  2168. When nil, the completion must match an existing entry.
  2169. Note that, if the new heading is not seen by the criteria
  2170. listed in `org-refile-targets', multiple instances of the same
  2171. heading would be created by trying again to file under the new
  2172. heading."
  2173. :group 'org-refile
  2174. :type '(choice
  2175. (const :tag "Never" nil)
  2176. (const :tag "Always" t)
  2177. (const :tag "Prompt for confirmation" confirm)))
  2178. (defcustom org-refile-active-region-within-subtree nil
  2179. "Non-nil means also refile active region within a subtree.
  2180. By default `org-refile' doesn't allow refiling regions if they
  2181. don't contain a set of subtrees, but it might be convenient to
  2182. do so sometimes: in that case, the first line of the region is
  2183. converted to a headline before refiling."
  2184. :group 'org-refile
  2185. :version "24.1"
  2186. :type 'boolean)
  2187. (defgroup org-todo nil
  2188. "Options concerning TODO items in Org-mode."
  2189. :tag "Org TODO"
  2190. :group 'org)
  2191. (defgroup org-progress nil
  2192. "Options concerning Progress logging in Org-mode."
  2193. :tag "Org Progress"
  2194. :group 'org-time)
  2195. (defvar org-todo-interpretation-widgets
  2196. '((:tag "Sequence (cycling hits every state)" sequence)
  2197. (:tag "Type (cycling directly to DONE)" type))
  2198. "The available interpretation symbols for customizing `org-todo-keywords'.
  2199. Interested libraries should add to this list.")
  2200. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2201. "List of TODO entry keyword sequences and their interpretation.
  2202. \\<org-mode-map>This is a list of sequences.
  2203. Each sequence starts with a symbol, either `sequence' or `type',
  2204. indicating if the keywords should be interpreted as a sequence of
  2205. action steps, or as different types of TODO items. The first
  2206. keywords are states requiring action - these states will select a headline
  2207. for inclusion into the global TODO list Org-mode produces. If one of
  2208. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2209. signify that no further action is necessary. If \"|\" is not found,
  2210. the last keyword is treated as the only DONE state of the sequence.
  2211. The command \\[org-todo] cycles an entry through these states, and one
  2212. additional state where no keyword is present. For details about this
  2213. cycling, see the manual.
  2214. TODO keywords and interpretation can also be set on a per-file basis with
  2215. the special #+SEQ_TODO and #+TYP_TODO lines.
  2216. Each keyword can optionally specify a character for fast state selection
  2217. \(in combination with the variable `org-use-fast-todo-selection')
  2218. and specifiers for state change logging, using the same syntax that
  2219. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2220. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2221. indicates to record a time stamp each time this state is selected.
  2222. Each keyword may also specify if a timestamp or a note should be
  2223. recorded when entering or leaving the state, by adding additional
  2224. characters in the parenthesis after the keyword. This looks like this:
  2225. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2226. record only the time of the state change. With X and Y being either
  2227. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2228. Y when leaving the state if and only if the *target* state does not
  2229. define X. You may omit any of the fast-selection key or X or /Y,
  2230. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2231. For backward compatibility, this variable may also be just a list
  2232. of keywords. In this case the interpretation (sequence or type) will be
  2233. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2234. :group 'org-todo
  2235. :group 'org-keywords
  2236. :type '(choice
  2237. (repeat :tag "Old syntax, just keywords"
  2238. (string :tag "Keyword"))
  2239. (repeat :tag "New syntax"
  2240. (cons
  2241. (choice
  2242. :tag "Interpretation"
  2243. ;;Quick and dirty way to see
  2244. ;;`org-todo-interpretations'. This takes the
  2245. ;;place of item arguments
  2246. :convert-widget
  2247. (lambda (widget)
  2248. (widget-put widget
  2249. :args (mapcar
  2250. (lambda (x)
  2251. (widget-convert
  2252. (cons 'const x)))
  2253. org-todo-interpretation-widgets))
  2254. widget))
  2255. (repeat
  2256. (string :tag "Keyword"))))))
  2257. (defvar org-todo-keywords-1 nil
  2258. "All TODO and DONE keywords active in a buffer.")
  2259. (make-variable-buffer-local 'org-todo-keywords-1)
  2260. (defvar org-todo-keywords-for-agenda nil)
  2261. (defvar org-done-keywords-for-agenda nil)
  2262. (defvar org-todo-keyword-alist-for-agenda nil)
  2263. (defvar org-tag-alist-for-agenda nil
  2264. "Alist of all tags from all agenda files.")
  2265. (defvar org-tag-groups-alist-for-agenda nil
  2266. "Alist of all groups tags from all current agenda files.")
  2267. (defvar org-tag-groups-alist nil)
  2268. (make-variable-buffer-local 'org-tag-groups-alist)
  2269. (defvar org-agenda-contributing-files nil)
  2270. (defvar org-not-done-keywords nil)
  2271. (make-variable-buffer-local 'org-not-done-keywords)
  2272. (defvar org-done-keywords nil)
  2273. (make-variable-buffer-local 'org-done-keywords)
  2274. (defvar org-todo-heads nil)
  2275. (make-variable-buffer-local 'org-todo-heads)
  2276. (defvar org-todo-sets nil)
  2277. (make-variable-buffer-local 'org-todo-sets)
  2278. (defvar org-todo-log-states nil)
  2279. (make-variable-buffer-local 'org-todo-log-states)
  2280. (defvar org-todo-kwd-alist nil)
  2281. (make-variable-buffer-local 'org-todo-kwd-alist)
  2282. (defvar org-todo-key-alist nil)
  2283. (make-variable-buffer-local 'org-todo-key-alist)
  2284. (defvar org-todo-key-trigger nil)
  2285. (make-variable-buffer-local 'org-todo-key-trigger)
  2286. (defcustom org-todo-interpretation 'sequence
  2287. "Controls how TODO keywords are interpreted.
  2288. This variable is in principle obsolete and is only used for
  2289. backward compatibility, if the interpretation of todo keywords is
  2290. not given already in `org-todo-keywords'. See that variable for
  2291. more information."
  2292. :group 'org-todo
  2293. :group 'org-keywords
  2294. :type '(choice (const sequence)
  2295. (const type)))
  2296. (defcustom org-use-fast-todo-selection t
  2297. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2298. This variable describes if and under what circumstances the cycling
  2299. mechanism for TODO keywords will be replaced by a single-key, direct
  2300. selection scheme.
  2301. When nil, fast selection is never used.
  2302. When the symbol `prefix', it will be used when `org-todo' is called
  2303. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2304. `C-u t' in an agenda buffer.
  2305. When t, fast selection is used by default. In this case, the prefix
  2306. argument forces cycling instead.
  2307. In all cases, the special interface is only used if access keys have
  2308. actually been assigned by the user, i.e. if keywords in the configuration
  2309. are followed by a letter in parenthesis, like TODO(t)."
  2310. :group 'org-todo
  2311. :type '(choice
  2312. (const :tag "Never" nil)
  2313. (const :tag "By default" t)
  2314. (const :tag "Only with C-u C-c C-t" prefix)))
  2315. (defcustom org-provide-todo-statistics t
  2316. "Non-nil means update todo statistics after insert and toggle.
  2317. ALL-HEADLINES means update todo statistics by including headlines
  2318. with no TODO keyword as well, counting them as not done.
  2319. A list of TODO keywords means the same, but skip keywords that are
  2320. not in this list.
  2321. When set to a list of two lists, the first list contains keywords
  2322. to consider as TODO keywords, the second list contains keywords
  2323. to consider as DONE keywords.
  2324. When this is set, todo statistics is updated in the parent of the
  2325. current entry each time a todo state is changed."
  2326. :group 'org-todo
  2327. :type '(choice
  2328. (const :tag "Yes, only for TODO entries" t)
  2329. (const :tag "Yes, including all entries" all-headlines)
  2330. (repeat :tag "Yes, for TODOs in this list"
  2331. (string :tag "TODO keyword"))
  2332. (list :tag "Yes, for TODOs and DONEs in these lists"
  2333. (repeat (string :tag "TODO keyword"))
  2334. (repeat (string :tag "DONE keyword")))
  2335. (other :tag "No TODO statistics" nil)))
  2336. (defcustom org-hierarchical-todo-statistics t
  2337. "Non-nil means TODO statistics covers just direct children.
  2338. When nil, all entries in the subtree are considered.
  2339. This has only an effect if `org-provide-todo-statistics' is set.
  2340. To set this to nil for only a single subtree, use a COOKIE_DATA
  2341. property and include the word \"recursive\" into the value."
  2342. :group 'org-todo
  2343. :type 'boolean)
  2344. (defcustom org-after-todo-state-change-hook nil
  2345. "Hook which is run after the state of a TODO item was changed.
  2346. The new state (a string with a TODO keyword, or nil) is available in the
  2347. Lisp variable `org-state'."
  2348. :group 'org-todo
  2349. :type 'hook)
  2350. (defvar org-blocker-hook nil
  2351. "Hook for functions that are allowed to block a state change.
  2352. Functions in this hook should not modify the buffer.
  2353. Each function gets as its single argument a property list,
  2354. see `org-trigger-hook' for more information about this list.
  2355. If any of the functions in this hook returns nil, the state change
  2356. is blocked.")
  2357. (defvar org-trigger-hook nil
  2358. "Hook for functions that are triggered by a state change.
  2359. Each function gets as its single argument a property list with at
  2360. least the following elements:
  2361. (:type type-of-change :position pos-at-entry-start
  2362. :from old-state :to new-state)
  2363. Depending on the type, more properties may be present.
  2364. This mechanism is currently implemented for:
  2365. TODO state changes
  2366. ------------------
  2367. :type todo-state-change
  2368. :from previous state (keyword as a string), or nil, or a symbol
  2369. 'todo' or 'done', to indicate the general type of state.
  2370. :to new state, like in :from")
  2371. (defcustom org-enforce-todo-dependencies nil
  2372. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2373. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2374. be blocked if any prior sibling is not yet done.
  2375. Finally, if the parent is blocked because of ordered siblings of its own,
  2376. the child will also be blocked."
  2377. :set (lambda (var val)
  2378. (set var val)
  2379. (if val
  2380. (add-hook 'org-blocker-hook
  2381. 'org-block-todo-from-children-or-siblings-or-parent)
  2382. (remove-hook 'org-blocker-hook
  2383. 'org-block-todo-from-children-or-siblings-or-parent)))
  2384. :group 'org-todo
  2385. :type 'boolean)
  2386. (defcustom org-enforce-todo-checkbox-dependencies nil
  2387. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2388. When this is nil, checkboxes have no influence on switching TODO states.
  2389. When non-nil, you first need to check off all check boxes before the TODO
  2390. entry can be switched to DONE.
  2391. This variable needs to be set before org.el is loaded, and you need to
  2392. restart Emacs after a change to make the change effective. The only way
  2393. to change is while Emacs is running is through the customize interface."
  2394. :set (lambda (var val)
  2395. (set var val)
  2396. (if val
  2397. (add-hook 'org-blocker-hook
  2398. 'org-block-todo-from-checkboxes)
  2399. (remove-hook 'org-blocker-hook
  2400. 'org-block-todo-from-checkboxes)))
  2401. :group 'org-todo
  2402. :type 'boolean)
  2403. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2404. "Non-nil means inserting a TODO heading is treated as state change.
  2405. So when the command \\[org-insert-todo-heading] is used, state change
  2406. logging will apply if appropriate. When nil, the new TODO item will
  2407. be inserted directly, and no logging will take place."
  2408. :group 'org-todo
  2409. :type 'boolean)
  2410. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2411. "Non-nil means switching TODO states with S-cursor counts as state change.
  2412. This is the default behavior. However, setting this to nil allows a
  2413. convenient way to select a TODO state and bypass any logging associated
  2414. with that."
  2415. :group 'org-todo
  2416. :type 'boolean)
  2417. (defcustom org-todo-state-tags-triggers nil
  2418. "Tag changes that should be triggered by TODO state changes.
  2419. This is a list. Each entry is
  2420. (state-change (tag . flag) .......)
  2421. State-change can be a string with a state, and empty string to indicate the
  2422. state that has no TODO keyword, or it can be one of the symbols `todo'
  2423. or `done', meaning any not-done or done state, respectively."
  2424. :group 'org-todo
  2425. :group 'org-tags
  2426. :type '(repeat
  2427. (cons (choice :tag "When changing to"
  2428. (const :tag "Not-done state" todo)
  2429. (const :tag "Done state" done)
  2430. (string :tag "State"))
  2431. (repeat
  2432. (cons :tag "Tag action"
  2433. (string :tag "Tag")
  2434. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2435. (defcustom org-log-done nil
  2436. "Information to record when a task moves to the DONE state.
  2437. Possible values are:
  2438. nil Don't add anything, just change the keyword
  2439. time Add a time stamp to the task
  2440. note Prompt for a note and add it with template `org-log-note-headings'
  2441. This option can also be set with on a per-file-basis with
  2442. #+STARTUP: nologdone
  2443. #+STARTUP: logdone
  2444. #+STARTUP: lognotedone
  2445. You can have local logging settings for a subtree by setting the LOGGING
  2446. property to one or more of these keywords."
  2447. :group 'org-todo
  2448. :group 'org-progress
  2449. :type '(choice
  2450. (const :tag "No logging" nil)
  2451. (const :tag "Record CLOSED timestamp" time)
  2452. (const :tag "Record CLOSED timestamp with note." note)))
  2453. ;; Normalize old uses of org-log-done.
  2454. (cond
  2455. ((eq org-log-done t) (setq org-log-done 'time))
  2456. ((and (listp org-log-done) (memq 'done org-log-done))
  2457. (setq org-log-done 'note)))
  2458. (defcustom org-log-reschedule nil
  2459. "Information to record when the scheduling date of a tasks is modified.
  2460. Possible values are:
  2461. nil Don't add anything, just change the date
  2462. time Add a time stamp to the task
  2463. note Prompt for a note and add it with template `org-log-note-headings'
  2464. This option can also be set with on a per-file-basis with
  2465. #+STARTUP: nologreschedule
  2466. #+STARTUP: logreschedule
  2467. #+STARTUP: lognotereschedule"
  2468. :group 'org-todo
  2469. :group 'org-progress
  2470. :type '(choice
  2471. (const :tag "No logging" nil)
  2472. (const :tag "Record timestamp" time)
  2473. (const :tag "Record timestamp with note." note)))
  2474. (defcustom org-log-redeadline nil
  2475. "Information to record when the deadline date of a tasks is modified.
  2476. Possible values are:
  2477. nil Don't add anything, just change the date
  2478. time Add a time stamp to the task
  2479. note Prompt for a note and add it with template `org-log-note-headings'
  2480. This option can also be set with on a per-file-basis with
  2481. #+STARTUP: nologredeadline
  2482. #+STARTUP: logredeadline
  2483. #+STARTUP: lognoteredeadline
  2484. You can have local logging settings for a subtree by setting the LOGGING
  2485. property to one or more of these keywords."
  2486. :group 'org-todo
  2487. :group 'org-progress
  2488. :type '(choice
  2489. (const :tag "No logging" nil)
  2490. (const :tag "Record timestamp" time)
  2491. (const :tag "Record timestamp with note." note)))
  2492. (defcustom org-log-note-clock-out nil
  2493. "Non-nil means record a note when clocking out of an item.
  2494. This can also be configured on a per-file basis by adding one of
  2495. the following lines anywhere in the buffer:
  2496. #+STARTUP: lognoteclock-out
  2497. #+STARTUP: nolognoteclock-out"
  2498. :group 'org-todo
  2499. :group 'org-progress
  2500. :type 'boolean)
  2501. (defcustom org-log-done-with-time t
  2502. "Non-nil means the CLOSED time stamp will contain date and time.
  2503. When nil, only the date will be recorded."
  2504. :group 'org-progress
  2505. :type 'boolean)
  2506. (defcustom org-log-note-headings
  2507. '((done . "CLOSING NOTE %t")
  2508. (state . "State %-12s from %-12S %t")
  2509. (note . "Note taken on %t")
  2510. (reschedule . "Rescheduled from %S on %t")
  2511. (delschedule . "Not scheduled, was %S on %t")
  2512. (redeadline . "New deadline from %S on %t")
  2513. (deldeadline . "Removed deadline, was %S on %t")
  2514. (refile . "Refiled on %t")
  2515. (clock-out . ""))
  2516. "Headings for notes added to entries.
  2517. The value is an alist, with the car being a symbol indicating the note
  2518. context, and the cdr is the heading to be used. The heading may also be the
  2519. empty string.
  2520. %t in the heading will be replaced by a time stamp.
  2521. %T will be an active time stamp instead the default inactive one
  2522. %d will be replaced by a short-format time stamp.
  2523. %D will be replaced by an active short-format time stamp.
  2524. %s will be replaced by the new TODO state, in double quotes.
  2525. %S will be replaced by the old TODO state, in double quotes.
  2526. %u will be replaced by the user name.
  2527. %U will be replaced by the full user name.
  2528. In fact, it is not a good idea to change the `state' entry, because
  2529. agenda log mode depends on the format of these entries."
  2530. :group 'org-todo
  2531. :group 'org-progress
  2532. :type '(list :greedy t
  2533. (cons (const :tag "Heading when closing an item" done) string)
  2534. (cons (const :tag
  2535. "Heading when changing todo state (todo sequence only)"
  2536. state) string)
  2537. (cons (const :tag "Heading when just taking a note" note) string)
  2538. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2539. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2540. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2541. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2542. (cons (const :tag "Heading when refiling" refile) string)
  2543. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2544. (unless (assq 'note org-log-note-headings)
  2545. (push '(note . "%t") org-log-note-headings))
  2546. (defcustom org-log-into-drawer nil
  2547. "Non-nil means insert state change notes and time stamps into a drawer.
  2548. When nil, state changes notes will be inserted after the headline and
  2549. any scheduling and clock lines, but not inside a drawer.
  2550. The value of this variable should be the name of the drawer to use.
  2551. LOGBOOK is proposed as the default drawer for this purpose, you can
  2552. also set this to a string to define the drawer of your choice.
  2553. A value of t is also allowed, representing \"LOGBOOK\".
  2554. A value of t or nil can also be set with on a per-file-basis with
  2555. #+STARTUP: logdrawer
  2556. #+STARTUP: nologdrawer
  2557. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2558. will be ignored.
  2559. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2560. a subtree.
  2561. Do not check directly this variable in a Lisp program. Call
  2562. function `org-log-into-drawer' instead."
  2563. :group 'org-todo
  2564. :group 'org-progress
  2565. :type '(choice
  2566. (const :tag "Not into a drawer" nil)
  2567. (const :tag "LOGBOOK" t)
  2568. (string :tag "Other")))
  2569. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2570. (defun org-log-into-drawer ()
  2571. "Name of the log drawer, as a string, or nil.
  2572. This is the value of `org-log-into-drawer'. However, if the
  2573. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2574. be used instead of the default value."
  2575. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2576. (cond ((equal p "nil") nil)
  2577. ((equal p "t") "LOGBOOK")
  2578. ((stringp p) p)
  2579. (p "LOGBOOK")
  2580. ((stringp org-log-into-drawer) org-log-into-drawer)
  2581. (org-log-into-drawer "LOGBOOK"))))
  2582. (defcustom org-log-state-notes-insert-after-drawers nil
  2583. "Non-nil means insert state change notes after any drawers in entry.
  2584. Only the drawers that *immediately* follow the headline and the
  2585. deadline/scheduled line are skipped.
  2586. When nil, insert notes right after the heading and perhaps the line
  2587. with deadline/scheduling if present.
  2588. This variable will have no effect if `org-log-into-drawer' is
  2589. set."
  2590. :group 'org-todo
  2591. :group 'org-progress
  2592. :type 'boolean)
  2593. (defcustom org-log-states-order-reversed t
  2594. "Non-nil means the latest state note will be directly after heading.
  2595. When nil, the state change notes will be ordered according to time.
  2596. This option can also be set with on a per-file-basis with
  2597. #+STARTUP: logstatesreversed
  2598. #+STARTUP: nologstatesreversed"
  2599. :group 'org-todo
  2600. :group 'org-progress
  2601. :type 'boolean)
  2602. (defcustom org-todo-repeat-to-state nil
  2603. "The TODO state to which a repeater should return the repeating task.
  2604. By default this is the first task in a TODO sequence, or the previous state
  2605. in a TODO_TYP set. But you can specify another task here.
  2606. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2607. :group 'org-todo
  2608. :version "24.1"
  2609. :type '(choice (const :tag "Head of sequence" nil)
  2610. (string :tag "Specific state")))
  2611. (defcustom org-log-repeat 'time
  2612. "Non-nil means record moving through the DONE state when triggering repeat.
  2613. An auto-repeating task is immediately switched back to TODO when
  2614. marked DONE. If you are not logging state changes (by adding \"@\"
  2615. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2616. record a closing note, there will be no record of the task moving
  2617. through DONE. This variable forces taking a note anyway.
  2618. nil Don't force a record
  2619. time Record a time stamp
  2620. note Prompt for a note and add it with template `org-log-note-headings'
  2621. This option can also be set with on a per-file-basis with
  2622. #+STARTUP: nologrepeat
  2623. #+STARTUP: logrepeat
  2624. #+STARTUP: lognoterepeat
  2625. You can have local logging settings for a subtree by setting the LOGGING
  2626. property to one or more of these keywords."
  2627. :group 'org-todo
  2628. :group 'org-progress
  2629. :type '(choice
  2630. (const :tag "Don't force a record" nil)
  2631. (const :tag "Force recording the DONE state" time)
  2632. (const :tag "Force recording a note with the DONE state" note)))
  2633. (defgroup org-priorities nil
  2634. "Priorities in Org-mode."
  2635. :tag "Org Priorities"
  2636. :group 'org-todo)
  2637. (defcustom org-enable-priority-commands t
  2638. "Non-nil means priority commands are active.
  2639. When nil, these commands will be disabled, so that you never accidentally
  2640. set a priority."
  2641. :group 'org-priorities
  2642. :type 'boolean)
  2643. (defcustom org-highest-priority ?A
  2644. "The highest priority of TODO items. A character like ?A, ?B etc.
  2645. Must have a smaller ASCII number than `org-lowest-priority'."
  2646. :group 'org-priorities
  2647. :type 'character)
  2648. (defcustom org-lowest-priority ?C
  2649. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2650. Must have a larger ASCII number than `org-highest-priority'."
  2651. :group 'org-priorities
  2652. :type 'character)
  2653. (defcustom org-default-priority ?B
  2654. "The default priority of TODO items.
  2655. This is the priority an item gets if no explicit priority is given.
  2656. When starting to cycle on an empty priority the first step in the cycle
  2657. depends on `org-priority-start-cycle-with-default'. The resulting first
  2658. step priority must not exceed the range from `org-highest-priority' to
  2659. `org-lowest-priority' which means that `org-default-priority' has to be
  2660. in this range exclusive or inclusive the range boundaries. Else the
  2661. first step refuses to set the default and the second will fall back
  2662. to (depending on the command used) the highest or lowest priority."
  2663. :group 'org-priorities
  2664. :type 'character)
  2665. (defcustom org-priority-start-cycle-with-default t
  2666. "Non-nil means start with default priority when starting to cycle.
  2667. When this is nil, the first step in the cycle will be (depending on the
  2668. command used) one higher or lower than the default priority.
  2669. See also `org-default-priority'."
  2670. :group 'org-priorities
  2671. :type 'boolean)
  2672. (defcustom org-get-priority-function nil
  2673. "Function to extract the priority from a string.
  2674. The string is normally the headline. If this is nil Org computes the
  2675. priority from the priority cookie like [#A] in the headline. It returns
  2676. an integer, increasing by 1000 for each priority level.
  2677. The user can set a different function here, which should take a string
  2678. as an argument and return the numeric priority."
  2679. :group 'org-priorities
  2680. :version "24.1"
  2681. :type '(choice
  2682. (const nil)
  2683. (function)))
  2684. (defgroup org-time nil
  2685. "Options concerning time stamps and deadlines in Org-mode."
  2686. :tag "Org Time"
  2687. :group 'org)
  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-iso-to-absolute "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 nil
  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" nil)
  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 "25.1"
  4100. :package-version '(Org . "8.3")
  4101. :group 'org-sparse-trees)
  4102. (defun org-cycle-hide-archived-subtrees (state)
  4103. "Re-hide all archived subtrees after a visibility state change."
  4104. (when (and (not org-cycle-open-archived-trees)
  4105. (not (memq state '(overview folded))))
  4106. (save-excursion
  4107. (let* ((globalp (memq state '(contents all)))
  4108. (beg (if globalp (point-min) (point)))
  4109. (end (if globalp (point-max) (org-end-of-subtree t))))
  4110. (org-hide-archived-subtrees beg end)
  4111. (goto-char beg)
  4112. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4113. (message "%s" (substitute-command-keys
  4114. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4115. (defun org-force-cycle-archived ()
  4116. "Cycle subtree even if it is archived."
  4117. (interactive)
  4118. (setq this-command 'org-cycle)
  4119. (let ((org-cycle-open-archived-trees t))
  4120. (call-interactively 'org-cycle)))
  4121. (defun org-hide-archived-subtrees (beg end)
  4122. "Re-hide all archived subtrees after a visibility state change."
  4123. (org-with-wide-buffer
  4124. (let ((case-fold-search nil)
  4125. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4126. (goto-char beg)
  4127. (while (and (< (point) end) (re-search-forward re end t))
  4128. (when (member org-archive-tag (org-get-tags))
  4129. (org-flag-subtree t)
  4130. (org-end-of-subtree t))))))
  4131. (declare-function outline-end-of-heading "outline" ())
  4132. (declare-function outline-flag-region "outline" (from to flag))
  4133. (defun org-flag-subtree (flag)
  4134. (save-excursion
  4135. (org-back-to-heading t)
  4136. (outline-end-of-heading)
  4137. (outline-flag-region (point)
  4138. (progn (org-end-of-subtree t) (point))
  4139. flag)))
  4140. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4141. ;; Declare Column View Code
  4142. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4143. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4144. (declare-function org-columns-compute "org-colview" (property))
  4145. ;; Declare ID code
  4146. (declare-function org-id-store-link "org-id")
  4147. (declare-function org-id-locations-load "org-id")
  4148. (declare-function org-id-locations-save "org-id")
  4149. (defvar org-id-track-globally)
  4150. ;;; Variables for pre-computed regular expressions, all buffer local
  4151. (defvar org-todo-regexp nil
  4152. "Matches any of the TODO state keywords.")
  4153. (make-variable-buffer-local 'org-todo-regexp)
  4154. (defvar org-not-done-regexp nil
  4155. "Matches any of the TODO state keywords except the last one.")
  4156. (make-variable-buffer-local 'org-not-done-regexp)
  4157. (defvar org-not-done-heading-regexp nil
  4158. "Matches a TODO headline that is not done.")
  4159. (make-variable-buffer-local 'org-not-done-regexp)
  4160. (defvar org-todo-line-regexp nil
  4161. "Matches a headline and puts TODO state into group 2 if present.")
  4162. (make-variable-buffer-local 'org-todo-line-regexp)
  4163. (defvar org-complex-heading-regexp nil
  4164. "Matches a headline and puts everything into groups:
  4165. group 1: the stars
  4166. group 2: The todo keyword, maybe
  4167. group 3: Priority cookie
  4168. group 4: True headline
  4169. group 5: Tags")
  4170. (make-variable-buffer-local 'org-complex-heading-regexp)
  4171. (defvar org-complex-heading-regexp-format nil
  4172. "Printf format to make regexp to match an exact headline.
  4173. This regexp will match the headline of any node which has the
  4174. exact headline text that is put into the format, but may have any
  4175. TODO state, priority and tags.")
  4176. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4177. (defvar org-todo-line-tags-regexp nil
  4178. "Matches a headline and puts TODO state into group 2 if present.
  4179. Also put tags into group 4 if tags are present.")
  4180. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4181. (defconst org-plain-time-of-day-regexp
  4182. (concat
  4183. "\\(\\<[012]?[0-9]"
  4184. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4185. "\\(--?"
  4186. "\\(\\<[012]?[0-9]"
  4187. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4188. "\\)?")
  4189. "Regular expression to match a plain time or time range.
  4190. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4191. groups carry important information:
  4192. 0 the full match
  4193. 1 the first time, range or not
  4194. 8 the second time, if it is a range.")
  4195. (defconst org-plain-time-extension-regexp
  4196. (concat
  4197. "\\(\\<[012]?[0-9]"
  4198. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4199. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4200. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4201. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4202. groups carry important information:
  4203. 0 the full match
  4204. 7 hours of duration
  4205. 9 minutes of duration")
  4206. (defconst org-stamp-time-of-day-regexp
  4207. (concat
  4208. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4209. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4210. "\\(--?"
  4211. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4212. "Regular expression to match a timestamp time or time range.
  4213. After a match, the following groups carry important information:
  4214. 0 the full match
  4215. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4216. 2 the first time, range or not
  4217. 4 the second time, if it is a range.")
  4218. (defconst org-startup-options
  4219. '(("fold" org-startup-folded t)
  4220. ("overview" org-startup-folded t)
  4221. ("nofold" org-startup-folded nil)
  4222. ("showall" org-startup-folded nil)
  4223. ("showeverything" org-startup-folded showeverything)
  4224. ("content" org-startup-folded content)
  4225. ("indent" org-startup-indented t)
  4226. ("noindent" org-startup-indented nil)
  4227. ("hidestars" org-hide-leading-stars t)
  4228. ("showstars" org-hide-leading-stars nil)
  4229. ("odd" org-odd-levels-only t)
  4230. ("oddeven" org-odd-levels-only nil)
  4231. ("align" org-startup-align-all-tables t)
  4232. ("noalign" org-startup-align-all-tables nil)
  4233. ("inlineimages" org-startup-with-inline-images t)
  4234. ("noinlineimages" org-startup-with-inline-images nil)
  4235. ("latexpreview" org-startup-with-latex-preview t)
  4236. ("nolatexpreview" org-startup-with-latex-preview nil)
  4237. ("customtime" org-display-custom-times t)
  4238. ("logdone" org-log-done time)
  4239. ("lognotedone" org-log-done note)
  4240. ("nologdone" org-log-done nil)
  4241. ("lognoteclock-out" org-log-note-clock-out t)
  4242. ("nolognoteclock-out" org-log-note-clock-out nil)
  4243. ("logrepeat" org-log-repeat state)
  4244. ("lognoterepeat" org-log-repeat note)
  4245. ("logdrawer" org-log-into-drawer t)
  4246. ("nologdrawer" org-log-into-drawer nil)
  4247. ("logstatesreversed" org-log-states-order-reversed t)
  4248. ("nologstatesreversed" org-log-states-order-reversed nil)
  4249. ("nologrepeat" org-log-repeat nil)
  4250. ("logreschedule" org-log-reschedule time)
  4251. ("lognotereschedule" org-log-reschedule note)
  4252. ("nologreschedule" org-log-reschedule nil)
  4253. ("logredeadline" org-log-redeadline time)
  4254. ("lognoteredeadline" org-log-redeadline note)
  4255. ("nologredeadline" org-log-redeadline nil)
  4256. ("logrefile" org-log-refile time)
  4257. ("lognoterefile" org-log-refile note)
  4258. ("nologrefile" org-log-refile nil)
  4259. ("fninline" org-footnote-define-inline t)
  4260. ("nofninline" org-footnote-define-inline nil)
  4261. ("fnlocal" org-footnote-section nil)
  4262. ("fnauto" org-footnote-auto-label t)
  4263. ("fnprompt" org-footnote-auto-label nil)
  4264. ("fnconfirm" org-footnote-auto-label confirm)
  4265. ("fnplain" org-footnote-auto-label plain)
  4266. ("fnadjust" org-footnote-auto-adjust t)
  4267. ("nofnadjust" org-footnote-auto-adjust nil)
  4268. ("constcgs" constants-unit-system cgs)
  4269. ("constSI" constants-unit-system SI)
  4270. ("noptag" org-tag-persistent-alist nil)
  4271. ("hideblocks" org-hide-block-startup t)
  4272. ("nohideblocks" org-hide-block-startup nil)
  4273. ("beamer" org-startup-with-beamer-mode t)
  4274. ("entitiespretty" org-pretty-entities t)
  4275. ("entitiesplain" org-pretty-entities nil))
  4276. "Variable associated with STARTUP options for org-mode.
  4277. Each element is a list of three items: the startup options (as written
  4278. in the #+STARTUP line), the corresponding variable, and the value to set
  4279. this variable to if the option is found. An optional forth element PUSH
  4280. means to push this value onto the list in the variable.")
  4281. (defcustom org-group-tags t
  4282. "When non-nil (the default), use group tags.
  4283. This can be turned on/off through `org-toggle-tags-groups'."
  4284. :group 'org-tags
  4285. :group 'org-startup
  4286. :type 'boolean)
  4287. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4288. (defun org-toggle-tags-groups ()
  4289. "Toggle support for group tags.
  4290. Support for group tags is controlled by the option
  4291. `org-group-tags', which is non-nil by default."
  4292. (interactive)
  4293. (setq org-group-tags (not org-group-tags))
  4294. (cond ((and (derived-mode-p 'org-agenda-mode)
  4295. org-group-tags)
  4296. (org-agenda-redo))
  4297. ((derived-mode-p 'org-mode)
  4298. (let ((org-inhibit-startup t)) (org-mode))))
  4299. (message "Groups tags support has been turned %s"
  4300. (if org-group-tags "on" "off")))
  4301. (defun org-set-regexps-and-options (&optional tags-only)
  4302. "Precompute regular expressions used in the current buffer.
  4303. When optional argument TAGS-ONLY is non-nil, only compute tags
  4304. related expressions."
  4305. (when (derived-mode-p 'org-mode)
  4306. (let ((alist (org--setup-collect-keywords
  4307. (org-make-options-regexp
  4308. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4309. (and (not tags-only)
  4310. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4311. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4312. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4313. (org--setup-process-tags
  4314. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4315. (unless tags-only
  4316. ;; File properties.
  4317. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4318. ;; Archive location.
  4319. (let ((archive (cdr (assq 'archive alist))))
  4320. (when archive (org-set-local 'org-archive-location archive)))
  4321. ;; Category.
  4322. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4323. (when cat
  4324. (org-set-local 'org-category (intern cat))
  4325. (org-set-local 'org-file-properties
  4326. (org--update-property-plist
  4327. "CATEGORY" cat org-file-properties))))
  4328. ;; Columns.
  4329. (let ((column (cdr (assq 'columns alist))))
  4330. (when column (org-set-local 'org-columns-default-format column)))
  4331. ;; Constants.
  4332. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4333. ;; Link abbreviations.
  4334. (let ((links (cdr (assq 'link alist))))
  4335. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4336. ;; Priorities.
  4337. (let ((priorities (cdr (assq 'priorities alist))))
  4338. (when priorities
  4339. (org-set-local 'org-highest-priority (nth 0 priorities))
  4340. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4341. (org-set-local 'org-default-priority (nth 2 priorities))))
  4342. ;; Scripts.
  4343. (let ((scripts (assq 'scripts alist)))
  4344. (when scripts
  4345. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4346. ;; Startup options.
  4347. (let ((startup (cdr (assq 'startup alist))))
  4348. (dolist (option startup)
  4349. (let ((entry (assoc-string option org-startup-options t)))
  4350. (when entry
  4351. (let ((var (nth 1 entry))
  4352. (val (nth 2 entry)))
  4353. (if (not (nth 3 entry)) (org-set-local var val)
  4354. (unless (listp (symbol-value var))
  4355. (org-set-local var nil))
  4356. (add-to-list var val)))))))
  4357. ;; TODO keywords.
  4358. (org-set-local 'org-todo-kwd-alist nil)
  4359. (org-set-local 'org-todo-key-alist nil)
  4360. (org-set-local 'org-todo-key-trigger nil)
  4361. (org-set-local 'org-todo-keywords-1 nil)
  4362. (org-set-local 'org-done-keywords nil)
  4363. (org-set-local 'org-todo-heads nil)
  4364. (org-set-local 'org-todo-sets nil)
  4365. (org-set-local 'org-todo-log-states nil)
  4366. (let ((todo-sequences
  4367. (or (nreverse (cdr (assq 'todo alist)))
  4368. (let ((d (default-value 'org-todo-keywords)))
  4369. (if (not (stringp (car d))) d
  4370. ;; XXX: Backward compatibility code.
  4371. (list (cons org-todo-interpretation d)))))))
  4372. (dolist (sequence todo-sequences)
  4373. (let* ((sequence (or (run-hook-with-args-until-success
  4374. 'org-todo-setup-filter-hook sequence)
  4375. sequence))
  4376. (sequence-type (car sequence))
  4377. (keywords (cdr sequence))
  4378. (sep (member "|" keywords))
  4379. names alist)
  4380. (dolist (k (remove "|" keywords))
  4381. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4382. k)
  4383. (error "Invalid TODO keyword %s" k))
  4384. (let ((name (match-string 1 k))
  4385. (key (match-string 2 k))
  4386. (log (org-extract-log-state-settings k)))
  4387. (push name names)
  4388. (push (cons name (and key (string-to-char key))) alist)
  4389. (when log (push log org-todo-log-states))))
  4390. (let* ((names (nreverse names))
  4391. (done (if sep (org-remove-keyword-keys (cdr sep))
  4392. (last names)))
  4393. (head (car names))
  4394. (tail (list sequence-type head (car done) (org-last done))))
  4395. (add-to-list 'org-todo-heads head 'append)
  4396. (push names org-todo-sets)
  4397. (setq org-done-keywords (append org-done-keywords done nil))
  4398. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4399. (setq org-todo-key-alist
  4400. (append org-todo-key-alist
  4401. (and alist
  4402. (append '((:startgroup))
  4403. (nreverse alist)
  4404. '((:endgroup))))))
  4405. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4406. (setq org-todo-sets (nreverse org-todo-sets)
  4407. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4408. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4409. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4410. ;; Compute the regular expressions and other local variables.
  4411. ;; Using `org-outline-regexp-bol' would complicate them much,
  4412. ;; because of the fixed white space at the end of that string.
  4413. (if (not org-done-keywords)
  4414. (setq org-done-keywords
  4415. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4416. (setq org-not-done-keywords
  4417. (org-delete-all org-done-keywords
  4418. (copy-sequence org-todo-keywords-1))
  4419. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4420. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4421. org-not-done-heading-regexp
  4422. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4423. org-todo-line-regexp
  4424. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4425. org-complex-heading-regexp
  4426. (concat "^\\(\\*+\\)"
  4427. "\\(?: +" org-todo-regexp "\\)?"
  4428. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4429. "\\(?: +\\(.*?\\)\\)??"
  4430. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4431. "[ \t]*$")
  4432. org-complex-heading-regexp-format
  4433. (concat "^\\(\\*+\\)"
  4434. "\\(?: +" org-todo-regexp "\\)?"
  4435. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4436. "\\(?: +"
  4437. ;; Stats cookies can be stuck to body.
  4438. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4439. "\\(%s\\)"
  4440. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4441. "\\)"
  4442. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4443. "[ \t]*$")
  4444. org-todo-line-tags-regexp
  4445. (concat "^\\(\\*+\\)"
  4446. "\\(?: +" org-todo-regexp "\\)?"
  4447. "\\(?: +\\(.*?\\)\\)??"
  4448. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4449. "[ \t]*$"))
  4450. (org-compute-latex-and-related-regexp)))))
  4451. (defun org--setup-collect-keywords (regexp &optional files alist)
  4452. "Return setup keywords values as an alist.
  4453. REGEXP matches a subset of setup keywords. FILES is a list of
  4454. file names already visited. It is used to avoid circular setup
  4455. files. ALIST, when non-nil, is the alist computed so far.
  4456. Return value contains the following keys: `archive', `category',
  4457. `columns', `constants', `filetags', `link', `priorities',
  4458. `property', `scripts', `startup', `tags' and `todo'."
  4459. (org-with-wide-buffer
  4460. (goto-char (point-min))
  4461. (let ((case-fold-search t))
  4462. (while (re-search-forward regexp nil t)
  4463. (let ((element (org-element-at-point)))
  4464. (when (eq (org-element-type element) 'keyword)
  4465. (let ((key (org-element-property :key element))
  4466. (value (org-element-property :value element)))
  4467. (cond
  4468. ((equal key "ARCHIVE")
  4469. (when (org-string-nw-p value)
  4470. (push (cons 'archive value) alist)))
  4471. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4472. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4473. ((equal key "CONSTANTS")
  4474. (let* ((constants (assq 'constants alist))
  4475. (store (cdr constants)))
  4476. (dolist (pair (org-split-string value))
  4477. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4478. pair)
  4479. (let* ((name (match-string 1 pair))
  4480. (value (match-string 2 pair))
  4481. (old (assoc name store)))
  4482. (if old (setcdr old value)
  4483. (push (cons name value) store)))))
  4484. (if constants (setcdr constants store)
  4485. (push (cons 'constants store) alist))))
  4486. ((equal key "FILETAGS")
  4487. (when (org-string-nw-p value)
  4488. (let ((old (assq 'filetags alist))
  4489. (new (apply #'nconc
  4490. (mapcar (lambda (x) (org-split-string x ":"))
  4491. (org-split-string value)))))
  4492. (if old (setcdr old (append new (cdr old)))
  4493. (push (cons 'filetags new) alist)))))
  4494. ((equal key "LINK")
  4495. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4496. (let ((links (assq 'link alist))
  4497. (pair (cons (org-match-string-no-properties 1 value)
  4498. (org-match-string-no-properties 2 value))))
  4499. (if links (push pair (cdr links))
  4500. (push (list 'link pair) alist)))))
  4501. ((equal key "OPTIONS")
  4502. (when (and (org-string-nw-p value)
  4503. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4504. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4505. ((equal key "PRIORITIES")
  4506. (push (cons 'priorities
  4507. (let ((prio (org-split-string value)))
  4508. (if (< (length prio) 3) '(?A ?C ?B)
  4509. (mapcar #'string-to-char prio))))
  4510. alist))
  4511. ((equal key "PROPERTY")
  4512. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4513. (let* ((property (assq 'property alist))
  4514. (value (org--update-property-plist
  4515. (org-match-string-no-properties 1 value)
  4516. (org-match-string-no-properties 2 value)
  4517. (cdr property))))
  4518. (if property (setcdr property value)
  4519. (push (cons 'property value) alist)))))
  4520. ((equal key "STARTUP")
  4521. (let ((startup (assq 'startup alist)))
  4522. (if startup
  4523. (setcdr startup
  4524. (append (cdr startup) (org-split-string value)))
  4525. (push (cons 'startup (org-split-string value)) alist))))
  4526. ((equal key "TAGS")
  4527. (let ((tag-cell (assq 'tags alist)))
  4528. (if tag-cell
  4529. (setcdr tag-cell
  4530. (append (cdr tag-cell)
  4531. '("\\n")
  4532. (org-split-string value)))
  4533. (push (cons 'tags (org-split-string value)) alist))))
  4534. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4535. (let ((todo (assq 'todo alist))
  4536. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4537. (org-split-string value))))
  4538. (if todo (push value (cdr todo))
  4539. (push (list 'todo value) alist))))
  4540. ((equal key "SETUPFILE")
  4541. (unless buffer-read-only ; Do not check in Gnus messages.
  4542. (let ((f (and (org-string-nw-p value)
  4543. (expand-file-name
  4544. (org-remove-double-quotes value)))))
  4545. (when (and f (file-readable-p f) (not (member f files)))
  4546. (with-temp-buffer
  4547. (insert-file-contents f)
  4548. (setq alist
  4549. ;; Fake Org mode to benefit from cache
  4550. ;; without recurring needlessly.
  4551. (let ((major-mode 'org-mode))
  4552. (org--setup-collect-keywords
  4553. regexp (cons f files) alist)))))))))))))))
  4554. alist)
  4555. (defun org--setup-process-tags (tags filetags)
  4556. "Precompute variables used for tags.
  4557. TAGS is a list of tags and tag group symbols, as strings.
  4558. FILETAGS is a list of tags, as strings."
  4559. ;; Process the file tags.
  4560. (org-set-local 'org-file-tags
  4561. (mapcar #'org-add-prop-inherited filetags))
  4562. ;; Provide default tags if no local tags are found.
  4563. (when (and (not tags) org-tag-alist)
  4564. (setq tags
  4565. (mapcar (lambda (tag)
  4566. (case (car tag)
  4567. (:startgroup "{")
  4568. (:endgroup "}")
  4569. (:grouptags ":")
  4570. (:newline "\\n")
  4571. (otherwise (concat (car tag)
  4572. (and (characterp (cdr tag))
  4573. (format "(%c)" (cdr tag)))))))
  4574. org-tag-alist)))
  4575. ;; Process the tags.
  4576. (org-set-local 'org-tag-groups-alist nil)
  4577. (org-set-local 'org-tag-alist nil)
  4578. (let (group-flag)
  4579. (while tags
  4580. (let ((e (car tags)))
  4581. (setq tags (cdr tags))
  4582. (cond
  4583. ((equal e "{")
  4584. (push '(:startgroup) org-tag-alist)
  4585. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4586. ((equal e "}")
  4587. (push '(:endgroup) org-tag-alist)
  4588. (setq group-flag nil))
  4589. ((equal e ":")
  4590. (push '(:grouptags) org-tag-alist)
  4591. (setq group-flag 'append))
  4592. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4593. ((string-match
  4594. (org-re "\\`\\([[:alnum:]_@#%]+\\)\\(?:(\\(.\\))\\)?\\'") e)
  4595. (let ((tag (match-string 1 e))
  4596. (key (and (match-beginning 2)
  4597. (string-to-char (match-string 2 e)))))
  4598. (cond ((eq group-flag 'append)
  4599. (setcar org-tag-groups-alist
  4600. (append (car org-tag-groups-alist) (list tag))))
  4601. (group-flag (push (list tag) org-tag-groups-alist)))
  4602. (unless (assoc tag org-tag-alist)
  4603. (push (cons tag key) org-tag-alist))))))))
  4604. (setq org-tag-alist (nreverse org-tag-alist)))
  4605. (defun org-file-contents (file &optional noerror)
  4606. "Return the contents of FILE, as a string."
  4607. (if (and file (file-readable-p file))
  4608. (with-temp-buffer
  4609. (insert-file-contents file)
  4610. (buffer-string))
  4611. (funcall (if noerror 'message 'error)
  4612. "Cannot read file \"%s\"%s"
  4613. file
  4614. (let ((from (buffer-file-name (buffer-base-buffer))))
  4615. (if from (concat " (referenced in file \"" from "\")") "")))))
  4616. (defun org-extract-log-state-settings (x)
  4617. "Extract the log state setting from a TODO keyword string.
  4618. This will extract info from a string like \"WAIT(w@/!)\"."
  4619. (let (kw key log1 log2)
  4620. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4621. (setq kw (match-string 1 x)
  4622. key (and (match-end 2) (match-string 2 x))
  4623. log1 (and (match-end 3) (match-string 3 x))
  4624. log2 (and (match-end 4) (match-string 4 x)))
  4625. (and (or log1 log2)
  4626. (list kw
  4627. (and log1 (if (equal log1 "!") 'time 'note))
  4628. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4629. (defun org-remove-keyword-keys (list)
  4630. "Remove a pair of parenthesis at the end of each string in LIST."
  4631. (mapcar (lambda (x)
  4632. (if (string-match "(.*)$" x)
  4633. (substring x 0 (match-beginning 0))
  4634. x))
  4635. list))
  4636. (defun org-assign-fast-keys (alist)
  4637. "Assign fast keys to a keyword-key alist.
  4638. Respect keys that are already there."
  4639. (let (new e (alt ?0))
  4640. (while (setq e (pop alist))
  4641. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4642. (cdr e)) ;; Key already assigned.
  4643. (push e new)
  4644. (let ((clist (string-to-list (downcase (car e))))
  4645. (used (append new alist)))
  4646. (when (= (car clist) ?@)
  4647. (pop clist))
  4648. (while (and clist (rassoc (car clist) used))
  4649. (pop clist))
  4650. (unless clist
  4651. (while (rassoc alt used)
  4652. (incf alt)))
  4653. (push (cons (car e) (or (car clist) alt)) new))))
  4654. (nreverse new)))
  4655. ;;; Some variables used in various places
  4656. (defvar org-window-configuration nil
  4657. "Used in various places to store a window configuration.")
  4658. (defvar org-selected-window nil
  4659. "Used in various places to store a window configuration.")
  4660. (defvar org-finish-function nil
  4661. "Function to be called when `C-c C-c' is used.
  4662. This is for getting out of special buffers like capture.")
  4663. ;; FIXME: Occasionally check by commenting these, to make sure
  4664. ;; no other functions uses these, forgetting to let-bind them.
  4665. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4666. (defvar org-last-state)
  4667. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4668. ;; Defined somewhere in this file, but used before definition.
  4669. (defvar org-entities) ;; defined in org-entities.el
  4670. (defvar org-struct-menu)
  4671. (defvar org-org-menu)
  4672. (defvar org-tbl-menu)
  4673. ;;;; Define the Org-mode
  4674. ;; We use a before-change function to check if a table might need
  4675. ;; an update.
  4676. (defvar org-table-may-need-update t
  4677. "Indicates that a table might need an update.
  4678. This variable is set by `org-before-change-function'.
  4679. `org-table-align' sets it back to nil.")
  4680. (defun org-before-change-function (beg end)
  4681. "Every change indicates that a table might need an update."
  4682. (setq org-table-may-need-update t))
  4683. (defvar org-mode-map)
  4684. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4685. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4686. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4687. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4688. (defvar org-table-buffer-is-an nil)
  4689. (defvar bidi-paragraph-direction)
  4690. (defvar buffer-face-mode-face)
  4691. (require 'outline)
  4692. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4693. (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"))
  4694. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4695. ;; Other stuff we need.
  4696. (require 'time-date)
  4697. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4698. (require 'easymenu)
  4699. (require 'overlay)
  4700. ;; (require 'org-macs) moved higher up in the file before it is first used
  4701. (require 'org-entities)
  4702. ;; (require 'org-compat) moved higher up in the file before it is first used
  4703. (require 'org-faces)
  4704. (require 'org-list)
  4705. (require 'org-pcomplete)
  4706. (require 'org-src)
  4707. (require 'org-footnote)
  4708. (require 'org-macro)
  4709. ;; babel
  4710. (require 'ob)
  4711. ;;;###autoload
  4712. (define-derived-mode org-mode outline-mode "Org"
  4713. "Outline-based notes management and organizer, alias
  4714. \"Carsten's outline-mode for keeping track of everything.\"
  4715. Org-mode develops organizational tasks around a NOTES file which
  4716. contains information about projects as plain text. Org-mode is
  4717. implemented on top of outline-mode, which is ideal to keep the content
  4718. of large files well structured. It supports ToDo items, deadlines and
  4719. time stamps, which magically appear in the diary listing of the Emacs
  4720. calendar. Tables are easily created with a built-in table editor.
  4721. Plain text URL-like links connect to websites, emails (VM), Usenet
  4722. messages (Gnus), BBDB entries, and any files related to the project.
  4723. For printing and sharing of notes, an Org-mode file (or a part of it)
  4724. can be exported as a structured ASCII or HTML file.
  4725. The following commands are available:
  4726. \\{org-mode-map}"
  4727. ;; Get rid of Outline menus, they are not needed
  4728. ;; Need to do this here because define-derived-mode sets up
  4729. ;; the keymap so late. Still, it is a waste to call this each time
  4730. ;; we switch another buffer into org-mode.
  4731. (if (featurep 'xemacs)
  4732. (when (boundp 'outline-mode-menu-heading)
  4733. ;; Assume this is Greg's port, it uses easymenu
  4734. (easy-menu-remove outline-mode-menu-heading)
  4735. (easy-menu-remove outline-mode-menu-show)
  4736. (easy-menu-remove outline-mode-menu-hide))
  4737. (define-key org-mode-map [menu-bar headings] 'undefined)
  4738. (define-key org-mode-map [menu-bar hide] 'undefined)
  4739. (define-key org-mode-map [menu-bar show] 'undefined))
  4740. (org-load-modules-maybe)
  4741. (easy-menu-add org-org-menu)
  4742. (easy-menu-add org-tbl-menu)
  4743. (org-install-agenda-files-menu)
  4744. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4745. (add-to-invisibility-spec '(org-cwidth))
  4746. (add-to-invisibility-spec '(org-hide-block . t))
  4747. (when (featurep 'xemacs)
  4748. (org-set-local 'line-move-ignore-invisible t))
  4749. (org-set-local 'outline-regexp org-outline-regexp)
  4750. (org-set-local 'outline-level 'org-outline-level)
  4751. (setq bidi-paragraph-direction 'left-to-right)
  4752. (when (and org-ellipsis
  4753. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4754. (fboundp 'make-glyph-code))
  4755. (unless org-display-table
  4756. (setq org-display-table (make-display-table)))
  4757. (set-display-table-slot
  4758. org-display-table 4
  4759. (vconcat (mapcar
  4760. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4761. org-ellipsis)))
  4762. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4763. (setq buffer-display-table org-display-table))
  4764. (org-set-regexps-and-options)
  4765. (org-set-font-lock-defaults)
  4766. (when (and org-tag-faces (not org-tags-special-faces-re))
  4767. ;; tag faces set outside customize.... force initialization.
  4768. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4769. ;; Calc embedded
  4770. (org-set-local 'calc-embedded-open-mode "# ")
  4771. ;; Modify a few syntax entries
  4772. (modify-syntax-entry ?@ "w")
  4773. (modify-syntax-entry ?\" "\"")
  4774. (modify-syntax-entry ?\\ "_")
  4775. (modify-syntax-entry ?~ "_")
  4776. (if org-startup-truncated (setq truncate-lines t))
  4777. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4778. (org-set-local 'font-lock-unfontify-region-function
  4779. 'org-unfontify-region)
  4780. ;; Activate before-change-function
  4781. (org-set-local 'org-table-may-need-update t)
  4782. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4783. 'local)
  4784. ;; Check for running clock before killing a buffer
  4785. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4786. ;; Initialize macros templates.
  4787. (org-macro-initialize-templates)
  4788. ;; Initialize radio targets.
  4789. (org-update-radio-target-regexp)
  4790. ;; Indentation.
  4791. (org-set-local 'indent-line-function 'org-indent-line)
  4792. (org-set-local 'indent-region-function 'org-indent-region)
  4793. ;; Filling and auto-filling.
  4794. (org-setup-filling)
  4795. ;; Comments.
  4796. (org-setup-comments-handling)
  4797. ;; Initialize cache.
  4798. (org-element-cache-reset)
  4799. ;; Beginning/end of defun
  4800. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4801. (org-set-local 'end-of-defun-function
  4802. (lambda ()
  4803. (if (not (org-at-heading-p))
  4804. (org-forward-element)
  4805. (org-forward-element)
  4806. (forward-char -1))))
  4807. ;; Next error for sparse trees
  4808. (org-set-local 'next-error-function 'org-occur-next-match)
  4809. ;; Make sure dependence stuff works reliably, even for users who set it
  4810. ;; too late :-(
  4811. (if org-enforce-todo-dependencies
  4812. (add-hook 'org-blocker-hook
  4813. 'org-block-todo-from-children-or-siblings-or-parent)
  4814. (remove-hook 'org-blocker-hook
  4815. 'org-block-todo-from-children-or-siblings-or-parent))
  4816. (if org-enforce-todo-checkbox-dependencies
  4817. (add-hook 'org-blocker-hook
  4818. 'org-block-todo-from-checkboxes)
  4819. (remove-hook 'org-blocker-hook
  4820. 'org-block-todo-from-checkboxes))
  4821. ;; Align options lines
  4822. (org-set-local
  4823. 'align-mode-rules-list
  4824. '((org-in-buffer-settings
  4825. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4826. (modes . '(org-mode)))))
  4827. ;; Imenu
  4828. (org-set-local 'imenu-create-index-function
  4829. 'org-imenu-get-tree)
  4830. ;; Make isearch reveal context
  4831. (if (or (featurep 'xemacs)
  4832. (not (boundp 'outline-isearch-open-invisible-function)))
  4833. ;; Emacs 21 and XEmacs make use of the hook
  4834. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4835. ;; Emacs 22 deals with this through a special variable
  4836. (org-set-local 'outline-isearch-open-invisible-function
  4837. (lambda (&rest ignore) (org-show-context 'isearch))))
  4838. ;; Setup the pcomplete hooks
  4839. (set (make-local-variable 'pcomplete-command-completion-function)
  4840. 'org-pcomplete-initial)
  4841. (set (make-local-variable 'pcomplete-command-name-function)
  4842. 'org-command-at-point)
  4843. (set (make-local-variable 'pcomplete-default-completion-function)
  4844. 'ignore)
  4845. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4846. 'org-parse-arguments)
  4847. (set (make-local-variable 'pcomplete-termination-string) "")
  4848. (when (>= emacs-major-version 23)
  4849. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4850. ;; If empty file that did not turn on org-mode automatically, make it to.
  4851. (if (and org-insert-mode-line-in-empty-file
  4852. (org-called-interactively-p 'any)
  4853. (= (point-min) (point-max)))
  4854. (insert "# -*- mode: org -*-\n\n"))
  4855. (unless org-inhibit-startup
  4856. (org-unmodified
  4857. (and org-startup-with-beamer-mode (org-beamer-mode))
  4858. (when org-startup-align-all-tables
  4859. (org-table-map-tables 'org-table-align 'quietly))
  4860. (when org-startup-with-inline-images
  4861. (org-display-inline-images))
  4862. (when org-startup-with-latex-preview
  4863. (org-toggle-latex-fragment))
  4864. (unless org-inhibit-startup-visibility-stuff
  4865. (org-set-startup-visibility))
  4866. (org-refresh-effort-properties)))
  4867. ;; Try to set org-hide correctly
  4868. (let ((foreground (org-find-invisible-foreground)))
  4869. (if foreground
  4870. (set-face-foreground 'org-hide foreground))))
  4871. ;; Update `customize-package-emacs-version-alist'
  4872. (add-to-list 'customize-package-emacs-version-alist
  4873. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4874. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4875. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4876. (defvar org-mode-transpose-word-syntax-table
  4877. (let ((st (make-syntax-table text-mode-syntax-table)))
  4878. (mapc (lambda(c) (modify-syntax-entry
  4879. (string-to-char (car c)) "w p" st))
  4880. org-emphasis-alist)
  4881. st))
  4882. (when (fboundp 'abbrev-table-put)
  4883. (abbrev-table-put org-mode-abbrev-table
  4884. :parents (list text-mode-abbrev-table)))
  4885. (defun org-find-invisible-foreground ()
  4886. (let ((candidates (remove
  4887. "unspecified-bg"
  4888. (nconc
  4889. (list (face-background 'default)
  4890. (face-background 'org-default))
  4891. (mapcar
  4892. (lambda (alist)
  4893. (when (boundp alist)
  4894. (cdr (assoc 'background-color (symbol-value alist)))))
  4895. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4896. (list (face-foreground 'org-hide))))))
  4897. (car (remove nil candidates))))
  4898. (defun org-current-time (&optional rounding-minutes past)
  4899. "Current time, possibly rounded to ROUNDING-MINUTES.
  4900. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4901. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4902. the rounding returns a past time."
  4903. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4904. (car org-time-stamp-rounding-minutes)))
  4905. (time (decode-time)) res)
  4906. (if (< r 1)
  4907. (current-time)
  4908. (setq res
  4909. (apply 'encode-time
  4910. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4911. (nthcdr 2 time))))
  4912. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4913. (seconds-to-time (- (org-float-time res) (* r 60)))
  4914. res))))
  4915. (defun org-today ()
  4916. "Return today date, considering `org-extend-today-until'."
  4917. (time-to-days
  4918. (time-subtract (current-time)
  4919. (list 0 (* 3600 org-extend-today-until) 0))))
  4920. ;;;; Font-Lock stuff, including the activators
  4921. (defvar org-mouse-map (make-sparse-keymap))
  4922. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4923. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4924. (when org-mouse-1-follows-link
  4925. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4926. (when org-tab-follows-link
  4927. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4928. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4929. (require 'font-lock)
  4930. (defconst org-non-link-chars "]\t\n\r<>")
  4931. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4932. "file+sys" "news" "shell" "elisp" "doi" "message"
  4933. "help"))
  4934. (defvar org-link-types-re nil
  4935. "Matches a link that has a url-like prefix like \"http:\"")
  4936. (defvar org-link-re-with-space nil
  4937. "Matches a link with spaces, optional angular brackets around it.")
  4938. (defvar org-link-re-with-space2 nil
  4939. "Matches a link with spaces, optional angular brackets around it.")
  4940. (defvar org-link-re-with-space3 nil
  4941. "Matches a link with spaces, only for internal part in bracket links.")
  4942. (defvar org-angle-link-re nil
  4943. "Matches link with angular brackets, spaces are allowed.")
  4944. (defvar org-plain-link-re nil
  4945. "Matches plain link, without spaces.")
  4946. (defvar org-bracket-link-regexp nil
  4947. "Matches a link in double brackets.")
  4948. (defvar org-bracket-link-analytic-regexp nil
  4949. "Regular expression used to analyze links.
  4950. Here is what the match groups contain after a match:
  4951. 1: http:
  4952. 2: http
  4953. 3: path
  4954. 4: [desc]
  4955. 5: desc")
  4956. (defvar org-bracket-link-analytic-regexp++ nil
  4957. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4958. (defvar org-any-link-re nil
  4959. "Regular expression matching any link.")
  4960. (defconst org-match-sexp-depth 3
  4961. "Number of stacked braces for sub/superscript matching.")
  4962. (defun org-create-multibrace-regexp (left right n)
  4963. "Create a regular expression which will match a balanced sexp.
  4964. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4965. as single character strings.
  4966. The regexp returned will match the entire expression including the
  4967. delimiters. It will also define a single group which contains the
  4968. match except for the outermost delimiters. The maximum depth of
  4969. stacked delimiters is N. Escaping delimiters is not possible."
  4970. (let* ((nothing (concat "[^" left right "]*?"))
  4971. (or "\\|")
  4972. (re nothing)
  4973. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4974. (while (> n 1)
  4975. (setq n (1- n)
  4976. re (concat re or next)
  4977. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4978. (concat left "\\(" re "\\)" right)))
  4979. (defconst org-match-substring-regexp
  4980. (concat
  4981. "\\(\\S-\\)\\([_^]\\)\\("
  4982. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4983. "\\|"
  4984. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4985. "\\|"
  4986. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4987. "The regular expression matching a sub- or superscript.")
  4988. (defconst org-match-substring-with-braces-regexp
  4989. (concat
  4990. "\\(\\S-\\)\\([_^]\\)"
  4991. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4992. "The regular expression matching a sub- or superscript, forcing braces.")
  4993. (defun org-make-link-regexps ()
  4994. "Update the link regular expressions.
  4995. This should be called after the variable `org-link-types' has changed."
  4996. (let ((types-re (regexp-opt org-link-types t)))
  4997. (setq org-link-types-re
  4998. (concat "\\`" types-re ":")
  4999. org-link-re-with-space
  5000. (concat "<?" types-re ":"
  5001. "\\([^" org-non-link-chars " ]"
  5002. "[^" org-non-link-chars "]*"
  5003. "[^" org-non-link-chars " ]\\)>?")
  5004. org-link-re-with-space2
  5005. (concat "<?" types-re ":"
  5006. "\\([^" org-non-link-chars " ]"
  5007. "[^\t\n\r]*"
  5008. "[^" org-non-link-chars " ]\\)>?")
  5009. org-link-re-with-space3
  5010. (concat "<?" types-re ":"
  5011. "\\([^" org-non-link-chars " ]"
  5012. "[^\t\n\r]*\\)")
  5013. org-angle-link-re
  5014. (concat "<" types-re ":"
  5015. "\\([^" org-non-link-chars " ]"
  5016. "[^" org-non-link-chars "]*"
  5017. "\\)>")
  5018. org-plain-link-re
  5019. (concat
  5020. "\\<" types-re ":"
  5021. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5022. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5023. org-bracket-link-regexp
  5024. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5025. org-bracket-link-analytic-regexp
  5026. (concat
  5027. "\\[\\["
  5028. "\\(" types-re ":\\)?"
  5029. "\\([^]]+\\)"
  5030. "\\]"
  5031. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5032. "\\]")
  5033. org-bracket-link-analytic-regexp++
  5034. (concat
  5035. "\\[\\["
  5036. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5037. "\\([^]]+\\)"
  5038. "\\]"
  5039. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5040. "\\]")
  5041. org-any-link-re
  5042. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5043. org-angle-link-re "\\)\\|\\("
  5044. org-plain-link-re "\\)"))))
  5045. (org-make-link-regexps)
  5046. (defvar org-emph-face nil)
  5047. (defun org-do-emphasis-faces (limit)
  5048. "Run through the buffer and emphasize strings."
  5049. (let (rtn a)
  5050. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5051. (let* ((border (char-after (match-beginning 3)))
  5052. (bre (regexp-quote (char-to-string border))))
  5053. (if (and (not (= border (char-after (match-beginning 4))))
  5054. (not (save-match-data
  5055. (string-match (concat bre ".*" bre)
  5056. (replace-regexp-in-string
  5057. "\n" " "
  5058. (substring (match-string 2) 1 -1))))))
  5059. (progn
  5060. (setq rtn t)
  5061. (setq a (assoc (match-string 3) org-emphasis-alist))
  5062. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5063. 'face
  5064. (nth 1 a))
  5065. (and (nth 2 a)
  5066. (org-remove-flyspell-overlays-in
  5067. (match-beginning 0) (match-end 0)))
  5068. (add-text-properties (match-beginning 2) (match-end 2)
  5069. '(font-lock-multiline t org-emphasis t))
  5070. (when org-hide-emphasis-markers
  5071. (add-text-properties (match-end 4) (match-beginning 5)
  5072. '(invisible org-link))
  5073. (add-text-properties (match-beginning 3) (match-end 3)
  5074. '(invisible org-link))))))
  5075. (goto-char (1+ (match-beginning 0))))
  5076. rtn))
  5077. (defun org-emphasize (&optional char)
  5078. "Insert or change an emphasis, i.e. a font like bold or italic.
  5079. If there is an active region, change that region to a new emphasis.
  5080. If there is no region, just insert the marker characters and position
  5081. the cursor between them.
  5082. CHAR should be the marker character. If it is a space, it means to
  5083. remove the emphasis of the selected region.
  5084. If CHAR is not given (for example in an interactive call) it will be
  5085. prompted for."
  5086. (interactive)
  5087. (let ((erc org-emphasis-regexp-components)
  5088. (prompt "")
  5089. (string "") beg end move c s)
  5090. (if (org-region-active-p)
  5091. (setq beg (region-beginning) end (region-end)
  5092. string (buffer-substring beg end))
  5093. (setq move t))
  5094. (unless char
  5095. (message "Emphasis marker or tag: [%s]"
  5096. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5097. (setq char (read-char-exclusive)))
  5098. (if (equal char ?\ )
  5099. (setq s "" move nil)
  5100. (unless (assoc (char-to-string char) org-emphasis-alist)
  5101. (user-error "No such emphasis marker: \"%c\"" char))
  5102. (setq s (char-to-string char)))
  5103. (while (and (> (length string) 1)
  5104. (equal (substring string 0 1) (substring string -1))
  5105. (assoc (substring string 0 1) org-emphasis-alist))
  5106. (setq string (substring string 1 -1)))
  5107. (setq string (concat s string s))
  5108. (if beg (delete-region beg end))
  5109. (unless (or (bolp)
  5110. (string-match (concat "[" (nth 0 erc) "\n]")
  5111. (char-to-string (char-before (point)))))
  5112. (insert " "))
  5113. (unless (or (eobp)
  5114. (string-match (concat "[" (nth 1 erc) "\n]")
  5115. (char-to-string (char-after (point)))))
  5116. (insert " ") (backward-char 1))
  5117. (insert string)
  5118. (and move (backward-char 1))))
  5119. (defconst org-nonsticky-props
  5120. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5121. (defsubst org-rear-nonsticky-at (pos)
  5122. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5123. (defun org-activate-plain-links (limit)
  5124. "Add link properties for plain links."
  5125. (let (f hl)
  5126. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5127. (not (member 'org-tag
  5128. (get-text-property (1- (match-beginning 0)) 'face)))
  5129. (not (org-in-src-block-p)))
  5130. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5131. (setq f (get-text-property (match-beginning 0) 'face))
  5132. (setq hl (org-match-string-no-properties 0))
  5133. (if (or (eq f 'org-tag)
  5134. (and (listp f) (memq 'org-tag f)))
  5135. nil
  5136. (add-text-properties (match-beginning 0) (match-end 0)
  5137. (list 'mouse-face 'highlight
  5138. 'face 'org-link
  5139. 'htmlize-link `(:uri ,hl)
  5140. 'keymap org-mouse-map))
  5141. (org-rear-nonsticky-at (match-end 0)))
  5142. t)))
  5143. (defun org-activate-code (limit)
  5144. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5145. (progn
  5146. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5147. (remove-text-properties (match-beginning 0) (match-end 0)
  5148. '(display t invisible t intangible t))
  5149. t)))
  5150. (defcustom org-src-fontify-natively t
  5151. "When non-nil, fontify code in code blocks."
  5152. :type 'boolean
  5153. :version "24.4"
  5154. :package-version '(Org . "8.3")
  5155. :group 'org-appearance
  5156. :group 'org-babel)
  5157. (defcustom org-allow-promoting-top-level-subtree nil
  5158. "When non-nil, allow promoting a top level subtree.
  5159. The leading star of the top level headline will be replaced
  5160. by a #."
  5161. :type 'boolean
  5162. :version "24.1"
  5163. :group 'org-appearance)
  5164. (defun org-fontify-meta-lines-and-blocks (limit)
  5165. (condition-case nil
  5166. (org-fontify-meta-lines-and-blocks-1 limit)
  5167. (error (message "org-mode fontification error"))))
  5168. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5169. "Fontify #+ lines and blocks."
  5170. (let ((case-fold-search t))
  5171. (if (re-search-forward
  5172. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5173. limit t)
  5174. (let ((beg (match-beginning 0))
  5175. (block-start (match-end 0))
  5176. (block-end nil)
  5177. (lang (match-string 7))
  5178. (beg1 (line-beginning-position 2))
  5179. (dc1 (downcase (match-string 2)))
  5180. (dc3 (downcase (match-string 3)))
  5181. end end1 quoting block-type ovl)
  5182. (cond
  5183. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5184. ;; a single line of backend-specific content
  5185. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5186. (remove-text-properties (match-beginning 0) (match-end 0)
  5187. '(display t invisible t intangible t))
  5188. (add-text-properties (match-beginning 1) (match-end 3)
  5189. '(font-lock-fontified t face org-meta-line))
  5190. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5191. '(font-lock-fontified t face org-block))
  5192. ; for backend-specific code
  5193. t)
  5194. ((and (match-end 4) (equal dc3 "+begin"))
  5195. ;; Truly a block
  5196. (setq block-type (downcase (match-string 5))
  5197. quoting (member block-type org-protecting-blocks))
  5198. (when (re-search-forward
  5199. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5200. nil t) ;; on purpose, we look further than LIMIT
  5201. (setq end (min (point-max) (match-end 0))
  5202. end1 (min (point-max) (1- (match-beginning 0))))
  5203. (setq block-end (match-beginning 0))
  5204. (when quoting
  5205. (org-remove-flyspell-overlays-in beg1 end1)
  5206. (remove-text-properties beg end
  5207. '(display t invisible t intangible t)))
  5208. (add-text-properties
  5209. beg end '(font-lock-fontified t font-lock-multiline t))
  5210. (add-text-properties beg beg1 '(face org-meta-line))
  5211. (org-remove-flyspell-overlays-in beg beg1)
  5212. (add-text-properties ; For end_src
  5213. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5214. (org-remove-flyspell-overlays-in end1 end)
  5215. (cond
  5216. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5217. (org-src-font-lock-fontify-block lang block-start block-end)
  5218. (add-text-properties beg1 block-end '(src-block t)))
  5219. (quoting
  5220. (add-text-properties beg1 (min (point-max) (1+ end1))
  5221. '(face org-block))) ; end of source block
  5222. ((not org-fontify-quote-and-verse-blocks))
  5223. ((string= block-type "quote")
  5224. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5225. ((string= block-type "verse")
  5226. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5227. (add-text-properties beg beg1 '(face org-block-begin-line))
  5228. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5229. '(face org-block-end-line))
  5230. t))
  5231. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5232. (org-remove-flyspell-overlays-in
  5233. (match-beginning 0)
  5234. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5235. (add-text-properties
  5236. beg (match-end 3)
  5237. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5238. '(font-lock-fontified t invisible t)
  5239. '(font-lock-fontified t face org-document-info-keyword)))
  5240. (add-text-properties
  5241. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5242. (if (string-equal dc1 "+title:")
  5243. '(font-lock-fontified t face org-document-title)
  5244. '(font-lock-fontified t face org-document-info))))
  5245. ((or (equal dc1 "+results")
  5246. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5247. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5248. "+call:" "+header:" "+headers:" "+name:"))
  5249. (and (match-end 4) (equal dc3 "+attr")))
  5250. (org-remove-flyspell-overlays-in
  5251. (match-beginning 0)
  5252. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5253. (add-text-properties
  5254. beg (match-end 0)
  5255. '(font-lock-fontified t face org-meta-line))
  5256. t)
  5257. ((member dc3 '(" " ""))
  5258. (org-remove-flyspell-overlays-in beg (match-end 0))
  5259. (add-text-properties
  5260. beg (match-end 0)
  5261. '(font-lock-fontified t face font-lock-comment-face)))
  5262. (t ;; just any other in-buffer setting, but not indented
  5263. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5264. (add-text-properties
  5265. beg (match-end 0)
  5266. '(font-lock-fontified t face org-meta-line))
  5267. t))))))
  5268. (defun org-fontify-drawers (limit)
  5269. "Fontify drawers."
  5270. (when (re-search-forward org-drawer-regexp limit t)
  5271. (add-text-properties
  5272. (match-beginning 0) (match-end 0)
  5273. '(font-lock-fontified t face org-special-keyword))
  5274. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5275. t))
  5276. (defun org-fontify-macros (limit)
  5277. "Fontify macros."
  5278. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5279. (add-text-properties
  5280. (match-beginning 0) (match-end 0)
  5281. '(font-lock-fontified t face org-macro))
  5282. (when org-hide-macro-markers
  5283. (add-text-properties (match-end 2) (match-beginning 2)
  5284. '(invisible t))
  5285. (add-text-properties (match-beginning 1) (match-end 1)
  5286. '(invisible t)))
  5287. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5288. t))
  5289. (defun org-activate-angle-links (limit)
  5290. "Add text properties for angle links."
  5291. (if (and (re-search-forward org-angle-link-re limit t)
  5292. (not (org-in-src-block-p)))
  5293. (progn
  5294. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5295. (add-text-properties (match-beginning 0) (match-end 0)
  5296. (list 'mouse-face 'highlight
  5297. 'keymap org-mouse-map))
  5298. (org-rear-nonsticky-at (match-end 0))
  5299. t)))
  5300. (defun org-activate-footnote-links (limit)
  5301. "Add text properties for footnotes."
  5302. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5303. (when fn
  5304. (let* ((beg (nth 1 fn))
  5305. (end (nth 2 fn))
  5306. (label (car fn))
  5307. (referencep (/= (line-beginning-position) beg)))
  5308. (when (and referencep (nth 3 fn))
  5309. (save-excursion
  5310. (goto-char beg)
  5311. (search-forward (or label "fn:"))
  5312. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5313. (add-text-properties beg end
  5314. (list 'mouse-face 'highlight
  5315. 'keymap org-mouse-map
  5316. 'help-echo
  5317. (if referencep "Footnote reference"
  5318. "Footnote definition")
  5319. 'font-lock-fontified t
  5320. 'font-lock-multiline t
  5321. 'face 'org-footnote))))))
  5322. (defun org-activate-bracket-links (limit)
  5323. "Add text properties for bracketed links."
  5324. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5325. (not (org-in-src-block-p)))
  5326. (let* ((hl (org-match-string-no-properties 1))
  5327. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5328. (ip (org-maybe-intangible
  5329. (list 'invisible 'org-link
  5330. 'keymap org-mouse-map 'mouse-face 'highlight
  5331. 'font-lock-multiline t 'help-echo help
  5332. 'htmlize-link `(:uri ,hl))))
  5333. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5334. 'font-lock-multiline t 'help-echo help
  5335. 'htmlize-link `(:uri ,hl))))
  5336. ;; We need to remove the invisible property here. Table narrowing
  5337. ;; may have made some of this invisible.
  5338. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5339. (remove-text-properties (match-beginning 0) (match-end 0)
  5340. '(invisible nil))
  5341. (if (match-end 3)
  5342. (progn
  5343. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5344. (org-rear-nonsticky-at (match-beginning 3))
  5345. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5346. (org-rear-nonsticky-at (match-end 3))
  5347. (add-text-properties (match-end 3) (match-end 0) ip)
  5348. (org-rear-nonsticky-at (match-end 0)))
  5349. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5350. (org-rear-nonsticky-at (match-beginning 1))
  5351. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5352. (org-rear-nonsticky-at (match-end 1))
  5353. (add-text-properties (match-end 1) (match-end 0) ip)
  5354. (org-rear-nonsticky-at (match-end 0)))
  5355. t)))
  5356. (defun org-activate-dates (limit)
  5357. "Add text properties for dates."
  5358. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5359. (not (equal (char-before (match-beginning 0)) 91)))
  5360. (progn
  5361. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5362. (add-text-properties (match-beginning 0) (match-end 0)
  5363. (list 'mouse-face 'highlight
  5364. 'keymap org-mouse-map))
  5365. (org-rear-nonsticky-at (match-end 0))
  5366. (when org-display-custom-times
  5367. (if (match-end 3)
  5368. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5369. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5370. t)))
  5371. (defvar org-target-link-regexp nil
  5372. "Regular expression matching radio targets in plain text.")
  5373. (make-variable-buffer-local 'org-target-link-regexp)
  5374. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5375. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5376. border border border))
  5377. "Regular expression matching a link target.")
  5378. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5379. "Regular expression matching a radio target.")
  5380. (defconst org-any-target-regexp
  5381. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5382. "Regular expression matching any target.")
  5383. (defun org-activate-target-links (limit)
  5384. "Add text properties for target matches."
  5385. (when org-target-link-regexp
  5386. (let ((case-fold-search t))
  5387. (if (re-search-forward org-target-link-regexp limit t)
  5388. (progn
  5389. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5390. (add-text-properties (match-beginning 1) (match-end 1)
  5391. (list 'mouse-face 'highlight
  5392. 'keymap org-mouse-map
  5393. 'help-echo "Radio target link"
  5394. 'org-linked-text t))
  5395. (org-rear-nonsticky-at (match-end 1))
  5396. t)))))
  5397. (defun org-update-radio-target-regexp ()
  5398. "Find all radio targets in this file and update the regular expression.
  5399. Also refresh fontification if needed."
  5400. (interactive)
  5401. (let ((old-regexp org-target-link-regexp)
  5402. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5403. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5404. (targets
  5405. (org-with-wide-buffer
  5406. (goto-char (point-min))
  5407. (let (rtn)
  5408. (while (re-search-forward org-radio-target-regexp nil t)
  5409. ;; Make sure point is really within the object.
  5410. (backward-char)
  5411. (let ((obj (org-element-context)))
  5412. (when (eq (org-element-type obj) 'radio-target)
  5413. (add-to-list 'rtn (org-element-property :value obj)))))
  5414. rtn))))
  5415. (setq org-target-link-regexp
  5416. (and targets
  5417. (concat before-re
  5418. (mapconcat
  5419. (lambda (x)
  5420. (replace-regexp-in-string
  5421. " +" "\\s-+" (regexp-quote x) t t))
  5422. targets
  5423. "\\|")
  5424. after-re)))
  5425. (unless (equal old-regexp org-target-link-regexp)
  5426. ;; Clean-up cache.
  5427. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5428. ((not org-target-link-regexp) old-regexp)
  5429. (t
  5430. (concat before-re
  5431. (mapconcat
  5432. (lambda (re)
  5433. (substring re (length before-re)
  5434. (- (length after-re))))
  5435. (list old-regexp org-target-link-regexp)
  5436. "\\|")
  5437. after-re)))))
  5438. (org-with-wide-buffer
  5439. (goto-char (point-min))
  5440. (while (re-search-forward regexp nil t)
  5441. (org-element-cache-refresh (match-beginning 1)))))
  5442. ;; Re fontify buffer.
  5443. (when (memq 'radio org-highlight-links)
  5444. (org-restart-font-lock)))))
  5445. (defun org-hide-wide-columns (limit)
  5446. (let (s e)
  5447. (setq s (text-property-any (point) (or limit (point-max))
  5448. 'org-cwidth t))
  5449. (when s
  5450. (setq e (next-single-property-change s 'org-cwidth))
  5451. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5452. (goto-char e)
  5453. t)))
  5454. (defvar org-latex-and-related-regexp nil
  5455. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5456. (defun org-compute-latex-and-related-regexp ()
  5457. "Compute regular expression for LaTeX, entities and sub/superscript.
  5458. Result depends on variable `org-highlight-latex-and-related'."
  5459. (org-set-local
  5460. 'org-latex-and-related-regexp
  5461. (let* ((re-sub
  5462. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5463. ((eq org-use-sub-superscripts '{})
  5464. (list org-match-substring-with-braces-regexp))
  5465. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5466. (re-latex
  5467. (when (memq 'latex org-highlight-latex-and-related)
  5468. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5469. (delq nil
  5470. (mapcar (lambda (x)
  5471. (and (member (car x) matchers) (nth 1 x)))
  5472. org-latex-regexps)))))
  5473. (re-entities
  5474. (when (memq 'entities org-highlight-latex-and-related)
  5475. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5476. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5477. (defun org-do-latex-and-related (limit)
  5478. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5479. LIMIT bounds the search for syntax to highlight. Stop at first
  5480. highlighted object, if any. Return t if some highlighting was
  5481. done, nil otherwise."
  5482. (when (org-string-nw-p org-latex-and-related-regexp)
  5483. (catch 'found
  5484. (while (re-search-forward org-latex-and-related-regexp limit t)
  5485. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5486. 'face))
  5487. '(org-code org-verbatim underline))
  5488. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5489. '(?_ ?^))
  5490. 1
  5491. 0)))
  5492. (font-lock-prepend-text-property
  5493. (+ offset (match-beginning 0)) (match-end 0)
  5494. 'face 'org-latex-and-related)
  5495. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5496. '(font-lock-multiline t)))
  5497. (throw 'found t)))
  5498. nil)))
  5499. (defun org-restart-font-lock ()
  5500. "Restart `font-lock-mode', to force refontification."
  5501. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5502. (font-lock-mode -1)
  5503. (font-lock-mode 1)))
  5504. (defun org-activate-tags (limit)
  5505. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5506. (progn
  5507. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5508. (add-text-properties (match-beginning 1) (match-end 1)
  5509. (list 'mouse-face 'highlight
  5510. 'keymap org-mouse-map))
  5511. (org-rear-nonsticky-at (match-end 1))
  5512. t)))
  5513. (defun org-outline-level ()
  5514. "Compute the outline level of the heading at point.
  5515. If this is called at a normal headline, the level is the number
  5516. of stars. Use `org-reduced-level' to remove the effect of
  5517. `org-odd-levels'. Unlike to `org-current-level', this function
  5518. takes into consideration inlinetasks."
  5519. (org-with-wide-buffer
  5520. (end-of-line)
  5521. (if (re-search-backward org-outline-regexp-bol nil t)
  5522. (1- (- (match-end 0) (match-beginning 0)))
  5523. 0)))
  5524. (defvar org-font-lock-keywords nil)
  5525. (defsubst org-re-property (property &optional literal allow-null)
  5526. "Return a regexp matching a PROPERTY line.
  5527. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5528. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5529. non-nil, match properties even without a value.
  5530. Match group 3 is set to the value when it exists. If there is no
  5531. value and ALLOW-NULL is non-nil, it is set to the empty string."
  5532. (concat
  5533. "^\\(?4:[ \t]*\\)"
  5534. (format "\\(?1::\\(?2:%s\\):\\)"
  5535. (if literal property (regexp-quote property)))
  5536. (if allow-null
  5537. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$"
  5538. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$")))
  5539. (defconst org-property-re
  5540. (org-re-property "\\S-+" 'literal t)
  5541. "Regular expression matching a property line.
  5542. There are four matching groups:
  5543. 1: :PROPKEY: including the leading and trailing colon,
  5544. 2: PROPKEY without the leading and trailing colon,
  5545. 3: PROPVAL without leading or trailing spaces,
  5546. 4: the indentation of the current line,
  5547. 5: trailing whitespace.")
  5548. (defvar org-font-lock-hook nil
  5549. "Functions to be called for special font lock stuff.")
  5550. (defvar org-font-lock-set-keywords-hook nil
  5551. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5552. This is called after `org-font-lock-extra-keywords' is defined, but before
  5553. it is installed to be used by font lock. This can be useful if something
  5554. needs to be inserted at a specific position in the font-lock sequence.")
  5555. (defun org-font-lock-hook (limit)
  5556. "Run `org-font-lock-hook' within LIMIT."
  5557. (run-hook-with-args 'org-font-lock-hook limit))
  5558. (defun org-set-font-lock-defaults ()
  5559. "Set font lock defaults for the current buffer."
  5560. (let* ((em org-fontify-emphasized-text)
  5561. (lk org-highlight-links)
  5562. (org-font-lock-extra-keywords
  5563. (list
  5564. ;; Call the hook
  5565. '(org-font-lock-hook)
  5566. ;; Headlines
  5567. `(,(if org-fontify-whole-heading-line
  5568. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5569. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5570. (1 (org-get-level-face 1))
  5571. (2 (org-get-level-face 2))
  5572. (3 (org-get-level-face 3)))
  5573. ;; Table lines
  5574. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5575. (1 'org-table t))
  5576. ;; Table internals
  5577. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5578. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5579. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5580. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5581. ;; Drawers
  5582. '(org-fontify-drawers)
  5583. ;; Properties
  5584. (list org-property-re
  5585. '(1 'org-special-keyword t)
  5586. '(3 'org-property-value t))
  5587. ;; Links
  5588. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5589. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5590. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5591. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5592. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5593. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5594. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5595. ;; Targets.
  5596. (list org-any-target-regexp '(0 'org-target t))
  5597. ;; Diary sexps.
  5598. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5599. ;; Macro
  5600. '(org-fontify-macros)
  5601. '(org-hide-wide-columns (0 nil append))
  5602. ;; TODO keyword
  5603. (list (format org-heading-keyword-regexp-format
  5604. org-todo-regexp)
  5605. '(2 (org-get-todo-face 2) t))
  5606. ;; DONE
  5607. (if org-fontify-done-headline
  5608. (list (format org-heading-keyword-regexp-format
  5609. (concat
  5610. "\\(?:"
  5611. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5612. "\\)"))
  5613. '(2 'org-headline-done t))
  5614. nil)
  5615. ;; Priorities
  5616. '(org-font-lock-add-priority-faces)
  5617. ;; Tags
  5618. '(org-font-lock-add-tag-faces)
  5619. ;; Tags groups
  5620. (if (and org-group-tags org-tag-groups-alist)
  5621. (list (concat org-outline-regexp-bol ".+\\(:"
  5622. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5623. ":\\).*$")
  5624. '(1 'org-tag-group prepend)))
  5625. ;; Special keywords
  5626. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5627. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5628. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5629. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5630. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5631. ;; Emphasis
  5632. (if em
  5633. (if (featurep 'xemacs)
  5634. '(org-do-emphasis-faces (0 nil append))
  5635. '(org-do-emphasis-faces)))
  5636. ;; Checkboxes
  5637. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5638. 1 'org-checkbox prepend)
  5639. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5640. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5641. (0 (org-get-checkbox-statistics-face) t)))
  5642. ;; Description list items
  5643. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5644. 1 'org-list-dt prepend)
  5645. ;; ARCHIVEd headings
  5646. (list (concat
  5647. org-outline-regexp-bol
  5648. "\\(.*:" org-archive-tag ":.*\\)")
  5649. '(1 'org-archived prepend))
  5650. ;; Specials
  5651. '(org-do-latex-and-related)
  5652. '(org-fontify-entities)
  5653. '(org-raise-scripts)
  5654. ;; Code
  5655. '(org-activate-code (1 'org-code t))
  5656. ;; COMMENT
  5657. (list (format
  5658. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5659. org-todo-regexp
  5660. org-comment-string)
  5661. '(9 'org-special-keyword t))
  5662. ;; Blocks and meta lines
  5663. '(org-fontify-meta-lines-and-blocks))))
  5664. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5665. (run-hooks 'org-font-lock-set-keywords-hook)
  5666. ;; Now set the full font-lock-keywords
  5667. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5668. (org-set-local 'font-lock-defaults
  5669. '(org-font-lock-keywords t nil nil backward-paragraph))
  5670. (kill-local-variable 'font-lock-keywords) nil))
  5671. (defun org-toggle-pretty-entities ()
  5672. "Toggle the composition display of entities as UTF8 characters."
  5673. (interactive)
  5674. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5675. (org-restart-font-lock)
  5676. (if org-pretty-entities
  5677. (message "Entities are now displayed as UTF8 characters")
  5678. (save-restriction
  5679. (widen)
  5680. (org-decompose-region (point-min) (point-max))
  5681. (message "Entities are now displayed as plain text"))))
  5682. (defvar org-custom-properties-overlays nil
  5683. "List of overlays used for custom properties.")
  5684. (make-variable-buffer-local 'org-custom-properties-overlays)
  5685. (defun org-toggle-custom-properties-visibility ()
  5686. "Display or hide properties in `org-custom-properties'."
  5687. (interactive)
  5688. (if org-custom-properties-overlays
  5689. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5690. (setq org-custom-properties-overlays nil))
  5691. (when org-custom-properties
  5692. (org-with-wide-buffer
  5693. (goto-char (point-min))
  5694. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5695. (while (re-search-forward regexp nil t)
  5696. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5697. (when (and end (< (point) end))
  5698. ;; Hide first custom property in current drawer.
  5699. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5700. (overlay-put o 'invisible t)
  5701. (overlay-put o 'org-custom-property t)
  5702. (push o org-custom-properties-overlays))
  5703. ;; Hide additional custom properties in the same drawer.
  5704. (while (re-search-forward regexp end t)
  5705. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5706. (overlay-put o 'invisible t)
  5707. (overlay-put o 'org-custom-property t)
  5708. (push o org-custom-properties-overlays)))))
  5709. ;; Each entry is limited to a single property drawer.
  5710. (outline-next-heading)))))))
  5711. (defun org-fontify-entities (limit)
  5712. "Find an entity to fontify."
  5713. (let (ee)
  5714. (when org-pretty-entities
  5715. (catch 'match
  5716. (while (re-search-forward
  5717. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5718. limit t)
  5719. (if (and (not (org-at-comment-p))
  5720. (setq ee (org-entity-get (match-string 1)))
  5721. (= (length (nth 6 ee)) 1))
  5722. (let*
  5723. ((end (if (equal (match-string 2) "{}")
  5724. (match-end 2)
  5725. (match-end 1))))
  5726. (add-text-properties
  5727. (match-beginning 0) end
  5728. (list 'font-lock-fontified t))
  5729. (compose-region (match-beginning 0) end
  5730. (nth 6 ee) nil)
  5731. (backward-char 1)
  5732. (throw 'match t))))
  5733. nil))))
  5734. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5735. "Fontify string S like in Org-mode."
  5736. (with-temp-buffer
  5737. (insert s)
  5738. (let ((org-odd-levels-only odd-levels))
  5739. (org-mode)
  5740. (font-lock-ensure)
  5741. (buffer-string))))
  5742. (defvar org-m nil)
  5743. (defvar org-l nil)
  5744. (defvar org-f nil)
  5745. (defun org-get-level-face (n)
  5746. "Get the right face for match N in font-lock matching of headlines."
  5747. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5748. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5749. (if org-cycle-level-faces
  5750. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5751. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5752. (cond
  5753. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5754. ((eq n 2) org-f)
  5755. (t (if org-level-color-stars-only nil org-f))))
  5756. (defun org-get-todo-face (kwd)
  5757. "Get the right face for a TODO keyword KWD.
  5758. If KWD is a number, get the corresponding match group."
  5759. (if (numberp kwd) (setq kwd (match-string kwd)))
  5760. (or (org-face-from-face-or-color
  5761. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5762. (and (member kwd org-done-keywords) 'org-done)
  5763. 'org-todo))
  5764. (defun org-face-from-face-or-color (context inherit face-or-color)
  5765. "Create a face list that inherits INHERIT, but sets the foreground color.
  5766. When FACE-OR-COLOR is not a string, just return it."
  5767. (if (stringp face-or-color)
  5768. (list :inherit inherit
  5769. (cdr (assoc context org-faces-easy-properties))
  5770. face-or-color)
  5771. face-or-color))
  5772. (defun org-font-lock-add-tag-faces (limit)
  5773. "Add the special tag faces."
  5774. (when (and org-tag-faces org-tags-special-faces-re)
  5775. (while (re-search-forward org-tags-special-faces-re limit t)
  5776. (add-text-properties (match-beginning 1) (match-end 1)
  5777. (list 'face (org-get-tag-face 1)
  5778. 'font-lock-fontified t))
  5779. (backward-char 1))))
  5780. (defun org-font-lock-add-priority-faces (limit)
  5781. "Add the special priority faces."
  5782. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5783. (when (save-match-data (org-at-heading-p))
  5784. (add-text-properties
  5785. (match-beginning 0) (match-end 0)
  5786. (list 'face (or (org-face-from-face-or-color
  5787. 'priority 'org-priority
  5788. (cdr (assoc (char-after (match-beginning 1))
  5789. org-priority-faces)))
  5790. 'org-priority)
  5791. 'font-lock-fontified t)))))
  5792. (defun org-get-tag-face (kwd)
  5793. "Get the right face for a TODO keyword KWD.
  5794. If KWD is a number, get the corresponding match group."
  5795. (if (numberp kwd) (setq kwd (match-string kwd)))
  5796. (or (org-face-from-face-or-color
  5797. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5798. 'org-tag))
  5799. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5800. "Remove fontification and activation overlays from links."
  5801. (font-lock-default-unfontify-region beg end)
  5802. (let* ((buffer-undo-list t)
  5803. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5804. (inhibit-modification-hooks t)
  5805. deactivate-mark buffer-file-name buffer-file-truename)
  5806. (org-decompose-region beg end)
  5807. (remove-text-properties beg end
  5808. '(mouse-face t keymap t org-linked-text t
  5809. invisible t intangible t
  5810. org-emphasis t))
  5811. (org-remove-font-lock-display-properties beg end)))
  5812. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5813. ((raise 0.3) (height 0.7))
  5814. ((raise -0.5))
  5815. ((raise 0.5)))
  5816. "Display properties for showing superscripts and subscripts.")
  5817. (defun org-remove-font-lock-display-properties (beg end)
  5818. "Remove specific display properties that have been added by font lock.
  5819. The will remove the raise properties that are used to show superscripts
  5820. and subscripts."
  5821. (let (next prop)
  5822. (while (< beg end)
  5823. (setq next (next-single-property-change beg 'display nil end)
  5824. prop (get-text-property beg 'display))
  5825. (if (member prop org-script-display)
  5826. (put-text-property beg next 'display nil))
  5827. (setq beg next))))
  5828. (defun org-raise-scripts (limit)
  5829. "Add raise properties to sub/superscripts."
  5830. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5831. (if (re-search-forward
  5832. (if (eq org-use-sub-superscripts t)
  5833. org-match-substring-regexp
  5834. org-match-substring-with-braces-regexp)
  5835. limit t)
  5836. (let* ((pos (point)) table-p comment-p
  5837. (mpos (match-beginning 3))
  5838. (emph-p (get-text-property mpos 'org-emphasis))
  5839. (link-p (get-text-property mpos 'mouse-face))
  5840. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5841. (goto-char (point-at-bol))
  5842. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5843. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5844. (goto-char pos)
  5845. ;; Handle a_b^c
  5846. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5847. (if (or comment-p emph-p link-p keyw-p)
  5848. t
  5849. (put-text-property (match-beginning 3) (match-end 0)
  5850. 'display
  5851. (if (equal (char-after (match-beginning 2)) ?^)
  5852. (nth (if table-p 3 1) org-script-display)
  5853. (nth (if table-p 2 0) org-script-display)))
  5854. (add-text-properties (match-beginning 2) (match-end 2)
  5855. (list 'invisible t
  5856. 'org-dwidth t 'org-dwidth-n 1))
  5857. (if (and (eq (char-after (match-beginning 3)) ?{)
  5858. (eq (char-before (match-end 3)) ?}))
  5859. (progn
  5860. (add-text-properties
  5861. (match-beginning 3) (1+ (match-beginning 3))
  5862. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5863. (add-text-properties
  5864. (1- (match-end 3)) (match-end 3)
  5865. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5866. t)))))
  5867. ;;;; Visibility cycling, including org-goto and indirect buffer
  5868. ;;; Cycling
  5869. (defvar org-cycle-global-status nil)
  5870. (make-variable-buffer-local 'org-cycle-global-status)
  5871. (put 'org-cycle-global-status 'org-state t)
  5872. (defvar org-cycle-subtree-status nil)
  5873. (make-variable-buffer-local 'org-cycle-subtree-status)
  5874. (put 'org-cycle-subtree-status 'org-state t)
  5875. (defvar org-inlinetask-min-level)
  5876. (defun org-unlogged-message (&rest args)
  5877. "Display a message, but avoid logging it in the *Messages* buffer."
  5878. (let ((message-log-max nil))
  5879. (apply 'message args)))
  5880. ;;;###autoload
  5881. (defun org-cycle (&optional arg)
  5882. "TAB-action and visibility cycling for Org-mode.
  5883. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5884. is outline visibility cycling, but it also invokes other actions
  5885. in special contexts.
  5886. - When this function is called with a prefix argument, rotate the entire
  5887. buffer through 3 states (global cycling)
  5888. 1. OVERVIEW: Show only top-level headlines.
  5889. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5890. 3. SHOW ALL: Show everything.
  5891. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5892. determined by the variable `org-startup-folded', and by any VISIBILITY
  5893. properties in the buffer.
  5894. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5895. including any drawers.
  5896. - When inside a table, re-align the table and move to the next field.
  5897. - When point is at the beginning of a headline, rotate the subtree started
  5898. by this line through 3 different states (local cycling)
  5899. 1. FOLDED: Only the main headline is shown.
  5900. 2. CHILDREN: The main headline and the direct children are shown.
  5901. From this state, you can move to one of the children
  5902. and zoom in further.
  5903. 3. SUBTREE: Show the entire subtree, including body text.
  5904. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5905. - When point is at the beginning of an empty headline and the variable
  5906. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5907. of the headline by demoting and promoting it to likely levels. This
  5908. speeds up creation document structure by pressing TAB once or several
  5909. times right after creating a new headline.
  5910. - When there is a numeric prefix, go up to a heading with level ARG, do
  5911. a `show-subtree' and return to the previous cursor position. If ARG
  5912. is negative, go up that many levels.
  5913. - When point is not at the beginning of a headline, execute the global
  5914. binding for TAB, which is re-indenting the line. See the option
  5915. `org-cycle-emulate-tab' for details.
  5916. - Special case: if point is at the beginning of the buffer and there is
  5917. no headline in line 1, this function will act as if called with prefix arg
  5918. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5919. But only if also the variable `org-cycle-global-at-bob' is t."
  5920. (interactive "P")
  5921. (org-load-modules-maybe)
  5922. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5923. (and org-cycle-level-after-item/entry-creation
  5924. (or (org-cycle-level)
  5925. (org-cycle-item-indentation))))
  5926. (let* ((limit-level
  5927. (or org-cycle-max-level
  5928. (and (boundp 'org-inlinetask-min-level)
  5929. org-inlinetask-min-level
  5930. (1- org-inlinetask-min-level))))
  5931. (nstars (and limit-level
  5932. (if org-odd-levels-only
  5933. (and limit-level (1- (* limit-level 2)))
  5934. limit-level)))
  5935. (org-outline-regexp
  5936. (if (not (derived-mode-p 'org-mode))
  5937. outline-regexp
  5938. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5939. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5940. (not (looking-at org-outline-regexp))))
  5941. (org-cycle-hook
  5942. (if bob-special
  5943. (delq 'org-optimize-window-after-visibility-change
  5944. (copy-sequence org-cycle-hook))
  5945. org-cycle-hook))
  5946. (pos (point)))
  5947. (if (or bob-special (equal arg '(4)))
  5948. ;; special case: use global cycling
  5949. (setq arg t))
  5950. (cond
  5951. ((equal arg '(16))
  5952. (setq last-command 'dummy)
  5953. (org-set-startup-visibility)
  5954. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5955. ((equal arg '(64))
  5956. (show-all)
  5957. (org-unlogged-message "Entire buffer visible, including drawers"))
  5958. ;; Try cdlatex TAB completion
  5959. ((org-try-cdlatex-tab))
  5960. ;; Table: enter it or move to the next field.
  5961. ((org-at-table-p 'any)
  5962. (if (org-at-table.el-p)
  5963. (message "Use C-c ' to edit table.el tables")
  5964. (if arg (org-table-edit-field t)
  5965. (org-table-justify-field-maybe)
  5966. (call-interactively 'org-table-next-field))))
  5967. ((run-hook-with-args-until-success
  5968. 'org-tab-after-check-for-table-hook))
  5969. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5970. ((eq arg t) (org-cycle-internal-global))
  5971. ;; Drawers: delegate to `org-flag-drawer'.
  5972. ((save-excursion
  5973. (beginning-of-line 1)
  5974. (looking-at org-drawer-regexp))
  5975. (org-flag-drawer ; toggle block visibility
  5976. (not (get-char-property (match-end 0) 'invisible))))
  5977. ;; Show-subtree, ARG levels up from here.
  5978. ((integerp arg)
  5979. (save-excursion
  5980. (org-back-to-heading)
  5981. (outline-up-heading (if (< arg 0) (- arg)
  5982. (- (funcall outline-level) arg)))
  5983. (org-show-subtree)))
  5984. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5985. ((and (featurep 'org-inlinetask)
  5986. (org-inlinetask-at-task-p)
  5987. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5988. (org-inlinetask-toggle-visibility))
  5989. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5990. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5991. (save-excursion (beginning-of-line 1)
  5992. (looking-at org-outline-regexp)))
  5993. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5994. (org-cycle-internal-local))
  5995. ;; From there: TAB emulation and template completion.
  5996. (buffer-read-only (org-back-to-heading))
  5997. ((run-hook-with-args-until-success
  5998. 'org-tab-after-check-for-cycling-hook))
  5999. ((org-try-structure-completion))
  6000. ((run-hook-with-args-until-success
  6001. 'org-tab-before-tab-emulation-hook))
  6002. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6003. (or (not (bolp))
  6004. (not (looking-at org-outline-regexp))))
  6005. (call-interactively (global-key-binding "\t")))
  6006. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6007. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6008. (or (and (eq org-cycle-emulate-tab 'white)
  6009. (= (match-end 0) (point-at-eol)))
  6010. (and (eq org-cycle-emulate-tab 'whitestart)
  6011. (>= (match-end 0) pos))))
  6012. t
  6013. (eq org-cycle-emulate-tab t))
  6014. (call-interactively (global-key-binding "\t")))
  6015. (t (save-excursion
  6016. (org-back-to-heading)
  6017. (org-cycle)))))))
  6018. (defun org-cycle-internal-global ()
  6019. "Do the global cycling action."
  6020. ;; Hack to avoid display of messages for .org attachments in Gnus
  6021. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6022. (cond
  6023. ((and (eq last-command this-command)
  6024. (eq org-cycle-global-status 'overview))
  6025. ;; We just created the overview - now do table of contents
  6026. ;; This can be slow in very large buffers, so indicate action
  6027. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6028. (unless ga (org-unlogged-message "CONTENTS..."))
  6029. (org-content)
  6030. (unless ga (org-unlogged-message "CONTENTS...done"))
  6031. (setq org-cycle-global-status 'contents)
  6032. (run-hook-with-args 'org-cycle-hook 'contents))
  6033. ((and (eq last-command this-command)
  6034. (eq org-cycle-global-status 'contents))
  6035. ;; We just showed the table of contents - now show everything
  6036. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6037. (show-all)
  6038. (unless ga (org-unlogged-message "SHOW ALL"))
  6039. (setq org-cycle-global-status 'all)
  6040. (run-hook-with-args 'org-cycle-hook 'all))
  6041. (t
  6042. ;; Default action: go to overview
  6043. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6044. (org-overview)
  6045. (unless ga (org-unlogged-message "OVERVIEW"))
  6046. (setq org-cycle-global-status 'overview)
  6047. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6048. (defvar org-called-with-limited-levels nil
  6049. "Non-nil when `org-with-limited-levels' is currently active.")
  6050. (defun org-cycle-internal-local ()
  6051. "Do the local cycling action."
  6052. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6053. ;; First, determine end of headline (EOH), end of subtree or item
  6054. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6055. (save-excursion
  6056. (if (org-at-item-p)
  6057. (progn
  6058. (beginning-of-line)
  6059. (setq struct (org-list-struct))
  6060. (setq eoh (point-at-eol))
  6061. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6062. (setq has-children (org-list-has-child-p (point) struct)))
  6063. (org-back-to-heading)
  6064. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6065. (setq eos (save-excursion (org-end-of-subtree t t)
  6066. (when (bolp) (backward-char)) (point)))
  6067. (setq has-children
  6068. (or (save-excursion
  6069. (let ((level (funcall outline-level)))
  6070. (outline-next-heading)
  6071. (and (org-at-heading-p t)
  6072. (> (funcall outline-level) level))))
  6073. (save-excursion
  6074. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6075. ;; Determine end invisible part of buffer (EOL)
  6076. (beginning-of-line 2)
  6077. ;; XEmacs doesn't have `next-single-char-property-change'
  6078. (if (featurep 'xemacs)
  6079. (while (and (not (eobp)) ;; this is like `next-line'
  6080. (get-char-property (1- (point)) 'invisible))
  6081. (beginning-of-line 2))
  6082. (while (and (not (eobp)) ;; this is like `next-line'
  6083. (get-char-property (1- (point)) 'invisible))
  6084. (goto-char (next-single-char-property-change (point) 'invisible))
  6085. (and (eolp) (beginning-of-line 2))))
  6086. (setq eol (point)))
  6087. ;; Find out what to do next and set `this-command'
  6088. (cond
  6089. ((= eos eoh)
  6090. ;; Nothing is hidden behind this heading
  6091. (unless (org-before-first-heading-p)
  6092. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6093. (org-unlogged-message "EMPTY ENTRY")
  6094. (setq org-cycle-subtree-status nil)
  6095. (save-excursion
  6096. (goto-char eos)
  6097. (outline-next-heading)
  6098. (if (outline-invisible-p) (org-flag-heading nil))))
  6099. ((and (or (>= eol eos)
  6100. (not (string-match "\\S-" (buffer-substring eol eos))))
  6101. (or has-children
  6102. (not (setq children-skipped
  6103. org-cycle-skip-children-state-if-no-children))))
  6104. ;; Entire subtree is hidden in one line: children view
  6105. (unless (org-before-first-heading-p)
  6106. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6107. (if (org-at-item-p)
  6108. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6109. (org-show-entry)
  6110. (org-with-limited-levels (show-children))
  6111. ;; FIXME: This slows down the func way too much.
  6112. ;; How keep drawers hidden in subtree anyway?
  6113. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6114. ;; (org-cycle-hide-drawers 'subtree))
  6115. ;; Fold every list in subtree to top-level items.
  6116. (when (eq org-cycle-include-plain-lists 'integrate)
  6117. (save-excursion
  6118. (org-back-to-heading)
  6119. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6120. (beginning-of-line 1)
  6121. (let* ((struct (org-list-struct))
  6122. (prevs (org-list-prevs-alist struct))
  6123. (end (org-list-get-bottom-point struct)))
  6124. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6125. (org-list-get-all-items (point) struct prevs))
  6126. (goto-char (if (< end eos) end eos)))))))
  6127. (org-unlogged-message "CHILDREN")
  6128. (save-excursion
  6129. (goto-char eos)
  6130. (outline-next-heading)
  6131. (if (outline-invisible-p) (org-flag-heading nil)))
  6132. (setq org-cycle-subtree-status 'children)
  6133. (unless (org-before-first-heading-p)
  6134. (run-hook-with-args 'org-cycle-hook 'children)))
  6135. ((or children-skipped
  6136. (and (eq last-command this-command)
  6137. (eq org-cycle-subtree-status 'children)))
  6138. ;; We just showed the children, or no children are there,
  6139. ;; now show everything.
  6140. (unless (org-before-first-heading-p)
  6141. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6142. (outline-flag-region eoh eos nil)
  6143. (org-unlogged-message
  6144. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6145. (setq org-cycle-subtree-status 'subtree)
  6146. (unless (org-before-first-heading-p)
  6147. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6148. (t
  6149. ;; Default action: hide the subtree.
  6150. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6151. (outline-flag-region eoh eos t)
  6152. (org-unlogged-message "FOLDED")
  6153. (setq org-cycle-subtree-status 'folded)
  6154. (unless (org-before-first-heading-p)
  6155. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6156. ;;;###autoload
  6157. (defun org-global-cycle (&optional arg)
  6158. "Cycle the global visibility. For details see `org-cycle'.
  6159. With \\[universal-argument] prefix arg, switch to startup visibility.
  6160. With a numeric prefix, show all headlines up to that level."
  6161. (interactive "P")
  6162. (let ((org-cycle-include-plain-lists
  6163. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6164. (cond
  6165. ((integerp arg)
  6166. (show-all)
  6167. (hide-sublevels arg)
  6168. (setq org-cycle-global-status 'contents))
  6169. ((equal arg '(4))
  6170. (org-set-startup-visibility)
  6171. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6172. (t
  6173. (org-cycle '(4))))))
  6174. (defun org-set-startup-visibility ()
  6175. "Set the visibility required by startup options and properties."
  6176. (cond
  6177. ((eq org-startup-folded t)
  6178. (org-overview))
  6179. ((eq org-startup-folded 'content)
  6180. (org-content))
  6181. ((or (eq org-startup-folded 'showeverything)
  6182. (eq org-startup-folded nil))
  6183. (show-all)))
  6184. (unless (eq org-startup-folded 'showeverything)
  6185. (if org-hide-block-startup (org-hide-block-all))
  6186. (org-set-visibility-according-to-property 'no-cleanup)
  6187. (org-cycle-hide-archived-subtrees 'all)
  6188. (org-cycle-hide-drawers 'all)
  6189. (org-cycle-show-empty-lines t)))
  6190. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6191. "Switch subtree visibilities according to :VISIBILITY: property."
  6192. (interactive)
  6193. (let (org-show-entry-below)
  6194. (org-with-wide-buffer
  6195. (goto-char (point-min))
  6196. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6197. (if (not (org-at-property-p)) (outline-next-heading)
  6198. (let ((state (match-string 3)))
  6199. (save-excursion
  6200. (org-back-to-heading t)
  6201. (hide-subtree)
  6202. (org-reveal)
  6203. (cond
  6204. ((equal state "folded")
  6205. (hide-subtree))
  6206. ((equal state "children")
  6207. (org-show-hidden-entry)
  6208. (show-children))
  6209. ((equal state "content")
  6210. (save-excursion
  6211. (save-restriction
  6212. (org-narrow-to-subtree)
  6213. (org-content))))
  6214. ((member state '("all" "showall"))
  6215. (show-subtree)))))))
  6216. (unless no-cleanup
  6217. (org-cycle-hide-archived-subtrees 'all)
  6218. (org-cycle-hide-drawers 'all)
  6219. (org-cycle-show-empty-lines 'all)))))
  6220. ;; This function uses outline-regexp instead of the more fundamental
  6221. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6222. ;; buffers, where outline-regexp is needed.
  6223. (defun org-overview ()
  6224. "Switch to overview mode, showing only top-level headlines.
  6225. This shows all headlines with a level equal or greater than the level
  6226. of the first headline in the buffer. This is important, because if the
  6227. first headline is not level one, then (hide-sublevels 1) gives confusing
  6228. results."
  6229. (interactive)
  6230. (save-excursion
  6231. (let ((level
  6232. (save-excursion
  6233. (goto-char (point-min))
  6234. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6235. (progn
  6236. (goto-char (match-beginning 0))
  6237. (funcall outline-level))))))
  6238. (and level (hide-sublevels level)))))
  6239. (defun org-content (&optional arg)
  6240. "Show all headlines in the buffer, like a table of contents.
  6241. With numerical argument N, show content up to level N."
  6242. (interactive "P")
  6243. (org-overview)
  6244. (save-excursion
  6245. ;; Visit all headings and show their offspring
  6246. (and (integerp arg) (org-overview))
  6247. (goto-char (point-max))
  6248. (catch 'exit
  6249. (while (and (progn (condition-case nil
  6250. (outline-previous-visible-heading 1)
  6251. (error (goto-char (point-min))))
  6252. t)
  6253. (looking-at org-outline-regexp))
  6254. (if (integerp arg)
  6255. (show-children (1- arg))
  6256. (show-branches))
  6257. (if (bobp) (throw 'exit nil))))))
  6258. (defun org-optimize-window-after-visibility-change (state)
  6259. "Adjust the window after a change in outline visibility.
  6260. This function is the default value of the hook `org-cycle-hook'."
  6261. (when (get-buffer-window (current-buffer))
  6262. (cond
  6263. ((eq state 'content) nil)
  6264. ((eq state 'all) nil)
  6265. ((eq state 'folded) nil)
  6266. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6267. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6268. (defun org-remove-empty-overlays-at (pos)
  6269. "Remove outline overlays that do not contain non-white stuff."
  6270. (mapc
  6271. (lambda (o)
  6272. (and (eq 'outline (overlay-get o 'invisible))
  6273. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6274. (overlay-end o))))
  6275. (delete-overlay o)))
  6276. (overlays-at pos)))
  6277. (defun org-clean-visibility-after-subtree-move ()
  6278. "Fix visibility issues after moving a subtree."
  6279. ;; First, find a reasonable region to look at:
  6280. ;; Start two siblings above, end three below
  6281. (let* ((beg (save-excursion
  6282. (and (org-get-last-sibling)
  6283. (org-get-last-sibling))
  6284. (point)))
  6285. (end (save-excursion
  6286. (and (org-get-next-sibling)
  6287. (org-get-next-sibling)
  6288. (org-get-next-sibling))
  6289. (if (org-at-heading-p)
  6290. (point-at-eol)
  6291. (point))))
  6292. (level (looking-at "\\*+"))
  6293. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6294. (save-excursion
  6295. (save-restriction
  6296. (narrow-to-region beg end)
  6297. (when re
  6298. ;; Properly fold already folded siblings
  6299. (goto-char (point-min))
  6300. (while (re-search-forward re nil t)
  6301. (if (and (not (outline-invisible-p))
  6302. (save-excursion
  6303. (goto-char (point-at-eol)) (outline-invisible-p)))
  6304. (hide-entry))))
  6305. (org-cycle-show-empty-lines 'overview)
  6306. (org-cycle-hide-drawers 'overview)))))
  6307. (defun org-cycle-show-empty-lines (state)
  6308. "Show empty lines above all visible headlines.
  6309. The region to be covered depends on STATE when called through
  6310. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6311. entire buffer covered. Note that an empty line is only shown if there
  6312. are at least `org-cycle-separator-lines' empty lines before the headline."
  6313. (when (not (= org-cycle-separator-lines 0))
  6314. (save-excursion
  6315. (let* ((n (abs org-cycle-separator-lines))
  6316. (re (cond
  6317. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6318. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6319. (t (let ((ns (number-to-string (- n 2))))
  6320. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6321. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6322. beg end b e)
  6323. (cond
  6324. ((memq state '(overview contents t))
  6325. (setq beg (point-min) end (point-max)))
  6326. ((memq state '(children folded))
  6327. (setq beg (point) end (progn (org-end-of-subtree t t)
  6328. (beginning-of-line 2)
  6329. (point)))))
  6330. (when beg
  6331. (goto-char beg)
  6332. (while (re-search-forward re end t)
  6333. (unless (get-char-property (match-end 1) 'invisible)
  6334. (setq e (match-end 1))
  6335. (if (< org-cycle-separator-lines 0)
  6336. (setq b (save-excursion
  6337. (goto-char (match-beginning 0))
  6338. (org-back-over-empty-lines)
  6339. (if (save-excursion
  6340. (goto-char (max (point-min) (1- (point))))
  6341. (org-at-heading-p))
  6342. (1- (point))
  6343. (point))))
  6344. (setq b (match-beginning 1)))
  6345. (outline-flag-region b e nil)))))))
  6346. ;; Never hide empty lines at the end of the file.
  6347. (save-excursion
  6348. (goto-char (point-max))
  6349. (outline-previous-heading)
  6350. (outline-end-of-heading)
  6351. (if (and (looking-at "[ \t\n]+")
  6352. (= (match-end 0) (point-max)))
  6353. (outline-flag-region (point) (match-end 0) nil))))
  6354. (defun org-show-empty-lines-in-parent ()
  6355. "Move to the parent and re-show empty lines before visible headlines."
  6356. (save-excursion
  6357. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6358. (org-cycle-show-empty-lines context))))
  6359. (defun org-files-list ()
  6360. "Return `org-agenda-files' list, plus all open org-mode files.
  6361. This is useful for operations that need to scan all of a user's
  6362. open and agenda-wise Org files."
  6363. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6364. (dolist (buf (buffer-list))
  6365. (with-current-buffer buf
  6366. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6367. (let ((file (expand-file-name (buffer-file-name))))
  6368. (unless (member file files)
  6369. (push file files))))))
  6370. files))
  6371. (defsubst org-entry-beginning-position ()
  6372. "Return the beginning position of the current entry."
  6373. (save-excursion (outline-back-to-heading t) (point)))
  6374. (defsubst org-entry-end-position ()
  6375. "Return the end position of the current entry."
  6376. (save-excursion (outline-next-heading) (point)))
  6377. (defun org-cycle-hide-drawers (state &optional exceptions)
  6378. "Re-hide all drawers after a visibility state change.
  6379. When non-nil, optional argument EXCEPTIONS is a list of strings
  6380. specifying which drawers should not be hidden."
  6381. (when (and (derived-mode-p 'org-mode)
  6382. (not (memq state '(overview folded contents))))
  6383. (save-excursion
  6384. (let* ((globalp (memq state '(contents all)))
  6385. (beg (if globalp (point-min) (point)))
  6386. (end (if globalp (point-max)
  6387. (if (eq state 'children)
  6388. (save-excursion (outline-next-heading) (point))
  6389. (org-end-of-subtree t)))))
  6390. (goto-char beg)
  6391. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6392. (unless (member-ignore-case (match-string 1) exceptions)
  6393. (let ((drawer (org-element-at-point)))
  6394. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6395. (org-flag-drawer t drawer)
  6396. ;; Make sure to skip drawer entirely or we might flag
  6397. ;; it another time when matching its ending line with
  6398. ;; `org-drawer-regexp'.
  6399. (goto-char (org-element-property :end drawer))))))))))
  6400. (defun org-cycle-hide-inline-tasks (state)
  6401. "Re-hide inline tasks when switching to 'contents or 'children
  6402. visibility state."
  6403. (case state
  6404. (contents
  6405. (when (org-bound-and-true-p org-inlinetask-min-level)
  6406. (hide-sublevels (1- org-inlinetask-min-level))))
  6407. (children
  6408. (when (featurep 'org-inlinetask)
  6409. (save-excursion
  6410. (while (and (outline-next-heading)
  6411. (org-inlinetask-at-task-p))
  6412. (org-inlinetask-toggle-visibility)
  6413. (org-inlinetask-goto-end)))))))
  6414. (defun org-flag-drawer (flag &optional element)
  6415. "When FLAG is non-nil, hide the drawer we are at.
  6416. Otherwise make it visible. When optional argument ELEMENT is
  6417. a parsed drawer, as returned by `org-element-at-point', hide or
  6418. show that drawer instead."
  6419. (when (save-excursion
  6420. (beginning-of-line)
  6421. (org-looking-at-p org-drawer-regexp))
  6422. (let ((drawer (or element (org-element-at-point))))
  6423. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6424. (let ((post (org-element-property :post-affiliated drawer)))
  6425. (save-excursion
  6426. (outline-flag-region
  6427. (progn (goto-char post) (line-end-position))
  6428. (progn (goto-char (org-element-property :end drawer))
  6429. (skip-chars-backward " \r\t\n")
  6430. (line-end-position))
  6431. flag))
  6432. ;; When the drawer is hidden away, make sure point lies in
  6433. ;; a visible part of the buffer.
  6434. (when (and flag (> (line-beginning-position) post))
  6435. (goto-char post)))))))
  6436. (defun org-subtree-end-visible-p ()
  6437. "Is the end of the current subtree visible?"
  6438. (pos-visible-in-window-p
  6439. (save-excursion (org-end-of-subtree t) (point))))
  6440. (defun org-first-headline-recenter ()
  6441. "Move cursor to the first headline and recenter the headline."
  6442. (goto-char (point-min))
  6443. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6444. (set-window-start (selected-window) (point-at-bol))))
  6445. ;;; Saving and restoring visibility
  6446. (defun org-outline-overlay-data (&optional use-markers)
  6447. "Return a list of the locations of all outline overlays.
  6448. These are overlays with the `invisible' property value `outline'.
  6449. The return value is a list of cons cells, with start and stop
  6450. positions for each overlay.
  6451. If USE-MARKERS is set, return the positions as markers."
  6452. (let (beg end)
  6453. (save-excursion
  6454. (save-restriction
  6455. (widen)
  6456. (delq nil
  6457. (mapcar (lambda (o)
  6458. (when (eq (overlay-get o 'invisible) 'outline)
  6459. (setq beg (overlay-start o)
  6460. end (overlay-end o))
  6461. (and beg end (> end beg)
  6462. (if use-markers
  6463. (cons (copy-marker beg)
  6464. (copy-marker end t))
  6465. (cons beg end)))))
  6466. (overlays-in (point-min) (point-max))))))))
  6467. (defun org-set-outline-overlay-data (data)
  6468. "Create visibility overlays for all positions in DATA.
  6469. DATA should have been made by `org-outline-overlay-data'."
  6470. (let (o)
  6471. (save-excursion
  6472. (save-restriction
  6473. (widen)
  6474. (show-all)
  6475. (mapc (lambda (c)
  6476. (outline-flag-region (car c) (cdr c) t))
  6477. data)))))
  6478. ;;; Folding of blocks
  6479. (defvar org-hide-block-overlays nil
  6480. "Overlays hiding blocks.")
  6481. (make-variable-buffer-local 'org-hide-block-overlays)
  6482. (defun org-block-map (function &optional start end)
  6483. "Call FUNCTION at the head of all source blocks in the current buffer.
  6484. Optional arguments START and END can be used to limit the range."
  6485. (let ((start (or start (point-min)))
  6486. (end (or end (point-max))))
  6487. (save-excursion
  6488. (goto-char start)
  6489. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6490. (save-excursion
  6491. (save-match-data
  6492. (goto-char (match-beginning 0))
  6493. (funcall function)))))))
  6494. (defun org-hide-block-toggle-all ()
  6495. "Toggle the visibility of all blocks in the current buffer."
  6496. (org-block-map 'org-hide-block-toggle))
  6497. (defun org-hide-block-all ()
  6498. "Fold all blocks in the current buffer."
  6499. (interactive)
  6500. (org-show-block-all)
  6501. (org-block-map 'org-hide-block-toggle-maybe))
  6502. (defun org-show-block-all ()
  6503. "Unfold all blocks in the current buffer."
  6504. (interactive)
  6505. (mapc 'delete-overlay org-hide-block-overlays)
  6506. (setq org-hide-block-overlays nil))
  6507. (defun org-hide-block-toggle-maybe ()
  6508. "Toggle visibility of block at point.
  6509. Unlike to `org-hide-block-toggle', this function does not throw
  6510. an error. Return a non-nil value when toggling is successful."
  6511. (interactive)
  6512. (ignore-errors (org-hide-block-toggle)))
  6513. (defun org-hide-block-toggle (&optional force)
  6514. "Toggle the visibility of the current block.
  6515. When optional argument FORCE is `off', make block visible. If it
  6516. is non-nil, hide it unconditionally. Throw an error when not at
  6517. a block. Return a non-nil value when toggling is successful."
  6518. (interactive)
  6519. (let ((element (org-element-at-point)))
  6520. (unless (memq (org-element-type element)
  6521. '(center-block comment-block dynamic-block example-block
  6522. export-block quote-block special-block
  6523. src-block verse-block))
  6524. (user-error "Not at a block"))
  6525. (let* ((start (save-excursion
  6526. (goto-char (org-element-property :post-affiliated element))
  6527. (line-end-position)))
  6528. (end (save-excursion
  6529. (goto-char (org-element-property :end element))
  6530. (skip-chars-backward " \r\t\n")
  6531. (line-end-position)))
  6532. (overlays (overlays-at start)))
  6533. (cond
  6534. ;; Do nothing when not before or at the block opening line or
  6535. ;; at the block closing line.
  6536. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6537. ((and (not (eq force 'off))
  6538. (not (memq t (mapcar
  6539. (lambda (o)
  6540. (eq (overlay-get o 'invisible) 'org-hide-block))
  6541. overlays))))
  6542. (let ((ov (make-overlay start end)))
  6543. (overlay-put ov 'invisible 'org-hide-block)
  6544. ;; Make the block accessible to `isearch'.
  6545. (overlay-put
  6546. ov 'isearch-open-invisible
  6547. (lambda (ov)
  6548. (when (memq ov org-hide-block-overlays)
  6549. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6550. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6551. (delete-overlay ov))))
  6552. (push ov org-hide-block-overlays)
  6553. ;; When the block is hidden away, make sure point is left in
  6554. ;; a visible part of the buffer.
  6555. (when (> (line-beginning-position) start)
  6556. (goto-char start)
  6557. (beginning-of-line))
  6558. ;; Signal successful toggling.
  6559. t))
  6560. ((or (not force) (eq force 'off))
  6561. (dolist (ov overlays t)
  6562. (when (memq ov org-hide-block-overlays)
  6563. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6564. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6565. (delete-overlay ov))))))))
  6566. ;; org-tab-after-check-for-cycling-hook
  6567. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6568. ;; Remove overlays when changing major mode
  6569. (add-hook 'org-mode-hook
  6570. (lambda () (org-add-hook 'change-major-mode-hook
  6571. 'org-show-block-all 'append 'local)))
  6572. ;;; Org-goto
  6573. (defvar org-goto-window-configuration nil)
  6574. (defvar org-goto-marker nil)
  6575. (defvar org-goto-map)
  6576. (defun org-goto-map ()
  6577. "Set the keymap `org-goto'."
  6578. (setq org-goto-map
  6579. (let ((map (make-sparse-keymap)))
  6580. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6581. mouse-drag-region universal-argument org-occur))
  6582. cmd)
  6583. (while (setq cmd (pop cmds))
  6584. (substitute-key-definition cmd cmd map global-map)))
  6585. (suppress-keymap map)
  6586. (org-defkey map "\C-m" 'org-goto-ret)
  6587. (org-defkey map [(return)] 'org-goto-ret)
  6588. (org-defkey map [(left)] 'org-goto-left)
  6589. (org-defkey map [(right)] 'org-goto-right)
  6590. (org-defkey map [(control ?g)] 'org-goto-quit)
  6591. (org-defkey map "\C-i" 'org-cycle)
  6592. (org-defkey map [(tab)] 'org-cycle)
  6593. (org-defkey map [(down)] 'outline-next-visible-heading)
  6594. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6595. (if org-goto-auto-isearch
  6596. (if (fboundp 'define-key-after)
  6597. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6598. nil)
  6599. (org-defkey map "q" 'org-goto-quit)
  6600. (org-defkey map "n" 'outline-next-visible-heading)
  6601. (org-defkey map "p" 'outline-previous-visible-heading)
  6602. (org-defkey map "f" 'outline-forward-same-level)
  6603. (org-defkey map "b" 'outline-backward-same-level)
  6604. (org-defkey map "u" 'outline-up-heading))
  6605. (org-defkey map "/" 'org-occur)
  6606. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6607. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6608. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6609. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6610. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6611. map)))
  6612. (defconst org-goto-help
  6613. "Browse buffer copy, to find location or copy text.%s
  6614. RET=jump to location C-g=quit and return to previous location
  6615. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6616. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6617. (defun org-goto (&optional alternative-interface)
  6618. "Look up a different location in the current file, keeping current visibility.
  6619. When you want look-up or go to a different location in a
  6620. document, the fastest way is often to fold the entire buffer and
  6621. then dive into the tree. This method has the disadvantage, that
  6622. the previous location will be folded, which may not be what you
  6623. want.
  6624. This command works around this by showing a copy of the current
  6625. buffer in an indirect buffer, in overview mode. You can dive
  6626. into the tree in that copy, use org-occur and incremental search
  6627. to find a location. When pressing RET or `Q', the command
  6628. returns to the original buffer in which the visibility is still
  6629. unchanged. After RET it will also jump to the location selected
  6630. in the indirect buffer and expose the headline hierarchy above.
  6631. With a prefix argument, use the alternative interface: e.g. if
  6632. `org-goto-interface' is 'outline use 'outline-path-completion."
  6633. (interactive "P")
  6634. (org-goto-map)
  6635. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6636. (org-refile-use-outline-path t)
  6637. (org-refile-target-verify-function nil)
  6638. (interface
  6639. (if (not alternative-interface)
  6640. org-goto-interface
  6641. (if (eq org-goto-interface 'outline)
  6642. 'outline-path-completion
  6643. 'outline)))
  6644. (org-goto-start-pos (point))
  6645. (selected-point
  6646. (if (eq interface 'outline)
  6647. (car (org-get-location (current-buffer) org-goto-help))
  6648. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6649. (org-refile-check-position pa)
  6650. (nth 3 pa)))))
  6651. (if selected-point
  6652. (progn
  6653. (org-mark-ring-push org-goto-start-pos)
  6654. (goto-char selected-point)
  6655. (if (or (outline-invisible-p) (org-invisible-p2))
  6656. (org-show-context 'org-goto)))
  6657. (message "Quit"))))
  6658. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6659. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6660. (defvar org-goto-local-auto-isearch-map) ; defined below
  6661. (defun org-get-location (buf help)
  6662. "Let the user select a location in the Org-mode buffer BUF.
  6663. This function uses a recursive edit. It returns the selected position
  6664. or nil."
  6665. (org-no-popups
  6666. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6667. (isearch-hide-immediately nil)
  6668. (isearch-search-fun-function
  6669. (lambda () 'org-goto-local-search-headings))
  6670. (org-goto-selected-point org-goto-exit-command))
  6671. (save-excursion
  6672. (save-window-excursion
  6673. (delete-other-windows)
  6674. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6675. (org-pop-to-buffer-same-window
  6676. (condition-case nil
  6677. (make-indirect-buffer (current-buffer) "*org-goto*")
  6678. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6679. (with-output-to-temp-buffer "*Org Help*"
  6680. (princ (format help (if org-goto-auto-isearch
  6681. " Just type for auto-isearch."
  6682. " n/p/f/b/u to navigate, q to quit."))))
  6683. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6684. (setq buffer-read-only nil)
  6685. (let ((org-startup-truncated t)
  6686. (org-startup-folded nil)
  6687. (org-startup-align-all-tables nil))
  6688. (org-mode)
  6689. (org-overview))
  6690. (setq buffer-read-only t)
  6691. (if (and (boundp 'org-goto-start-pos)
  6692. (integer-or-marker-p org-goto-start-pos))
  6693. (let ((org-show-hierarchy-above t)
  6694. (org-show-siblings t)
  6695. (org-show-following-heading t))
  6696. (goto-char org-goto-start-pos)
  6697. (and (outline-invisible-p) (org-show-context)))
  6698. (goto-char (point-min)))
  6699. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6700. (message "Select location and press RET")
  6701. (use-local-map org-goto-map)
  6702. (recursive-edit)))
  6703. (kill-buffer "*org-goto*")
  6704. (cons org-goto-selected-point org-goto-exit-command))))
  6705. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6706. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6707. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6708. (if (fboundp 'isearch-other-control-char)
  6709. (progn
  6710. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6711. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6712. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6713. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6714. (define-key org-goto-local-auto-isearch-map [return] nil))
  6715. (defun org-goto-local-search-headings (string bound noerror)
  6716. "Search and make sure that any matches are in headlines."
  6717. (catch 'return
  6718. (while (if isearch-forward
  6719. (search-forward string bound noerror)
  6720. (search-backward string bound noerror))
  6721. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6722. (and (member :headline context)
  6723. (not (member :tags context))))
  6724. (throw 'return (point))))))
  6725. (defun org-goto-local-auto-isearch ()
  6726. "Start isearch."
  6727. (interactive)
  6728. (goto-char (point-min))
  6729. (let ((keys (this-command-keys)))
  6730. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6731. (isearch-mode t)
  6732. (isearch-process-search-char (string-to-char keys)))))
  6733. (defun org-goto-ret (&optional arg)
  6734. "Finish `org-goto' by going to the new location."
  6735. (interactive "P")
  6736. (setq org-goto-selected-point (point)
  6737. org-goto-exit-command 'return)
  6738. (throw 'exit nil))
  6739. (defun org-goto-left ()
  6740. "Finish `org-goto' by going to the new location."
  6741. (interactive)
  6742. (if (org-at-heading-p)
  6743. (progn
  6744. (beginning-of-line 1)
  6745. (setq org-goto-selected-point (point)
  6746. org-goto-exit-command 'left)
  6747. (throw 'exit nil))
  6748. (user-error "Not on a heading")))
  6749. (defun org-goto-right ()
  6750. "Finish `org-goto' by going to the new location."
  6751. (interactive)
  6752. (if (org-at-heading-p)
  6753. (progn
  6754. (setq org-goto-selected-point (point)
  6755. org-goto-exit-command 'right)
  6756. (throw 'exit nil))
  6757. (user-error "Not on a heading")))
  6758. (defun org-goto-quit ()
  6759. "Finish `org-goto' without cursor motion."
  6760. (interactive)
  6761. (setq org-goto-selected-point nil)
  6762. (setq org-goto-exit-command 'quit)
  6763. (throw 'exit nil))
  6764. ;;; Indirect buffer display of subtrees
  6765. (defvar org-indirect-dedicated-frame nil
  6766. "This is the frame being used for indirect tree display.")
  6767. (defvar org-last-indirect-buffer nil)
  6768. (defun org-tree-to-indirect-buffer (&optional arg)
  6769. "Create indirect buffer and narrow it to current subtree.
  6770. With a numerical prefix ARG, go up to this level and then take that tree.
  6771. If ARG is negative, go up that many levels.
  6772. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6773. indirect buffer previously made with this command, to avoid proliferation of
  6774. indirect buffers. However, when you call the command with a \
  6775. \\[universal-argument] prefix, or
  6776. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6777. is kept so that you can work with several indirect buffers at the same time.
  6778. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6779. \\[universal-argument] prefix also
  6780. requests that a new frame be made for the new buffer, so that the dedicated
  6781. frame is not changed."
  6782. (interactive "P")
  6783. (let ((cbuf (current-buffer))
  6784. (cwin (selected-window))
  6785. (pos (point))
  6786. beg end level heading ibuf)
  6787. (save-excursion
  6788. (org-back-to-heading t)
  6789. (when (numberp arg)
  6790. (setq level (org-outline-level))
  6791. (if (< arg 0) (setq arg (+ level arg)))
  6792. (while (> (setq level (org-outline-level)) arg)
  6793. (org-up-heading-safe)))
  6794. (setq beg (point)
  6795. heading (org-get-heading))
  6796. (org-end-of-subtree t t)
  6797. (if (org-at-heading-p) (backward-char 1))
  6798. (setq end (point)))
  6799. (if (and (buffer-live-p org-last-indirect-buffer)
  6800. (not (eq org-indirect-buffer-display 'new-frame))
  6801. (not arg))
  6802. (kill-buffer org-last-indirect-buffer))
  6803. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6804. org-last-indirect-buffer ibuf)
  6805. (cond
  6806. ((or (eq org-indirect-buffer-display 'new-frame)
  6807. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6808. (select-frame (make-frame))
  6809. (delete-other-windows)
  6810. (org-pop-to-buffer-same-window ibuf)
  6811. (org-set-frame-title heading))
  6812. ((eq org-indirect-buffer-display 'dedicated-frame)
  6813. (raise-frame
  6814. (select-frame (or (and org-indirect-dedicated-frame
  6815. (frame-live-p org-indirect-dedicated-frame)
  6816. org-indirect-dedicated-frame)
  6817. (setq org-indirect-dedicated-frame (make-frame)))))
  6818. (delete-other-windows)
  6819. (org-pop-to-buffer-same-window ibuf)
  6820. (org-set-frame-title (concat "Indirect: " heading)))
  6821. ((eq org-indirect-buffer-display 'current-window)
  6822. (org-pop-to-buffer-same-window ibuf))
  6823. ((eq org-indirect-buffer-display 'other-window)
  6824. (pop-to-buffer ibuf))
  6825. (t (error "Invalid value")))
  6826. (if (featurep 'xemacs)
  6827. (save-excursion (org-mode) (turn-on-font-lock)))
  6828. (narrow-to-region beg end)
  6829. (show-all)
  6830. (goto-char pos)
  6831. (run-hook-with-args 'org-cycle-hook 'all)
  6832. (and (window-live-p cwin) (select-window cwin))))
  6833. (defun org-get-indirect-buffer (&optional buffer heading)
  6834. (setq buffer (or buffer (current-buffer)))
  6835. (let ((n 1) (base (buffer-name buffer)) bname)
  6836. (while (buffer-live-p
  6837. (get-buffer
  6838. (setq bname
  6839. (concat base "-"
  6840. (if heading (concat heading "-" (number-to-string n))
  6841. (number-to-string n))))))
  6842. (setq n (1+ n)))
  6843. (condition-case nil
  6844. (make-indirect-buffer buffer bname 'clone)
  6845. (error (make-indirect-buffer buffer bname)))))
  6846. (defun org-set-frame-title (title)
  6847. "Set the title of the current frame to the string TITLE."
  6848. ;; FIXME: how to name a single frame in XEmacs???
  6849. (unless (featurep 'xemacs)
  6850. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6851. ;;;; Structure editing
  6852. ;;; Inserting headlines
  6853. (defun org-previous-line-empty-p (&optional next)
  6854. "Is the previous line a blank line?
  6855. When NEXT is non-nil, check the next line instead."
  6856. (save-excursion
  6857. (and (not (bobp))
  6858. (or (beginning-of-line (if next 2 0)) t)
  6859. (save-match-data
  6860. (looking-at "[ \t]*$")))))
  6861. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6862. "Insert a new heading or an item with the same depth at point.
  6863. If point is at the beginning of a heading or a list item, insert
  6864. a new heading or a new item above the current one. If point is
  6865. at the beginning of a normal line, turn the line into a heading.
  6866. If point is in the middle of a headline or a list item, split the
  6867. headline or the item and create a new headline/item with the text
  6868. in the current line after point \(see `org-M-RET-may-split-line'
  6869. on how to modify this behavior).
  6870. With one universal prefix argument, set the user option
  6871. `org-insert-heading-respect-content' to t for the duration of
  6872. the command. This modifies the behavior described above in this
  6873. ways: on list items and at the beginning of normal lines, force
  6874. the insertion of a heading after the current subtree.
  6875. With two universal prefix arguments, insert the heading at the
  6876. end of the grandparent subtree. For example, if point is within
  6877. a 2nd-level heading, then it will insert a 2nd-level heading at
  6878. the end of the 1st-level parent heading.
  6879. If point is at the beginning of a headline, insert a sibling
  6880. before the current headline. If point is not at the beginning,
  6881. split the line and create a new headline with the text in the
  6882. current line after point \(see `org-M-RET-may-split-line' on how
  6883. to modify this behavior).
  6884. If point is at the beginning of a normal line, turn this line
  6885. into a heading.
  6886. When INVISIBLE-OK is set, stop at invisible headlines when going
  6887. back. This is important for non-interactive uses of the
  6888. command.
  6889. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6890. heading, unconditionally."
  6891. (interactive "P")
  6892. (if (org-called-interactively-p 'any) (org-reveal))
  6893. (let ((itemp (and (not top-level) (org-in-item-p)))
  6894. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6895. (respect-content (or org-insert-heading-respect-content
  6896. (equal arg '(4))))
  6897. (initial-content "")
  6898. (adjust-empty-lines t))
  6899. (cond
  6900. ((or (= (buffer-size) 0)
  6901. (and (not (save-excursion
  6902. (and (ignore-errors (org-back-to-heading invisible-ok))
  6903. (org-at-heading-p))))
  6904. (or arg (not itemp))))
  6905. ;; At beginning of buffer or so high up that only a heading
  6906. ;; makes sense.
  6907. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6908. ((not respect-content)
  6909. (unless may-split (end-of-line))
  6910. (insert "\n* "))
  6911. ((re-search-forward org-outline-regexp-bol nil t)
  6912. (beginning-of-line)
  6913. (insert "* \n")
  6914. (backward-char))
  6915. (t (goto-char (point-max))
  6916. (insert "\n* ")))
  6917. (run-hooks 'org-insert-heading-hook))
  6918. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6919. (t
  6920. ;; Maybe move at the end of the subtree
  6921. (when (equal arg '(16))
  6922. (org-up-heading-safe)
  6923. (org-end-of-subtree t))
  6924. ;; Insert a heading
  6925. (save-restriction
  6926. (widen)
  6927. (let* ((level nil)
  6928. (on-heading (org-at-heading-p))
  6929. (empty-line-p (if on-heading
  6930. (org-previous-line-empty-p)
  6931. ;; We will decide later
  6932. nil))
  6933. ;; Get a level string to fall back on.
  6934. (fix-level
  6935. (if (org-before-first-heading-p) "*"
  6936. (save-excursion
  6937. (org-back-to-heading t)
  6938. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6939. (looking-at org-outline-regexp)
  6940. (make-string (1- (length (match-string 0))) ?*))))
  6941. (stars
  6942. (save-excursion
  6943. (condition-case nil
  6944. (if top-level "* "
  6945. (org-back-to-heading invisible-ok)
  6946. (when (and (not on-heading)
  6947. (featurep 'org-inlinetask)
  6948. (integerp org-inlinetask-min-level)
  6949. (>= (length (match-string 0))
  6950. org-inlinetask-min-level))
  6951. ;; Find a heading level before the inline
  6952. ;; task.
  6953. (while (and (setq level (org-up-heading-safe))
  6954. (>= level org-inlinetask-min-level)))
  6955. (if (org-at-heading-p)
  6956. (org-back-to-heading invisible-ok)
  6957. (error "This should not happen")))
  6958. (unless (and (save-excursion
  6959. (save-match-data
  6960. (org-backward-heading-same-level
  6961. 1 invisible-ok))
  6962. (= (point) (match-beginning 0)))
  6963. (not (org-previous-line-empty-p t)))
  6964. (setq empty-line-p (or empty-line-p
  6965. (org-previous-line-empty-p))))
  6966. (match-string 0))
  6967. (error (or fix-level "* ")))))
  6968. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6969. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6970. pos hide-previous previous-pos)
  6971. ;; If we insert after content, move there and clean up
  6972. ;; whitespace.
  6973. (when (and respect-content
  6974. (not (org-looking-at-p org-outline-regexp-bol)))
  6975. (if (not (org-before-first-heading-p))
  6976. (org-end-of-subtree nil t)
  6977. (re-search-forward org-outline-regexp-bol)
  6978. (beginning-of-line 0))
  6979. (skip-chars-backward " \r\t\n")
  6980. (and (not (looking-back "^\\*+"))
  6981. (looking-at "[ \t]+") (replace-match ""))
  6982. (unless (eobp) (forward-char 1))
  6983. (when (looking-at "^\\*")
  6984. (unless (bobp) (backward-char 1))
  6985. (insert "\n")))
  6986. ;; If we are splitting, grab the text that should be moved
  6987. ;; to the new headline.
  6988. (when may-split
  6989. (if (org-on-heading-p)
  6990. ;; This is a heading: split intelligently (keeping
  6991. ;; tags).
  6992. (let ((pos (point)))
  6993. (beginning-of-line)
  6994. (unless (looking-at org-complex-heading-regexp)
  6995. (error "This should not happen"))
  6996. (when (and (match-beginning 4)
  6997. (> pos (match-beginning 4))
  6998. (< pos (match-end 4)))
  6999. (setq initial-content (buffer-substring pos (match-end 4)))
  7000. (goto-char pos)
  7001. (delete-region (point) (match-end 4))
  7002. (if (looking-at "[ \t]*$")
  7003. (replace-match "")
  7004. (insert (make-string (length initial-content) ?\s)))
  7005. (setq initial-content (org-trim initial-content)))
  7006. (goto-char pos))
  7007. ;; A normal line.
  7008. (setq initial-content
  7009. (org-trim
  7010. (delete-and-extract-region (point) (line-end-position))))))
  7011. ;; If we are at the beginning of the line, insert before it.
  7012. ;; Otherwise, after it.
  7013. (cond
  7014. ((and (bolp) (looking-at "[ \t]*$")))
  7015. ((bolp) (open-line 1))
  7016. (t (end-of-line)
  7017. (insert "\n")))
  7018. ;; Insert the new heading
  7019. (insert stars)
  7020. (just-one-space)
  7021. (insert initial-content)
  7022. (when (and adjust-empty-lines
  7023. (not (and blank (org-previous-line-empty-p))))
  7024. (org-N-empty-lines-before-current (if blank 1 0)))
  7025. (run-hooks 'org-insert-heading-hook)))))))
  7026. (defun org-N-empty-lines-before-current (N)
  7027. "Make the number of empty lines before current exactly N.
  7028. So this will delete or add empty lines."
  7029. (save-excursion
  7030. (beginning-of-line)
  7031. (let ((p (point)))
  7032. (skip-chars-backward " \r\t\n")
  7033. (unless (bolp) (forward-line))
  7034. (delete-region (point) p))
  7035. (when (> N 0) (insert (make-string N ?\n)))))
  7036. (defun org-get-heading (&optional no-tags no-todo)
  7037. "Return the heading of the current entry, without the stars.
  7038. When NO-TAGS is non-nil, don't include tags.
  7039. When NO-TODO is non-nil, don't include TODO keywords."
  7040. (save-excursion
  7041. (org-back-to-heading t)
  7042. (cond
  7043. ((and no-tags no-todo)
  7044. (looking-at org-complex-heading-regexp)
  7045. (match-string 4))
  7046. (no-tags
  7047. (looking-at (concat org-outline-regexp
  7048. "\\(.*?\\)"
  7049. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7050. (match-string 1))
  7051. (no-todo
  7052. (looking-at org-todo-line-regexp)
  7053. (match-string 3))
  7054. (t (looking-at org-heading-regexp)
  7055. (match-string 2)))))
  7056. (defvar orgstruct-mode) ; defined below
  7057. (defun org-heading-components ()
  7058. "Return the components of the current heading.
  7059. This is a list with the following elements:
  7060. - the level as an integer
  7061. - the reduced level, different if `org-odd-levels-only' is set.
  7062. - the TODO keyword, or nil
  7063. - the priority character, like ?A, or nil if no priority is given
  7064. - the headline text itself, or the tags string if no headline text
  7065. - the tags string, or nil."
  7066. (save-excursion
  7067. (org-back-to-heading t)
  7068. (if (let (case-fold-search)
  7069. (looking-at
  7070. (if orgstruct-mode
  7071. org-heading-regexp
  7072. org-complex-heading-regexp)))
  7073. (if orgstruct-mode
  7074. (list (length (match-string 1))
  7075. (org-reduced-level (length (match-string 1)))
  7076. nil
  7077. nil
  7078. (match-string 2)
  7079. nil)
  7080. (list (length (match-string 1))
  7081. (org-reduced-level (length (match-string 1)))
  7082. (org-match-string-no-properties 2)
  7083. (and (match-end 3) (aref (match-string 3) 2))
  7084. (org-match-string-no-properties 4)
  7085. (org-match-string-no-properties 5))))))
  7086. (defun org-get-entry ()
  7087. "Get the entry text, after heading, entire subtree."
  7088. (save-excursion
  7089. (org-back-to-heading t)
  7090. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7091. (defun org-insert-heading-after-current ()
  7092. "Insert a new heading with same level as current, after current subtree."
  7093. (interactive)
  7094. (org-back-to-heading)
  7095. (org-insert-heading)
  7096. (org-move-subtree-down)
  7097. (end-of-line 1))
  7098. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7099. "Insert heading with `org-insert-heading-respect-content' set to t."
  7100. (interactive)
  7101. (org-insert-heading '(4) invisible-ok))
  7102. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7103. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7104. (interactive)
  7105. (org-insert-todo-heading force-state '(4)))
  7106. (defun org-insert-todo-heading (arg &optional force-heading)
  7107. "Insert a new heading with the same level and TODO state as current heading.
  7108. If the heading has no TODO state, or if the state is DONE, use the first
  7109. state (TODO by default). Also with one prefix arg, force first state. With
  7110. two prefix args, force inserting at the end of the parent subtree."
  7111. (interactive "P")
  7112. (when (or force-heading (not (org-insert-item 'checkbox)))
  7113. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7114. force-heading))
  7115. (save-excursion
  7116. (org-back-to-heading)
  7117. (outline-previous-heading)
  7118. (looking-at org-todo-line-regexp))
  7119. (let*
  7120. ((new-mark-x
  7121. (if (or (equal arg '(4))
  7122. (not (match-beginning 2))
  7123. (member (match-string 2) org-done-keywords))
  7124. (car org-todo-keywords-1)
  7125. (match-string 2)))
  7126. (new-mark
  7127. (or
  7128. (run-hook-with-args-until-success
  7129. 'org-todo-get-default-hook new-mark-x nil)
  7130. new-mark-x)))
  7131. (beginning-of-line 1)
  7132. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7133. (if org-treat-insert-todo-heading-as-state-change
  7134. (org-todo new-mark)
  7135. (insert new-mark " "))))
  7136. (when org-provide-todo-statistics
  7137. (org-update-parent-todo-statistics))))
  7138. (defun org-insert-subheading (arg)
  7139. "Insert a new subheading and demote it.
  7140. Works for outline headings and for plain lists alike."
  7141. (interactive "P")
  7142. (org-insert-heading arg)
  7143. (cond
  7144. ((org-at-heading-p) (org-do-demote))
  7145. ((org-at-item-p) (org-indent-item))))
  7146. (defun org-insert-todo-subheading (arg)
  7147. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7148. Works for outline headings and for plain lists alike."
  7149. (interactive "P")
  7150. (org-insert-todo-heading arg)
  7151. (cond
  7152. ((org-at-heading-p) (org-do-demote))
  7153. ((org-at-item-p) (org-indent-item))))
  7154. ;;; Promotion and Demotion
  7155. (defvar org-after-demote-entry-hook nil
  7156. "Hook run after an entry has been demoted.
  7157. The cursor will be at the beginning of the entry.
  7158. When a subtree is being demoted, the hook will be called for each node.")
  7159. (defvar org-after-promote-entry-hook nil
  7160. "Hook run after an entry has been promoted.
  7161. The cursor will be at the beginning of the entry.
  7162. When a subtree is being promoted, the hook will be called for each node.")
  7163. (defun org-promote-subtree ()
  7164. "Promote the entire subtree.
  7165. See also `org-promote'."
  7166. (interactive)
  7167. (save-excursion
  7168. (org-with-limited-levels (org-map-tree 'org-promote)))
  7169. (org-fix-position-after-promote))
  7170. (defun org-demote-subtree ()
  7171. "Demote the entire subtree. See `org-demote'.
  7172. See also `org-promote'."
  7173. (interactive)
  7174. (save-excursion
  7175. (org-with-limited-levels (org-map-tree 'org-demote)))
  7176. (org-fix-position-after-promote))
  7177. (defun org-do-promote ()
  7178. "Promote the current heading higher up the tree.
  7179. If the region is active in `transient-mark-mode', promote all headings
  7180. in the region."
  7181. (interactive)
  7182. (save-excursion
  7183. (if (org-region-active-p)
  7184. (org-map-region 'org-promote (region-beginning) (region-end))
  7185. (org-promote)))
  7186. (org-fix-position-after-promote))
  7187. (defun org-do-demote ()
  7188. "Demote the current heading lower down the tree.
  7189. If the region is active in `transient-mark-mode', demote all headings
  7190. in the region."
  7191. (interactive)
  7192. (save-excursion
  7193. (if (org-region-active-p)
  7194. (org-map-region 'org-demote (region-beginning) (region-end))
  7195. (org-demote)))
  7196. (org-fix-position-after-promote))
  7197. (defun org-fix-position-after-promote ()
  7198. "Make sure that after pro/demotion cursor position is right."
  7199. (let ((pos (point)))
  7200. (when (save-excursion
  7201. (beginning-of-line 1)
  7202. (looking-at org-todo-line-regexp)
  7203. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7204. (cond ((eobp) (insert " "))
  7205. ((eolp) (insert " "))
  7206. ((equal (char-after) ?\ ) (forward-char 1))))))
  7207. (defun org-current-level ()
  7208. "Return the level of the current entry, or nil if before the first headline.
  7209. The level is the number of stars at the beginning of the
  7210. headline. Use `org-reduced-level' to remove the effect of
  7211. `org-odd-levels'. Unlike to `org-outline-level', this function
  7212. ignores inlinetasks."
  7213. (let ((level (org-with-limited-levels (org-outline-level))))
  7214. (and (> level 0) level)))
  7215. (defun org-get-previous-line-level ()
  7216. "Return the outline depth of the last headline before the current line.
  7217. Returns 0 for the first headline in the buffer, and nil if before the
  7218. first headline."
  7219. (and (org-current-level)
  7220. (or (and (/= (line-beginning-position) (point-min))
  7221. (save-excursion (beginning-of-line 0) (org-current-level)))
  7222. 0)))
  7223. (defun org-reduced-level (l)
  7224. "Compute the effective level of a heading.
  7225. This takes into account the setting of `org-odd-levels-only'."
  7226. (cond
  7227. ((zerop l) 0)
  7228. (org-odd-levels-only (1+ (floor (/ l 2))))
  7229. (t l)))
  7230. (defun org-level-increment ()
  7231. "Return the number of stars that will be added or removed at a
  7232. time to headlines when structure editing, based on the value of
  7233. `org-odd-levels-only'."
  7234. (if org-odd-levels-only 2 1))
  7235. (defun org-get-valid-level (level &optional change)
  7236. "Rectify a level change under the influence of `org-odd-levels-only'
  7237. LEVEL is a current level, CHANGE is by how much the level should be
  7238. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7239. even level numbers will become the next higher odd number."
  7240. (if org-odd-levels-only
  7241. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7242. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7243. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7244. (max 1 (+ level (or change 0)))))
  7245. (if (boundp 'define-obsolete-function-alias)
  7246. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7247. (define-obsolete-function-alias 'org-get-legal-level
  7248. 'org-get-valid-level)
  7249. (define-obsolete-function-alias 'org-get-legal-level
  7250. 'org-get-valid-level "23.1")))
  7251. (defun org-promote ()
  7252. "Promote the current heading higher up the tree."
  7253. (org-with-wide-buffer
  7254. (org-back-to-heading t)
  7255. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7256. after-change-functions))
  7257. (level (save-match-data (funcall outline-level)))
  7258. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7259. (diff (abs (- level (length up-head) -1))))
  7260. (cond
  7261. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7262. (replace-match "# " nil t))
  7263. ((= level 1)
  7264. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7265. (t (replace-match up-head nil t)))
  7266. (unless (= level 1)
  7267. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7268. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7269. (run-hooks 'org-after-promote-entry-hook))))
  7270. (defun org-demote ()
  7271. "Demote the current heading lower down the tree."
  7272. (org-with-wide-buffer
  7273. (org-back-to-heading t)
  7274. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7275. after-change-functions))
  7276. (level (save-match-data (funcall outline-level)))
  7277. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7278. (diff (abs (- level (length down-head) -1))))
  7279. (replace-match down-head nil t)
  7280. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7281. (when org-adapt-indentation (org-fixup-indentation diff))
  7282. (run-hooks 'org-after-demote-entry-hook))))
  7283. (defun org-cycle-level ()
  7284. "Cycle the level of an empty headline through possible states.
  7285. This goes first to child, then to parent, level, then up the hierarchy.
  7286. After top level, it switches back to sibling level."
  7287. (interactive)
  7288. (let ((org-adapt-indentation nil))
  7289. (when (org-point-at-end-of-empty-headline)
  7290. (setq this-command 'org-cycle-level) ; Only needed for caching
  7291. (let ((cur-level (org-current-level))
  7292. (prev-level (org-get-previous-line-level)))
  7293. (cond
  7294. ;; If first headline in file, promote to top-level.
  7295. ((= prev-level 0)
  7296. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7297. do (org-do-promote)))
  7298. ;; If same level as prev, demote one.
  7299. ((= prev-level cur-level)
  7300. (org-do-demote))
  7301. ;; If parent is top-level, promote to top level if not already.
  7302. ((= prev-level 1)
  7303. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7304. do (org-do-promote)))
  7305. ;; If top-level, return to prev-level.
  7306. ((= cur-level 1)
  7307. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7308. do (org-do-demote)))
  7309. ;; If less than prev-level, promote one.
  7310. ((< cur-level prev-level)
  7311. (org-do-promote))
  7312. ;; If deeper than prev-level, promote until higher than
  7313. ;; prev-level.
  7314. ((> cur-level prev-level)
  7315. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7316. do (org-do-promote))))
  7317. t))))
  7318. (defun org-map-tree (fun)
  7319. "Call FUN for every heading underneath the current one."
  7320. (org-back-to-heading)
  7321. (let ((level (funcall outline-level)))
  7322. (save-excursion
  7323. (funcall fun)
  7324. (while (and (progn
  7325. (outline-next-heading)
  7326. (> (funcall outline-level) level))
  7327. (not (eobp)))
  7328. (funcall fun)))))
  7329. (defun org-map-region (fun beg end)
  7330. "Call FUN for every heading between BEG and END."
  7331. (let ((org-ignore-region t))
  7332. (save-excursion
  7333. (setq end (copy-marker end))
  7334. (goto-char beg)
  7335. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7336. (< (point) end))
  7337. (funcall fun))
  7338. (while (and (progn
  7339. (outline-next-heading)
  7340. (< (point) end))
  7341. (not (eobp)))
  7342. (funcall fun)))))
  7343. (defun org-fixup-indentation (diff)
  7344. "Change the indentation in the current entry by DIFF.
  7345. DIFF is an integer. Indentation is done according to the
  7346. following rules:
  7347. - Planning information and property drawers are always indented
  7348. according to the new level of the headline;
  7349. - Footnote definitions and their contents are ignored;
  7350. - Inlinetasks' boundaries are not shifted;
  7351. - Empty lines are ignored;
  7352. - Other lines' indentation are shifted by DIFF columns, unless
  7353. it would introduce a structural change in the document, in
  7354. which case no shifting is done at all.
  7355. Assume point is at a heading or an inlinetask beginning."
  7356. (org-with-wide-buffer
  7357. (narrow-to-region (line-beginning-position)
  7358. (save-excursion
  7359. (if (org-with-limited-levels (org-at-heading-p))
  7360. (org-with-limited-levels (outline-next-heading))
  7361. (org-inlinetask-goto-end))
  7362. (point)))
  7363. (forward-line)
  7364. ;; Indent properly planning info and property drawer.
  7365. (when (org-looking-at-p org-planning-line-re)
  7366. (org-indent-line)
  7367. (forward-line))
  7368. (when (looking-at org-property-drawer-re)
  7369. (goto-char (match-end 0))
  7370. (forward-line)
  7371. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7372. (catch 'no-shift
  7373. (when (zerop diff) (throw 'no-shift nil))
  7374. ;; If DIFF is negative, first check if a shift is possible at all
  7375. ;; (e.g., it doesn't break structure). This can only happen if
  7376. ;; some contents are not properly indented.
  7377. (let ((case-fold-search t))
  7378. (when (< diff 0)
  7379. (let ((diff (- diff))
  7380. (forbidden-re (concat org-outline-regexp
  7381. "\\|"
  7382. (substring org-footnote-definition-re 1))))
  7383. (save-excursion
  7384. (while (not (eobp))
  7385. (cond
  7386. ((org-looking-at-p "[ \t]*$") (forward-line))
  7387. ((and (org-looking-at-p org-footnote-definition-re)
  7388. (let ((e (org-element-at-point)))
  7389. (and (eq (org-element-type e) 'footnote-definition)
  7390. (goto-char (org-element-property :end e))))))
  7391. ((org-looking-at-p org-outline-regexp) (forward-line))
  7392. ;; Give up if shifting would move before column 0 or
  7393. ;; if it would introduce a headline or a footnote
  7394. ;; definition.
  7395. (t
  7396. (skip-chars-forward " \t")
  7397. (let ((ind (current-column)))
  7398. (when (or (< ind diff)
  7399. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7400. (throw 'no-shift nil)))
  7401. ;; Ignore contents of example blocks and source
  7402. ;; blocks if their indentation is meant to be
  7403. ;; preserved. Jump to block's closing line.
  7404. (beginning-of-line)
  7405. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7406. (let ((e (org-element-at-point)))
  7407. (and (memq (org-element-type e)
  7408. '(example-block src-block))
  7409. (or org-src-preserve-indentation
  7410. (org-element-property :preserve-indent e))
  7411. (goto-char (org-element-property :end e))
  7412. (progn (skip-chars-backward " \r\t\n")
  7413. (beginning-of-line)
  7414. t))))
  7415. (forward-line))))))))
  7416. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7417. ;; by DIFF. Also skip contents of source or example blocks
  7418. ;; when indentation is meant to be preserved.
  7419. (while (not (eobp))
  7420. (cond
  7421. ((and (org-looking-at-p org-footnote-definition-re)
  7422. (let ((e (org-element-at-point)))
  7423. (and (eq (org-element-type e) 'footnote-definition)
  7424. (goto-char (org-element-property :end e))))))
  7425. ((org-looking-at-p org-outline-regexp) (forward-line))
  7426. ((org-looking-at-p "[ \t]*$") (forward-line))
  7427. (t
  7428. (org-indent-line-to (+ (org-get-indentation) diff))
  7429. (beginning-of-line)
  7430. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7431. (let ((e (org-element-at-point)))
  7432. (and (memq (org-element-type e)
  7433. '(example-block src-block))
  7434. (or org-src-preserve-indentation
  7435. (org-element-property :preserve-indent e))
  7436. (goto-char (org-element-property :end e))
  7437. (progn (skip-chars-backward " \r\t\n")
  7438. (beginning-of-line)
  7439. t))))
  7440. (forward-line)))))))))
  7441. (defun org-convert-to-odd-levels ()
  7442. "Convert an org-mode file with all levels allowed to one with odd levels.
  7443. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7444. level 5 etc."
  7445. (interactive)
  7446. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7447. (let ((outline-level 'org-outline-level)
  7448. (org-odd-levels-only nil) n)
  7449. (save-excursion
  7450. (goto-char (point-min))
  7451. (while (re-search-forward "^\\*\\*+ " nil t)
  7452. (setq n (- (length (match-string 0)) 2))
  7453. (while (>= (setq n (1- n)) 0)
  7454. (org-demote))
  7455. (end-of-line 1))))))
  7456. (defun org-convert-to-oddeven-levels ()
  7457. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7458. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7459. file contains a section with an even level, conversion would
  7460. destroy the structure of the file. An error is signaled in this
  7461. case."
  7462. (interactive)
  7463. (goto-char (point-min))
  7464. ;; First check if there are no even levels
  7465. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7466. (org-show-context t)
  7467. (error "Not all levels are odd in this file. Conversion not possible"))
  7468. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7469. (let ((outline-regexp org-outline-regexp)
  7470. (outline-level 'org-outline-level)
  7471. (org-odd-levels-only nil) n)
  7472. (save-excursion
  7473. (goto-char (point-min))
  7474. (while (re-search-forward "^\\*\\*+ " nil t)
  7475. (setq n (/ (1- (length (match-string 0))) 2))
  7476. (while (>= (setq n (1- n)) 0)
  7477. (org-promote))
  7478. (end-of-line 1))))))
  7479. (defun org-tr-level (n)
  7480. "Make N odd if required."
  7481. (if org-odd-levels-only (1+ (/ n 2)) n))
  7482. ;;; Vertical tree motion, cutting and pasting of subtrees
  7483. (defun org-move-subtree-up (&optional arg)
  7484. "Move the current subtree up past ARG headlines of the same level."
  7485. (interactive "p")
  7486. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7487. (defun org-move-subtree-down (&optional arg)
  7488. "Move the current subtree down past ARG headlines of the same level."
  7489. (interactive "p")
  7490. (setq arg (prefix-numeric-value arg))
  7491. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7492. 'org-get-last-sibling))
  7493. (ins-point (make-marker))
  7494. (cnt (abs arg))
  7495. (col (current-column))
  7496. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7497. ;; Select the tree
  7498. (org-back-to-heading)
  7499. (setq beg0 (point))
  7500. (save-excursion
  7501. (setq ne-beg (org-back-over-empty-lines))
  7502. (setq beg (point)))
  7503. (save-match-data
  7504. (save-excursion (outline-end-of-heading)
  7505. (setq folded (outline-invisible-p)))
  7506. (progn (org-end-of-subtree nil t)
  7507. (unless (eobp) (backward-char))))
  7508. (outline-next-heading)
  7509. (setq ne-end (org-back-over-empty-lines))
  7510. (setq end (point))
  7511. (goto-char beg0)
  7512. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7513. ;; include less whitespace
  7514. (save-excursion
  7515. (goto-char beg)
  7516. (forward-line (- ne-beg ne-end))
  7517. (setq beg (point))))
  7518. ;; Find insertion point, with error handling
  7519. (while (> cnt 0)
  7520. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7521. (progn (goto-char beg0)
  7522. (user-error "Cannot move past superior level or buffer limit")))
  7523. (setq cnt (1- cnt)))
  7524. (if (> arg 0)
  7525. ;; Moving forward - still need to move over subtree
  7526. (progn (org-end-of-subtree t t)
  7527. (save-excursion
  7528. (org-back-over-empty-lines)
  7529. (or (bolp) (newline)))))
  7530. (setq ne-ins (org-back-over-empty-lines))
  7531. (move-marker ins-point (point))
  7532. (setq txt (buffer-substring beg end))
  7533. (org-save-markers-in-region beg end)
  7534. (delete-region beg end)
  7535. (org-remove-empty-overlays-at beg)
  7536. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7537. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7538. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7539. (let ((bbb (point)))
  7540. (insert-before-markers txt)
  7541. (org-reinstall-markers-in-region bbb)
  7542. (move-marker ins-point bbb))
  7543. (or (bolp) (insert "\n"))
  7544. (setq ins-end (point))
  7545. (goto-char ins-point)
  7546. (org-skip-whitespace)
  7547. (when (and (< arg 0)
  7548. (org-first-sibling-p)
  7549. (> ne-ins ne-beg))
  7550. ;; Move whitespace back to beginning
  7551. (save-excursion
  7552. (goto-char ins-end)
  7553. (let ((kill-whole-line t))
  7554. (kill-line (- ne-ins ne-beg)) (point)))
  7555. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7556. (move-marker ins-point nil)
  7557. (if folded
  7558. (hide-subtree)
  7559. (org-show-entry)
  7560. (show-children)
  7561. (org-cycle-hide-drawers 'children))
  7562. (org-clean-visibility-after-subtree-move)
  7563. ;; move back to the initial column we were at
  7564. (move-to-column col)))
  7565. (defvar org-subtree-clip ""
  7566. "Clipboard for cut and paste of subtrees.
  7567. This is actually only a copy of the kill, because we use the normal kill
  7568. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7569. (defvar org-subtree-clip-folded nil
  7570. "Was the last copied subtree folded?
  7571. This is used to fold the tree back after pasting.")
  7572. (defun org-cut-subtree (&optional n)
  7573. "Cut the current subtree into the clipboard.
  7574. With prefix arg N, cut this many sequential subtrees.
  7575. This is a short-hand for marking the subtree and then cutting it."
  7576. (interactive "p")
  7577. (org-copy-subtree n 'cut))
  7578. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7579. "Copy the current subtree it in the clipboard.
  7580. With prefix arg N, copy this many sequential subtrees.
  7581. This is a short-hand for marking the subtree and then copying it.
  7582. If CUT is non-nil, actually cut the subtree.
  7583. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7584. of some markers in the region, even if CUT is non-nil. This is
  7585. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7586. (interactive "p")
  7587. (let (beg end folded (beg0 (point)))
  7588. (if (org-called-interactively-p 'any)
  7589. (org-back-to-heading nil) ; take what looks like a subtree
  7590. (org-back-to-heading t)) ; take what is really there
  7591. (setq beg (point))
  7592. (skip-chars-forward " \t\r\n")
  7593. (save-match-data
  7594. (if nosubtrees
  7595. (outline-next-heading)
  7596. (save-excursion (outline-end-of-heading)
  7597. (setq folded (outline-invisible-p)))
  7598. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7599. (org-end-of-subtree t t)))
  7600. (setq end (point))
  7601. (goto-char beg0)
  7602. (when (> end beg)
  7603. (setq org-subtree-clip-folded folded)
  7604. (when (or cut force-store-markers)
  7605. (org-save-markers-in-region beg end))
  7606. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7607. (setq org-subtree-clip (current-kill 0))
  7608. (message "%s: Subtree(s) with %d characters"
  7609. (if cut "Cut" "Copied")
  7610. (length org-subtree-clip)))))
  7611. (defun org-paste-subtree (&optional level tree for-yank remove)
  7612. "Paste the clipboard as a subtree, with modification of headline level.
  7613. The entire subtree is promoted or demoted in order to match a new headline
  7614. level.
  7615. If the cursor is at the beginning of a headline, the same level as
  7616. that headline is used to paste the tree.
  7617. If not, the new level is derived from the *visible* headings
  7618. before and after the insertion point, and taken to be the inferior headline
  7619. level of the two. So if the previous visible heading is level 3 and the
  7620. next is level 4 (or vice versa), level 4 will be used for insertion.
  7621. This makes sure that the subtree remains an independent subtree and does
  7622. not swallow low level entries.
  7623. You can also force a different level, either by using a numeric prefix
  7624. argument, or by inserting the heading marker by hand. For example, if the
  7625. cursor is after \"*****\", then the tree will be shifted to level 5.
  7626. If optional TREE is given, use this text instead of the kill ring.
  7627. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7628. move back over whitespace before inserting, and move point to the end of
  7629. the inserted text when done.
  7630. When REMOVE is non-nil, remove the subtree from the clipboard."
  7631. (interactive "P")
  7632. (setq tree (or tree (and kill-ring (current-kill 0))))
  7633. (unless (org-kill-is-subtree-p tree)
  7634. (user-error "%s"
  7635. (substitute-command-keys
  7636. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7637. (org-with-limited-levels
  7638. (let* ((visp (not (outline-invisible-p)))
  7639. (txt tree)
  7640. (^re_ "\\(\\*+\\)[ \t]*")
  7641. (old-level (if (string-match org-outline-regexp-bol txt)
  7642. (- (match-end 0) (match-beginning 0) 1)
  7643. -1))
  7644. (force-level (cond (level (prefix-numeric-value level))
  7645. ((and (looking-at "[ \t]*$")
  7646. (string-match
  7647. "^\\*+$" (buffer-substring
  7648. (point-at-bol) (point))))
  7649. (- (match-end 0) (match-beginning 0)))
  7650. ((and (bolp)
  7651. (looking-at org-outline-regexp))
  7652. (- (match-end 0) (point) 1))))
  7653. (previous-level (save-excursion
  7654. (condition-case nil
  7655. (progn
  7656. (outline-previous-visible-heading 1)
  7657. (if (looking-at ^re_)
  7658. (- (match-end 0) (match-beginning 0) 1)
  7659. 1))
  7660. (error 1))))
  7661. (next-level (save-excursion
  7662. (condition-case nil
  7663. (progn
  7664. (or (looking-at org-outline-regexp)
  7665. (outline-next-visible-heading 1))
  7666. (if (looking-at ^re_)
  7667. (- (match-end 0) (match-beginning 0) 1)
  7668. 1))
  7669. (error 1))))
  7670. (new-level (or force-level (max previous-level next-level)))
  7671. (shift (if (or (= old-level -1)
  7672. (= new-level -1)
  7673. (= old-level new-level))
  7674. 0
  7675. (- new-level old-level)))
  7676. (delta (if (> shift 0) -1 1))
  7677. (func (if (> shift 0) 'org-demote 'org-promote))
  7678. (org-odd-levels-only nil)
  7679. beg end newend)
  7680. ;; Remove the forced level indicator
  7681. (if force-level
  7682. (delete-region (point-at-bol) (point)))
  7683. ;; Paste
  7684. (beginning-of-line (if (bolp) 1 2))
  7685. (setq beg (point))
  7686. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7687. (insert-before-markers txt)
  7688. (unless (string-match "\n\\'" txt) (insert "\n"))
  7689. (setq newend (point))
  7690. (org-reinstall-markers-in-region beg)
  7691. (setq end (point))
  7692. (goto-char beg)
  7693. (skip-chars-forward " \t\n\r")
  7694. (setq beg (point))
  7695. (if (and (outline-invisible-p) visp)
  7696. (save-excursion (outline-show-heading)))
  7697. ;; Shift if necessary
  7698. (unless (= shift 0)
  7699. (save-restriction
  7700. (narrow-to-region beg end)
  7701. (while (not (= shift 0))
  7702. (org-map-region func (point-min) (point-max))
  7703. (setq shift (+ delta shift)))
  7704. (goto-char (point-min))
  7705. (setq newend (point-max))))
  7706. (when (or (org-called-interactively-p 'interactive) for-yank)
  7707. (message "Clipboard pasted as level %d subtree" new-level))
  7708. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7709. kill-ring
  7710. (eq org-subtree-clip (current-kill 0))
  7711. org-subtree-clip-folded)
  7712. ;; The tree was folded before it was killed/copied
  7713. (hide-subtree))
  7714. (and for-yank (goto-char newend))
  7715. (and remove (setq kill-ring (cdr kill-ring))))))
  7716. (defun org-kill-is-subtree-p (&optional txt)
  7717. "Check if the current kill is an outline subtree, or a set of trees.
  7718. Returns nil if kill does not start with a headline, or if the first
  7719. headline level is not the largest headline level in the tree.
  7720. So this will actually accept several entries of equal levels as well,
  7721. which is OK for `org-paste-subtree'.
  7722. If optional TXT is given, check this string instead of the current kill."
  7723. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7724. (re (org-get-limited-outline-regexp))
  7725. (^re (concat "^" re))
  7726. (start-level (and kill
  7727. (string-match
  7728. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7729. kill)
  7730. (- (match-end 2) (match-beginning 2) 1)))
  7731. (start (1+ (or (match-beginning 2) -1))))
  7732. (if (not start-level)
  7733. (progn
  7734. nil) ;; does not even start with a heading
  7735. (catch 'exit
  7736. (while (setq start (string-match ^re kill (1+ start)))
  7737. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7738. (throw 'exit nil)))
  7739. t))))
  7740. (defvar org-markers-to-move nil
  7741. "Markers that should be moved with a cut-and-paste operation.
  7742. Those markers are stored together with their positions relative to
  7743. the start of the region.")
  7744. (defun org-save-markers-in-region (beg end)
  7745. "Check markers in region.
  7746. If these markers are between BEG and END, record their position relative
  7747. to BEG, so that after moving the block of text, we can put the markers back
  7748. into place.
  7749. This function gets called just before an entry or tree gets cut from the
  7750. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7751. called immediately, to move the markers with the entries."
  7752. (setq org-markers-to-move nil)
  7753. (when (featurep 'org-clock)
  7754. (org-clock-save-markers-for-cut-and-paste beg end))
  7755. (when (featurep 'org-agenda)
  7756. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7757. (defun org-check-and-save-marker (marker beg end)
  7758. "Check if MARKER is between BEG and END.
  7759. If yes, remember the marker and the distance to BEG."
  7760. (when (and (marker-buffer marker)
  7761. (equal (marker-buffer marker) (current-buffer)))
  7762. (if (and (>= marker beg) (< marker end))
  7763. (push (cons marker (- marker beg)) org-markers-to-move))))
  7764. (defun org-reinstall-markers-in-region (beg)
  7765. "Move all remembered markers to their position relative to BEG."
  7766. (mapc (lambda (x)
  7767. (move-marker (car x) (+ beg (cdr x))))
  7768. org-markers-to-move)
  7769. (setq org-markers-to-move nil))
  7770. (defun org-narrow-to-subtree ()
  7771. "Narrow buffer to the current subtree."
  7772. (interactive)
  7773. (save-excursion
  7774. (save-match-data
  7775. (org-with-limited-levels
  7776. (narrow-to-region
  7777. (progn (org-back-to-heading t) (point))
  7778. (progn (org-end-of-subtree t t)
  7779. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7780. (point)))))))
  7781. (defun org-narrow-to-block ()
  7782. "Narrow buffer to the current block."
  7783. (interactive)
  7784. (let* ((case-fold-search t)
  7785. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7786. "^[ \t]*#\\+end_.*")))
  7787. (if blockp
  7788. (narrow-to-region (car blockp) (cdr blockp))
  7789. (user-error "Not in a block"))))
  7790. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7791. "Clone the task (subtree) at point N times.
  7792. The clones will be inserted as siblings.
  7793. In interactive use, the user will be prompted for the number of
  7794. clones to be produced. If the entry has a timestamp, the user
  7795. will also be prompted for a time shift, which may be a repeater
  7796. as used in time stamps, for example `+3d'. To disable this,
  7797. you can call the function with a universal prefix argument.
  7798. When a valid repeater is given and the entry contains any time
  7799. stamps, the clones will become a sequence in time, with time
  7800. stamps in the subtree shifted for each clone produced. If SHIFT
  7801. is nil or the empty string, time stamps will be left alone. The
  7802. ID property of the original subtree is removed.
  7803. If the original subtree did contain time stamps with a repeater,
  7804. the following will happen:
  7805. - the repeater will be removed in each clone
  7806. - an additional clone will be produced, with the current, unshifted
  7807. date(s) in the entry.
  7808. - the original entry will be placed *after* all the clones, with
  7809. repeater intact.
  7810. - the start days in the repeater in the original entry will be shifted
  7811. to past the last clone.
  7812. In this way you can spell out a number of instances of a repeating task,
  7813. and still retain the repeater to cover future instances of the task."
  7814. (interactive "nNumber of clones to produce: ")
  7815. (let ((shift
  7816. (or shift
  7817. (if (and (not (equal current-prefix-arg '(4)))
  7818. (save-excursion
  7819. (re-search-forward org-ts-regexp-both
  7820. (save-excursion
  7821. (org-end-of-subtree t)
  7822. (point)) t)))
  7823. (read-from-minibuffer
  7824. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7825. ""))) ;; No time shift
  7826. (n-no-remove -1)
  7827. (drawer-re org-drawer-regexp)
  7828. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7829. beg end template task idprop
  7830. shift-n shift-what doshift nmin nmax)
  7831. (if (not (and (integerp n) (> n 0)))
  7832. (user-error "Invalid number of replications %s" n))
  7833. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7834. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7835. shift)))
  7836. (user-error "Invalid shift specification %s" shift))
  7837. (when doshift
  7838. (setq shift-n (string-to-number (match-string 1 shift))
  7839. shift-what (cdr (assoc (match-string 2 shift)
  7840. '(("d" . day) ("w" . week)
  7841. ("m" . month) ("y" . year))))))
  7842. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7843. (setq nmin 1 nmax n)
  7844. (org-back-to-heading t)
  7845. (setq beg (point))
  7846. (setq idprop (org-entry-get nil "ID"))
  7847. (org-end-of-subtree t t)
  7848. (or (bolp) (insert "\n"))
  7849. (setq end (point))
  7850. (setq template (buffer-substring beg end))
  7851. (when (and doshift
  7852. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7853. (delete-region beg end)
  7854. (setq end beg)
  7855. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7856. (goto-char end)
  7857. (loop for n from nmin to nmax do
  7858. ;; prepare clone
  7859. (with-temp-buffer
  7860. (insert template)
  7861. (org-mode)
  7862. (goto-char (point-min))
  7863. (org-show-subtree)
  7864. (and idprop (if org-clone-delete-id
  7865. (org-entry-delete nil "ID")
  7866. (org-id-get-create t)))
  7867. (unless (= n 0)
  7868. (while (re-search-forward org-clock-re nil t)
  7869. (kill-whole-line))
  7870. (goto-char (point-min))
  7871. (while (re-search-forward drawer-re nil t)
  7872. (org-remove-empty-drawer-at (point))))
  7873. (goto-char (point-min))
  7874. (when doshift
  7875. (while (re-search-forward org-ts-regexp-both nil t)
  7876. (org-timestamp-change (* n shift-n) shift-what))
  7877. (unless (= n n-no-remove)
  7878. (goto-char (point-min))
  7879. (while (re-search-forward org-ts-regexp nil t)
  7880. (save-excursion
  7881. (goto-char (match-beginning 0))
  7882. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7883. (delete-region (match-beginning 1) (match-end 1)))))))
  7884. (setq task (buffer-string)))
  7885. (insert task))
  7886. (goto-char beg)))
  7887. ;;; Outline Sorting
  7888. (defun org-sort (with-case)
  7889. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7890. Optional argument WITH-CASE means sort case-sensitively."
  7891. (interactive "P")
  7892. (cond
  7893. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7894. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7895. (t
  7896. (org-call-with-arg 'org-sort-entries with-case))))
  7897. (defun org-sort-remove-invisible (s)
  7898. "Remove invisible links from string S."
  7899. (remove-text-properties 0 (length s) org-rm-props s)
  7900. (while (string-match org-bracket-link-regexp s)
  7901. (setq s (replace-match (if (match-end 2)
  7902. (match-string 3 s)
  7903. (match-string 1 s)) t t s)))
  7904. (let ((st (format " %s " s)))
  7905. (while (string-match org-emph-re st)
  7906. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7907. (setq s (substring st 1 -1)))
  7908. s)
  7909. (defvar org-priority-regexp) ; defined later in the file
  7910. (defvar org-after-sorting-entries-or-items-hook nil
  7911. "Hook that is run after a bunch of entries or items have been sorted.
  7912. When children are sorted, the cursor is in the parent line when this
  7913. hook gets called. When a region or a plain list is sorted, the cursor
  7914. will be in the first entry of the sorted region/list.")
  7915. (defun org-sort-entries
  7916. (&optional with-case sorting-type getkey-func compare-func property)
  7917. "Sort entries on a certain level of an outline tree.
  7918. If there is an active region, the entries in the region are sorted.
  7919. Else, if the cursor is before the first entry, sort the top-level items.
  7920. Else, the children of the entry at point are sorted.
  7921. Sorting can be alphabetically, numerically, by date/time as given by
  7922. a time stamp, by a property, by priority order, or by a custom function.
  7923. The command prompts for the sorting type unless it has been given to the
  7924. function through the SORTING-TYPE argument, which needs to be a character,
  7925. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7926. the precise meaning of each character:
  7927. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7928. c By creation time, which is assumed to be the first inactive time stamp
  7929. at the beginning of a line.
  7930. d By deadline date/time.
  7931. k By clocking time.
  7932. n Numerically, by converting the beginning of the entry/item to a number.
  7933. o By order of TODO keywords.
  7934. p By priority according to the cookie.
  7935. r By the value of a property.
  7936. s By scheduled date/time.
  7937. t By date/time, either the first active time stamp in the entry, or, if
  7938. none exist, by the first inactive one.
  7939. Capital letters will reverse the sort order.
  7940. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7941. called with point at the beginning of the record. It must return either
  7942. a string or a number that should serve as the sorting key for that record.
  7943. Comparing entries ignores case by default. However, with an optional argument
  7944. WITH-CASE, the sorting considers case as well.
  7945. Sorting is done against the visible part of the headlines, it ignores hidden
  7946. links.
  7947. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7948. (interactive "P")
  7949. (let ((case-func (if with-case 'identity 'downcase))
  7950. (cmstr
  7951. ;; The clock marker is lost when using `sort-subr', let's
  7952. ;; store the clocking string.
  7953. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7954. (save-excursion
  7955. (goto-char org-clock-marker)
  7956. (looking-back "^.*") (match-string-no-properties 0))))
  7957. start beg end stars re re2
  7958. txt what tmp)
  7959. ;; Find beginning and end of region to sort
  7960. (cond
  7961. ((org-region-active-p)
  7962. ;; we will sort the region
  7963. (setq end (region-end)
  7964. what "region")
  7965. (goto-char (region-beginning))
  7966. (if (not (org-at-heading-p)) (outline-next-heading))
  7967. (setq start (point)))
  7968. ((or (org-at-heading-p)
  7969. (ignore-errors (progn (org-back-to-heading) t)))
  7970. ;; we will sort the children of the current headline
  7971. (org-back-to-heading)
  7972. (setq start (point)
  7973. end (progn (org-end-of-subtree t t)
  7974. (or (bolp) (insert "\n"))
  7975. (when (>= (org-back-over-empty-lines) 1)
  7976. (forward-line 1))
  7977. (point))
  7978. what "children")
  7979. (goto-char start)
  7980. (show-subtree)
  7981. (outline-next-heading))
  7982. (t
  7983. ;; we will sort the top-level entries in this file
  7984. (goto-char (point-min))
  7985. (or (org-at-heading-p) (outline-next-heading))
  7986. (setq start (point))
  7987. (goto-char (point-max))
  7988. (beginning-of-line 1)
  7989. (when (looking-at ".*?\\S-")
  7990. ;; File ends in a non-white line
  7991. (end-of-line 1)
  7992. (insert "\n"))
  7993. (setq end (point-max))
  7994. (setq what "top-level")
  7995. (goto-char start)
  7996. (show-all)))
  7997. (setq beg (point))
  7998. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7999. (looking-at "\\(\\*+\\)")
  8000. (setq stars (match-string 1)
  8001. re (concat "^" (regexp-quote stars) " +")
  8002. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8003. txt (buffer-substring beg end))
  8004. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8005. (if (and (not (equal stars "*")) (string-match re2 txt))
  8006. (user-error "Region to sort contains a level above the first entry"))
  8007. (unless sorting-type
  8008. (message
  8009. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8010. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8011. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8012. what)
  8013. (setq sorting-type (read-char-exclusive))
  8014. (unless getkey-func
  8015. (and (= (downcase sorting-type) ?f)
  8016. (setq getkey-func
  8017. (org-icompleting-read "Sort using function: "
  8018. obarray 'fboundp t nil nil))
  8019. (setq getkey-func (intern getkey-func))))
  8020. (and (= (downcase sorting-type) ?r)
  8021. (not property)
  8022. (setq property
  8023. (org-icompleting-read "Property: "
  8024. (mapcar 'list (org-buffer-property-keys t))
  8025. nil t))))
  8026. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8027. (message "Sorting entries...")
  8028. (save-restriction
  8029. (narrow-to-region start end)
  8030. (let ((dcst (downcase sorting-type))
  8031. (case-fold-search nil)
  8032. (now (current-time)))
  8033. (sort-subr
  8034. (/= dcst sorting-type)
  8035. ;; This function moves to the beginning character of the "record" to
  8036. ;; be sorted.
  8037. (lambda nil
  8038. (if (re-search-forward re nil t)
  8039. (goto-char (match-beginning 0))
  8040. (goto-char (point-max))))
  8041. ;; This function moves to the last character of the "record" being
  8042. ;; sorted.
  8043. (lambda nil
  8044. (save-match-data
  8045. (condition-case nil
  8046. (outline-forward-same-level 1)
  8047. (error
  8048. (goto-char (point-max))))))
  8049. ;; This function returns the value that gets sorted against.
  8050. (lambda nil
  8051. (cond
  8052. ((= dcst ?n)
  8053. (if (looking-at org-complex-heading-regexp)
  8054. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8055. nil))
  8056. ((= dcst ?a)
  8057. (if (looking-at org-complex-heading-regexp)
  8058. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8059. nil))
  8060. ((= dcst ?k)
  8061. (or (get-text-property (point) :org-clock-minutes) 0))
  8062. ((= dcst ?t)
  8063. (let ((end (save-excursion (outline-next-heading) (point))))
  8064. (if (or (re-search-forward org-ts-regexp end t)
  8065. (re-search-forward org-ts-regexp-both end t))
  8066. (org-time-string-to-seconds (match-string 0))
  8067. (org-float-time now))))
  8068. ((= dcst ?c)
  8069. (let ((end (save-excursion (outline-next-heading) (point))))
  8070. (if (re-search-forward
  8071. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8072. end t)
  8073. (org-time-string-to-seconds (match-string 0))
  8074. (org-float-time now))))
  8075. ((= dcst ?s)
  8076. (let ((end (save-excursion (outline-next-heading) (point))))
  8077. (if (re-search-forward org-scheduled-time-regexp end t)
  8078. (org-time-string-to-seconds (match-string 1))
  8079. (org-float-time now))))
  8080. ((= dcst ?d)
  8081. (let ((end (save-excursion (outline-next-heading) (point))))
  8082. (if (re-search-forward org-deadline-time-regexp end t)
  8083. (org-time-string-to-seconds (match-string 1))
  8084. (org-float-time now))))
  8085. ((= dcst ?p)
  8086. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8087. (string-to-char (match-string 2))
  8088. org-default-priority))
  8089. ((= dcst ?r)
  8090. (or (org-entry-get nil property) ""))
  8091. ((= dcst ?o)
  8092. (if (looking-at org-complex-heading-regexp)
  8093. (let* ((m (match-string 2))
  8094. (s (if (member m org-done-keywords) '- '+)))
  8095. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8096. ((= dcst ?f)
  8097. (if getkey-func
  8098. (progn
  8099. (setq tmp (funcall getkey-func))
  8100. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8101. tmp)
  8102. (error "Invalid key function `%s'" getkey-func)))
  8103. (t (error "Invalid sorting type `%c'" sorting-type))))
  8104. nil
  8105. (cond
  8106. ((= dcst ?a) 'string<)
  8107. ((= dcst ?f) compare-func)
  8108. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8109. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8110. ;; Reset the clock marker if needed
  8111. (when cmstr
  8112. (save-excursion
  8113. (goto-char start)
  8114. (search-forward cmstr nil t)
  8115. (move-marker org-clock-marker (point))))
  8116. (message "Sorting entries...done")))
  8117. ;;; The orgstruct minor mode
  8118. ;; Define a minor mode which can be used in other modes in order to
  8119. ;; integrate the org-mode structure editing commands.
  8120. ;; This is really a hack, because the org-mode structure commands use
  8121. ;; keys which normally belong to the major mode. Here is how it
  8122. ;; works: The minor mode defines all the keys necessary to operate the
  8123. ;; structure commands, but wraps the commands into a function which
  8124. ;; tests if the cursor is currently at a headline or a plain list
  8125. ;; item. If that is the case, the structure command is used,
  8126. ;; temporarily setting many Org-mode variables like regular
  8127. ;; expressions for filling etc. However, when any of those keys is
  8128. ;; used at a different location, function uses `key-binding' to look
  8129. ;; up if the key has an associated command in another currently active
  8130. ;; keymap (minor modes, major mode, global), and executes that
  8131. ;; command. There might be problems if any of the keys is otherwise
  8132. ;; used as a prefix key.
  8133. (defcustom orgstruct-heading-prefix-regexp ""
  8134. "Regexp that matches the custom prefix of Org headlines in
  8135. orgstruct(++)-mode."
  8136. :group 'org
  8137. :version "24.4"
  8138. :package-version '(Org . "8.3")
  8139. :type 'regexp)
  8140. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8141. (defcustom orgstruct-setup-hook nil
  8142. "Hook run after orgstruct-mode-map is filled."
  8143. :group 'org
  8144. :version "24.4"
  8145. :package-version '(Org . "8.0")
  8146. :type 'hook)
  8147. (defvar orgstruct-initialized nil)
  8148. (defvar org-local-vars nil
  8149. "List of local variables, for use by `orgstruct-mode'.")
  8150. ;;;###autoload
  8151. (define-minor-mode orgstruct-mode
  8152. "Toggle the minor mode `orgstruct-mode'.
  8153. This mode is for using Org-mode structure commands in other
  8154. modes. The following keys behave as if Org-mode were active, if
  8155. the cursor is on a headline, or on a plain list item (both as
  8156. defined by Org-mode)."
  8157. nil " OrgStruct" (make-sparse-keymap)
  8158. (funcall (if orgstruct-mode
  8159. 'add-to-invisibility-spec
  8160. 'remove-from-invisibility-spec)
  8161. '(outline . t))
  8162. (when orgstruct-mode
  8163. (org-load-modules-maybe)
  8164. (unless orgstruct-initialized
  8165. (orgstruct-setup)
  8166. (setq orgstruct-initialized t))))
  8167. ;;;###autoload
  8168. (defun turn-on-orgstruct ()
  8169. "Unconditionally turn on `orgstruct-mode'."
  8170. (orgstruct-mode 1))
  8171. (defvar org-fb-vars nil)
  8172. (make-variable-buffer-local 'org-fb-vars)
  8173. (defun orgstruct++-mode (&optional arg)
  8174. "Toggle `orgstruct-mode', the enhanced version of it.
  8175. In addition to setting orgstruct-mode, this also exports all
  8176. indentation and autofilling variables from org-mode into the
  8177. buffer. It will also recognize item context in multiline items."
  8178. (interactive "P")
  8179. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8180. (if (< arg 1)
  8181. (progn (orgstruct-mode -1)
  8182. (mapc (lambda(v)
  8183. (org-set-local (car v)
  8184. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8185. org-fb-vars))
  8186. (orgstruct-mode 1)
  8187. (setq org-fb-vars nil)
  8188. (unless org-local-vars
  8189. (setq org-local-vars (org-get-local-variables)))
  8190. (let (var val)
  8191. (mapc
  8192. (lambda (x)
  8193. (when (string-match
  8194. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8195. (symbol-name (car x)))
  8196. (setq var (car x) val (nth 1 x))
  8197. (push (list var `(quote ,(eval var))) org-fb-vars)
  8198. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8199. org-local-vars)
  8200. (org-set-local 'orgstruct-is-++ t))))
  8201. (defvar orgstruct-is-++ nil
  8202. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8203. (make-variable-buffer-local 'orgstruct-is-++)
  8204. ;;;###autoload
  8205. (defun turn-on-orgstruct++ ()
  8206. "Unconditionally turn on `orgstruct++-mode'."
  8207. (orgstruct++-mode 1))
  8208. (defun orgstruct-error ()
  8209. "Error when there is no default binding for a structure key."
  8210. (interactive)
  8211. (funcall (if (fboundp 'user-error)
  8212. 'user-error
  8213. 'error)
  8214. "This key has no function outside structure elements"))
  8215. (defun orgstruct-setup ()
  8216. "Setup orgstruct keymap."
  8217. (dolist (cell '((org-demote . t)
  8218. (org-metaleft . t)
  8219. (org-metaright . t)
  8220. (org-promote . t)
  8221. (org-shiftmetaleft . t)
  8222. (org-shiftmetaright . t)
  8223. org-backward-element
  8224. org-backward-heading-same-level
  8225. org-ctrl-c-ret
  8226. org-ctrl-c-minus
  8227. org-ctrl-c-star
  8228. org-cycle
  8229. org-forward-heading-same-level
  8230. org-insert-heading
  8231. org-insert-heading-respect-content
  8232. org-kill-note-or-show-branches
  8233. org-mark-subtree
  8234. org-meta-return
  8235. org-metadown
  8236. org-metaup
  8237. org-narrow-to-subtree
  8238. org-promote-subtree
  8239. org-reveal
  8240. org-shiftdown
  8241. org-shiftleft
  8242. org-shiftmetadown
  8243. org-shiftmetaup
  8244. org-shiftright
  8245. org-shifttab
  8246. org-shifttab
  8247. org-shiftup
  8248. org-show-subtree
  8249. org-sort
  8250. org-up-element
  8251. outline-demote
  8252. outline-next-visible-heading
  8253. outline-previous-visible-heading
  8254. outline-promote
  8255. outline-up-heading
  8256. show-children))
  8257. (let ((f (or (car-safe cell) cell))
  8258. (disable-when-heading-prefix (cdr-safe cell)))
  8259. (when (fboundp f)
  8260. (let ((new-bindings))
  8261. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8262. (where-is-internal f outline-mode-map)))
  8263. (push binding new-bindings)
  8264. ;; TODO use local-function-key-map
  8265. (dolist (rep '(("<tab>" . "TAB")
  8266. ("<return>" . "RET")
  8267. ("<escape>" . "ESC")
  8268. ("<delete>" . "DEL")))
  8269. (setq binding (read-kbd-macro
  8270. (let ((case-fold-search))
  8271. (replace-regexp-in-string
  8272. (regexp-quote (cdr rep))
  8273. (car rep)
  8274. (key-description binding)))))
  8275. (pushnew binding new-bindings :test 'equal)))
  8276. (dolist (binding new-bindings)
  8277. (let ((key (lookup-key orgstruct-mode-map binding)))
  8278. (when (or (not key) (numberp key))
  8279. (ignore-errors
  8280. (org-defkey orgstruct-mode-map
  8281. binding
  8282. (orgstruct-make-binding
  8283. f binding disable-when-heading-prefix))))))))))
  8284. (run-hooks 'orgstruct-setup-hook))
  8285. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8286. "Create a function for binding in the structure minor mode.
  8287. FUN is the command to call inside a table. KEY is the key that
  8288. should be checked in for a command to execute outside of tables.
  8289. Non-nil `disable-when-heading-prefix' means to disable the command
  8290. if `orgstruct-heading-prefix-regexp' is not empty."
  8291. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8292. (let ((nname name)
  8293. (i 0))
  8294. (while (fboundp (intern nname))
  8295. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8296. (setq name (intern nname)))
  8297. (eval
  8298. (let ((bindings '((org-heading-regexp
  8299. (concat "^"
  8300. orgstruct-heading-prefix-regexp
  8301. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8302. (org-outline-regexp
  8303. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8304. (org-outline-regexp-bol
  8305. (concat "^" org-outline-regexp))
  8306. (outline-regexp org-outline-regexp)
  8307. (outline-heading-end-regexp "\n")
  8308. (outline-level 'org-outline-level)
  8309. (outline-heading-alist))))
  8310. `(defun ,name (arg)
  8311. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8312. "Outside of structure, run the binding of `"
  8313. (key-description key) "'."
  8314. (when disable-when-heading-prefix
  8315. (concat
  8316. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8317. "back to the default binding due to limitations of Org's implementation of\n"
  8318. "`" (symbol-name fun) "'.")))
  8319. (interactive "p")
  8320. (let* ((disable
  8321. ,(and disable-when-heading-prefix
  8322. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8323. (fallback
  8324. (or disable
  8325. (not
  8326. (let* ,bindings
  8327. (org-context-p 'headline 'item
  8328. ,(when (memq fun
  8329. '(org-insert-heading
  8330. org-insert-heading-respect-content
  8331. org-meta-return))
  8332. '(when orgstruct-is-++
  8333. 'item-body))))))))
  8334. (if fallback
  8335. (let* ((orgstruct-mode)
  8336. (binding
  8337. (let ((key ,key))
  8338. (catch 'exit
  8339. (dolist
  8340. (rep
  8341. '(nil
  8342. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8343. ("<\\([^>]*\\)return>" . "\\1RET")
  8344. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8345. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8346. nil)
  8347. (when rep
  8348. (setq key (read-kbd-macro
  8349. (let ((case-fold-search))
  8350. (replace-regexp-in-string
  8351. (car rep)
  8352. (cdr rep)
  8353. (key-description key))))))
  8354. (when (key-binding key)
  8355. (throw 'exit (key-binding key))))))))
  8356. (if (keymapp binding)
  8357. (org-set-transient-map binding)
  8358. (let ((func (or binding
  8359. (unless disable
  8360. 'orgstruct-error))))
  8361. (when func
  8362. (call-interactively func)))))
  8363. (org-run-like-in-org-mode
  8364. (lambda ()
  8365. (interactive)
  8366. (let* ,bindings
  8367. (call-interactively ',fun)))))))))
  8368. name))
  8369. (defun org-contextualize-keys (alist contexts)
  8370. "Return valid elements in ALIST depending on CONTEXTS.
  8371. `org-agenda-custom-commands' or `org-capture-templates' are the
  8372. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8373. or `org-capture-templates-contexts' are the associated contexts
  8374. definitions."
  8375. (let ((contexts
  8376. ;; normalize contexts
  8377. (mapcar
  8378. (lambda(c) (cond ((listp (cadr c))
  8379. (list (car c) (car c) (cadr c)))
  8380. ((string= "" (cadr c))
  8381. (list (car c) (car c) (caddr c)))
  8382. (t c))) contexts))
  8383. (a alist) c r s)
  8384. ;; loop over all commands or templates
  8385. (while (setq c (pop a))
  8386. (let (vrules repl)
  8387. (cond
  8388. ((not (assoc (car c) contexts))
  8389. (push c r))
  8390. ((and (assoc (car c) contexts)
  8391. (setq vrules (org-contextualize-validate-key
  8392. (car c) contexts)))
  8393. (mapc (lambda (vr)
  8394. (when (not (equal (car vr) (cadr vr)))
  8395. (setq repl vr))) vrules)
  8396. (if (not repl) (push c r)
  8397. (push (cadr repl) s)
  8398. (push
  8399. (cons (car c)
  8400. (cdr (or (assoc (cadr repl) alist)
  8401. (error "Undefined key `%s' as contextual replacement for `%s'"
  8402. (cadr repl) (car c)))))
  8403. r))))))
  8404. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8405. (delq
  8406. nil
  8407. (delete-dups
  8408. (mapcar (lambda (x)
  8409. (let ((tpl (car x)))
  8410. (when (not (delq
  8411. nil
  8412. (mapcar (lambda(y)
  8413. (equal y tpl)) s))) x)))
  8414. (reverse r))))))
  8415. (defun org-contextualize-validate-key (key contexts)
  8416. "Check CONTEXTS for agenda or capture KEY."
  8417. (let (r rr res)
  8418. (while (setq r (pop contexts))
  8419. (mapc
  8420. (lambda (rr)
  8421. (when
  8422. (and (equal key (car r))
  8423. (if (functionp rr) (funcall rr)
  8424. (or (and (eq (car rr) 'in-file)
  8425. (buffer-file-name)
  8426. (string-match (cdr rr) (buffer-file-name)))
  8427. (and (eq (car rr) 'in-mode)
  8428. (string-match (cdr rr) (symbol-name major-mode)))
  8429. (and (eq (car rr) 'in-buffer)
  8430. (string-match (cdr rr) (buffer-name)))
  8431. (when (and (eq (car rr) 'not-in-file)
  8432. (buffer-file-name))
  8433. (not (string-match (cdr rr) (buffer-file-name))))
  8434. (when (eq (car rr) 'not-in-mode)
  8435. (not (string-match (cdr rr) (symbol-name major-mode))))
  8436. (when (eq (car rr) 'not-in-buffer)
  8437. (not (string-match (cdr rr) (buffer-name)))))))
  8438. (push r res)))
  8439. (car (last r))))
  8440. (delete-dups (delq nil res))))
  8441. (defun org-context-p (&rest contexts)
  8442. "Check if local context is any of CONTEXTS.
  8443. Possible values in the list of contexts are `table', `headline', and `item'."
  8444. (let ((pos (point)))
  8445. (goto-char (point-at-bol))
  8446. (prog1 (or (and (memq 'table contexts)
  8447. (looking-at "[ \t]*|"))
  8448. (and (memq 'headline contexts)
  8449. (looking-at org-outline-regexp))
  8450. (and (memq 'item contexts)
  8451. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8452. (and (memq 'item-body contexts)
  8453. (org-in-item-p)))
  8454. (goto-char pos))))
  8455. (defun org-get-local-variables ()
  8456. "Return a list of all local variables in an Org mode buffer."
  8457. (let (varlist)
  8458. (with-current-buffer (get-buffer-create "*Org tmp*")
  8459. (erase-buffer)
  8460. (org-mode)
  8461. (setq varlist (buffer-local-variables)))
  8462. (kill-buffer "*Org tmp*")
  8463. (delq nil
  8464. (mapcar
  8465. (lambda (x)
  8466. (setq x
  8467. (if (symbolp x)
  8468. (list x)
  8469. (list (car x) (cdr x))))
  8470. (if (and (not (get (car x) 'org-state))
  8471. (string-match
  8472. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8473. (symbol-name (car x))))
  8474. x nil))
  8475. varlist))))
  8476. (defun org-clone-local-variables (from-buffer &optional regexp)
  8477. "Clone local variables from FROM-BUFFER.
  8478. Optional argument REGEXP selects variables to clone."
  8479. (mapc
  8480. (lambda (pair)
  8481. (and (symbolp (car pair))
  8482. (or (null regexp)
  8483. (string-match regexp (symbol-name (car pair))))
  8484. (set (make-local-variable (car pair))
  8485. (cdr pair))))
  8486. (buffer-local-variables from-buffer)))
  8487. ;;;###autoload
  8488. (defun org-run-like-in-org-mode (cmd)
  8489. "Run a command, pretending that the current buffer is in Org-mode.
  8490. This will temporarily bind local variables that are typically bound in
  8491. Org-mode to the values they have in Org-mode, and then interactively
  8492. call CMD."
  8493. (org-load-modules-maybe)
  8494. (unless org-local-vars
  8495. (setq org-local-vars (org-get-local-variables)))
  8496. (let (binds)
  8497. (dolist (var org-local-vars)
  8498. (when (or (not (boundp (car var)))
  8499. (eq (symbol-value (car var))
  8500. (default-value (car var))))
  8501. (push (list (car var) `(quote ,(cadr var))) binds)))
  8502. (eval `(let ,binds
  8503. (call-interactively (quote ,cmd))))))
  8504. (defun org-get-category (&optional pos force-refresh)
  8505. "Get the category applying to position POS."
  8506. (save-match-data
  8507. (if force-refresh (org-refresh-category-properties))
  8508. (let ((pos (or pos (point))))
  8509. (or (get-text-property pos 'org-category)
  8510. (progn (org-refresh-category-properties)
  8511. (get-text-property pos 'org-category))))))
  8512. ;;; Refresh properties
  8513. (defun org-refresh-properties (dprop tprop)
  8514. "Refresh buffer text properties.
  8515. DPROP is the drawer property and TPROP is either the
  8516. corresponding text property to set, or an alist with each element
  8517. being a text property (as a symbol) and a function to apply to
  8518. the value of the drawer property."
  8519. (let ((case-fold-search t)
  8520. (inhibit-read-only t))
  8521. (org-with-silent-modifications
  8522. (save-excursion
  8523. (save-restriction
  8524. (widen)
  8525. (goto-char (point-min))
  8526. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8527. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8528. (defun org-refresh-property (tprop p)
  8529. "Refresh the buffer text property TPROP from the drawer property P.
  8530. The refresh happens only for the current tree (not subtree)."
  8531. (save-excursion
  8532. (org-back-to-heading t)
  8533. ;; tprop is a text property symbol
  8534. (if (symbolp tprop)
  8535. (put-text-property
  8536. (point) (or (outline-next-heading) (point-max)) tprop p)
  8537. ;; tprop is an alist with (properties . function) elements
  8538. (mapc (lambda(al)
  8539. (save-excursion
  8540. (put-text-property
  8541. (point-at-bol) (or (outline-next-heading) (point-max))
  8542. (car al)
  8543. (funcall (cdr al) p))))
  8544. tprop))))
  8545. (defun org-refresh-category-properties ()
  8546. "Refresh category text properties in the buffer."
  8547. (let ((case-fold-search t)
  8548. (inhibit-read-only t)
  8549. (def-cat (cond
  8550. ((null org-category)
  8551. (if buffer-file-name
  8552. (file-name-sans-extension
  8553. (file-name-nondirectory buffer-file-name))
  8554. "???"))
  8555. ((symbolp org-category) (symbol-name org-category))
  8556. (t org-category)))
  8557. beg end cat pos optionp)
  8558. (org-with-silent-modifications
  8559. (save-excursion
  8560. (save-restriction
  8561. (widen)
  8562. (goto-char (point-min))
  8563. (put-text-property (point) (point-max) 'org-category def-cat)
  8564. (while (re-search-forward
  8565. "^[ \t]*\\(\\(?:#\\+\\|:\\)CATEGORY:\\)\\(.*\\)" nil t)
  8566. (setq pos (match-end 0)
  8567. optionp (equal (char-after (match-beginning 0)) ?#)
  8568. cat (org-trim (match-string 2)))
  8569. (if optionp
  8570. (setq beg (point-at-bol) end (point-max))
  8571. (org-back-to-heading t)
  8572. (setq beg (point) end (org-end-of-subtree t t)))
  8573. (put-text-property beg end 'org-category cat)
  8574. (goto-char pos)))))))
  8575. (defun org-refresh-stats-properties ()
  8576. "Refresh stats text properties in the buffer."
  8577. (let (stats)
  8578. (org-with-silent-modifications
  8579. (save-excursion
  8580. (save-restriction
  8581. (widen)
  8582. (goto-char (point-min))
  8583. (while (re-search-forward
  8584. (concat org-outline-regexp-bol ".*"
  8585. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8586. nil t)
  8587. (setq stats (cond ((equal (match-string 3) "0") 0)
  8588. ((match-string 2)
  8589. (/ (* (string-to-number (match-string 2)) 100)
  8590. (string-to-number (match-string 3))))
  8591. (t (string-to-number (match-string 1)))))
  8592. (org-back-to-heading t)
  8593. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8594. 'org-stats stats)))))))
  8595. (defun org-refresh-effort-properties ()
  8596. "Refresh effort properties"
  8597. (org-refresh-properties
  8598. org-effort-property
  8599. '((effort . identity)
  8600. (effort-minutes . org-duration-string-to-minutes))))
  8601. ;;;; Link Stuff
  8602. ;;; Link abbreviations
  8603. (defun org-link-expand-abbrev (link)
  8604. "Apply replacements as defined in `org-link-abbrev-alist'."
  8605. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8606. (let* ((key (match-string 1 link))
  8607. (as (or (assoc key org-link-abbrev-alist-local)
  8608. (assoc key org-link-abbrev-alist)))
  8609. (tag (and (match-end 2) (match-string 3 link)))
  8610. rpl)
  8611. (if (not as)
  8612. link
  8613. (setq rpl (cdr as))
  8614. (cond
  8615. ((symbolp rpl) (funcall rpl tag))
  8616. ((string-match "%(\\([^)]+\\))" rpl)
  8617. (replace-match
  8618. (save-match-data
  8619. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8620. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8621. ((string-match "%h" rpl)
  8622. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8623. (t (concat rpl tag)))))
  8624. link))
  8625. ;;; Storing and inserting links
  8626. (defvar org-insert-link-history nil
  8627. "Minibuffer history for links inserted with `org-insert-link'.")
  8628. (defvar org-stored-links nil
  8629. "Contains the links stored with `org-store-link'.")
  8630. (defvar org-store-link-plist nil
  8631. "Plist with info about the most recently link created with `org-store-link'.")
  8632. (defvar org-link-protocols nil
  8633. "Link protocols added to Org-mode using `org-add-link-type'.")
  8634. (defvar org-store-link-functions nil
  8635. "List of functions that are called to create and store a link.
  8636. Each function will be called in turn until one returns a non-nil
  8637. value. Each function should check if it is responsible for creating
  8638. this link (for example by looking at the major mode).
  8639. If not, it must exit and return nil.
  8640. If yes, it should return a non-nil value after a calling
  8641. `org-store-link-props' with a list of properties and values.
  8642. Special properties are:
  8643. :type The link prefix, like \"http\". This must be given.
  8644. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8645. This is obligatory as well.
  8646. :description Optional default description for the second pair
  8647. of brackets in an Org-mode link. The user can still change
  8648. this when inserting this link into an Org-mode buffer.
  8649. In addition to these, any additional properties can be specified
  8650. and then used in capture templates.")
  8651. (defun org-add-link-type (type &optional follow export)
  8652. "Add TYPE to the list of `org-link-types'.
  8653. Re-compute all regular expressions depending on `org-link-types'
  8654. FOLLOW and EXPORT are two functions.
  8655. FOLLOW should take the link path as the single argument and do whatever
  8656. is necessary to follow the link, for example find a file or display
  8657. a mail message.
  8658. EXPORT should format the link path for export to one of the export formats.
  8659. It should be a function accepting three arguments:
  8660. path the path of the link, the text after the prefix (like \"http:\")
  8661. desc the description of the link, if any
  8662. format the export format, a symbol like `html' or `latex' or `ascii'.
  8663. The function may use the FORMAT information to return different values
  8664. depending on the format. The return value will be put literally into
  8665. the exported file. If the return value is nil, this means Org should
  8666. do what it normally does with links which do not have EXPORT defined.
  8667. Org mode has a built-in default for exporting links. If you are happy with
  8668. this default, there is no need to define an export function for the link
  8669. type. For a simple example of an export function, see `org-bbdb.el'."
  8670. (add-to-list 'org-link-types type t)
  8671. (org-make-link-regexps)
  8672. (org-element-update-syntax)
  8673. (if (assoc type org-link-protocols)
  8674. (setcdr (assoc type org-link-protocols) (list follow export))
  8675. (push (list type follow export) org-link-protocols)))
  8676. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8677. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8678. ;;;###autoload
  8679. (defun org-store-link (arg)
  8680. "\\<org-mode-map>Store an org-link to the current location.
  8681. This link is added to `org-stored-links' and can later be inserted
  8682. into an org-buffer with \\[org-insert-link].
  8683. For some link types, a prefix arg is interpreted.
  8684. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8685. For file links, arg negates `org-context-in-file-links'.
  8686. A double prefix arg force skipping storing functions that are not
  8687. part of Org's core.
  8688. A triple prefix arg force storing a link for each line in the
  8689. active region."
  8690. (interactive "P")
  8691. (org-load-modules-maybe)
  8692. (if (and (equal arg '(64)) (org-region-active-p))
  8693. (save-excursion
  8694. (let ((end (region-end)))
  8695. (goto-char (region-beginning))
  8696. (set-mark (point))
  8697. (while (< (point-at-eol) end)
  8698. (move-end-of-line 1) (activate-mark)
  8699. (let (current-prefix-arg)
  8700. (call-interactively 'org-store-link))
  8701. (move-beginning-of-line 2)
  8702. (set-mark (point)))))
  8703. (org-with-limited-levels
  8704. (setq org-store-link-plist nil)
  8705. (let (link cpltxt desc description search
  8706. txt custom-id agenda-link sfuns sfunsn)
  8707. (cond
  8708. ;; Store a link using an external link type
  8709. ((and (not (equal arg '(16)))
  8710. (setq sfuns
  8711. (delq
  8712. nil (mapcar (lambda (f)
  8713. (let (fs) (if (funcall f) (push f fs))))
  8714. org-store-link-functions))
  8715. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8716. (or (and (cdr sfuns)
  8717. (funcall (intern
  8718. (completing-read
  8719. "Which function for creating the link? "
  8720. sfunsn nil t (car sfunsn)))))
  8721. (funcall (caar sfuns)))
  8722. (setq link (plist-get org-store-link-plist :link)
  8723. desc (or (plist-get org-store-link-plist
  8724. :description) link))))
  8725. ;; Store a link from a source code buffer
  8726. ((org-src-edit-buffer-p)
  8727. (let (label gc)
  8728. (while (or (not label)
  8729. (save-excursion
  8730. (save-restriction
  8731. (widen)
  8732. (goto-char (point-min))
  8733. (re-search-forward
  8734. (regexp-quote (format org-coderef-label-format label))
  8735. nil t))))
  8736. (when label (message "Label exists already") (sit-for 2))
  8737. (setq label (read-string "Code line label: " label)))
  8738. (end-of-line 1)
  8739. (setq link (format org-coderef-label-format label))
  8740. (setq gc (- 79 (length link)))
  8741. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8742. (insert link)
  8743. (setq link (concat "(" label ")") desc nil)))
  8744. ;; We are in the agenda, link to referenced location
  8745. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8746. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8747. (get-text-property (point) 'org-marker))))
  8748. (when m
  8749. (org-with-point-at m
  8750. (setq agenda-link
  8751. (if (org-called-interactively-p 'any)
  8752. (call-interactively 'org-store-link)
  8753. (org-store-link nil)))))))
  8754. ((eq major-mode 'calendar-mode)
  8755. (let ((cd (calendar-cursor-to-date)))
  8756. (setq link
  8757. (format-time-string
  8758. (car org-time-stamp-formats)
  8759. (apply 'encode-time
  8760. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8761. nil nil nil))))
  8762. (org-store-link-props :type "calendar" :date cd)))
  8763. ((eq major-mode 'help-mode)
  8764. (setq link (concat "help:" (save-excursion
  8765. (goto-char (point-min))
  8766. (looking-at "^[^ ]+")
  8767. (match-string 0))))
  8768. (org-store-link-props :type "help"))
  8769. ((eq major-mode 'w3-mode)
  8770. (setq cpltxt (if (and (buffer-name)
  8771. (not (string-match "Untitled" (buffer-name))))
  8772. (buffer-name)
  8773. (url-view-url t))
  8774. link (url-view-url t))
  8775. (org-store-link-props :type "w3" :url (url-view-url t)))
  8776. ((eq major-mode 'image-mode)
  8777. (setq cpltxt (concat "file:"
  8778. (abbreviate-file-name buffer-file-name))
  8779. link cpltxt)
  8780. (org-store-link-props :type "image" :file buffer-file-name))
  8781. ;; In dired, store a link to the file of the current line
  8782. ((derived-mode-p 'dired-mode)
  8783. (let ((file (dired-get-filename nil t)))
  8784. (setq file (if file
  8785. (abbreviate-file-name
  8786. (expand-file-name (dired-get-filename nil t)))
  8787. ;; otherwise, no file so use current directory.
  8788. default-directory))
  8789. (setq cpltxt (concat "file:" file)
  8790. link cpltxt)))
  8791. ((setq search (run-hook-with-args-until-success
  8792. 'org-create-file-search-functions))
  8793. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8794. "::" search))
  8795. (setq cpltxt (or description link)))
  8796. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8797. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8798. (cond
  8799. ;; Store a link using the target at point
  8800. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8801. (setq cpltxt
  8802. (concat "file:"
  8803. (abbreviate-file-name
  8804. (buffer-file-name (buffer-base-buffer)))
  8805. "::" (match-string 1))
  8806. link cpltxt))
  8807. ((and (featurep 'org-id)
  8808. (or (eq org-id-link-to-org-use-id t)
  8809. (and (org-called-interactively-p 'any)
  8810. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8811. (and (eq org-id-link-to-org-use-id
  8812. 'create-if-interactive-and-no-custom-id)
  8813. (not custom-id))))
  8814. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8815. ;; Store a link using the ID at point
  8816. (setq link (condition-case nil
  8817. (prog1 (org-id-store-link)
  8818. (setq desc (or (plist-get org-store-link-plist
  8819. :description)
  8820. "")))
  8821. (error
  8822. ;; Probably before first headline, link only to file
  8823. (concat "file:"
  8824. (abbreviate-file-name
  8825. (buffer-file-name (buffer-base-buffer))))))))
  8826. (t
  8827. ;; Just link to current headline
  8828. (setq cpltxt (concat "file:"
  8829. (abbreviate-file-name
  8830. (buffer-file-name (buffer-base-buffer)))))
  8831. ;; Add a context search string
  8832. (when (org-xor org-context-in-file-links arg)
  8833. (let* ((ee (org-element-at-point))
  8834. (et (org-element-type ee))
  8835. (ev (plist-get (cadr ee) :value))
  8836. (ek (plist-get (cadr ee) :key))
  8837. (eok (and (stringp ek) (string-match "name" ek))))
  8838. (setq txt (cond
  8839. ((org-at-heading-p) nil)
  8840. ((and (eq et 'keyword) eok) ev)
  8841. ((org-region-active-p)
  8842. (buffer-substring (region-beginning) (region-end)))))
  8843. (when (or (null txt) (string-match "\\S-" txt))
  8844. (setq cpltxt
  8845. (concat cpltxt "::"
  8846. (condition-case nil
  8847. (org-make-org-heading-search-string txt)
  8848. (error "")))
  8849. desc (or (and (eq et 'keyword) eok ev)
  8850. (nth 4 (ignore-errors (org-heading-components)))
  8851. "NONE")))))
  8852. (if (string-match "::\\'" cpltxt)
  8853. (setq cpltxt (substring cpltxt 0 -2)))
  8854. (setq link cpltxt))))
  8855. ((buffer-file-name (buffer-base-buffer))
  8856. ;; Just link to this file here.
  8857. (setq cpltxt (concat "file:"
  8858. (abbreviate-file-name
  8859. (buffer-file-name (buffer-base-buffer)))))
  8860. ;; Add a context string.
  8861. (when (org-xor org-context-in-file-links arg)
  8862. (setq txt (if (org-region-active-p)
  8863. (buffer-substring (region-beginning) (region-end))
  8864. (buffer-substring (point-at-bol) (point-at-eol))))
  8865. ;; Only use search option if there is some text.
  8866. (when (string-match "\\S-" txt)
  8867. (setq cpltxt
  8868. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8869. desc "NONE")))
  8870. (setq link cpltxt))
  8871. ((org-called-interactively-p 'interactive)
  8872. (user-error "No method for storing a link from this buffer"))
  8873. (t (setq link nil)))
  8874. ;; We're done setting link and desc, clean up
  8875. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8876. (setq link (or link cpltxt)
  8877. desc (or desc cpltxt))
  8878. (cond ((equal desc "NONE") (setq desc nil))
  8879. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8880. (let ((d0 (match-string 3 desc))
  8881. (p0 (match-string 5 desc)))
  8882. (setq desc
  8883. (replace-regexp-in-string
  8884. org-bracket-link-regexp
  8885. (concat (or p0 d0)
  8886. (if (equal (length (match-string 0 desc))
  8887. (length desc)) "*" "")) desc)))))
  8888. ;; Return the link
  8889. (if (not (and (or (org-called-interactively-p 'any)
  8890. executing-kbd-macro) link))
  8891. (or agenda-link (and link (org-make-link-string link desc)))
  8892. (push (list link desc) org-stored-links)
  8893. (message "Stored: %s" (or desc link))
  8894. (when custom-id
  8895. (setq link (concat "file:" (abbreviate-file-name
  8896. (buffer-file-name)) "::#" custom-id))
  8897. (push (list link desc) org-stored-links))
  8898. (car org-stored-links))))))
  8899. (defun org-store-link-props (&rest plist)
  8900. "Store link properties, extract names and addresses."
  8901. (let (x adr)
  8902. (when (setq x (plist-get plist :from))
  8903. (setq adr (mail-extract-address-components x))
  8904. (setq plist (plist-put plist :fromname (car adr)))
  8905. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8906. (when (setq x (plist-get plist :to))
  8907. (setq adr (mail-extract-address-components x))
  8908. (setq plist (plist-put plist :toname (car adr)))
  8909. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8910. (let ((from (plist-get plist :from))
  8911. (to (plist-get plist :to)))
  8912. (when (and from to org-from-is-user-regexp)
  8913. (setq plist
  8914. (plist-put plist :fromto
  8915. (if (string-match org-from-is-user-regexp from)
  8916. (concat "to %t")
  8917. (concat "from %f"))))))
  8918. (setq org-store-link-plist plist))
  8919. (defun org-add-link-props (&rest plist)
  8920. "Add these properties to the link property list."
  8921. (let (key value)
  8922. (while plist
  8923. (setq key (pop plist) value (pop plist))
  8924. (setq org-store-link-plist
  8925. (plist-put org-store-link-plist key value)))))
  8926. (defun org-email-link-description (&optional fmt)
  8927. "Return the description part of an email link.
  8928. This takes information from `org-store-link-plist' and formats it
  8929. according to FMT (default from `org-email-link-description-format')."
  8930. (setq fmt (or fmt org-email-link-description-format))
  8931. (let* ((p org-store-link-plist)
  8932. (to (plist-get p :toaddress))
  8933. (from (plist-get p :fromaddress))
  8934. (table
  8935. (list
  8936. (cons "%c" (plist-get p :fromto))
  8937. (cons "%F" (plist-get p :from))
  8938. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8939. (cons "%T" (plist-get p :to))
  8940. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8941. (cons "%s" (plist-get p :subject))
  8942. (cons "%d" (plist-get p :date))
  8943. (cons "%m" (plist-get p :message-id)))))
  8944. (when (string-match "%c" fmt)
  8945. ;; Check if the user wrote this message
  8946. (if (and org-from-is-user-regexp from to
  8947. (save-match-data (string-match org-from-is-user-regexp from)))
  8948. (setq fmt (replace-match "to %t" t t fmt))
  8949. (setq fmt (replace-match "from %f" t t fmt))))
  8950. (org-replace-escapes fmt table)))
  8951. (defun org-make-org-heading-search-string (&optional string)
  8952. "Make search string for the current headline or STRING."
  8953. (let ((s (or string
  8954. (and (derived-mode-p 'org-mode)
  8955. (save-excursion
  8956. (org-back-to-heading t)
  8957. (org-element-property :raw-value (org-element-at-point))))))
  8958. (lines org-context-in-file-links))
  8959. (or string (setq s (concat "*" s))) ; Add * for headlines
  8960. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8961. (when (and string (integerp lines) (> lines 0))
  8962. (let ((slines (org-split-string s "\n")))
  8963. (when (< lines (length slines))
  8964. (setq s (mapconcat
  8965. 'identity
  8966. (reverse (nthcdr (- (length slines) lines)
  8967. (reverse slines))) "\n")))))
  8968. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8969. (defun org-make-link-string (link &optional description)
  8970. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8971. (unless (string-match "\\S-" link)
  8972. (error "Empty link"))
  8973. (when (and description
  8974. (stringp description)
  8975. (not (string-match "\\S-" description)))
  8976. (setq description nil))
  8977. (when (stringp description)
  8978. ;; Remove brackets from the description, they are fatal.
  8979. (while (string-match "\\[" description)
  8980. (setq description (replace-match "{" t t description)))
  8981. (while (string-match "\\]" description)
  8982. (setq description (replace-match "}" t t description))))
  8983. (when (equal link description)
  8984. ;; No description needed, it is identical
  8985. (setq description nil))
  8986. (when (and (not description)
  8987. (not (string-match (org-image-file-name-regexp) link))
  8988. (not (equal link (org-link-escape link))))
  8989. (setq description (org-extract-attributes link)))
  8990. (setq link
  8991. (cond ((string-match (org-image-file-name-regexp) link) link)
  8992. ((string-match org-link-types-re link)
  8993. (concat (match-string 1 link)
  8994. (org-link-escape (substring link (match-end 1)))))
  8995. (t (org-link-escape link))))
  8996. (concat "[[" link "]"
  8997. (if description (concat "[" description "]") "")
  8998. "]"))
  8999. (defconst org-link-escape-chars
  9000. ;;%20 %5B %5D
  9001. '(?\ ?\[ ?\])
  9002. "List of characters that should be escaped in a link when stored to Org.
  9003. This is the list that is used for internal purposes.")
  9004. (defconst org-link-escape-chars-browser
  9005. ;;%20 %22
  9006. '(?\ ?\")
  9007. "List of characters to be escaped before handing over to the browser.
  9008. If you consider using this constant then you probably want to use
  9009. the function `org-link-escape-browser' instead. See there why
  9010. this constant is a candidate to be removed once Org drops support
  9011. for Emacs 24.1 and 24.2.")
  9012. (defun org-link-escape (text &optional table merge)
  9013. "Return percent escaped representation of TEXT.
  9014. TEXT is a string with the text to escape.
  9015. Optional argument TABLE is a list with characters that should be
  9016. escaped. When nil, `org-link-escape-chars' is used.
  9017. If optional argument MERGE is set, merge TABLE into
  9018. `org-link-escape-chars'."
  9019. ;; Don't escape chars in internal links
  9020. (if (string-match "^\\*[[:alnum:]]+" text)
  9021. text
  9022. (cond
  9023. ((and table merge)
  9024. (mapc (lambda (defchr)
  9025. (unless (member defchr table)
  9026. (setq table (cons defchr table))))
  9027. org-link-escape-chars))
  9028. ((null table)
  9029. (setq table org-link-escape-chars)))
  9030. (mapconcat
  9031. (lambda (char)
  9032. (if (or (member char table)
  9033. (and (or (< char 32) (= char ?\%) (> char 126))
  9034. org-url-hexify-p))
  9035. (mapconcat (lambda (sequence-element)
  9036. (format "%%%.2X" sequence-element))
  9037. (or (encode-coding-char char 'utf-8)
  9038. (error "Unable to percent escape character: %s"
  9039. (char-to-string char))) "")
  9040. (char-to-string char))) text "")))
  9041. (defun org-link-escape-browser (text)
  9042. "Escape some characters before handing over to the browser.
  9043. This function is a candidate to be removed together with the
  9044. constant `org-link-escape-chars-browser' once Org drops support
  9045. for Emacs 24.1 and 24.2. All calls to this function will have to
  9046. be replaced with `url-encode-url' which is available since Emacs
  9047. 24.3.1."
  9048. ;; Example with the Org link
  9049. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9050. ;; to open the browser with +subject:"Release 8.2" filled into the
  9051. ;; query field: In this case the variable TEXT contains the
  9052. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9053. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9054. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9055. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9056. (if (fboundp 'url-encode-url)
  9057. (url-encode-url text)
  9058. (if (org-string-match-p
  9059. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9060. text)
  9061. (org-link-escape text org-link-escape-chars-browser)
  9062. text)))
  9063. (defun org-link-unescape (str)
  9064. "Unhex hexified Unicode strings as returned from the JavaScript function
  9065. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9066. (unless (and (null str) (string= "" str))
  9067. (let ((pos 0) (case-fold-search t) unhexed)
  9068. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9069. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9070. (setq str (replace-match unhexed t t str))
  9071. (setq pos (+ pos (length unhexed))))))
  9072. str)
  9073. (defun org-link-unescape-compound (hex)
  9074. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9075. Note: this function also decodes single byte encodings like
  9076. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9077. (save-match-data
  9078. (let* ((bytes (cdr (split-string hex "%")))
  9079. (ret "")
  9080. (eat 0)
  9081. (sum 0))
  9082. (while bytes
  9083. (let* ((val (string-to-number (pop bytes) 16))
  9084. (shift-xor
  9085. (if (= 0 eat)
  9086. (cond
  9087. ((>= val 252) (cons 6 252))
  9088. ((>= val 248) (cons 5 248))
  9089. ((>= val 240) (cons 4 240))
  9090. ((>= val 224) (cons 3 224))
  9091. ((>= val 192) (cons 2 192))
  9092. (t (cons 0 0)))
  9093. (cons 6 128))))
  9094. (if (>= val 192) (setq eat (car shift-xor)))
  9095. (setq val (logxor val (cdr shift-xor)))
  9096. (setq sum (+ (lsh sum (car shift-xor)) val))
  9097. (if (> eat 0) (setq eat (- eat 1)))
  9098. (cond
  9099. ((= 0 eat) ;multi byte
  9100. (setq ret (concat ret (org-char-to-string sum)))
  9101. (setq sum 0))
  9102. ((not bytes) ; single byte(s)
  9103. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9104. ret)))
  9105. (defun org-link-unescape-single-byte-sequence (hex)
  9106. "Unhexify hex-encoded single byte character sequences."
  9107. (mapconcat (lambda (byte)
  9108. (char-to-string (string-to-number byte 16)))
  9109. (cdr (split-string hex "%")) ""))
  9110. (defun org-xor (a b)
  9111. "Exclusive or."
  9112. (if a (not b) b))
  9113. (defun org-fixup-message-id-for-http (s)
  9114. "Replace special characters in a message id, so it can be used in an http query."
  9115. (when (string-match "%" s)
  9116. (setq s (mapconcat (lambda (c)
  9117. (if (eq c ?%)
  9118. "%25"
  9119. (char-to-string c)))
  9120. s "")))
  9121. (while (string-match "<" s)
  9122. (setq s (replace-match "%3C" t t s)))
  9123. (while (string-match ">" s)
  9124. (setq s (replace-match "%3E" t t s)))
  9125. (while (string-match "@" s)
  9126. (setq s (replace-match "%40" t t s)))
  9127. s)
  9128. (defun org-link-prettify (link)
  9129. "Return a human-readable representation of LINK.
  9130. The car of LINK must be a raw link.
  9131. The cdr of LINK must be either a link description or nil."
  9132. (let ((desc (or (cadr link) "<no description>")))
  9133. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9134. "<" (car link) ">")))
  9135. ;;;###autoload
  9136. (defun org-insert-link-global ()
  9137. "Insert a link like Org-mode does.
  9138. This command can be called in any mode to insert a link in Org-mode syntax."
  9139. (interactive)
  9140. (org-load-modules-maybe)
  9141. (org-run-like-in-org-mode 'org-insert-link))
  9142. (defun org-insert-all-links (arg &optional pre post)
  9143. "Insert all links in `org-stored-links'.
  9144. When a universal prefix, do not delete the links from `org-stored-links'.
  9145. When `ARG' is a number, insert the last N link(s).
  9146. `PRE' and `POST' are optional arguments to define a string to
  9147. prepend or to append."
  9148. (interactive "P")
  9149. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9150. (links (copy-seq org-stored-links))
  9151. (pr (or pre "- "))
  9152. (po (or post "\n"))
  9153. (cnt 1) l)
  9154. (if (null org-stored-links)
  9155. (message "No link to insert")
  9156. (while (and (or (listp arg) (>= arg cnt))
  9157. (setq l (if (listp arg)
  9158. (pop links)
  9159. (pop org-stored-links))))
  9160. (setq cnt (1+ cnt))
  9161. (insert pr)
  9162. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9163. (insert po)))))
  9164. (defun org-insert-last-stored-link (arg)
  9165. "Insert the last link stored in `org-stored-links'."
  9166. (interactive "p")
  9167. (org-insert-all-links arg "" "\n"))
  9168. (defun org-link-fontify-links-to-this-file ()
  9169. "Fontify links to the current file in `org-stored-links'."
  9170. (let ((f (buffer-file-name)) a b)
  9171. (setq a (mapcar (lambda(l)
  9172. (let ((ll (car l)))
  9173. (when (and (string-match "^file:\\(.+\\)::" ll)
  9174. (equal f (expand-file-name (match-string 1 ll))))
  9175. ll)))
  9176. org-stored-links))
  9177. (when (featurep 'org-id)
  9178. (setq b (mapcar (lambda(l)
  9179. (let ((ll (car l)))
  9180. (when (and (string-match "^id:\\(.+\\)$" ll)
  9181. (equal f (expand-file-name
  9182. (or (org-id-find-id-file
  9183. (match-string 1 ll)) ""))))
  9184. ll)))
  9185. org-stored-links)))
  9186. (mapcar (lambda(l)
  9187. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9188. (delq nil (append a b)))))
  9189. (defvar org-link-links-in-this-file nil)
  9190. (defun org-insert-link (&optional complete-file link-location default-description)
  9191. "Insert a link. At the prompt, enter the link.
  9192. Completion can be used to insert any of the link protocol prefixes like
  9193. http or ftp in use.
  9194. The history can be used to select a link previously stored with
  9195. `org-store-link'. When the empty string is entered (i.e. if you just
  9196. press RET at the prompt), the link defaults to the most recently
  9197. stored link. As SPC triggers completion in the minibuffer, you need to
  9198. use M-SPC or C-q SPC to force the insertion of a space character.
  9199. You will also be prompted for a description, and if one is given, it will
  9200. be displayed in the buffer instead of the link.
  9201. If there is already a link at point, this command will allow you to edit link
  9202. and description parts.
  9203. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9204. be selected using completion. The path to the file will be relative to the
  9205. current directory if the file is in the current directory or a subdirectory.
  9206. Otherwise, the link will be the absolute path as completed in the minibuffer
  9207. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9208. option `org-link-file-path-type'.
  9209. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9210. the current directory or below.
  9211. With three \\[universal-argument] prefixes, negate the meaning of
  9212. `org-keep-stored-link-after-insertion'.
  9213. If `org-make-link-description-function' is non-nil, this function will be
  9214. called with the link target, and the result will be the default
  9215. link description.
  9216. If the LINK-LOCATION parameter is non-nil, this value will be
  9217. used as the link location instead of reading one interactively.
  9218. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9219. be used as the default description."
  9220. (interactive "P")
  9221. (let* ((wcf (current-window-configuration))
  9222. (origbuf (current-buffer))
  9223. (region (if (org-region-active-p)
  9224. (buffer-substring (region-beginning) (region-end))))
  9225. (remove (and region (list (region-beginning) (region-end))))
  9226. (desc region)
  9227. tmphist ; byte-compile incorrectly complains about this
  9228. (link link-location)
  9229. (abbrevs org-link-abbrev-alist-local)
  9230. entry file all-prefixes auto-desc)
  9231. (cond
  9232. (link-location) ; specified by arg, just use it.
  9233. ((org-in-regexp org-bracket-link-regexp 1)
  9234. ;; We do have a link at point, and we are going to edit it.
  9235. (setq remove (list (match-beginning 0) (match-end 0)))
  9236. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9237. (setq link (read-string "Link: "
  9238. (org-link-unescape
  9239. (org-match-string-no-properties 1)))))
  9240. ((or (org-in-regexp org-angle-link-re)
  9241. (org-in-regexp org-plain-link-re))
  9242. ;; Convert to bracket link
  9243. (setq remove (list (match-beginning 0) (match-end 0))
  9244. link (read-string "Link: "
  9245. (org-remove-angle-brackets (match-string 0)))))
  9246. ((member complete-file '((4) (16)))
  9247. ;; Completing read for file names.
  9248. (setq link (org-file-complete-link complete-file)))
  9249. (t
  9250. ;; Read link, with completion for stored links.
  9251. (org-link-fontify-links-to-this-file)
  9252. (org-switch-to-buffer-other-window "*Org Links*")
  9253. (with-current-buffer "*Org Links*"
  9254. (erase-buffer)
  9255. (insert "Insert a link.
  9256. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9257. (when org-stored-links
  9258. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9259. (insert (mapconcat 'org-link-prettify
  9260. (reverse org-stored-links) "\n")))
  9261. (goto-char (point-min)))
  9262. (let ((cw (selected-window)))
  9263. (select-window (get-buffer-window "*Org Links*" 'visible))
  9264. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9265. (unless (pos-visible-in-window-p (point-max))
  9266. (org-fit-window-to-buffer))
  9267. (and (window-live-p cw) (select-window cw)))
  9268. ;; Fake a link history, containing the stored links.
  9269. (setq tmphist (append (mapcar 'car org-stored-links)
  9270. org-insert-link-history))
  9271. (setq all-prefixes (append (mapcar 'car abbrevs)
  9272. (mapcar 'car org-link-abbrev-alist)
  9273. org-link-types))
  9274. (unwind-protect
  9275. (progn
  9276. (setq link
  9277. (org-completing-read
  9278. "Link: "
  9279. (append
  9280. (mapcar (lambda (x) (concat x ":"))
  9281. all-prefixes)
  9282. (mapcar 'car org-stored-links))
  9283. nil nil nil
  9284. 'tmphist
  9285. (caar org-stored-links)))
  9286. (if (not (string-match "\\S-" link))
  9287. (user-error "No link selected"))
  9288. (mapc (lambda(l)
  9289. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9290. org-stored-links)
  9291. (if (or (member link all-prefixes)
  9292. (and (equal ":" (substring link -1))
  9293. (member (substring link 0 -1) all-prefixes)
  9294. (setq link (substring link 0 -1))))
  9295. (setq link (with-current-buffer origbuf
  9296. (org-link-try-special-completion link)))))
  9297. (set-window-configuration wcf)
  9298. (kill-buffer "*Org Links*"))
  9299. (setq entry (assoc link org-stored-links))
  9300. (or entry (push link org-insert-link-history))
  9301. (setq desc (or desc (nth 1 entry)))))
  9302. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9303. (not org-keep-stored-link-after-insertion))
  9304. (setq org-stored-links (delq (assoc link org-stored-links)
  9305. org-stored-links)))
  9306. (if (and (string-match org-plain-link-re link)
  9307. (not (string-match org-ts-regexp link)))
  9308. ;; URL-like link, normalize the use of angular brackets.
  9309. (setq link (org-remove-angle-brackets link)))
  9310. ;; Check if we are linking to the current file with a search
  9311. ;; option If yes, simplify the link by using only the search
  9312. ;; option.
  9313. (when (and buffer-file-name
  9314. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9315. (let* ((path (match-string 1 link))
  9316. (case-fold-search nil)
  9317. (search (match-string 2 link)))
  9318. (save-match-data
  9319. (if (equal (file-truename buffer-file-name) (file-truename path))
  9320. ;; We are linking to this same file, with a search option
  9321. (setq link search)))))
  9322. ;; Check if we can/should use a relative path. If yes, simplify the link
  9323. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9324. (let* ((type (match-string 1 link))
  9325. (path (match-string 2 link))
  9326. (origpath path)
  9327. (case-fold-search nil))
  9328. (cond
  9329. ((or (eq org-link-file-path-type 'absolute)
  9330. (equal complete-file '(16)))
  9331. (setq path (abbreviate-file-name (expand-file-name path))))
  9332. ((eq org-link-file-path-type 'noabbrev)
  9333. (setq path (expand-file-name path)))
  9334. ((eq org-link-file-path-type 'relative)
  9335. (setq path (file-relative-name path)))
  9336. (t
  9337. (save-match-data
  9338. (if (string-match (concat "^" (regexp-quote
  9339. (expand-file-name
  9340. (file-name-as-directory
  9341. default-directory))))
  9342. (expand-file-name path))
  9343. ;; We are linking a file with relative path name.
  9344. (setq path (substring (expand-file-name path)
  9345. (match-end 0)))
  9346. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9347. (setq link (concat type path))
  9348. (if (equal desc origpath)
  9349. (setq desc path))))
  9350. (if org-make-link-description-function
  9351. (setq desc
  9352. (or (condition-case nil
  9353. (funcall org-make-link-description-function link desc)
  9354. (error (progn (message "Can't get link description from `%s'"
  9355. (symbol-name org-make-link-description-function))
  9356. (sit-for 2) nil)))
  9357. (read-string "Description: " default-description)))
  9358. (if default-description (setq desc default-description)
  9359. (setq desc (or (and auto-desc desc)
  9360. (read-string "Description: " desc)))))
  9361. (unless (string-match "\\S-" desc) (setq desc nil))
  9362. (if remove (apply 'delete-region remove))
  9363. (insert (org-make-link-string link desc))))
  9364. (defun org-link-try-special-completion (type)
  9365. "If there is completion support for link type TYPE, offer it."
  9366. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9367. (if (functionp fun)
  9368. (funcall fun)
  9369. (read-string "Link (no completion support): " (concat type ":")))))
  9370. (defun org-file-complete-link (&optional arg)
  9371. "Create a file link using completion."
  9372. (let (file link)
  9373. (setq file (org-iread-file-name "File: "))
  9374. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9375. (pwd1 (file-name-as-directory (abbreviate-file-name
  9376. (expand-file-name ".")))))
  9377. (cond
  9378. ((equal arg '(16))
  9379. (setq link (concat
  9380. "file:"
  9381. (abbreviate-file-name (expand-file-name file)))))
  9382. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9383. (setq link (concat "file:" (match-string 1 file))))
  9384. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9385. (expand-file-name file))
  9386. (setq link (concat
  9387. "file:" (match-string 1 (expand-file-name file)))))
  9388. (t (setq link (concat "file:" file)))))
  9389. link))
  9390. (defun org-iread-file-name (&rest args)
  9391. "Read-file-name using `ido-mode' speedup if available.
  9392. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9393. See `read-file-name' for a description of parameters."
  9394. (org-without-partial-completion
  9395. (if (and org-completion-use-ido
  9396. (fboundp 'ido-read-file-name)
  9397. (boundp 'ido-mode) ido-mode
  9398. (listp (second args)))
  9399. (let ((ido-enter-matching-directory nil))
  9400. (apply 'ido-read-file-name args))
  9401. (apply 'read-file-name args))))
  9402. (defun org-completing-read (&rest args)
  9403. "Completing-read with SPACE being a normal character."
  9404. (let ((enable-recursive-minibuffers t)
  9405. (minibuffer-local-completion-map
  9406. (copy-keymap minibuffer-local-completion-map)))
  9407. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9408. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9409. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9410. (apply 'org-icompleting-read args)))
  9411. (defun org-completing-read-no-i (&rest args)
  9412. (let (org-completion-use-ido org-completion-use-iswitchb)
  9413. (apply 'org-completing-read args)))
  9414. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9415. "Use iswitch as a completing-read replacement to choose from choices.
  9416. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9417. from."
  9418. (let* ((iswitchb-use-virtual-buffers nil)
  9419. (iswitchb-make-buflist-hook
  9420. (lambda ()
  9421. (setq iswitchb-temp-buflist choices))))
  9422. (iswitchb-read-buffer prompt)))
  9423. (defun org-icompleting-read (&rest args)
  9424. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9425. (org-without-partial-completion
  9426. (if (and org-completion-use-ido
  9427. (fboundp 'ido-completing-read)
  9428. (boundp 'ido-mode) ido-mode
  9429. (listp (second args)))
  9430. (let ((ido-enter-matching-directory nil))
  9431. (apply 'ido-completing-read (concat (car args))
  9432. (if (consp (car (nth 1 args)))
  9433. (mapcar 'car (nth 1 args))
  9434. (nth 1 args))
  9435. (cddr args)))
  9436. (if (and org-completion-use-iswitchb
  9437. (boundp 'iswitchb-mode) iswitchb-mode
  9438. (listp (second args)))
  9439. (apply 'org-iswitchb-completing-read (concat (car args))
  9440. (if (consp (car (nth 1 args)))
  9441. (mapcar 'car (nth 1 args))
  9442. (nth 1 args))
  9443. (cddr args))
  9444. (apply 'completing-read args)))))
  9445. (defun org-extract-attributes (s)
  9446. "Extract the attributes cookie from a string and set as text property."
  9447. (let (a attr (start 0) key value)
  9448. (save-match-data
  9449. (when (string-match "{{\\([^}]+\\)}}$" s)
  9450. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9451. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9452. (setq key (match-string 1 a) value (match-string 2 a)
  9453. start (match-end 0)
  9454. attr (plist-put attr (intern key) value))))
  9455. (org-add-props s nil 'org-attr attr))
  9456. s))
  9457. ;;; Opening/following a link
  9458. (defvar org-link-search-failed nil)
  9459. (defvar org-open-link-functions nil
  9460. "Hook for functions finding a plain text link.
  9461. These functions must take a single argument, the link content.
  9462. They will be called for links that look like [[link text][description]]
  9463. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9464. like a filename (e.g. \"./blue.png\").
  9465. These functions will be called *before* Org attempts to resolve the
  9466. link by doing text searches in the current buffer - so if you want a
  9467. link \"[[target]]\" to still find \"<<target>>\", your function should
  9468. handle this as a special case.
  9469. When the function does handle the link, it must return a non-nil value.
  9470. If it decides that it is not responsible for this link, it must return
  9471. nil to indicate that that Org-mode can continue with other options
  9472. like exact and fuzzy text search.")
  9473. (defun org-next-link (&optional search-backward)
  9474. "Move forward to the next link.
  9475. If the link is in hidden text, expose it."
  9476. (interactive "P")
  9477. (when (and org-link-search-failed (eq this-command last-command))
  9478. (goto-char (point-min))
  9479. (message "Link search wrapped back to beginning of buffer"))
  9480. (setq org-link-search-failed nil)
  9481. (let* ((pos (point))
  9482. (ct (org-context))
  9483. (a (assoc :link ct))
  9484. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9485. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9486. ((looking-at org-any-link-re)
  9487. ;; Don't stay stuck at link without an org-link face
  9488. (forward-char (if search-backward -1 1))))
  9489. (if (funcall srch-fun org-any-link-re nil t)
  9490. (progn
  9491. (goto-char (match-beginning 0))
  9492. (if (outline-invisible-p) (org-show-context)))
  9493. (goto-char pos)
  9494. (setq org-link-search-failed t)
  9495. (message "No further link found"))))
  9496. (defun org-previous-link ()
  9497. "Move backward to the previous link.
  9498. If the link is in hidden text, expose it."
  9499. (interactive)
  9500. (funcall 'org-next-link t))
  9501. (defun org-translate-link (s)
  9502. "Translate a link string if a translation function has been defined."
  9503. (if (and org-link-translation-function
  9504. (fboundp org-link-translation-function)
  9505. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9506. (progn
  9507. (setq s (funcall org-link-translation-function
  9508. (match-string 1 s) (match-string 2 s)))
  9509. (concat (car s) ":" (cdr s)))
  9510. s))
  9511. (defun org-translate-link-from-planner (type path)
  9512. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9513. This is still an experimental function, your mileage may vary."
  9514. (cond
  9515. ((member type '("http" "https" "news" "ftp"))
  9516. ;; standard Internet links are the same.
  9517. nil)
  9518. ((and (equal type "irc") (string-match "^//" path))
  9519. ;; Planner has two / at the beginning of an irc link, we have 1.
  9520. ;; We should have zero, actually....
  9521. (setq path (substring path 1)))
  9522. ((and (equal type "lisp") (string-match "^/" path))
  9523. ;; Planner has a slash, we do not.
  9524. (setq type "elisp" path (substring path 1)))
  9525. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9526. ;; A typical message link. Planner has the id after the final slash,
  9527. ;; we separate it with a hash mark
  9528. (setq path (concat (match-string 1 path) "#"
  9529. (org-remove-angle-brackets (match-string 2 path))))))
  9530. (cons type path))
  9531. (defun org-find-file-at-mouse (ev)
  9532. "Open file link or URL at mouse."
  9533. (interactive "e")
  9534. (mouse-set-point ev)
  9535. (org-open-at-point 'in-emacs))
  9536. (defun org-open-at-mouse (ev)
  9537. "Open file link or URL at mouse.
  9538. See the docstring of `org-open-file' for details."
  9539. (interactive "e")
  9540. (mouse-set-point ev)
  9541. (if (eq major-mode 'org-agenda-mode)
  9542. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9543. (org-open-at-point))
  9544. (defvar org-window-config-before-follow-link nil
  9545. "The window configuration before following a link.
  9546. This is saved in case the need arises to restore it.")
  9547. (defvar org-open-link-marker (make-marker)
  9548. "Marker pointing to the location where `org-open-at-point' was called.")
  9549. ;;;###autoload
  9550. (defun org-open-at-point-global ()
  9551. "Follow a link like Org-mode does.
  9552. This command can be called in any mode to follow a link that has
  9553. Org-mode syntax."
  9554. (interactive)
  9555. (org-run-like-in-org-mode 'org-open-at-point))
  9556. ;;;###autoload
  9557. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9558. "Open a link in the string S, as if it was in Org-mode."
  9559. (interactive "sLink: \nP")
  9560. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9561. (with-temp-buffer
  9562. (let ((org-inhibit-startup (not reference-buffer)))
  9563. (org-mode)
  9564. (insert s)
  9565. (goto-char (point-min))
  9566. (when reference-buffer
  9567. (setq org-link-abbrev-alist-local
  9568. (with-current-buffer reference-buffer
  9569. org-link-abbrev-alist-local)))
  9570. (org-open-at-point arg reference-buffer)))))
  9571. (defvar org-open-at-point-functions nil
  9572. "Hook that is run when following a link at point.
  9573. Functions in this hook must return t if they identify and follow
  9574. a link at point. If they don't find anything interesting at point,
  9575. they must return nil.")
  9576. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9577. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9578. (defun org-open-at-point (&optional arg reference-buffer)
  9579. "Open link, timestamp, footnote or tags at point.
  9580. When point is on a link, follow it. Normally, files will be
  9581. opened by an appropriate application. If the optional prefix
  9582. argument ARG is non-nil, Emacs will visit the file. With
  9583. a double prefix argument, try to open outside of Emacs, in the
  9584. application the system uses for this file type.
  9585. When point is on a timestamp, open the agenda at the day
  9586. specified.
  9587. When point is a footnote definition, move to the first reference
  9588. found. If it is on a reference, move to the associated
  9589. definition.
  9590. When point is on a headline, display a list of every link in the
  9591. entry, so it is possible to pick one, or all, of them. If point
  9592. is on a tag, call `org-tags-view' instead.
  9593. When optional argument REFERENCE-BUFFER is non-nil, it should
  9594. specify a buffer from where the link search should happen. This
  9595. is used internally by `org-open-link-from-string'.
  9596. On top of syntactically correct links, this function will open
  9597. the link at point in comments or comment blocks and the first
  9598. link in a property drawer line."
  9599. (interactive "P")
  9600. ;; On a code block, open block's results.
  9601. (unless (call-interactively 'org-babel-open-src-block-result)
  9602. (org-load-modules-maybe)
  9603. (move-marker org-open-link-marker (point))
  9604. (setq org-window-config-before-follow-link (current-window-configuration))
  9605. (org-remove-occur-highlights nil nil t)
  9606. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9607. (let* ((context
  9608. ;; Only consider supported types, even if they are not
  9609. ;; the closest one.
  9610. (org-element-lineage
  9611. (org-element-context)
  9612. '(comment comment-block footnote-definition footnote-reference
  9613. headline inlinetask link node-property timestamp)
  9614. t))
  9615. (type (org-element-type context))
  9616. (value (org-element-property :value context)))
  9617. (cond
  9618. ((not context) (user-error "No link found"))
  9619. ;; Exception: open timestamps and links in properties drawers
  9620. ;; and comments.
  9621. ((memq type '(comment comment-block node-property))
  9622. (cond ((org-in-regexp org-any-link-re)
  9623. (org-open-link-from-string (match-string-no-properties 0)))
  9624. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9625. (org-follow-timestamp-link))
  9626. (t (user-error "No link found"))))
  9627. ;; On a headline or an inlinetask, but not on a timestamp,
  9628. ;; a link, a footnote reference or on tags.
  9629. ((and (memq type '(headline inlinetask))
  9630. ;; Not on tags.
  9631. (progn (save-excursion (beginning-of-line)
  9632. (looking-at org-complex-heading-regexp))
  9633. (or (not (match-beginning 5))
  9634. (< (point) (match-beginning 5)))))
  9635. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9636. (links (car data))
  9637. (links-end (cdr data)))
  9638. (if links
  9639. (dolist (link (if (stringp links) (list links) links))
  9640. (search-forward link nil links-end)
  9641. (goto-char (match-beginning 0))
  9642. (org-open-at-point))
  9643. (require 'org-attach)
  9644. (org-attach-reveal 'if-exists))))
  9645. ;; Do nothing on white spaces after an object, unless point
  9646. ;; is right after it.
  9647. ((> (point)
  9648. (save-excursion
  9649. (goto-char (org-element-property :end context))
  9650. (skip-chars-backward " \t")
  9651. (point)))
  9652. (user-error "No link found"))
  9653. ((eq type 'timestamp) (org-follow-timestamp-link))
  9654. ;; On tags within a headline or an inlinetask.
  9655. ((and (memq type '(headline inlinetask))
  9656. (progn (save-excursion (beginning-of-line)
  9657. (looking-at org-complex-heading-regexp))
  9658. (and (match-beginning 5)
  9659. (>= (point) (match-beginning 5)))))
  9660. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9661. ((eq type 'link)
  9662. ;; When link is located within the description of another
  9663. ;; link (e.g., an inline image), always open the parent
  9664. ;; link.
  9665. (let*((link (let ((up (org-element-property :parent context)))
  9666. (if (eq (org-element-type up) 'link) up context)))
  9667. (type (org-element-property :type link))
  9668. (path (org-link-unescape (org-element-property :path link))))
  9669. ;; Switch back to REFERENCE-BUFFER needed when called in
  9670. ;; a temporary buffer through `org-open-link-from-string'.
  9671. (with-current-buffer (or reference-buffer (current-buffer))
  9672. (cond
  9673. ((equal type "file")
  9674. (if (string-match "[*?{]" (file-name-nondirectory path))
  9675. (dired path)
  9676. ;; Look into `org-link-protocols' in order to find
  9677. ;; a DEDICATED-FUNCTION to open file. The function
  9678. ;; will be applied on raw link instead of parsed
  9679. ;; link due to the limitation in `org-add-link-type'
  9680. ;; ("open" function called with a single argument).
  9681. ;; If no such function is found, fallback to
  9682. ;; `org-open-file'.
  9683. ;;
  9684. ;; Note : "file+emacs" and "file+sys" types are
  9685. ;; hard-coded in order to escape the previous
  9686. ;; limitation.
  9687. (let* ((option (org-element-property :search-option link))
  9688. (app (org-element-property :application link))
  9689. (dedicated-function
  9690. (nth 1 (assoc app org-link-protocols))))
  9691. (if dedicated-function
  9692. (funcall dedicated-function
  9693. (concat path
  9694. (and option (concat "::" option))))
  9695. (apply 'org-open-file
  9696. path
  9697. (cond (arg)
  9698. ((equal app "emacs") 'emacs)
  9699. ((equal app "sys") 'system))
  9700. (cond ((not option) nil)
  9701. ((org-string-match-p "\\`[0-9]+\\'" option)
  9702. (list (string-to-number option)))
  9703. (t (list nil
  9704. (org-link-unescape option)))))))))
  9705. ((assoc type org-link-protocols)
  9706. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9707. ((equal type "help")
  9708. (let ((f-or-v (intern path)))
  9709. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9710. ((boundp f-or-v) (describe-variable f-or-v))
  9711. (t (error "Not a known function or variable")))))
  9712. ((member type '("http" "https" "ftp" "mailto" "news"))
  9713. (browse-url (org-link-escape-browser (concat type ":" path))))
  9714. ((equal type "doi")
  9715. (browse-url
  9716. (org-link-escape-browser (concat org-doi-server-url path))))
  9717. ((equal type "message") (browse-url (concat type ":" path)))
  9718. ((equal type "shell")
  9719. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9720. (cmd path))
  9721. (if (or (and (org-string-nw-p
  9722. org-confirm-shell-link-not-regexp)
  9723. (string-match
  9724. org-confirm-shell-link-not-regexp cmd))
  9725. (not org-confirm-shell-link-function)
  9726. (funcall org-confirm-shell-link-function
  9727. (format "Execute \"%s\" in shell? "
  9728. (org-add-props cmd nil
  9729. 'face 'org-warning))))
  9730. (progn
  9731. (message "Executing %s" cmd)
  9732. (shell-command cmd buf)
  9733. (when (featurep 'midnight)
  9734. (setq clean-buffer-list-kill-buffer-names
  9735. (cons buf
  9736. clean-buffer-list-kill-buffer-names))))
  9737. (user-error "Abort"))))
  9738. ((equal type "elisp")
  9739. (let ((cmd path))
  9740. (if (or (and (org-string-nw-p
  9741. org-confirm-elisp-link-not-regexp)
  9742. (org-string-match-p
  9743. org-confirm-elisp-link-not-regexp cmd))
  9744. (not org-confirm-elisp-link-function)
  9745. (funcall org-confirm-elisp-link-function
  9746. (format "Execute \"%s\" as elisp? "
  9747. (org-add-props cmd nil
  9748. 'face 'org-warning))))
  9749. (message "%s => %s" cmd
  9750. (if (eq (string-to-char cmd) ?\()
  9751. (eval (read cmd))
  9752. (call-interactively (read cmd))))
  9753. (user-error "Abort"))))
  9754. ((equal type "id")
  9755. (require 'ord-id)
  9756. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9757. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9758. (unless (run-hook-with-args-until-success
  9759. 'org-open-link-functions path)
  9760. (if (not arg) (org-mark-ring-push)
  9761. (switch-to-buffer-other-window
  9762. (org-get-buffer-for-internal-link (current-buffer))))
  9763. (let ((cmd `(org-link-search
  9764. ,(if (member type '("custom-id" "coderef"))
  9765. (org-element-property :raw-link link)
  9766. path)
  9767. ,(cond ((equal arg '(4)) 'occur)
  9768. ((equal arg '(16)) 'org-occur))
  9769. ,(org-element-property :begin link))))
  9770. (condition-case nil
  9771. (let ((org-link-search-inhibit-query t))
  9772. (eval cmd))
  9773. (error (progn (widen) (eval cmd)))))))
  9774. (t (browse-url-at-point))))))
  9775. ;; On a footnote reference or at a footnote definition's label.
  9776. ((or (eq type 'footnote-reference)
  9777. (and (eq type 'footnote-definition)
  9778. (save-excursion
  9779. ;; Do not validate action when point is on the
  9780. ;; spaces right after the footnote label, in
  9781. ;; order to be on par with behaviour on links.
  9782. (skip-chars-forward " \t")
  9783. (let ((begin
  9784. (org-element-property :contents-begin context)))
  9785. (if begin (< (point) begin)
  9786. (= (org-element-property :post-affiliated context)
  9787. (line-beginning-position)))))))
  9788. (org-footnote-action))
  9789. (t (user-error "No link found")))))
  9790. (move-marker org-open-link-marker nil)
  9791. (run-hook-with-args 'org-follow-link-hook)))
  9792. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9793. "Offer links in the current entry and return the selected link.
  9794. If there is only one link, return it.
  9795. If NTH is an integer, return the NTH link found.
  9796. If ZERO is a string, check also this string for a link, and if
  9797. there is one, return it."
  9798. (with-current-buffer buffer
  9799. (save-excursion
  9800. (save-restriction
  9801. (widen)
  9802. (goto-char marker)
  9803. (let ((cnt ?0)
  9804. (in-emacs (if (integerp nth) nil nth))
  9805. have-zero end links link c)
  9806. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9807. (push (match-string 0 zero) links)
  9808. (setq cnt (1- cnt) have-zero t))
  9809. (save-excursion
  9810. (org-back-to-heading t)
  9811. (setq end (save-excursion (outline-next-heading) (point)))
  9812. (while (re-search-forward org-any-link-re end t)
  9813. (push (match-string 0) links))
  9814. (setq links (org-uniquify (reverse links))))
  9815. (cond
  9816. ((null links)
  9817. (message "No links"))
  9818. ((equal (length links) 1)
  9819. (setq link (car links)))
  9820. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9821. (setq link (nth (if have-zero nth (1- nth)) links)))
  9822. (t ; we have to select a link
  9823. (save-excursion
  9824. (save-window-excursion
  9825. (delete-other-windows)
  9826. (with-output-to-temp-buffer "*Select Link*"
  9827. (mapc (lambda (l)
  9828. (if (not (string-match org-bracket-link-regexp l))
  9829. (princ (format "[%c] %s\n" (incf cnt)
  9830. (org-remove-angle-brackets l)))
  9831. (if (match-end 3)
  9832. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9833. (match-string 3 l) (match-string 1 l)))
  9834. (princ (format "[%c] %s\n" (incf cnt)
  9835. (match-string 1 l))))))
  9836. links))
  9837. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9838. (message "Select link to open, RET to open all:")
  9839. (setq c (read-char-exclusive))
  9840. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9841. (when (equal c ?q) (user-error "Abort"))
  9842. (if (equal c ?\C-m)
  9843. (setq link links)
  9844. (setq nth (- c ?0))
  9845. (if have-zero (setq nth (1+ nth)))
  9846. (unless (and (integerp nth) (>= (length links) nth))
  9847. (user-error "Invalid link selection"))
  9848. (setq link (nth (1- nth) links)))))
  9849. (cons link end))))))
  9850. ;; TODO: These functions are deprecated since `org-open-at-point'
  9851. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9852. (defun org-open-file-with-system (path)
  9853. "Open file at PATH using the system way of opening it."
  9854. (org-open-file path 'system))
  9855. (defun org-open-file-with-emacs (path)
  9856. "Open file at PATH in Emacs."
  9857. (org-open-file path 'emacs))
  9858. ;;; File search
  9859. (defvar org-create-file-search-functions nil
  9860. "List of functions to construct the right search string for a file link.
  9861. These functions are called in turn with point at the location to
  9862. which the link should point.
  9863. A function in the hook should first test if it would like to
  9864. handle this file type, for example by checking the `major-mode'
  9865. or the file extension. If it decides not to handle this file, it
  9866. should just return nil to give other functions a chance. If it
  9867. does handle the file, it must return the search string to be used
  9868. when following the link. The search string will be part of the
  9869. file link, given after a double colon, and `org-open-at-point'
  9870. will automatically search for it. If special measures must be
  9871. taken to make the search successful, another function should be
  9872. added to the companion hook `org-execute-file-search-functions',
  9873. which see.
  9874. A function in this hook may also use `setq' to set the variable
  9875. `description' to provide a suggestion for the descriptive text to
  9876. be used for this link when it gets inserted into an Org-mode
  9877. buffer with \\[org-insert-link].")
  9878. (defvar org-execute-file-search-functions nil
  9879. "List of functions to execute a file search triggered by a link.
  9880. Functions added to this hook must accept a single argument, the
  9881. search string that was part of the file link, the part after the
  9882. double colon. The function must first check if it would like to
  9883. handle this search, for example by checking the `major-mode' or
  9884. the file extension. If it decides not to handle this search, it
  9885. should just return nil to give other functions a chance. If it
  9886. does handle the search, it must return a non-nil value to keep
  9887. other functions from trying.
  9888. Each function can access the current prefix argument through the
  9889. variable `current-prefix-arg'. Note that a single prefix is used
  9890. to force opening a link in Emacs, so it may be good to only use a
  9891. numeric or double prefix to guide the search function.
  9892. In case this is needed, a function in this hook can also restore
  9893. the window configuration before `org-open-at-point' was called using:
  9894. (set-window-configuration org-window-config-before-follow-link)")
  9895. (defun org-link-search (s &optional type avoid-pos stealth)
  9896. "Search for a link search option.
  9897. If S is surrounded by forward slashes, it is interpreted as a
  9898. regular expression. In org-mode files, this will create an `org-occur'
  9899. sparse tree. In ordinary files, `occur' will be used to list matches.
  9900. If the current buffer is in `dired-mode', grep will be used to search
  9901. in all files. If AVOID-POS is given, ignore matches near that position.
  9902. When optional argument STEALTH is non-nil, do not modify
  9903. visibility around point, thus ignoring
  9904. `org-show-hierarchy-above', `org-show-following-heading' and
  9905. `org-show-siblings' variables."
  9906. (let ((case-fold-search t)
  9907. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9908. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9909. (append '(("") (" ") ("\t") ("\n"))
  9910. org-emphasis-alist)
  9911. "\\|") "\\)"))
  9912. (pos (point))
  9913. words re0 re2 re4_ re4 re5 re2a re2a_ reall)
  9914. (cond
  9915. ;; First check if there are any special search functions
  9916. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9917. ;; Now try the builtin stuff
  9918. ((and (equal (string-to-char s0) ?#)
  9919. (> (length s0) 1)
  9920. (save-excursion
  9921. (goto-char (point-min))
  9922. (and
  9923. (re-search-forward
  9924. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9925. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9926. (setq type 'dedicated
  9927. pos (match-beginning 0))))
  9928. ;; There is an exact target for this
  9929. (goto-char pos)
  9930. (org-back-to-heading t)))
  9931. ((save-excursion
  9932. (goto-char (point-min))
  9933. (and
  9934. (re-search-forward
  9935. (concat "<<" (regexp-quote s0) ">>") nil t)
  9936. (setq type 'dedicated
  9937. pos (match-beginning 0))))
  9938. ;; There is an exact target for this
  9939. (goto-char pos))
  9940. ((save-excursion
  9941. (goto-char (point-min))
  9942. (and
  9943. (re-search-forward
  9944. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9945. (setq type 'dedicated pos (match-beginning 0))))
  9946. ;; Found an element with a matching #+name affiliated keyword.
  9947. (goto-char pos))
  9948. ((and (string-match "^(\\(.*\\))$" s0)
  9949. (save-excursion
  9950. (goto-char (point-min))
  9951. (and
  9952. (re-search-forward
  9953. (concat "[^[]" (regexp-quote
  9954. (format org-coderef-label-format
  9955. (match-string 1 s0))))
  9956. nil t)
  9957. (setq type 'dedicated
  9958. pos (1+ (match-beginning 0))))))
  9959. ;; There is a coderef target for this
  9960. (goto-char pos))
  9961. ((string-match "^/\\(.*\\)/$" s)
  9962. ;; A regular expression
  9963. (cond
  9964. ((derived-mode-p 'org-mode)
  9965. (org-occur (match-string 1 s)))
  9966. (t (org-do-occur (match-string 1 s)))))
  9967. ((and (derived-mode-p 'org-mode)
  9968. (or (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9969. org-link-search-must-match-exact-headline))
  9970. ;; Headline search.
  9971. (goto-char (point-min))
  9972. (cond
  9973. ((let (case-fold-search)
  9974. (re-search-forward
  9975. (let* ((wspace "[ \t]")
  9976. (wspaceopt (concat wspace "*"))
  9977. (cookie (concat "\\(?:"
  9978. wspaceopt
  9979. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9980. wspaceopt
  9981. "\\)"))
  9982. (sep (concat "\\(?:" wspace "+\\|" cookie "+\\)")))
  9983. (concat
  9984. org-outline-regexp-bol
  9985. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9986. "\\(?:\\[#.\\][ \t]+\\)?"
  9987. sep "*"
  9988. (mapconcat #'identity
  9989. (org-split-string (regexp-quote s))
  9990. (concat sep "+"))
  9991. sep "*"
  9992. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9993. "[ \t]*$"))
  9994. nil t))
  9995. ;; OK, found a match
  9996. (setq type 'dedicated)
  9997. (goto-char (match-beginning 0)))
  9998. ((and (not org-link-search-inhibit-query)
  9999. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10000. (y-or-n-p "No match - create this as a new heading? "))
  10001. (goto-char (point-max))
  10002. (unless (bolp) (newline))
  10003. (org-insert-heading nil t t)
  10004. (insert s "\n")
  10005. (beginning-of-line 0))
  10006. (t
  10007. (goto-char pos)
  10008. (error "No match"))))
  10009. (t
  10010. ;; A normal search string
  10011. (remove-text-properties
  10012. 0 (length s)
  10013. '(face nil mouse-face nil keymap nil fontified nil) s)
  10014. ;; Make a series of regular expressions to find a match
  10015. (setq words (org-split-string s "[ \n\r\t]+")
  10016. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10017. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10018. "\\)" markers)
  10019. re2a_ (concat "\\(" (mapconcat 'downcase words
  10020. "[ \t\r\n]+") "\\)[ \t\r\n]")
  10021. re2a (concat "[ \t\r\n]" re2a_)
  10022. re4_ (concat "\\(" (mapconcat 'downcase words
  10023. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10024. re4 (concat "[^a-zA-Z_]" re4_)
  10025. re5 (concat ".*" re4)
  10026. reall (concat "\\(" re0 "\\)\\|\\(" re2 "\\)\\|\\(" re4
  10027. "\\)\\|\\(" re5 "\\)"))
  10028. (cond
  10029. ((eq type 'org-occur) (org-occur reall))
  10030. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10031. (t (goto-char (point-min))
  10032. (setq type 'fuzzy)
  10033. (if (or (and (org-search-not-self 1 re0 nil t)
  10034. (setq type 'dedicated))
  10035. (org-search-not-self 1 re2 nil t)
  10036. (org-search-not-self 1 re2a nil t)
  10037. (org-search-not-self 1 re4 nil t)
  10038. (org-search-not-self 1 re5 nil t))
  10039. (goto-char (match-beginning 1))
  10040. (goto-char pos)
  10041. (error "No match"))))))
  10042. (and (derived-mode-p 'org-mode)
  10043. (not stealth)
  10044. (org-show-context 'link-search))
  10045. type))
  10046. (defun org-search-not-self (group &rest args)
  10047. "Execute `re-search-forward', but only accept matches that do not
  10048. enclose the position of `org-open-link-marker'."
  10049. (let ((m org-open-link-marker))
  10050. (catch 'exit
  10051. (while (apply 're-search-forward args)
  10052. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10053. (goto-char (match-end group))
  10054. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10055. (> (match-beginning 0) (marker-position m))
  10056. (< (match-end 0) (marker-position m)))
  10057. (save-match-data
  10058. (or (not (org-in-regexp
  10059. org-bracket-link-analytic-regexp 1))
  10060. (not (match-end 4)) ; no description
  10061. (and (<= (match-beginning 4) (point))
  10062. (>= (match-end 4) (point))))))
  10063. (throw 'exit (point))))))))
  10064. (defun org-get-buffer-for-internal-link (buffer)
  10065. "Return a buffer to be used for displaying the link target of internal links."
  10066. (cond
  10067. ((not org-display-internal-link-with-indirect-buffer)
  10068. buffer)
  10069. ((string-match "(Clone)$" (buffer-name buffer))
  10070. (message "Buffer is already a clone, not making another one")
  10071. ;; we also do not modify visibility in this case
  10072. buffer)
  10073. (t ; make a new indirect buffer for displaying the link
  10074. (let* ((bn (buffer-name buffer))
  10075. (ibn (concat bn "(Clone)"))
  10076. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10077. (with-current-buffer ib (org-overview))
  10078. ib))))
  10079. (defun org-do-occur (regexp &optional cleanup)
  10080. "Call the Emacs command `occur'.
  10081. If CLEANUP is non-nil, remove the printout of the regular expression
  10082. in the *Occur* buffer. This is useful if the regex is long and not useful
  10083. to read."
  10084. (occur regexp)
  10085. (when cleanup
  10086. (let ((cwin (selected-window)) win beg end)
  10087. (when (setq win (get-buffer-window "*Occur*"))
  10088. (select-window win))
  10089. (goto-char (point-min))
  10090. (when (re-search-forward "match[a-z]+" nil t)
  10091. (setq beg (match-end 0))
  10092. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10093. (setq end (1- (match-beginning 0)))))
  10094. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10095. (goto-char (point-min))
  10096. (select-window cwin))))
  10097. ;;; The mark ring for links jumps
  10098. (defvar org-mark-ring nil
  10099. "Mark ring for positions before jumps in Org-mode.")
  10100. (defvar org-mark-ring-last-goto nil
  10101. "Last position in the mark ring used to go back.")
  10102. ;; Fill and close the ring
  10103. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10104. (loop for i from 1 to org-mark-ring-length do
  10105. (push (make-marker) org-mark-ring))
  10106. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10107. org-mark-ring)
  10108. (defun org-mark-ring-push (&optional pos buffer)
  10109. "Put the current position or POS into the mark ring and rotate it."
  10110. (interactive)
  10111. (setq pos (or pos (point)))
  10112. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10113. (move-marker (car org-mark-ring)
  10114. (or pos (point))
  10115. (or buffer (current-buffer)))
  10116. (message "%s"
  10117. (substitute-command-keys
  10118. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10119. (defun org-mark-ring-goto (&optional n)
  10120. "Jump to the previous position in the mark ring.
  10121. With prefix arg N, jump back that many stored positions. When
  10122. called several times in succession, walk through the entire ring.
  10123. Org-mode commands jumping to a different position in the current file,
  10124. or to another Org-mode file, automatically push the old position
  10125. onto the ring."
  10126. (interactive "p")
  10127. (let (p m)
  10128. (if (eq last-command this-command)
  10129. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10130. (setq p org-mark-ring))
  10131. (setq org-mark-ring-last-goto p)
  10132. (setq m (car p))
  10133. (org-pop-to-buffer-same-window (marker-buffer m))
  10134. (goto-char m)
  10135. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10136. (defun org-remove-angle-brackets (s)
  10137. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10138. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10139. s)
  10140. (defun org-add-angle-brackets (s)
  10141. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10142. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10143. s)
  10144. (defun org-remove-double-quotes (s)
  10145. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10146. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10147. s)
  10148. ;;; Following specific links
  10149. (defun org-follow-timestamp-link ()
  10150. "Open an agenda view for the time-stamp date/range at point."
  10151. (cond
  10152. ((org-at-date-range-p t)
  10153. (let ((org-agenda-start-on-weekday)
  10154. (t1 (match-string 1))
  10155. (t2 (match-string 2)) tt1 tt2)
  10156. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10157. tt2 (time-to-days (org-time-string-to-time t2)))
  10158. (let ((org-agenda-buffer-tmp-name
  10159. (format "*Org Agenda(a:%s)"
  10160. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10161. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10162. ((org-at-timestamp-p t)
  10163. (let ((org-agenda-buffer-tmp-name
  10164. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10165. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10166. (substring (match-string 1) 0 10)))
  10167. 1)))
  10168. (t (error "This should not happen"))))
  10169. ;;; Following file links
  10170. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10171. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10172. (declare-function mailcap-mime-info
  10173. "mailcap" (string &optional request no-decode))
  10174. (defvar org-wait nil)
  10175. (defun org-open-file (path &optional in-emacs line search)
  10176. "Open the file at PATH.
  10177. First, this expands any special file name abbreviations. Then the
  10178. configuration variable `org-file-apps' is checked if it contains an
  10179. entry for this file type, and if yes, the corresponding command is launched.
  10180. If no application is found, Emacs simply visits the file.
  10181. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10182. With a double \\[universal-argument] \\[universal-argument] \
  10183. prefix arg, Org tries to avoid opening in Emacs
  10184. and to use an external application to visit the file.
  10185. Optional LINE specifies a line to go to, optional SEARCH a string
  10186. to search for. If LINE or SEARCH is given, the file will be
  10187. opened in Emacs, unless an entry from org-file-apps that makes
  10188. use of groups in a regexp matches.
  10189. If you want to change the way frames are used when following a
  10190. link, please customize `org-link-frame-setup'.
  10191. If the file does not exist, an error is thrown."
  10192. (let* ((file (if (equal path "")
  10193. buffer-file-name
  10194. (substitute-in-file-name (expand-file-name path))))
  10195. (file-apps (append org-file-apps (org-default-apps)))
  10196. (apps (org-remove-if
  10197. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10198. (apps-dlink (org-remove-if-not
  10199. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10200. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10201. (dirp (if remp nil (file-directory-p file)))
  10202. (file (if (and dirp org-open-directory-means-index-dot-org)
  10203. (concat (file-name-as-directory file) "index.org")
  10204. file))
  10205. (a-m-a-p (assq 'auto-mode apps))
  10206. (dfile (downcase file))
  10207. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10208. (link (cond ((and (eq line nil)
  10209. (eq search nil))
  10210. file)
  10211. (line
  10212. (concat file "::" (number-to-string line)))
  10213. (search
  10214. (concat file "::" search))))
  10215. (dlink (downcase link))
  10216. (old-buffer (current-buffer))
  10217. (old-pos (point))
  10218. (old-mode major-mode)
  10219. ext cmd link-match-data)
  10220. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10221. (setq ext (match-string 1 dfile))
  10222. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10223. (setq ext (match-string 1 dfile))))
  10224. (cond
  10225. ((member in-emacs '((16) system))
  10226. (setq cmd (cdr (assoc 'system apps))))
  10227. (in-emacs (setq cmd 'emacs))
  10228. (t
  10229. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10230. (and dirp (cdr (assoc 'directory apps)))
  10231. ; first, try matching against apps-dlink
  10232. ; if we get a match here, store the match data for later
  10233. (let ((match (assoc-default dlink apps-dlink
  10234. 'string-match)))
  10235. (if match
  10236. (progn (setq link-match-data (match-data))
  10237. match)
  10238. (progn (setq in-emacs (or in-emacs line search))
  10239. nil))) ; if we have no match in apps-dlink,
  10240. ; always open the file in emacs if line or search
  10241. ; is given (for backwards compatibility)
  10242. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10243. 'string-match)
  10244. (cdr (assoc ext apps))
  10245. (cdr (assoc t apps))))))
  10246. (when (eq cmd 'system)
  10247. (setq cmd (cdr (assoc 'system apps))))
  10248. (when (eq cmd 'default)
  10249. (setq cmd (cdr (assoc t apps))))
  10250. (when (eq cmd 'mailcap)
  10251. (require 'mailcap)
  10252. (mailcap-parse-mailcaps)
  10253. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10254. (command (mailcap-mime-info mime-type)))
  10255. (if (stringp command)
  10256. (setq cmd command)
  10257. (setq cmd 'emacs))))
  10258. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10259. (not (file-exists-p file))
  10260. (not org-open-non-existing-files))
  10261. (user-error "No such file: %s" file))
  10262. (cond
  10263. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10264. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10265. (while (string-match "['\"]%s['\"]" cmd)
  10266. (setq cmd (replace-match "%s" t t cmd)))
  10267. (while (string-match "%s" cmd)
  10268. (setq cmd (replace-match
  10269. (save-match-data
  10270. (shell-quote-argument
  10271. (convert-standard-filename file)))
  10272. t t cmd)))
  10273. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10274. (save-match-data
  10275. (let ((match-index 1)
  10276. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10277. (set-match-data link-match-data)
  10278. (while (<= match-index number-of-groups)
  10279. (let ((regex (concat "%" (number-to-string match-index)))
  10280. (replace-with (match-string match-index dlink)))
  10281. (while (string-match regex cmd)
  10282. (setq cmd (replace-match replace-with t t cmd))))
  10283. (setq match-index (+ match-index 1)))))
  10284. (save-window-excursion
  10285. (message "Running %s...done" cmd)
  10286. (start-process-shell-command cmd nil cmd)
  10287. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10288. ((or (stringp cmd)
  10289. (eq cmd 'emacs))
  10290. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10291. (widen)
  10292. (if line (progn (org-goto-line line)
  10293. (if (derived-mode-p 'org-mode)
  10294. (org-reveal)))
  10295. (if search (org-link-search search))))
  10296. ((consp cmd)
  10297. (let ((file (convert-standard-filename file)))
  10298. (save-match-data
  10299. (set-match-data link-match-data)
  10300. (eval cmd))))
  10301. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10302. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10303. (or (not (equal old-buffer (current-buffer)))
  10304. (not (equal old-pos (point))))
  10305. (org-mark-ring-push old-pos old-buffer))))
  10306. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10307. "This function returns non-nil if `entry' uses a regular
  10308. expression which should be matched against the whole link by
  10309. org-open-file.
  10310. It assumes that is the case when the entry uses a regular
  10311. expression which has at least one grouping construct and the
  10312. action is either a lisp form or a command string containing
  10313. '%1', i.e. using at least one subexpression match as a
  10314. parameter."
  10315. (let ((selector (car entry))
  10316. (action (cdr entry)))
  10317. (if (stringp selector)
  10318. (and (> (regexp-opt-depth selector) 0)
  10319. (or (and (stringp action)
  10320. (string-match "%[0-9]" action))
  10321. (consp action)))
  10322. nil)))
  10323. (defun org-default-apps ()
  10324. "Return the default applications for this operating system."
  10325. (cond
  10326. ((eq system-type 'darwin)
  10327. org-file-apps-defaults-macosx)
  10328. ((eq system-type 'windows-nt)
  10329. org-file-apps-defaults-windowsnt)
  10330. (t org-file-apps-defaults-gnu)))
  10331. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10332. "Convert extensions to regular expressions in the cars of LIST.
  10333. Also, weed out any non-string entries, because the return value is used
  10334. only for regexp matching.
  10335. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10336. point to the symbol `emacs', indicating that the file should
  10337. be opened in Emacs."
  10338. (append
  10339. (delq nil
  10340. (mapcar (lambda (x)
  10341. (if (not (stringp (car x)))
  10342. nil
  10343. (if (string-match "\\W" (car x))
  10344. x
  10345. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10346. list))
  10347. (if add-auto-mode
  10348. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10349. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10350. (defun org-file-remote-p (file)
  10351. "Test whether FILE specifies a location on a remote system.
  10352. Return non-nil if the location is indeed remote.
  10353. For example, the filename \"/user@host:/foo\" specifies a location
  10354. on the system \"/user@host:\"."
  10355. (cond ((fboundp 'file-remote-p)
  10356. (file-remote-p file))
  10357. ((fboundp 'tramp-handle-file-remote-p)
  10358. (tramp-handle-file-remote-p file))
  10359. ((and (boundp 'ange-ftp-name-format)
  10360. (string-match (car ange-ftp-name-format) file))
  10361. t)))
  10362. ;;;; Refiling
  10363. (defun org-get-org-file ()
  10364. "Read a filename, with default directory `org-directory'."
  10365. (let ((default (or org-default-notes-file remember-data-file)))
  10366. (read-file-name (format "File name [%s]: " default)
  10367. (file-name-as-directory org-directory)
  10368. default)))
  10369. (defun org-notes-order-reversed-p ()
  10370. "Check if the current file should receive notes in reversed order."
  10371. (cond
  10372. ((not org-reverse-note-order) nil)
  10373. ((eq t org-reverse-note-order) t)
  10374. ((not (listp org-reverse-note-order)) nil)
  10375. (t (catch 'exit
  10376. (let ((all org-reverse-note-order)
  10377. entry)
  10378. (while (setq entry (pop all))
  10379. (if (string-match (car entry) buffer-file-name)
  10380. (throw 'exit (cdr entry))))
  10381. nil)))))
  10382. (defvar org-refile-target-table nil
  10383. "The list of refile targets, created by `org-refile'.")
  10384. (defvar org-agenda-new-buffers nil
  10385. "Buffers created to visit agenda files.")
  10386. (defvar org-refile-cache nil
  10387. "Cache for refile targets.")
  10388. (defvar org-refile-markers nil
  10389. "All the markers used for caching refile locations.")
  10390. (defun org-refile-marker (pos)
  10391. "Get a new refile marker, but only if caching is in use."
  10392. (if (not org-refile-use-cache)
  10393. pos
  10394. (let ((m (make-marker)))
  10395. (move-marker m pos)
  10396. (push m org-refile-markers)
  10397. m)))
  10398. (defun org-refile-cache-clear ()
  10399. "Clear the refile cache and disable all the markers."
  10400. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10401. (setq org-refile-markers nil)
  10402. (setq org-refile-cache nil)
  10403. (message "Refile cache has been cleared"))
  10404. (defun org-refile-cache-check-set (set)
  10405. "Check if all the markers in the cache still have live buffers."
  10406. (let (marker)
  10407. (catch 'exit
  10408. (while (and set (setq marker (nth 3 (pop set))))
  10409. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10410. (when (and marker (null (marker-buffer marker)))
  10411. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10412. (sit-for 3)
  10413. (throw 'exit nil)))
  10414. t)))
  10415. (defun org-refile-cache-put (set &rest identifiers)
  10416. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10417. (let* ((key (sha1 (prin1-to-string identifiers)))
  10418. (entry (assoc key org-refile-cache)))
  10419. (if entry
  10420. (setcdr entry set)
  10421. (push (cons key set) org-refile-cache))))
  10422. (defun org-refile-cache-get (&rest identifiers)
  10423. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10424. (cond
  10425. ((not org-refile-cache) nil)
  10426. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10427. (t
  10428. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10429. org-refile-cache))))
  10430. (and set (org-refile-cache-check-set set) set)))))
  10431. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10432. "Produce a table with refile targets."
  10433. (let ((case-fold-search nil)
  10434. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10435. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10436. targets tgs txt re files f desc descre fast-path-p level pos0)
  10437. (message "Getting targets...")
  10438. (with-current-buffer (or default-buffer (current-buffer))
  10439. (while (setq entry (pop entries))
  10440. (setq files (car entry) desc (cdr entry))
  10441. (setq fast-path-p nil)
  10442. (cond
  10443. ((null files) (setq files (list (current-buffer))))
  10444. ((eq files 'org-agenda-files)
  10445. (setq files (org-agenda-files 'unrestricted)))
  10446. ((and (symbolp files) (fboundp files))
  10447. (setq files (funcall files)))
  10448. ((and (symbolp files) (boundp files))
  10449. (setq files (symbol-value files))))
  10450. (if (stringp files) (setq files (list files)))
  10451. (cond
  10452. ((eq (car desc) :tag)
  10453. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10454. ((eq (car desc) :todo)
  10455. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10456. ((eq (car desc) :regexp)
  10457. (setq descre (cdr desc)))
  10458. ((eq (car desc) :level)
  10459. (setq descre (concat "^\\*\\{" (number-to-string
  10460. (if org-odd-levels-only
  10461. (1- (* 2 (cdr desc)))
  10462. (cdr desc)))
  10463. "\\}[ \t]")))
  10464. ((eq (car desc) :maxlevel)
  10465. (setq fast-path-p t)
  10466. (setq descre (concat "^\\*\\{1," (number-to-string
  10467. (if org-odd-levels-only
  10468. (1- (* 2 (cdr desc)))
  10469. (cdr desc)))
  10470. "\\}[ \t]")))
  10471. (t (error "Bad refiling target description %s" desc)))
  10472. (while (setq f (pop files))
  10473. (with-current-buffer
  10474. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10475. (or
  10476. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10477. (progn
  10478. (if (bufferp f) (setq f (buffer-file-name
  10479. (buffer-base-buffer f))))
  10480. (setq f (and f (expand-file-name f)))
  10481. (if (eq org-refile-use-outline-path 'file)
  10482. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10483. (save-excursion
  10484. (save-restriction
  10485. (widen)
  10486. (goto-char (point-min))
  10487. (while (re-search-forward descre nil t)
  10488. (goto-char (setq pos0 (point-at-bol)))
  10489. (catch 'next
  10490. (when org-refile-target-verify-function
  10491. (save-match-data
  10492. (or (funcall org-refile-target-verify-function)
  10493. (throw 'next t))))
  10494. (when (and (looking-at org-complex-heading-regexp)
  10495. (not (member (match-string 4) excluded-entries))
  10496. (match-string 4))
  10497. (setq level (org-reduced-level
  10498. (- (match-end 1) (match-beginning 1)))
  10499. txt (org-link-display-format (match-string 4))
  10500. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10501. re (format org-complex-heading-regexp-format
  10502. (regexp-quote (match-string 4))))
  10503. (when org-refile-use-outline-path
  10504. (setq txt (mapconcat
  10505. 'org-protect-slash
  10506. (append
  10507. (if (eq org-refile-use-outline-path
  10508. 'file)
  10509. (list (file-name-nondirectory
  10510. (buffer-file-name
  10511. (buffer-base-buffer))))
  10512. (if (eq org-refile-use-outline-path
  10513. 'full-file-path)
  10514. (list (buffer-file-name
  10515. (buffer-base-buffer)))))
  10516. (org-get-outline-path fast-path-p
  10517. level txt)
  10518. (list txt))
  10519. "/")))
  10520. (push (list txt f re (org-refile-marker (point)))
  10521. tgs)))
  10522. (when (= (point) pos0)
  10523. ;; verification function has not moved point
  10524. (goto-char (point-at-eol))))))))
  10525. (when org-refile-use-cache
  10526. (org-refile-cache-put tgs (buffer-file-name) descre))
  10527. (setq targets (append tgs targets))))))
  10528. (message "Getting targets...done")
  10529. (nreverse targets)))
  10530. (defun org-protect-slash (s)
  10531. (while (string-match "/" s)
  10532. (setq s (replace-match "\\" t t s)))
  10533. s)
  10534. (defvar org-olpa (make-vector 20 nil))
  10535. (defun org-get-outline-path (&optional fastp level heading)
  10536. "Return the outline path to the current entry, as a list.
  10537. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10538. routine which makes outline path derivations for an entire file,
  10539. avoiding backtracing. Refile target collection makes use of that."
  10540. (if fastp
  10541. (progn
  10542. (if (> level 19)
  10543. (error "Outline path failure, more than 19 levels"))
  10544. (loop for i from level upto 19 do
  10545. (aset org-olpa i nil))
  10546. (prog1
  10547. (delq nil (append org-olpa nil))
  10548. (aset org-olpa level heading)))
  10549. (let (rtn case-fold-search)
  10550. (save-excursion
  10551. (save-restriction
  10552. (widen)
  10553. (while (org-up-heading-safe)
  10554. (when (looking-at org-complex-heading-regexp)
  10555. (push (org-trim
  10556. (replace-regexp-in-string
  10557. ;; Remove statistical/checkboxes cookies
  10558. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10559. (org-match-string-no-properties 4)))
  10560. rtn)))
  10561. rtn)))))
  10562. (defun org-format-outline-path (path &optional width prefix separator)
  10563. "Format the outline path PATH for display.
  10564. WIDTH is the maximum number of characters that is available.
  10565. PREFIX is a prefix to be included in the returned string,
  10566. such as the file name.
  10567. SEPARATOR is inserted between the different parts of the path,
  10568. the default is \"/\"."
  10569. (setq width (or width 79))
  10570. (if prefix (setq width (- width (length prefix))))
  10571. (if (not path)
  10572. (or prefix "")
  10573. (let* ((nsteps (length path))
  10574. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10575. (maxwidth (if (<= total-width width)
  10576. 10000 ;; everything fits
  10577. ;; we need to shorten the level headings
  10578. (/ (- width nsteps) nsteps)))
  10579. (org-odd-levels-only nil)
  10580. (n 0)
  10581. (total (1+ (length prefix))))
  10582. (setq maxwidth (max maxwidth 10))
  10583. (concat prefix
  10584. (if prefix (or separator "/"))
  10585. (mapconcat
  10586. (lambda (h)
  10587. (setq n (1+ n))
  10588. (if (and (= n nsteps) (< maxwidth 10000))
  10589. (setq maxwidth (- total-width total)))
  10590. (if (< (length h) maxwidth)
  10591. (progn (setq total (+ total (length h) 1)) h)
  10592. (setq h (substring h 0 (- maxwidth 2))
  10593. total (+ total maxwidth 1))
  10594. (if (string-match "[ \t]+\\'" h)
  10595. (setq h (substring h 0 (match-beginning 0))))
  10596. (setq h (concat h "..")))
  10597. (org-add-props h nil 'face
  10598. (nth (% (1- n) org-n-level-faces)
  10599. org-level-faces))
  10600. h)
  10601. path (or separator "/"))))))
  10602. (defun org-display-outline-path (&optional file current separator just-return-string)
  10603. "Display the current outline path in the echo area.
  10604. If FILE is non-nil, prepend the output with the file name.
  10605. If CURRENT is non-nil, append the current heading to the output.
  10606. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10607. the different parts of the path and defaults to \"/\".
  10608. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10609. (interactive "P")
  10610. (let* (case-fold-search
  10611. (bfn (buffer-file-name (buffer-base-buffer)))
  10612. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10613. res)
  10614. (if current (setq path (append path
  10615. (save-excursion
  10616. (org-back-to-heading t)
  10617. (if (looking-at org-complex-heading-regexp)
  10618. (list (match-string 4)))))))
  10619. (setq res
  10620. (org-format-outline-path
  10621. path
  10622. (1- (frame-width))
  10623. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10624. separator))
  10625. (if just-return-string
  10626. (org-no-properties res)
  10627. (org-unlogged-message "%s" res))))
  10628. (defvar org-refile-history nil
  10629. "History for refiling operations.")
  10630. (defvar org-after-refile-insert-hook nil
  10631. "Hook run after `org-refile' has inserted its stuff at the new location.
  10632. Note that this is still *before* the stuff will be removed from
  10633. the *old* location.")
  10634. (defvar org-capture-last-stored-marker)
  10635. (defvar org-refile-keep nil
  10636. "Non-nil means `org-refile' will copy instead of refile.")
  10637. (defun org-copy ()
  10638. "Like `org-refile', but copy."
  10639. (interactive)
  10640. (let ((org-refile-keep t))
  10641. (funcall 'org-refile nil nil nil "Copy")))
  10642. (defun org-refile (&optional arg default-buffer rfloc msg)
  10643. "Move the entry or entries at point to another heading.
  10644. The list of target headings is compiled using the information in
  10645. `org-refile-targets', which see.
  10646. At the target location, the entry is filed as a subitem of the
  10647. target heading. Depending on `org-reverse-note-order', the new
  10648. subitem will either be the first or the last subitem.
  10649. If there is an active region, all entries in that region will be
  10650. refiled. However, the region must fulfill the requirement that
  10651. the first heading sets the top-level of the moved text.
  10652. With prefix arg ARG, the command will only visit the target
  10653. location and not actually move anything.
  10654. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10655. refiling operation has put the subtree.
  10656. With a numeric prefix argument of `2', refile to the running clock.
  10657. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10658. being set to `t' and copy to the target location, don't move it.
  10659. Beware that keeping refiled entries may result in duplicated ID
  10660. properties.
  10661. RFLOC can be a refile location obtained in a different way.
  10662. MSG is a string to replace \"Refile\" in the default prompt with
  10663. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10664. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10665. If you are using target caching (see `org-refile-use-cache'), you
  10666. have to clear the target cache in order to find new targets.
  10667. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10668. prefix argument (`C-u C-u C-u C-c C-w')."
  10669. (interactive "P")
  10670. (if (member arg '(0 (64)))
  10671. (org-refile-cache-clear)
  10672. (let* ((actionmsg (cond (msg msg)
  10673. ((equal arg 3) "Refile (and keep)")
  10674. (t "Refile")))
  10675. (cbuf (current-buffer))
  10676. (regionp (org-region-active-p))
  10677. (region-start (and regionp (region-beginning)))
  10678. (region-end (and regionp (region-end)))
  10679. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10680. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10681. pos it nbuf file re level reversed)
  10682. (setq last-command nil)
  10683. (when regionp
  10684. (goto-char region-start)
  10685. (or (bolp) (goto-char (point-at-bol)))
  10686. (setq region-start (point))
  10687. (unless (or (org-kill-is-subtree-p
  10688. (buffer-substring region-start region-end))
  10689. (prog1 org-refile-active-region-within-subtree
  10690. (let ((s (point-at-eol)))
  10691. (org-toggle-heading)
  10692. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10693. (user-error "The region is not a (sequence of) subtree(s)")))
  10694. (if (equal arg '(16))
  10695. (org-refile-goto-last-stored)
  10696. (when (or
  10697. (and (equal arg 2)
  10698. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10699. (prog1
  10700. (setq it (list (or org-clock-heading "running clock")
  10701. (buffer-file-name
  10702. (marker-buffer org-clock-hd-marker))
  10703. ""
  10704. (marker-position org-clock-hd-marker)))
  10705. (setq arg nil)))
  10706. (setq it (or rfloc
  10707. (let (heading-text)
  10708. (save-excursion
  10709. (unless (and arg (listp arg))
  10710. (org-back-to-heading t)
  10711. (setq heading-text
  10712. (replace-regexp-in-string
  10713. org-bracket-link-regexp
  10714. "\\3"
  10715. (nth 4 (org-heading-components)))))
  10716. (org-refile-get-location
  10717. (cond ((and arg (listp arg)) "Goto")
  10718. (regionp (concat actionmsg " region to"))
  10719. (t (concat actionmsg " subtree \""
  10720. heading-text "\" to")))
  10721. default-buffer
  10722. (and (not (equal '(4) arg))
  10723. org-refile-allow-creating-parent-nodes)
  10724. arg))))))
  10725. (setq file (nth 1 it)
  10726. re (nth 2 it)
  10727. pos (nth 3 it))
  10728. (if (and (not arg)
  10729. pos
  10730. (equal (buffer-file-name) file)
  10731. (if regionp
  10732. (and (>= pos region-start)
  10733. (<= pos region-end))
  10734. (and (>= pos (point))
  10735. (< pos (save-excursion
  10736. (org-end-of-subtree t t))))))
  10737. (error "Cannot refile to position inside the tree or region"))
  10738. (setq nbuf (or (find-buffer-visiting file)
  10739. (find-file-noselect file)))
  10740. (if (and arg (not (equal arg 3)))
  10741. (progn
  10742. (org-pop-to-buffer-same-window nbuf)
  10743. (goto-char pos)
  10744. (org-show-context 'org-goto))
  10745. (if regionp
  10746. (progn
  10747. (org-kill-new (buffer-substring region-start region-end))
  10748. (org-save-markers-in-region region-start region-end))
  10749. (org-copy-subtree 1 nil t))
  10750. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10751. (find-file-noselect file)))
  10752. (setq reversed (org-notes-order-reversed-p))
  10753. (save-excursion
  10754. (save-restriction
  10755. (widen)
  10756. (if pos
  10757. (progn
  10758. (goto-char pos)
  10759. (looking-at org-outline-regexp)
  10760. (setq level (org-get-valid-level (funcall outline-level) 1))
  10761. (goto-char
  10762. (if reversed
  10763. (or (outline-next-heading) (point-max))
  10764. (or (save-excursion (org-get-next-sibling))
  10765. (org-end-of-subtree t t)
  10766. (point-max)))))
  10767. (setq level 1)
  10768. (if (not reversed)
  10769. (goto-char (point-max))
  10770. (goto-char (point-min))
  10771. (or (outline-next-heading) (goto-char (point-max)))))
  10772. (if (not (bolp)) (newline))
  10773. (org-paste-subtree level nil nil t)
  10774. (when org-log-refile
  10775. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10776. (unless (eq org-log-refile 'note)
  10777. (save-excursion (org-add-log-note))))
  10778. (and org-auto-align-tags
  10779. (let ((org-loop-over-headlines-in-active-region nil))
  10780. (org-set-tags nil t)))
  10781. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10782. :last-refile)))
  10783. (when bookmark-name
  10784. (with-demoted-errors
  10785. (bookmark-set bookmark-name))))
  10786. ;; If we are refiling for capture, make sure that the
  10787. ;; last-capture pointers point here
  10788. (when (org-bound-and-true-p org-refile-for-capture)
  10789. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10790. :last-capture-marker)))
  10791. (when bookmark-name
  10792. (with-demoted-errors
  10793. (bookmark-set bookmark-name))))
  10794. (move-marker org-capture-last-stored-marker (point)))
  10795. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10796. (run-hooks 'org-after-refile-insert-hook))))
  10797. (unless org-refile-keep
  10798. (if regionp
  10799. (delete-region (point) (+ (point) (- region-end region-start)))
  10800. (delete-region
  10801. (and (org-back-to-heading t) (point))
  10802. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10803. (when (featurep 'org-inlinetask)
  10804. (org-inlinetask-remove-END-maybe))
  10805. (setq org-markers-to-move nil)
  10806. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10807. (defun org-refile-goto-last-stored ()
  10808. "Go to the location where the last refile was stored."
  10809. (interactive)
  10810. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10811. (message "This is the location of the last refile"))
  10812. (defun org-refile--get-location (refloc tbl)
  10813. "When user refile to REFLOC, find the associated target in TBL.
  10814. Also check `org-refile-target-table'."
  10815. (car (delq
  10816. nil
  10817. (mapcar
  10818. (lambda (r) (or (assoc r tbl)
  10819. (assoc r org-refile-target-table)))
  10820. (list (replace-regexp-in-string "/$" "" refloc)
  10821. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10822. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10823. no-exclude)
  10824. "Prompt the user for a refile location, using PROMPT.
  10825. PROMPT should not be suffixed with a colon and a space, because
  10826. this function appends the default value from
  10827. `org-refile-history' automatically, if that is not empty.
  10828. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10829. this is used for the GOTO interface."
  10830. (let ((org-refile-targets org-refile-targets)
  10831. (org-refile-use-outline-path org-refile-use-outline-path)
  10832. excluded-entries)
  10833. (when (and (derived-mode-p 'org-mode)
  10834. (not org-refile-use-cache)
  10835. (not no-exclude))
  10836. (org-map-tree
  10837. (lambda()
  10838. (setq excluded-entries
  10839. (append excluded-entries (list (org-get-heading t t)))))))
  10840. (setq org-refile-target-table
  10841. (org-refile-get-targets default-buffer excluded-entries)))
  10842. (unless org-refile-target-table
  10843. (user-error "No refile targets"))
  10844. (let* ((cbuf (current-buffer))
  10845. (partial-completion-mode nil)
  10846. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10847. (cfunc (if (and org-refile-use-outline-path
  10848. org-outline-path-complete-in-steps)
  10849. 'org-olpath-completing-read
  10850. 'org-icompleting-read))
  10851. (extra (if org-refile-use-outline-path "/" ""))
  10852. (cbnex (concat (buffer-name) extra))
  10853. (filename (and cfn (expand-file-name cfn)))
  10854. (tbl (mapcar
  10855. (lambda (x)
  10856. (if (and (not (member org-refile-use-outline-path
  10857. '(file full-file-path)))
  10858. (not (equal filename (nth 1 x))))
  10859. (cons (concat (car x) extra " ("
  10860. (file-name-nondirectory (nth 1 x)) ")")
  10861. (cdr x))
  10862. (cons (concat (car x) extra) (cdr x))))
  10863. org-refile-target-table))
  10864. (completion-ignore-case t)
  10865. cdef
  10866. (prompt (concat prompt
  10867. (or (and (car org-refile-history)
  10868. (concat " (default " (car org-refile-history) ")"))
  10869. (and (assoc cbnex tbl) (setq cdef cbnex)
  10870. (concat " (default " cbnex ")"))) ": "))
  10871. pa answ parent-target child parent old-hist)
  10872. (setq old-hist org-refile-history)
  10873. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10874. nil 'org-refile-history (or cdef (car org-refile-history))))
  10875. (if (setq pa (org-refile--get-location answ tbl))
  10876. (progn
  10877. (org-refile-check-position pa)
  10878. (when (or (not org-refile-history)
  10879. (not (eq old-hist org-refile-history))
  10880. (not (equal (car pa) (car org-refile-history))))
  10881. (setq org-refile-history
  10882. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10883. org-refile-history
  10884. (cdr org-refile-history))))
  10885. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10886. (pop org-refile-history)))
  10887. pa)
  10888. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10889. (progn
  10890. (setq parent (match-string 1 answ)
  10891. child (match-string 2 answ))
  10892. (setq parent-target (org-refile--get-location parent tbl))
  10893. (when (and parent-target
  10894. (or (eq new-nodes t)
  10895. (and (eq new-nodes 'confirm)
  10896. (y-or-n-p (format "Create new node \"%s\"? "
  10897. child)))))
  10898. (org-refile-new-child parent-target child)))
  10899. (user-error "Invalid target location")))))
  10900. (declare-function org-string-nw-p "org-macs" (s))
  10901. (defun org-refile-check-position (refile-pointer)
  10902. "Check if the refile pointer matches the headline to which it points."
  10903. (let* ((file (nth 1 refile-pointer))
  10904. (re (nth 2 refile-pointer))
  10905. (pos (nth 3 refile-pointer))
  10906. buffer)
  10907. (if (and (not (markerp pos)) (not file))
  10908. (user-error "Please indicate a target file in the refile path")
  10909. (when (org-string-nw-p re)
  10910. (setq buffer (if (markerp pos)
  10911. (marker-buffer pos)
  10912. (or (find-buffer-visiting file)
  10913. (find-file-noselect file))))
  10914. (with-current-buffer buffer
  10915. (save-excursion
  10916. (save-restriction
  10917. (widen)
  10918. (goto-char pos)
  10919. (beginning-of-line 1)
  10920. (unless (org-looking-at-p re)
  10921. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10922. (defun org-refile-new-child (parent-target child)
  10923. "Use refile target PARENT-TARGET to add new CHILD below it."
  10924. (unless parent-target
  10925. (error "Cannot find parent for new node"))
  10926. (let ((file (nth 1 parent-target))
  10927. (pos (nth 3 parent-target))
  10928. level)
  10929. (with-current-buffer (or (find-buffer-visiting file)
  10930. (find-file-noselect file))
  10931. (save-excursion
  10932. (save-restriction
  10933. (widen)
  10934. (if pos
  10935. (goto-char pos)
  10936. (goto-char (point-max))
  10937. (if (not (bolp)) (newline)))
  10938. (when (looking-at org-outline-regexp)
  10939. (setq level (funcall outline-level))
  10940. (org-end-of-subtree t t))
  10941. (org-back-over-empty-lines)
  10942. (insert "\n" (make-string
  10943. (if pos (org-get-valid-level level 1) 1) ?*)
  10944. " " child "\n")
  10945. (beginning-of-line 0)
  10946. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10947. (defun org-olpath-completing-read (prompt collection &rest args)
  10948. "Read an outline path like a file name."
  10949. (let ((thetable collection)
  10950. (org-completion-use-ido nil) ; does not work with ido.
  10951. (org-completion-use-iswitchb nil)) ; or iswitchb
  10952. (apply
  10953. 'org-icompleting-read prompt
  10954. (lambda (string predicate &optional flag)
  10955. (let (rtn r f (l (length string)))
  10956. (cond
  10957. ((eq flag nil)
  10958. ;; try completion
  10959. (try-completion string thetable))
  10960. ((eq flag t)
  10961. ;; all-completions
  10962. (setq rtn (all-completions string thetable predicate))
  10963. (mapcar
  10964. (lambda (x)
  10965. (setq r (substring x l))
  10966. (if (string-match " ([^)]*)$" x)
  10967. (setq f (match-string 0 x))
  10968. (setq f ""))
  10969. (if (string-match "/" r)
  10970. (concat string (substring r 0 (match-end 0)) f)
  10971. x))
  10972. rtn))
  10973. ((eq flag 'lambda)
  10974. ;; exact match?
  10975. (assoc string thetable)))))
  10976. args)))
  10977. ;;;; Dynamic blocks
  10978. (defun org-find-dblock (name)
  10979. "Find the first dynamic block with name NAME in the buffer.
  10980. If not found, stay at current position and return nil."
  10981. (let ((case-fold-search t) pos)
  10982. (save-excursion
  10983. (goto-char (point-min))
  10984. (setq pos (and (re-search-forward
  10985. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10986. (match-beginning 0))))
  10987. (if pos (goto-char pos))
  10988. pos))
  10989. (defun org-create-dblock (plist)
  10990. "Create a dynamic block section, with parameters taken from PLIST.
  10991. PLIST must contain a :name entry which is used as the name of the block."
  10992. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10993. (end-of-line 1)
  10994. (newline))
  10995. (let ((col (current-column))
  10996. (name (plist-get plist :name)))
  10997. (insert "#+BEGIN: " name)
  10998. (while plist
  10999. (if (eq (car plist) :name)
  11000. (setq plist (cddr plist))
  11001. (insert " " (prin1-to-string (pop plist)))))
  11002. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11003. (beginning-of-line -2)))
  11004. (defun org-prepare-dblock ()
  11005. "Prepare dynamic block for refresh.
  11006. This empties the block, puts the cursor at the insert position and returns
  11007. the property list including an extra property :name with the block name."
  11008. (unless (looking-at org-dblock-start-re)
  11009. (user-error "Not at a dynamic block"))
  11010. (let* ((begdel (1+ (match-end 0)))
  11011. (name (org-no-properties (match-string 1)))
  11012. (params (append (list :name name)
  11013. (read (concat "(" (match-string 3) ")")))))
  11014. (save-excursion
  11015. (beginning-of-line 1)
  11016. (skip-chars-forward " \t")
  11017. (setq params (plist-put params :indentation-column (current-column))))
  11018. (unless (re-search-forward org-dblock-end-re nil t)
  11019. (error "Dynamic block not terminated"))
  11020. (setq params
  11021. (append params
  11022. (list :content (buffer-substring
  11023. begdel (match-beginning 0)))))
  11024. (delete-region begdel (match-beginning 0))
  11025. (goto-char begdel)
  11026. (open-line 1)
  11027. params))
  11028. (defun org-map-dblocks (&optional command)
  11029. "Apply COMMAND to all dynamic blocks in the current buffer.
  11030. If COMMAND is not given, use `org-update-dblock'."
  11031. (let ((cmd (or command 'org-update-dblock)))
  11032. (save-excursion
  11033. (goto-char (point-min))
  11034. (while (re-search-forward org-dblock-start-re nil t)
  11035. (goto-char (match-beginning 0))
  11036. (save-excursion
  11037. (condition-case nil
  11038. (funcall cmd)
  11039. (error (message "Error during update of dynamic block"))))
  11040. (unless (re-search-forward org-dblock-end-re nil t)
  11041. (error "Dynamic block not terminated"))))))
  11042. (defun org-dblock-update (&optional arg)
  11043. "User command for updating dynamic blocks.
  11044. Update the dynamic block at point. With prefix ARG, update all dynamic
  11045. blocks in the buffer."
  11046. (interactive "P")
  11047. (if arg
  11048. (org-update-all-dblocks)
  11049. (or (looking-at org-dblock-start-re)
  11050. (org-beginning-of-dblock))
  11051. (org-update-dblock)))
  11052. (defun org-update-dblock ()
  11053. "Update the dynamic block at point.
  11054. This means to empty the block, parse for parameters and then call
  11055. the correct writing function."
  11056. (interactive)
  11057. (save-excursion
  11058. (let* ((win (selected-window))
  11059. (pos (point))
  11060. (line (org-current-line))
  11061. (params (org-prepare-dblock))
  11062. (name (plist-get params :name))
  11063. (indent (plist-get params :indentation-column))
  11064. (cmd (intern (concat "org-dblock-write:" name))))
  11065. (message "Updating dynamic block `%s' at line %d..." name line)
  11066. (funcall cmd params)
  11067. (message "Updating dynamic block `%s' at line %d...done" name line)
  11068. (goto-char pos)
  11069. (when (and indent (> indent 0))
  11070. (setq indent (make-string indent ?\ ))
  11071. (save-excursion
  11072. (select-window win)
  11073. (org-beginning-of-dblock)
  11074. (forward-line 1)
  11075. (while (not (looking-at org-dblock-end-re))
  11076. (insert indent)
  11077. (beginning-of-line 2))
  11078. (when (looking-at org-dblock-end-re)
  11079. (and (looking-at "[ \t]+")
  11080. (replace-match ""))
  11081. (insert indent)))))))
  11082. (defun org-beginning-of-dblock ()
  11083. "Find the beginning of the dynamic block at point.
  11084. Error if there is no such block at point."
  11085. (let ((pos (point))
  11086. beg)
  11087. (end-of-line 1)
  11088. (if (and (re-search-backward org-dblock-start-re nil t)
  11089. (setq beg (match-beginning 0))
  11090. (re-search-forward org-dblock-end-re nil t)
  11091. (> (match-end 0) pos))
  11092. (goto-char beg)
  11093. (goto-char pos)
  11094. (error "Not in a dynamic block"))))
  11095. (defun org-update-all-dblocks ()
  11096. "Update all dynamic blocks in the buffer.
  11097. This function can be used in a hook."
  11098. (interactive)
  11099. (when (derived-mode-p 'org-mode)
  11100. (org-map-dblocks 'org-update-dblock)))
  11101. ;;;; Completion
  11102. (declare-function org-export-backend-name "org-export" (cl-x))
  11103. (declare-function org-export-backend-options "org-export" (cl-x))
  11104. (defun org-get-export-keywords ()
  11105. "Return a list of all currently understood export keywords.
  11106. Export keywords include options, block names, attributes and
  11107. keywords relative to each registered export back-end."
  11108. (let (keywords)
  11109. (dolist (backend
  11110. (org-bound-and-true-p org-export--registered-backends)
  11111. (delq nil keywords))
  11112. ;; Back-end name (for keywords, like #+LATEX:)
  11113. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11114. (dolist (option-entry (org-export-backend-options backend))
  11115. ;; Back-end options.
  11116. (push (nth 1 option-entry) keywords)))))
  11117. (defconst org-options-keywords
  11118. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11119. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11120. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11121. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11122. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11123. (defcustom org-structure-template-alist
  11124. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11125. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11126. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11127. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11128. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11129. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11130. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11131. ("L" "#+LaTeX: ")
  11132. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11133. ("H" "#+HTML: ")
  11134. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11135. ("A" "#+ASCII: ")
  11136. ("i" "#+INDEX: ?")
  11137. ("I" "#+INCLUDE: %file ?"))
  11138. "Structure completion elements.
  11139. This is a list of abbreviation keys and values. The value gets inserted
  11140. if you type `<' followed by the key and then press the completion key,
  11141. usually `TAB'. %file will be replaced by a file name after prompting
  11142. for the file using completion. The cursor will be placed at the position
  11143. of the `?` in the template.
  11144. There are two templates for each key, the first uses the original Org syntax,
  11145. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11146. the default when the /org-mtags.el/ module has been loaded. See also the
  11147. variable `org-mtags-prefer-muse-templates'."
  11148. :group 'org-completion
  11149. :type '(repeat
  11150. (list
  11151. (string :tag "Key")
  11152. (string :tag "Template")))
  11153. :version "25.1"
  11154. :package-version '(Org . "8.3"))
  11155. (defun org-try-structure-completion ()
  11156. "Try to complete a structure template before point.
  11157. This looks for strings like \"<e\" on an otherwise empty line and
  11158. expands them."
  11159. (let ((l (buffer-substring (point-at-bol) (point)))
  11160. a)
  11161. (when (and (looking-at "[ \t]*$")
  11162. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11163. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11164. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11165. (match-beginning 1)) a)
  11166. t)))
  11167. (defun org-complete-expand-structure-template (start cell)
  11168. "Expand a structure template."
  11169. (let ((rpl (nth 1 cell))
  11170. (ind ""))
  11171. (delete-region start (point))
  11172. (when (string-match "\\`[ \t]*#\\+" rpl)
  11173. (cond
  11174. ((bolp))
  11175. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11176. (setq ind (buffer-substring (point-at-bol) (point))))
  11177. (t (newline))))
  11178. (setq start (point))
  11179. (if (string-match "%file" rpl)
  11180. (setq rpl (replace-match
  11181. (concat
  11182. "\""
  11183. (save-match-data
  11184. (abbreviate-file-name (read-file-name "Include file: ")))
  11185. "\"")
  11186. t t rpl)))
  11187. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11188. (concat "\n" ind)))
  11189. (insert rpl)
  11190. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11191. ;;;; TODO, DEADLINE, Comments
  11192. (defun org-toggle-comment ()
  11193. "Change the COMMENT state of an entry."
  11194. (interactive)
  11195. (save-excursion
  11196. (org-back-to-heading)
  11197. (looking-at org-complex-heading-regexp)
  11198. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11199. (skip-chars-forward " \t")
  11200. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11201. (if (org-in-commented-heading-p t)
  11202. (delete-region (point)
  11203. (progn (search-forward " " (line-end-position) 'move)
  11204. (skip-chars-forward " \t")
  11205. (point)))
  11206. (insert org-comment-string)
  11207. (unless (eolp) (insert " ")))))
  11208. (defvar org-last-todo-state-is-todo nil
  11209. "This is non-nil when the last TODO state change led to a TODO state.
  11210. If the last change removed the TODO tag or switched to DONE, then
  11211. this is nil.")
  11212. (defvar org-setting-tags nil) ; dynamically skipped
  11213. (defvar org-todo-setup-filter-hook nil
  11214. "Hook for functions that pre-filter todo specs.
  11215. Each function takes a todo spec and returns either nil or the spec
  11216. transformed into canonical form." )
  11217. (defvar org-todo-get-default-hook nil
  11218. "Hook for functions that get a default item for todo.
  11219. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11220. nil or a string to be used for the todo mark." )
  11221. (defvar org-agenda-headline-snapshot-before-repeat)
  11222. (defun org-current-effective-time ()
  11223. "Return current time adjusted for `org-extend-today-until' variable."
  11224. (let* ((ct (org-current-time))
  11225. (dct (decode-time ct))
  11226. (ct1
  11227. (cond
  11228. (org-use-last-clock-out-time-as-effective-time
  11229. (or (org-clock-get-last-clock-out-time) ct))
  11230. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11231. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11232. (t ct))))
  11233. ct1))
  11234. (defun org-todo-yesterday (&optional arg)
  11235. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11236. (interactive "P")
  11237. (if (eq major-mode 'org-agenda-mode)
  11238. (apply 'org-agenda-todo-yesterday arg)
  11239. (let* ((org-use-effective-time t)
  11240. (hour (third (decode-time
  11241. (org-current-time))))
  11242. (org-extend-today-until (1+ hour)))
  11243. (org-todo arg))))
  11244. (defvar org-block-entry-blocking ""
  11245. "First entry preventing the TODO state change.")
  11246. (defun org-cancel-repeater ()
  11247. "Cancel a repeater by setting its numeric value to zero."
  11248. (interactive)
  11249. (save-excursion
  11250. (org-back-to-heading t)
  11251. (let ((bound1 (point))
  11252. (bound0 (save-excursion (outline-next-heading) (point))))
  11253. (when (re-search-forward
  11254. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11255. org-deadline-time-regexp "\\)\\|\\("
  11256. org-ts-regexp "\\)")
  11257. bound0 t)
  11258. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11259. (replace-match "0" t nil nil 1))))))
  11260. (defun org-todo (&optional arg)
  11261. "Change the TODO state of an item.
  11262. The state of an item is given by a keyword at the start of the heading,
  11263. like
  11264. *** TODO Write paper
  11265. *** DONE Call mom
  11266. The different keywords are specified in the variable `org-todo-keywords'.
  11267. By default the available states are \"TODO\" and \"DONE\".
  11268. So for this example: when the item starts with TODO, it is changed to DONE.
  11269. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11270. DONE are present, add TODO at the beginning of the heading.
  11271. With \\[universal-argument] prefix arg, use completion to determine the new \
  11272. state.
  11273. With numeric prefix arg, switch to that state.
  11274. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11275. keywords (nextset).
  11276. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11277. With a numeric prefix arg of 0, inhibit note taking for the change.
  11278. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11279. When called through ELisp, arg is also interpreted in the following way:
  11280. 'none -> empty state
  11281. \"\"(empty string) -> switch to empty state
  11282. 'done -> switch to DONE
  11283. 'nextset -> switch to the next set of keywords
  11284. 'previousset -> switch to the previous set of keywords
  11285. \"WAITING\" -> switch to the specified keyword, but only if it
  11286. really is a member of `org-todo-keywords'."
  11287. (interactive "P")
  11288. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11289. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11290. 'region-start-level 'region))
  11291. org-loop-over-headlines-in-active-region)
  11292. (org-map-entries
  11293. `(org-todo ,arg)
  11294. org-loop-over-headlines-in-active-region
  11295. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11296. (if (equal arg '(16)) (setq arg 'nextset))
  11297. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11298. (let ((org-blocker-hook org-blocker-hook)
  11299. commentp
  11300. case-fold-search)
  11301. (when (equal arg '(64))
  11302. (setq arg nil org-blocker-hook nil))
  11303. (when (and org-blocker-hook
  11304. (or org-inhibit-blocking
  11305. (org-entry-get nil "NOBLOCKING")))
  11306. (setq org-blocker-hook nil))
  11307. (save-excursion
  11308. (catch 'exit
  11309. (org-back-to-heading t)
  11310. (when (org-in-commented-heading-p t)
  11311. (org-toggle-comment)
  11312. (setq commentp t))
  11313. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11314. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11315. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11316. (let* ((match-data (match-data))
  11317. (startpos (point-at-bol))
  11318. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11319. (org-log-done org-log-done)
  11320. (org-log-repeat org-log-repeat)
  11321. (org-todo-log-states org-todo-log-states)
  11322. (org-inhibit-logging
  11323. (if (equal arg 0)
  11324. (progn (setq arg nil) 'note) org-inhibit-logging))
  11325. (this (match-string 1))
  11326. (hl-pos (match-beginning 0))
  11327. (head (org-get-todo-sequence-head this))
  11328. (ass (assoc head org-todo-kwd-alist))
  11329. (interpret (nth 1 ass))
  11330. (done-word (nth 3 ass))
  11331. (final-done-word (nth 4 ass))
  11332. (org-last-state (or this ""))
  11333. (completion-ignore-case t)
  11334. (member (member this org-todo-keywords-1))
  11335. (tail (cdr member))
  11336. (org-state (cond
  11337. ((and org-todo-key-trigger
  11338. (or (and (equal arg '(4))
  11339. (eq org-use-fast-todo-selection 'prefix))
  11340. (and (not arg) org-use-fast-todo-selection
  11341. (not (eq org-use-fast-todo-selection
  11342. 'prefix)))))
  11343. ;; Use fast selection
  11344. (org-fast-todo-selection))
  11345. ((and (equal arg '(4))
  11346. (or (not org-use-fast-todo-selection)
  11347. (not org-todo-key-trigger)))
  11348. ;; Read a state with completion
  11349. (org-icompleting-read
  11350. "State: " (mapcar 'list org-todo-keywords-1)
  11351. nil t))
  11352. ((eq arg 'right)
  11353. (if this
  11354. (if tail (car tail) nil)
  11355. (car org-todo-keywords-1)))
  11356. ((eq arg 'left)
  11357. (if (equal member org-todo-keywords-1)
  11358. nil
  11359. (if this
  11360. (nth (- (length org-todo-keywords-1)
  11361. (length tail) 2)
  11362. org-todo-keywords-1)
  11363. (org-last org-todo-keywords-1))))
  11364. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11365. (setq arg nil))) ; hack to fall back to cycling
  11366. (arg
  11367. ;; user or caller requests a specific state
  11368. (cond
  11369. ((equal arg "") nil)
  11370. ((eq arg 'none) nil)
  11371. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11372. ((eq arg 'nextset)
  11373. (or (car (cdr (member head org-todo-heads)))
  11374. (car org-todo-heads)))
  11375. ((eq arg 'previousset)
  11376. (let ((org-todo-heads (reverse org-todo-heads)))
  11377. (or (car (cdr (member head org-todo-heads)))
  11378. (car org-todo-heads))))
  11379. ((car (member arg org-todo-keywords-1)))
  11380. ((stringp arg)
  11381. (user-error "State `%s' not valid in this file" arg))
  11382. ((nth (1- (prefix-numeric-value arg))
  11383. org-todo-keywords-1))))
  11384. ((null member) (or head (car org-todo-keywords-1)))
  11385. ((equal this final-done-word) nil) ;; -> make empty
  11386. ((null tail) nil) ;; -> first entry
  11387. ((memq interpret '(type priority))
  11388. (if (eq this-command last-command)
  11389. (car tail)
  11390. (if (> (length tail) 0)
  11391. (or done-word (car org-done-keywords))
  11392. nil)))
  11393. (t
  11394. (car tail))))
  11395. (org-state (or
  11396. (run-hook-with-args-until-success
  11397. 'org-todo-get-default-hook org-state org-last-state)
  11398. org-state))
  11399. (next (if org-state (concat " " org-state " ") " "))
  11400. (change-plist (list :type 'todo-state-change :from this :to org-state
  11401. :position startpos))
  11402. dolog now-done-p)
  11403. (when org-blocker-hook
  11404. (setq org-last-todo-state-is-todo
  11405. (not (member this org-done-keywords)))
  11406. (unless (save-excursion
  11407. (save-match-data
  11408. (org-with-wide-buffer
  11409. (run-hook-with-args-until-failure
  11410. 'org-blocker-hook change-plist))))
  11411. (if (org-called-interactively-p 'interactive)
  11412. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11413. this org-state org-block-entry-blocking)
  11414. ;; fail silently
  11415. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11416. this org-state org-block-entry-blocking)
  11417. (throw 'exit nil))))
  11418. (store-match-data match-data)
  11419. (replace-match next t t)
  11420. (cond ((equal this org-state)
  11421. (message "TODO state was already %s" (org-trim next)))
  11422. ((pos-visible-in-window-p hl-pos)
  11423. (message "TODO state changed to %s" (org-trim next))))
  11424. (unless head
  11425. (setq head (org-get-todo-sequence-head org-state)
  11426. ass (assoc head org-todo-kwd-alist)
  11427. interpret (nth 1 ass)
  11428. done-word (nth 3 ass)
  11429. final-done-word (nth 4 ass)))
  11430. (when (memq arg '(nextset previousset))
  11431. (message "Keyword-Set %d/%d: %s"
  11432. (- (length org-todo-sets) -1
  11433. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11434. (length org-todo-sets)
  11435. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11436. (setq org-last-todo-state-is-todo
  11437. (not (member org-state org-done-keywords)))
  11438. (setq now-done-p (and (member org-state org-done-keywords)
  11439. (not (member this org-done-keywords))))
  11440. (and logging (org-local-logging logging))
  11441. (when (and (or org-todo-log-states org-log-done)
  11442. (not (eq org-inhibit-logging t))
  11443. (not (memq arg '(nextset previousset))))
  11444. ;; we need to look at recording a time and note
  11445. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11446. (nth 2 (assoc this org-todo-log-states))))
  11447. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11448. (setq dolog 'time))
  11449. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11450. (and org-state
  11451. (member org-state org-not-done-keywords)
  11452. (not (member this org-not-done-keywords))))
  11453. ;; This is now a todo state and was not one before
  11454. ;; If there was a CLOSED time stamp, get rid of it.
  11455. (org-add-planning-info nil nil 'closed))
  11456. (when (and now-done-p org-log-done)
  11457. ;; It is now done, and it was not done before
  11458. (org-add-planning-info 'closed (org-current-effective-time))
  11459. (if (and (not dolog) (eq 'note org-log-done))
  11460. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11461. (when (and org-state dolog)
  11462. ;; This is a non-nil state, and we need to log it
  11463. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11464. ;; Fixup tag positioning
  11465. (org-todo-trigger-tag-changes org-state)
  11466. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11467. (when org-provide-todo-statistics
  11468. (org-update-parent-todo-statistics))
  11469. (run-hooks 'org-after-todo-state-change-hook)
  11470. (if (and arg (not (member org-state org-done-keywords)))
  11471. (setq head (org-get-todo-sequence-head org-state)))
  11472. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11473. ;; Do we need to trigger a repeat?
  11474. (when now-done-p
  11475. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11476. ;; This is for the agenda, take a snapshot of the headline.
  11477. (save-match-data
  11478. (setq org-agenda-headline-snapshot-before-repeat
  11479. (org-get-heading))))
  11480. (org-auto-repeat-maybe org-state))
  11481. ;; Fixup cursor location if close to the keyword
  11482. (if (and (outline-on-heading-p)
  11483. (not (bolp))
  11484. (save-excursion (beginning-of-line 1)
  11485. (looking-at org-todo-line-regexp))
  11486. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11487. (progn
  11488. (goto-char (or (match-end 2) (match-end 1)))
  11489. (and (looking-at " ") (just-one-space))))
  11490. (when org-trigger-hook
  11491. (save-excursion
  11492. (run-hook-with-args 'org-trigger-hook change-plist)))
  11493. (when commentp (org-toggle-comment))))))))
  11494. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11495. "Block turning an entry into a TODO, using the hierarchy.
  11496. This checks whether the current task should be blocked from state
  11497. changes. Such blocking occurs when:
  11498. 1. The task has children which are not all in a completed state.
  11499. 2. A task has a parent with the property :ORDERED:, and there
  11500. are siblings prior to the current task with incomplete
  11501. status.
  11502. 3. The parent of the task is blocked because it has siblings that should
  11503. be done first, or is child of a block grandparent TODO entry."
  11504. (if (not org-enforce-todo-dependencies)
  11505. t ; if locally turned off don't block
  11506. (catch 'dont-block
  11507. ;; If this is not a todo state change, or if this entry is already DONE,
  11508. ;; do not block
  11509. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11510. (member (plist-get change-plist :from)
  11511. (cons 'done org-done-keywords))
  11512. (member (plist-get change-plist :to)
  11513. (cons 'todo org-not-done-keywords))
  11514. (not (plist-get change-plist :to)))
  11515. (throw 'dont-block t))
  11516. ;; If this task has children, and any are undone, it's blocked
  11517. (save-excursion
  11518. (org-back-to-heading t)
  11519. (let ((this-level (funcall outline-level)))
  11520. (outline-next-heading)
  11521. (let ((child-level (funcall outline-level)))
  11522. (while (and (not (eobp))
  11523. (> child-level this-level))
  11524. ;; this todo has children, check whether they are all
  11525. ;; completed
  11526. (if (and (not (org-entry-is-done-p))
  11527. (org-entry-is-todo-p))
  11528. (progn (setq org-block-entry-blocking (org-get-heading))
  11529. (throw 'dont-block nil)))
  11530. (outline-next-heading)
  11531. (setq child-level (funcall outline-level))))))
  11532. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11533. ;; any previous siblings are undone, it's blocked
  11534. (save-excursion
  11535. (org-back-to-heading t)
  11536. (let* ((pos (point))
  11537. (parent-pos (and (org-up-heading-safe) (point))))
  11538. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11539. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11540. (forward-line 1)
  11541. (re-search-forward org-not-done-heading-regexp pos t))
  11542. (setq org-block-entry-blocking (match-string 0))
  11543. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11544. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11545. (while t
  11546. (goto-char parent-pos)
  11547. (if (not (looking-at org-not-done-heading-regexp))
  11548. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11549. (setq pos (point))
  11550. (setq parent-pos (and (org-up-heading-safe) (point)))
  11551. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11552. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11553. (forward-line 1)
  11554. (re-search-forward org-not-done-heading-regexp pos t)
  11555. (setq org-block-entry-blocking (org-get-heading)))
  11556. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11557. (defcustom org-track-ordered-property-with-tag nil
  11558. "Should the ORDERED property also be shown as a tag?
  11559. The ORDERED property decides if an entry should require subtasks to be
  11560. completed in sequence. Since a property is not very visible, setting
  11561. this option means that toggling the ORDERED property with the command
  11562. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11563. not relevant for the behavior, but it makes things more visible.
  11564. Note that toggling the tag with tags commands will not change the property
  11565. and therefore not influence behavior!
  11566. This can be t, meaning the tag ORDERED should be used, It can also be a
  11567. string to select a different tag for this task."
  11568. :group 'org-todo
  11569. :type '(choice
  11570. (const :tag "No tracking" nil)
  11571. (const :tag "Track with ORDERED tag" t)
  11572. (string :tag "Use other tag")))
  11573. (defun org-toggle-ordered-property ()
  11574. "Toggle the ORDERED property of the current entry.
  11575. For better visibility, you can track the value of this property with a tag.
  11576. See variable `org-track-ordered-property-with-tag'."
  11577. (interactive)
  11578. (let* ((t1 org-track-ordered-property-with-tag)
  11579. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11580. (save-excursion
  11581. (org-back-to-heading)
  11582. (if (org-entry-get nil "ORDERED")
  11583. (progn
  11584. (org-delete-property "ORDERED")
  11585. (and tag (org-toggle-tag tag 'off))
  11586. (message "Subtasks can be completed in arbitrary order"))
  11587. (org-entry-put nil "ORDERED" "t")
  11588. (and tag (org-toggle-tag tag 'on))
  11589. (message "Subtasks must be completed in sequence")))))
  11590. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11591. (defun org-block-todo-from-checkboxes (change-plist)
  11592. "Block turning an entry into a TODO, using checkboxes.
  11593. This checks whether the current task should be blocked from state
  11594. changes because there are unchecked boxes in this entry."
  11595. (if (not org-enforce-todo-checkbox-dependencies)
  11596. t ; if locally turned off don't block
  11597. (catch 'dont-block
  11598. ;; If this is not a todo state change, or if this entry is already DONE,
  11599. ;; do not block
  11600. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11601. (member (plist-get change-plist :from)
  11602. (cons 'done org-done-keywords))
  11603. (member (plist-get change-plist :to)
  11604. (cons 'todo org-not-done-keywords))
  11605. (not (plist-get change-plist :to)))
  11606. (throw 'dont-block t))
  11607. ;; If this task has checkboxes that are not checked, it's blocked
  11608. (save-excursion
  11609. (org-back-to-heading t)
  11610. (let ((beg (point)) end)
  11611. (outline-next-heading)
  11612. (setq end (point))
  11613. (goto-char beg)
  11614. (if (org-list-search-forward
  11615. (concat (org-item-beginning-re)
  11616. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11617. "\\[[- ]\\]")
  11618. end t)
  11619. (progn
  11620. (if (boundp 'org-blocked-by-checkboxes)
  11621. (setq org-blocked-by-checkboxes t))
  11622. (throw 'dont-block nil)))))
  11623. t))) ; do not block
  11624. (defun org-entry-blocked-p ()
  11625. "Is the current entry blocked?"
  11626. (org-with-silent-modifications
  11627. (if (org-entry-get nil "NOBLOCKING")
  11628. nil ;; Never block this entry
  11629. (not (run-hook-with-args-until-failure
  11630. 'org-blocker-hook
  11631. (list :type 'todo-state-change
  11632. :position (point)
  11633. :from 'todo
  11634. :to 'done))))))
  11635. (defun org-update-statistics-cookies (all)
  11636. "Update the statistics cookie, either from TODO or from checkboxes.
  11637. This should be called with the cursor in a line with a statistics cookie."
  11638. (interactive "P")
  11639. (if all
  11640. (progn
  11641. (org-update-checkbox-count 'all)
  11642. (org-map-entries 'org-update-parent-todo-statistics))
  11643. (if (not (org-at-heading-p))
  11644. (org-update-checkbox-count)
  11645. (let ((pos (point-marker))
  11646. end l1 l2)
  11647. (ignore-errors (org-back-to-heading t))
  11648. (if (not (org-at-heading-p))
  11649. (org-update-checkbox-count)
  11650. (setq l1 (org-outline-level))
  11651. (setq end (save-excursion
  11652. (outline-next-heading)
  11653. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11654. (point)))
  11655. (if (and (save-excursion
  11656. (re-search-forward
  11657. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11658. (not (save-excursion (re-search-forward
  11659. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11660. (org-update-checkbox-count)
  11661. (if (and l2 (> l2 l1))
  11662. (progn
  11663. (goto-char end)
  11664. (org-update-parent-todo-statistics))
  11665. (goto-char pos)
  11666. (beginning-of-line 1)
  11667. (while (re-search-forward
  11668. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11669. (point-at-eol) t)
  11670. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11671. (goto-char pos)
  11672. (move-marker pos nil)))))
  11673. (defvar org-entry-property-inherited-from) ;; defined below
  11674. (defun org-update-parent-todo-statistics ()
  11675. "Update any statistics cookie in the parent of the current headline.
  11676. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11677. the entire subtree and this will travel up the hierarchy and update
  11678. statistics everywhere."
  11679. (let* ((prop (save-excursion (org-up-heading-safe)
  11680. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11681. (recursive (or (not org-hierarchical-todo-statistics)
  11682. (and prop (string-match "\\<recursive\\>" prop))))
  11683. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11684. 0))
  11685. (first t)
  11686. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11687. level ltoggle l1 new ndel
  11688. (cnt-all 0) (cnt-done 0) is-percent kwd
  11689. checkbox-beg ov ovs ove cookie-present)
  11690. (catch 'exit
  11691. (save-excursion
  11692. (beginning-of-line 1)
  11693. (setq ltoggle (funcall outline-level))
  11694. ;; Three situations are to consider:
  11695. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11696. ;; to the top-level ancestor on the headline;
  11697. ;; 2. If parent has "recursive" property, repeat up to the
  11698. ;; headline setting that property, taking inheritance into
  11699. ;; account;
  11700. ;; 3. Else, move up to direct parent and proceed only once.
  11701. (while (and (setq level (org-up-heading-safe))
  11702. (or recursive first)
  11703. (>= (point) lim))
  11704. (setq first nil cookie-present nil)
  11705. (unless (and level
  11706. (not (string-match
  11707. "\\<checkbox\\>"
  11708. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11709. "")))))
  11710. (throw 'exit nil))
  11711. (while (re-search-forward box-re (point-at-eol) t)
  11712. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11713. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11714. (save-match-data
  11715. (unless (outline-next-heading) (throw 'exit nil))
  11716. (while (and (looking-at org-complex-heading-regexp)
  11717. (> (setq l1 (length (match-string 1))) level))
  11718. (setq kwd (and (or recursive (= l1 ltoggle))
  11719. (match-string 2)))
  11720. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11721. (and (eq org-provide-todo-statistics t)
  11722. (or (member kwd org-done-keywords)))
  11723. (and (listp org-provide-todo-statistics)
  11724. (stringp (car org-provide-todo-statistics))
  11725. (or (member kwd org-provide-todo-statistics)
  11726. (member kwd org-done-keywords)))
  11727. (and (listp org-provide-todo-statistics)
  11728. (listp (car org-provide-todo-statistics))
  11729. (or (member kwd (car org-provide-todo-statistics))
  11730. (and (member kwd org-done-keywords)
  11731. (member kwd (cadr org-provide-todo-statistics))))))
  11732. (setq cnt-all (1+ cnt-all))
  11733. (if (eq org-provide-todo-statistics t)
  11734. (and kwd (setq cnt-all (1+ cnt-all)))))
  11735. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11736. (member kwd org-done-keywords))
  11737. (and (listp org-provide-todo-statistics)
  11738. (listp (car org-provide-todo-statistics))
  11739. (member kwd org-done-keywords)
  11740. (member kwd (cadr org-provide-todo-statistics)))
  11741. (and (listp org-provide-todo-statistics)
  11742. (stringp (car org-provide-todo-statistics))
  11743. (member kwd org-done-keywords)))
  11744. (setq cnt-done (1+ cnt-done)))
  11745. (outline-next-heading)))
  11746. (setq new
  11747. (if is-percent
  11748. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11749. (format "[%d/%d]" cnt-done cnt-all))
  11750. ndel (- (match-end 0) checkbox-beg))
  11751. ;; handle overlays when updating cookie from column view
  11752. (when (setq ov (car (overlays-at checkbox-beg)))
  11753. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11754. (delete-overlay ov))
  11755. (goto-char checkbox-beg)
  11756. (insert new)
  11757. (delete-region (point) (+ (point) ndel))
  11758. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11759. (when ov (move-overlay ov ovs ove)))
  11760. (when cookie-present
  11761. (run-hook-with-args 'org-after-todo-statistics-hook
  11762. cnt-done (- cnt-all cnt-done))))))
  11763. (run-hooks 'org-todo-statistics-hook)))
  11764. (defvar org-after-todo-statistics-hook nil
  11765. "Hook that is called after a TODO statistics cookie has been updated.
  11766. Each function is called with two arguments: the number of not-done entries
  11767. and the number of done entries.
  11768. For example, the following function, when added to this hook, will switch
  11769. an entry to DONE when all children are done, and back to TODO when new
  11770. entries are set to a TODO status. Note that this hook is only called
  11771. when there is a statistics cookie in the headline!
  11772. (defun org-summary-todo (n-done n-not-done)
  11773. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11774. (let (org-log-done org-log-states) ; turn off logging
  11775. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11776. ")
  11777. (defvar org-todo-statistics-hook nil
  11778. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11779. This hook runs even if there is no statistics cookie present, in which case
  11780. `org-after-todo-statistics-hook' would not run.")
  11781. (defun org-todo-trigger-tag-changes (state)
  11782. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11783. (let ((l org-todo-state-tags-triggers)
  11784. changes)
  11785. (when (or (not state) (equal state ""))
  11786. (setq changes (append changes (cdr (assoc "" l)))))
  11787. (when (and (stringp state) (> (length state) 0))
  11788. (setq changes (append changes (cdr (assoc state l)))))
  11789. (when (member state org-not-done-keywords)
  11790. (setq changes (append changes (cdr (assoc 'todo l)))))
  11791. (when (member state org-done-keywords)
  11792. (setq changes (append changes (cdr (assoc 'done l)))))
  11793. (dolist (c changes)
  11794. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11795. (defun org-local-logging (value)
  11796. "Get logging settings from a property VALUE."
  11797. (let* (words w a)
  11798. ;; directly set the variables, they are already local.
  11799. (setq org-log-done nil
  11800. org-log-repeat nil
  11801. org-todo-log-states nil)
  11802. (setq words (org-split-string value))
  11803. (while (setq w (pop words))
  11804. (cond
  11805. ((setq a (assoc w org-startup-options))
  11806. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11807. (set (nth 1 a) (nth 2 a))))
  11808. ((setq a (org-extract-log-state-settings w))
  11809. (and (member (car a) org-todo-keywords-1)
  11810. (push a org-todo-log-states)))))))
  11811. (defun org-get-todo-sequence-head (kwd)
  11812. "Return the head of the TODO sequence to which KWD belongs.
  11813. If KWD is not set, check if there is a text property remembering the
  11814. right sequence."
  11815. (let (p)
  11816. (cond
  11817. ((not kwd)
  11818. (or (get-text-property (point-at-bol) 'org-todo-head)
  11819. (progn
  11820. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11821. nil (point-at-eol)))
  11822. (get-text-property p 'org-todo-head))))
  11823. ((not (member kwd org-todo-keywords-1))
  11824. (car org-todo-keywords-1))
  11825. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11826. (defun org-fast-todo-selection ()
  11827. "Fast TODO keyword selection with single keys.
  11828. Returns the new TODO keyword, or nil if no state change should occur."
  11829. (let* ((fulltable org-todo-key-alist)
  11830. (done-keywords org-done-keywords) ;; needed for the faces.
  11831. (maxlen (apply 'max (mapcar
  11832. (lambda (x)
  11833. (if (stringp (car x)) (string-width (car x)) 0))
  11834. fulltable)))
  11835. (expert nil)
  11836. (fwidth (+ maxlen 3 1 3))
  11837. (ncol (/ (- (window-width) 4) fwidth))
  11838. tg cnt e c tbl
  11839. groups ingroup)
  11840. (save-excursion
  11841. (save-window-excursion
  11842. (if expert
  11843. (set-buffer (get-buffer-create " *Org todo*"))
  11844. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11845. (erase-buffer)
  11846. (org-set-local 'org-done-keywords done-keywords)
  11847. (setq tbl fulltable cnt 0)
  11848. (while (setq e (pop tbl))
  11849. (cond
  11850. ((equal e '(:startgroup))
  11851. (push '() groups) (setq ingroup t)
  11852. (when (not (= cnt 0))
  11853. (setq cnt 0)
  11854. (insert "\n"))
  11855. (insert "{ "))
  11856. ((equal e '(:endgroup))
  11857. (setq ingroup nil cnt 0)
  11858. (insert "}\n"))
  11859. ((equal e '(:newline))
  11860. (when (not (= cnt 0))
  11861. (setq cnt 0)
  11862. (insert "\n")
  11863. (setq e (car tbl))
  11864. (while (equal (car tbl) '(:newline))
  11865. (insert "\n")
  11866. (setq tbl (cdr tbl)))))
  11867. (t
  11868. (setq tg (car e) c (cdr e))
  11869. (if ingroup (push tg (car groups)))
  11870. (setq tg (org-add-props tg nil 'face
  11871. (org-get-todo-face tg)))
  11872. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11873. (insert "[" c "] " tg (make-string
  11874. (- fwidth 4 (length tg)) ?\ ))
  11875. (when (= (setq cnt (1+ cnt)) ncol)
  11876. (insert "\n")
  11877. (if ingroup (insert " "))
  11878. (setq cnt 0)))))
  11879. (insert "\n")
  11880. (goto-char (point-min))
  11881. (if (not expert) (org-fit-window-to-buffer))
  11882. (message "[a-z..]:Set [SPC]:clear")
  11883. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11884. (cond
  11885. ((or (= c ?\C-g)
  11886. (and (= c ?q) (not (rassoc c fulltable))))
  11887. (setq quit-flag t))
  11888. ((= c ?\ ) nil)
  11889. ((setq e (rassoc c fulltable) tg (car e))
  11890. tg)
  11891. (t (setq quit-flag t)))))))
  11892. (defun org-entry-is-todo-p ()
  11893. (member (org-get-todo-state) org-not-done-keywords))
  11894. (defun org-entry-is-done-p ()
  11895. (member (org-get-todo-state) org-done-keywords))
  11896. (defun org-get-todo-state ()
  11897. "Return the TODO keyword of the current subtree."
  11898. (save-excursion
  11899. (org-back-to-heading t)
  11900. (and (looking-at org-todo-line-regexp)
  11901. (match-end 2)
  11902. (match-string 2))))
  11903. (defun org-at-date-range-p (&optional inactive-ok)
  11904. "Is the cursor inside a date range?"
  11905. (interactive)
  11906. (save-excursion
  11907. (catch 'exit
  11908. (let ((pos (point)))
  11909. (skip-chars-backward "^[<\r\n")
  11910. (skip-chars-backward "<[")
  11911. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11912. (>= (match-end 0) pos)
  11913. (throw 'exit t))
  11914. (skip-chars-backward "^<[\r\n")
  11915. (skip-chars-backward "<[")
  11916. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11917. (>= (match-end 0) pos)
  11918. (throw 'exit t)))
  11919. nil)))
  11920. (defun org-get-repeat (&optional tagline)
  11921. "Check if there is a deadline/schedule with repeater in this entry."
  11922. (save-match-data
  11923. (save-excursion
  11924. (org-back-to-heading t)
  11925. (and (re-search-forward (if tagline
  11926. (concat tagline "\\s-*" org-repeat-re)
  11927. org-repeat-re)
  11928. (org-entry-end-position) t)
  11929. (match-string-no-properties 1)))))
  11930. (defvar org-last-changed-timestamp)
  11931. (defvar org-last-inserted-timestamp)
  11932. (defvar org-log-post-message)
  11933. (defvar org-log-note-purpose)
  11934. (defvar org-log-note-how nil)
  11935. (defvar org-log-note-extra)
  11936. (defun org-auto-repeat-maybe (done-word)
  11937. "Check if the current headline contains a repeated deadline/schedule.
  11938. If yes, set TODO state back to what it was and change the base date
  11939. of repeating deadline/scheduled time stamps to new date.
  11940. This function is run automatically after each state change to a DONE state."
  11941. ;; last-state is dynamically scoped into this function
  11942. (let* ((repeat (org-get-repeat))
  11943. (aa (assoc org-last-state org-todo-kwd-alist))
  11944. (interpret (nth 1 aa))
  11945. (head (nth 2 aa))
  11946. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11947. (msg "Entry repeats: ")
  11948. (org-log-done nil)
  11949. (org-todo-log-states nil)
  11950. re type n what ts time to-state)
  11951. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11952. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11953. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11954. org-todo-repeat-to-state))
  11955. (unless (and to-state (member to-state org-todo-keywords-1))
  11956. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11957. (org-todo to-state)
  11958. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11959. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11960. (org-time-stamp-format t t))))
  11961. (when org-log-repeat
  11962. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11963. (memq 'org-add-log-note post-command-hook))
  11964. ;; OK, we are already setup for some record
  11965. (if (eq org-log-repeat 'note)
  11966. ;; make sure we take a note, not only a time stamp
  11967. (setq org-log-note-how 'note))
  11968. ;; Set up for taking a record
  11969. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11970. org-last-state
  11971. 'findpos org-log-repeat)))
  11972. (org-back-to-heading t)
  11973. (org-add-planning-info nil nil 'closed)
  11974. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11975. org-deadline-time-regexp "\\)\\|\\("
  11976. org-ts-regexp "\\)"))
  11977. (while (re-search-forward
  11978. re (save-excursion (outline-next-heading) (point)) t)
  11979. (setq type (if (match-end 1) org-scheduled-string
  11980. (if (match-end 3) org-deadline-string "Plain:"))
  11981. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11982. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11983. (setq n (string-to-number (match-string 2 ts))
  11984. what (match-string 3 ts))
  11985. (if (equal what "w") (setq n (* n 7) what "d"))
  11986. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11987. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11988. ;; Preparation, see if we need to modify the start date for the change
  11989. (when (match-end 1)
  11990. (setq time (save-match-data (org-time-string-to-time ts)))
  11991. (cond
  11992. ((equal (match-string 1 ts) ".")
  11993. ;; Shift starting date to today
  11994. (org-timestamp-change
  11995. (- (org-today) (time-to-days time))
  11996. 'day))
  11997. ((equal (match-string 1 ts) "+")
  11998. (let ((nshiftmax 10) (nshift 0))
  11999. (while (or (= nshift 0)
  12000. (<= (time-to-days time)
  12001. (time-to-days (current-time))))
  12002. (when (= (incf nshift) nshiftmax)
  12003. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12004. (user-error "Abort")))
  12005. (org-timestamp-change n (cdr (assoc what whata)))
  12006. (org-at-timestamp-p t)
  12007. (setq ts (match-string 1))
  12008. (setq time (save-match-data (org-time-string-to-time ts)))))
  12009. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12010. ;; rematch, so that we have everything in place for the real shift
  12011. (org-at-timestamp-p t)
  12012. (setq ts (match-string 1))
  12013. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12014. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12015. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12016. (setq org-log-post-message msg)
  12017. (message "%s" msg))))
  12018. (defun org-show-todo-tree (arg)
  12019. "Make a compact tree which shows all headlines marked with TODO.
  12020. The tree will show the lines where the regexp matches, and all higher
  12021. headlines above the match.
  12022. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12023. With a numeric prefix N, construct a sparse tree for the Nth element
  12024. of `org-todo-keywords-1'."
  12025. (interactive "P")
  12026. (let ((case-fold-search nil)
  12027. (kwd-re
  12028. (cond ((null arg) org-not-done-regexp)
  12029. ((equal arg '(4))
  12030. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12031. (mapcar 'list org-todo-keywords-1))))
  12032. (concat "\\("
  12033. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12034. "\\)\\>")))
  12035. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12036. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12037. org-todo-keywords-1)))
  12038. (t (user-error "Invalid prefix argument: %s" arg)))))
  12039. (message "%d TODO entries found"
  12040. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12041. (defun org-deadline (arg &optional time)
  12042. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12043. With one universal prefix argument, remove any deadline from the item.
  12044. With two universal prefix arguments, prompt for a warning delay.
  12045. With argument TIME, set the deadline at the corresponding date. TIME
  12046. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12047. (interactive "P")
  12048. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12049. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12050. 'region-start-level 'region))
  12051. org-loop-over-headlines-in-active-region)
  12052. (org-map-entries
  12053. `(org-deadline ',arg ,time)
  12054. org-loop-over-headlines-in-active-region
  12055. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12056. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12057. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12058. (repeater (and old-date
  12059. (string-match
  12060. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12061. old-date)
  12062. (match-string 1 old-date))))
  12063. (cond
  12064. ((equal arg '(4))
  12065. (when (and old-date org-log-redeadline)
  12066. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12067. org-log-redeadline))
  12068. (org-remove-timestamp-with-keyword org-deadline-string)
  12069. (message "Item no longer has a deadline."))
  12070. ((equal arg '(16))
  12071. (save-excursion
  12072. (org-back-to-heading t)
  12073. (if (re-search-forward
  12074. org-deadline-time-regexp
  12075. (save-excursion (outline-next-heading) (point)) t)
  12076. (let* ((rpl0 (match-string 1))
  12077. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12078. (replace-match
  12079. (concat org-deadline-string
  12080. " <" rpl
  12081. (format " -%dd"
  12082. (abs
  12083. (- (time-to-days
  12084. (save-match-data
  12085. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12086. (time-to-days old-date-time))))
  12087. ">") t t))
  12088. (user-error "No deadline information to update"))))
  12089. (t
  12090. (org-add-planning-info 'deadline time 'closed)
  12091. (when (and old-date org-log-redeadline
  12092. (not (equal old-date
  12093. (substring org-last-inserted-timestamp 1 -1))))
  12094. (org-add-log-setup 'redeadline nil old-date 'findpos
  12095. org-log-redeadline))
  12096. (when repeater
  12097. (save-excursion
  12098. (org-back-to-heading t)
  12099. (when (re-search-forward (concat org-deadline-string " "
  12100. org-last-inserted-timestamp)
  12101. (save-excursion
  12102. (outline-next-heading) (point)) t)
  12103. (goto-char (1- (match-end 0)))
  12104. (insert " " repeater)
  12105. (setq org-last-inserted-timestamp
  12106. (concat (substring org-last-inserted-timestamp 0 -1)
  12107. " " repeater
  12108. (substring org-last-inserted-timestamp -1))))))
  12109. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12110. (defun org-schedule (arg &optional time)
  12111. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12112. With one universal prefix argument, remove any scheduling date from the item.
  12113. With two universal prefix arguments, prompt for a delay cookie.
  12114. With argument TIME, scheduled at the corresponding date. TIME can
  12115. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12116. (interactive "P")
  12117. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12118. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12119. 'region-start-level 'region))
  12120. org-loop-over-headlines-in-active-region)
  12121. (org-map-entries
  12122. `(org-schedule ',arg ,time)
  12123. org-loop-over-headlines-in-active-region
  12124. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12125. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12126. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12127. (repeater (and old-date
  12128. (string-match
  12129. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12130. old-date)
  12131. (match-string 1 old-date))))
  12132. (cond
  12133. ((equal arg '(4))
  12134. (progn
  12135. (when (and old-date org-log-reschedule)
  12136. (org-add-log-setup 'delschedule nil old-date 'findpos
  12137. org-log-reschedule))
  12138. (org-remove-timestamp-with-keyword org-scheduled-string)
  12139. (message "Item is no longer scheduled.")))
  12140. ((equal arg '(16))
  12141. (save-excursion
  12142. (org-back-to-heading t)
  12143. (if (re-search-forward
  12144. org-scheduled-time-regexp
  12145. (save-excursion (outline-next-heading) (point)) t)
  12146. (let* ((rpl0 (match-string 1))
  12147. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12148. (replace-match
  12149. (concat org-scheduled-string
  12150. " <" rpl
  12151. (format " -%dd"
  12152. (abs
  12153. (- (time-to-days
  12154. (save-match-data
  12155. (org-read-date nil t nil "Delay until" old-date-time)))
  12156. (time-to-days old-date-time))))
  12157. ">") t t))
  12158. (user-error "No scheduled information to update"))))
  12159. (t
  12160. (org-add-planning-info 'scheduled time 'closed)
  12161. (when (and old-date org-log-reschedule
  12162. (not (equal old-date
  12163. (substring org-last-inserted-timestamp 1 -1))))
  12164. (org-add-log-setup 'reschedule nil old-date 'findpos
  12165. org-log-reschedule))
  12166. (when repeater
  12167. (save-excursion
  12168. (org-back-to-heading t)
  12169. (when (re-search-forward (concat org-scheduled-string " "
  12170. org-last-inserted-timestamp)
  12171. (save-excursion
  12172. (outline-next-heading) (point)) t)
  12173. (goto-char (1- (match-end 0)))
  12174. (insert " " repeater)
  12175. (setq org-last-inserted-timestamp
  12176. (concat (substring org-last-inserted-timestamp 0 -1)
  12177. " " repeater
  12178. (substring org-last-inserted-timestamp -1))))))
  12179. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12180. (defun org-get-scheduled-time (pom &optional inherit)
  12181. "Get the scheduled time as a time tuple, of a format suitable
  12182. for calling org-schedule with, or if there is no scheduling,
  12183. returns nil."
  12184. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12185. (when time
  12186. (apply 'encode-time (org-parse-time-string time)))))
  12187. (defun org-get-deadline-time (pom &optional inherit)
  12188. "Get the deadline as a time tuple, of a format suitable for
  12189. calling org-deadline with, or if there is no scheduling, returns
  12190. nil."
  12191. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12192. (when time
  12193. (apply 'encode-time (org-parse-time-string time)))))
  12194. (defun org-remove-timestamp-with-keyword (keyword)
  12195. "Remove all time stamps with KEYWORD in the current entry."
  12196. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12197. beg)
  12198. (save-excursion
  12199. (org-back-to-heading t)
  12200. (setq beg (point))
  12201. (outline-next-heading)
  12202. (while (re-search-backward re beg t)
  12203. (replace-match "")
  12204. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12205. (equal (char-before) ?\ ))
  12206. (backward-delete-char 1)
  12207. (if (string-match "^[ \t]*$" (buffer-substring
  12208. (point-at-bol) (point-at-eol)))
  12209. (delete-region (point-at-bol)
  12210. (min (point-max) (1+ (point-at-eol))))))))))
  12211. (defvar org-time-was-given) ; dynamically scoped parameter
  12212. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12213. (defun org-at-planning-p ()
  12214. "Non-nil when point is on a planning info line."
  12215. ;; This is as accurate and faster than `org-element-at-point' since
  12216. ;; planning info location is fixed in the section.
  12217. (org-with-wide-buffer
  12218. (beginning-of-line)
  12219. (and (org-looking-at-p org-planning-line-re)
  12220. (eq (point)
  12221. (ignore-errors
  12222. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12223. (org-back-to-heading t)
  12224. (org-with-limited-levels (org-back-to-heading t)))
  12225. (line-beginning-position 2))))))
  12226. (defun org-add-planning-info (what &optional time &rest remove)
  12227. "Insert new timestamp with keyword in the planning line.
  12228. WHAT indicates what kind of time stamp to add. It is a symbol
  12229. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12230. the time to use. If none is given, the user is prompted for
  12231. a date. REMOVE indicates what kind of entries to remove. An old
  12232. WHAT entry will also be removed."
  12233. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12234. (catch 'exit
  12235. (when (and (memq what '(scheduled deadline))
  12236. (or (not time)
  12237. (and (stringp time)
  12238. (string-match "^[-+]+[0-9]" time))))
  12239. ;; Try to get a default date/time from existing timestamp
  12240. (save-excursion
  12241. (org-back-to-heading t)
  12242. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12243. (when (re-search-forward (if (eq what 'scheduled)
  12244. org-scheduled-time-regexp
  12245. org-deadline-time-regexp)
  12246. end t)
  12247. (setq ts (match-string 1)
  12248. default-time (apply 'encode-time (org-parse-time-string ts))
  12249. default-input (and ts (org-get-compact-tod ts)))))))
  12250. (when what
  12251. (setq time
  12252. (if (stringp time)
  12253. ;; This is a string (relative or absolute), set
  12254. ;; proper date.
  12255. (apply #'encode-time
  12256. (org-read-date-analyze
  12257. time default-time (decode-time default-time)))
  12258. ;; If necessary, get the time from the user
  12259. (or time (org-read-date nil 'to-time nil nil
  12260. default-time default-input)))))
  12261. (org-with-wide-buffer
  12262. (org-back-to-heading t)
  12263. (forward-line)
  12264. (unless (bolp) (insert "\n"))
  12265. (cond ((org-looking-at-p org-planning-line-re)
  12266. ;; Move to current indentation.
  12267. (skip-chars-forward " \t")
  12268. ;; Check if we have to remove something.
  12269. (dolist (type (if what (cons what remove) remove))
  12270. (when (save-excursion
  12271. (re-search-forward
  12272. (case type
  12273. (closed org-closed-time-regexp)
  12274. (deadline org-deadline-time-regexp)
  12275. (scheduled org-scheduled-time-regexp)
  12276. (otherwise (error "Invalid planning type: %s" type)))
  12277. (line-end-position) t))
  12278. (replace-match "")
  12279. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12280. (when (and (not what) (eq type 'closed))
  12281. (save-excursion
  12282. (beginning-of-line)
  12283. (if (looking-at "[ \t]*$")
  12284. (delete-region (point) (1+ (point-at-eol)))))))
  12285. ;; Remove leading white spaces.
  12286. (when (and (not (bolp)) (looking-at "[ \t]+")) (replace-match ""))))
  12287. ((not what) (throw 'exit nil)) ; Nothing to do.
  12288. (t (insert-before-markers "\n")
  12289. (backward-char 1)
  12290. (when org-adapt-indentation
  12291. (org-indent-to-column (1+ (org-outline-level))))))
  12292. (when what
  12293. ;; Insert planning keyword.
  12294. (insert (case what
  12295. (closed org-closed-string)
  12296. (deadline org-deadline-string)
  12297. (scheduled org-scheduled-string)
  12298. (otherwise (error "Invalid planning type: %s" what)))
  12299. " ")
  12300. ;; Insert associated timestamp.
  12301. (let ((ts (org-insert-time-stamp
  12302. time
  12303. (or org-time-was-given
  12304. (and (eq what 'closed) org-log-done-with-time))
  12305. (eq what 'closed)
  12306. nil nil (list org-end-time-was-given))))
  12307. (unless (eolp) (insert " "))
  12308. ts))))))
  12309. (defvar org-log-note-marker (make-marker))
  12310. (defvar org-log-note-purpose nil)
  12311. (defvar org-log-note-state nil)
  12312. (defvar org-log-note-previous-state nil)
  12313. (defvar org-log-note-extra nil)
  12314. (defvar org-log-note-window-configuration nil)
  12315. (defvar org-log-note-return-to (make-marker))
  12316. (defvar org-log-note-effective-time nil
  12317. "Remembered current time so that dynamically scoped
  12318. `org-extend-today-until' affects tha timestamps in state change
  12319. log")
  12320. (defvar org-log-post-message nil
  12321. "Message to be displayed after a log note has been stored.
  12322. The auto-repeater uses this.")
  12323. (defun org-add-note ()
  12324. "Add a note to the current entry.
  12325. This is done in the same way as adding a state change note."
  12326. (interactive)
  12327. (org-add-log-setup 'note nil nil 'findpos nil))
  12328. (defun org-log-beginning (&optional create)
  12329. "Return expected start of log notes in current entry.
  12330. When optional argument CREATE is non-nil, the function creates
  12331. a drawer to store notes, if necessary. Returned position ignores
  12332. narrowing."
  12333. (org-with-wide-buffer
  12334. (org-end-of-meta-data)
  12335. (let ((end (if (org-at-heading-p) (point)
  12336. (save-excursion (outline-next-heading) (point))))
  12337. (drawer (org-log-into-drawer)))
  12338. (cond
  12339. (drawer
  12340. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12341. (case-fold-search t))
  12342. (catch 'exit
  12343. ;; Try to find existing drawer.
  12344. (while (re-search-forward regexp end t)
  12345. (let ((element (org-element-at-point)))
  12346. (when (eq (org-element-type element) 'drawer)
  12347. (let ((cend (org-element-property :contents-end element)))
  12348. (when (and (not org-log-states-order-reversed) cend)
  12349. (goto-char cend)))
  12350. (throw 'exit nil))))
  12351. ;; No drawer found. Create one, if permitted.
  12352. (when create
  12353. (unless (bolp) (insert "\n"))
  12354. (let ((beg (point)))
  12355. (insert ":" drawer ":\n:END:\n")
  12356. (org-indent-region beg (point)))
  12357. (end-of-line -1)))))
  12358. (org-log-state-notes-insert-after-drawers
  12359. (while (and (looking-at org-drawer-regexp)
  12360. (progn (goto-char (match-end 0))
  12361. (re-search-forward org-property-end-re end t)))
  12362. (forward-line)))))
  12363. (if (bolp) (point) (line-beginning-position 2))))
  12364. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12365. "Set up the post command hook to take a note.
  12366. If this is about to TODO state change, the new state is expected in STATE.
  12367. When FINDPOS is non-nil, find the correct position for the note in
  12368. the current entry. If not, assume that it can be inserted at point.
  12369. HOW is an indicator what kind of note should be created.
  12370. EXTRA is additional text that will be inserted into the notes buffer."
  12371. (org-with-wide-buffer
  12372. (when findpos
  12373. (goto-char (org-log-beginning t))
  12374. (unless org-log-states-order-reversed
  12375. (org-skip-over-state-notes)
  12376. (skip-chars-backward " \t\n\r")
  12377. (forward-line)))
  12378. (move-marker org-log-note-marker (point))
  12379. ;; Preserve position even if a property drawer is inserted in the
  12380. ;; process.
  12381. (set-marker-insertion-type org-log-note-marker t)
  12382. (setq org-log-note-purpose purpose
  12383. org-log-note-state state
  12384. org-log-note-previous-state prev-state
  12385. org-log-note-how how
  12386. org-log-note-extra extra
  12387. org-log-note-effective-time (org-current-effective-time))
  12388. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12389. (defun org-skip-over-state-notes ()
  12390. "Skip past the list of State notes in an entry."
  12391. (when (ignore-errors (goto-char (org-in-item-p)))
  12392. (let* ((struct (org-list-struct))
  12393. (prevs (org-list-prevs-alist struct))
  12394. (regexp
  12395. (concat "[ \t]*- +"
  12396. (replace-regexp-in-string
  12397. " +" " +"
  12398. (org-replace-escapes
  12399. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12400. `(("%d" . ,org-ts-regexp-inactive)
  12401. ("%D" . ,org-ts-regexp)
  12402. ("%s" . "\"\\S-+\"")
  12403. ("%S" . "\"\\S-+\"")
  12404. ("%t" . ,org-ts-regexp-inactive)
  12405. ("%T" . ,org-ts-regexp)
  12406. ("%u" . ".*?")
  12407. ("%U" . ".*?")))))))
  12408. (while (org-looking-at-p regexp)
  12409. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12410. (org-list-get-item-end (point) struct)))))))
  12411. (defun org-add-log-note (&optional purpose)
  12412. "Pop up a window for taking a note, and add this note later at point."
  12413. (remove-hook 'post-command-hook 'org-add-log-note)
  12414. (setq org-log-note-window-configuration (current-window-configuration))
  12415. (delete-other-windows)
  12416. (move-marker org-log-note-return-to (point))
  12417. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12418. (goto-char org-log-note-marker)
  12419. (org-switch-to-buffer-other-window "*Org Note*")
  12420. (erase-buffer)
  12421. (if (memq org-log-note-how '(time state))
  12422. (let (current-prefix-arg) (org-store-log-note))
  12423. (let ((org-inhibit-startup t)) (org-mode))
  12424. (insert (format "# Insert note for %s.
  12425. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12426. (cond
  12427. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12428. ((eq org-log-note-purpose 'done) "closed todo item")
  12429. ((eq org-log-note-purpose 'state)
  12430. (format "state change from \"%s\" to \"%s\""
  12431. (or org-log-note-previous-state "")
  12432. (or org-log-note-state "")))
  12433. ((eq org-log-note-purpose 'reschedule)
  12434. "rescheduling")
  12435. ((eq org-log-note-purpose 'delschedule)
  12436. "no longer scheduled")
  12437. ((eq org-log-note-purpose 'redeadline)
  12438. "changing deadline")
  12439. ((eq org-log-note-purpose 'deldeadline)
  12440. "removing deadline")
  12441. ((eq org-log-note-purpose 'refile)
  12442. "refiling")
  12443. ((eq org-log-note-purpose 'note)
  12444. "this entry")
  12445. (t (error "This should not happen")))))
  12446. (if org-log-note-extra (insert org-log-note-extra))
  12447. (org-set-local 'org-finish-function 'org-store-log-note)
  12448. (run-hooks 'org-log-buffer-setup-hook)))
  12449. (defvar org-note-abort nil) ; dynamically scoped
  12450. (defun org-store-log-note ()
  12451. "Finish taking a log note, and insert it to where it belongs."
  12452. (let ((txt (buffer-string)))
  12453. (kill-buffer (current-buffer))
  12454. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12455. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12456. (setq txt (replace-match "" t t txt)))
  12457. (if (string-match "\\s-+\\'" txt)
  12458. (setq txt (replace-match "" t t txt)))
  12459. (setq lines (org-split-string txt "\n"))
  12460. (when (and note (string-match "\\S-" note))
  12461. (setq note
  12462. (org-replace-escapes
  12463. note
  12464. (list (cons "%u" (user-login-name))
  12465. (cons "%U" user-full-name)
  12466. (cons "%t" (format-time-string
  12467. (org-time-stamp-format 'long 'inactive)
  12468. org-log-note-effective-time))
  12469. (cons "%T" (format-time-string
  12470. (org-time-stamp-format 'long nil)
  12471. org-log-note-effective-time))
  12472. (cons "%d" (format-time-string
  12473. (org-time-stamp-format nil 'inactive)
  12474. org-log-note-effective-time))
  12475. (cons "%D" (format-time-string
  12476. (org-time-stamp-format nil nil)
  12477. org-log-note-effective-time))
  12478. (cons "%s" (if org-log-note-state
  12479. (concat "\"" org-log-note-state "\"")
  12480. ""))
  12481. (cons "%S" (if org-log-note-previous-state
  12482. (concat "\"" org-log-note-previous-state "\"")
  12483. "\"\"")))))
  12484. (when lines (setq note (concat note " \\\\")))
  12485. (push note lines))
  12486. (when (or current-prefix-arg org-note-abort)
  12487. (when (org-log-into-drawer)
  12488. (org-remove-empty-drawer-at org-log-note-marker))
  12489. (setq lines nil))
  12490. (when lines
  12491. (with-current-buffer (marker-buffer org-log-note-marker)
  12492. (save-excursion
  12493. (goto-char org-log-note-marker)
  12494. (move-marker org-log-note-marker nil)
  12495. ;; Make sure point is at the beginning of an empty line.
  12496. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12497. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12498. ;; In an existing list, add a new item at the top level.
  12499. ;; Otherwise, indent line like a regular one.
  12500. (let ((itemp (org-in-item-p)))
  12501. (if itemp
  12502. (org-indent-line-to
  12503. (let ((struct (save-excursion
  12504. (goto-char itemp) (org-list-struct))))
  12505. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12506. (org-indent-line)))
  12507. (insert (org-list-bullet-string "-") (pop lines))
  12508. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12509. (dolist (line lines)
  12510. (insert "\n")
  12511. (org-indent-line-to ind)
  12512. (insert line)))
  12513. (message "Note stored")
  12514. (org-back-to-heading t)
  12515. (org-cycle-hide-drawers 'children))
  12516. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12517. ;; from within `org-add-log-note' because `buffer-undo-list'
  12518. ;; is then modified outside of `org-with-remote-undo'.
  12519. (when (eq this-command 'org-agenda-todo)
  12520. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12521. ;; Don't add undo information when called from `org-agenda-todo'
  12522. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12523. (set-window-configuration org-log-note-window-configuration)
  12524. (with-current-buffer (marker-buffer org-log-note-return-to)
  12525. (goto-char org-log-note-return-to))
  12526. (move-marker org-log-note-return-to nil)
  12527. (and org-log-post-message (message "%s" org-log-post-message))))
  12528. (defun org-remove-empty-drawer-at (pos)
  12529. "Remove an empty drawer at position POS.
  12530. POS may also be a marker."
  12531. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12532. (org-with-wide-buffer
  12533. (goto-char pos)
  12534. (let ((drawer (org-element-at-point)))
  12535. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12536. (not (org-element-property :contents-begin drawer)))
  12537. (delete-region (org-element-property :begin drawer)
  12538. (progn (goto-char (org-element-property :end drawer))
  12539. (skip-chars-backward " \r\t\n")
  12540. (forward-line)
  12541. (point))))))))
  12542. (defvar org-ts-type nil)
  12543. (defun org-sparse-tree (&optional arg type)
  12544. "Create a sparse tree, prompt for the details.
  12545. This command can create sparse trees. You first need to select the type
  12546. of match used to create the tree:
  12547. t Show all TODO entries.
  12548. T Show entries with a specific TODO keyword.
  12549. m Show entries selected by a tags/property match.
  12550. p Enter a property name and its value (both with completion on existing
  12551. names/values) and show entries with that property.
  12552. r Show entries matching a regular expression (`/' can be used as well).
  12553. b Show deadlines and scheduled items before a date.
  12554. a Show deadlines and scheduled items after a date.
  12555. d Show deadlines due within `org-deadline-warning-days'.
  12556. D Show deadlines and scheduled items between a date range."
  12557. (interactive "P")
  12558. (setq type (or type org-sparse-tree-default-date-type))
  12559. (setq org-ts-type type)
  12560. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12561. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12562. [c]ycle through date types: %s"
  12563. (case type
  12564. (all "all timestamps")
  12565. (scheduled "only scheduled")
  12566. (deadline "only deadline")
  12567. (active "only active timestamps")
  12568. (inactive "only inactive timestamps")
  12569. (closed "with a closed time-stamp")
  12570. (otherwise "scheduled/deadline")))
  12571. (let ((answer (read-char-exclusive)))
  12572. (case answer
  12573. (?c
  12574. (org-sparse-tree
  12575. arg
  12576. (cadr
  12577. (memq type '(nil all scheduled deadline active inactive closed)))))
  12578. (?d (call-interactively 'org-check-deadlines))
  12579. (?b (call-interactively 'org-check-before-date))
  12580. (?a (call-interactively 'org-check-after-date))
  12581. (?D (call-interactively 'org-check-dates-range))
  12582. (?t (call-interactively 'org-show-todo-tree))
  12583. (?T (org-show-todo-tree '(4)))
  12584. (?m (call-interactively 'org-match-sparse-tree))
  12585. ((?p ?P)
  12586. (let* ((kwd (org-icompleting-read
  12587. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12588. (value (org-icompleting-read
  12589. "Value: " (mapcar 'list (org-property-values kwd)))))
  12590. (unless (string-match "\\`{.*}\\'" value)
  12591. (setq value (concat "\"" value "\"")))
  12592. (org-match-sparse-tree arg (concat kwd "=" value))))
  12593. ((?r ?R ?/) (call-interactively 'org-occur))
  12594. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12595. (defvar org-occur-highlights nil
  12596. "List of overlays used for occur matches.")
  12597. (make-variable-buffer-local 'org-occur-highlights)
  12598. (defvar org-occur-parameters nil
  12599. "Parameters of the active org-occur calls.
  12600. This is a list, each call to org-occur pushes as cons cell,
  12601. containing the regular expression and the callback, onto the list.
  12602. The list can contain several entries if `org-occur' has been called
  12603. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12604. will only contain one set of parameters. When the highlights are
  12605. removed (for example with `C-c C-c', or with the next edit (depending
  12606. on `org-remove-highlights-with-change'), this variable is emptied
  12607. as well.")
  12608. (make-variable-buffer-local 'org-occur-parameters)
  12609. (defun org-occur (regexp &optional keep-previous callback)
  12610. "Make a compact tree which shows all matches of REGEXP.
  12611. The tree will show the lines where the regexp matches, and all higher
  12612. headlines above the match. It will also show the heading after the match,
  12613. to make sure editing the matching entry is easy.
  12614. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12615. call to `org-occur' will be kept, to allow stacking of calls to this
  12616. command.
  12617. If CALLBACK is non-nil, it is a function which is called to confirm
  12618. that the match should indeed be shown."
  12619. (interactive "sRegexp: \nP")
  12620. (when (equal regexp "")
  12621. (user-error "Regexp cannot be empty"))
  12622. (unless keep-previous
  12623. (org-remove-occur-highlights nil nil t))
  12624. (push (cons regexp callback) org-occur-parameters)
  12625. (let ((cnt 0))
  12626. (save-excursion
  12627. (goto-char (point-min))
  12628. (if (or (not keep-previous) ; do not want to keep
  12629. (not org-occur-highlights)) ; no previous matches
  12630. ;; hide everything
  12631. (org-overview))
  12632. (while (re-search-forward regexp nil t)
  12633. (when (or (not callback)
  12634. (save-match-data (funcall callback)))
  12635. (setq cnt (1+ cnt))
  12636. (when org-highlight-sparse-tree-matches
  12637. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12638. (org-show-context 'occur-tree))))
  12639. (when org-remove-highlights-with-change
  12640. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12641. nil 'local))
  12642. (unless org-sparse-tree-open-archived-trees
  12643. (org-hide-archived-subtrees (point-min) (point-max)))
  12644. (run-hooks 'org-occur-hook)
  12645. (if (org-called-interactively-p 'interactive)
  12646. (message "%d match(es) for regexp %s" cnt regexp))
  12647. cnt))
  12648. (defun org-occur-next-match (&optional n reset)
  12649. "Function for `next-error-function' to find sparse tree matches.
  12650. N is the number of matches to move, when negative move backwards.
  12651. RESET is entirely ignored - this function always goes back to the
  12652. starting point when no match is found."
  12653. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12654. (search-func (if (< n 0)
  12655. 'previous-single-char-property-change
  12656. 'next-single-char-property-change))
  12657. (n (abs n))
  12658. (pos (point))
  12659. p1)
  12660. (catch 'exit
  12661. (while (setq p1 (funcall search-func (point) 'org-type))
  12662. (when (equal p1 limit)
  12663. (goto-char pos)
  12664. (user-error "No more matches"))
  12665. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12666. (setq n (1- n))
  12667. (when (= n 0)
  12668. (goto-char p1)
  12669. (throw 'exit (point))))
  12670. (goto-char p1))
  12671. (goto-char p1)
  12672. (user-error "No more matches"))))
  12673. (defun org-show-context (&optional key)
  12674. "Make sure point and context are visible.
  12675. How much context is shown depends upon the variables
  12676. `org-show-hierarchy-above', `org-show-following-heading',
  12677. `org-show-entry-below' and `org-show-siblings'."
  12678. (let ((heading-p (org-at-heading-p t))
  12679. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12680. (following-p (org-get-alist-option org-show-following-heading key))
  12681. (entry-p (org-get-alist-option org-show-entry-below key))
  12682. (siblings-p (org-get-alist-option org-show-siblings key)))
  12683. ;; Show heading or entry text
  12684. (if (and heading-p (not entry-p))
  12685. (org-flag-heading nil) ; only show the heading
  12686. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12687. (org-show-hidden-entry))) ; show entire entry
  12688. (when following-p
  12689. ;; Show next sibling, or heading below text
  12690. (save-excursion
  12691. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12692. (org-flag-heading nil))))
  12693. (when siblings-p (org-show-siblings))
  12694. (when hierarchy-p
  12695. ;; show all higher headings, possibly with siblings
  12696. (save-excursion
  12697. (while (and (ignore-errors (progn (org-up-heading-all 1) t))
  12698. (not (bobp)))
  12699. (org-flag-heading nil)
  12700. (when siblings-p (org-show-siblings)))))))
  12701. (defvar org-reveal-start-hook nil
  12702. "Hook run before revealing a location.")
  12703. (defun org-reveal (&optional siblings)
  12704. "Show current entry, hierarchy above it, and the following headline.
  12705. This can be used to show a consistent set of context around locations
  12706. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12707. not t for the search context.
  12708. With optional argument SIBLINGS, on each level of the hierarchy all
  12709. siblings are shown. This repairs the tree structure to what it would
  12710. look like when opened with hierarchical calls to `org-cycle'.
  12711. With double optional argument \\[universal-argument] \\[universal-argument], \
  12712. go to the parent and show the
  12713. entire tree."
  12714. (interactive "P")
  12715. (run-hooks 'org-reveal-start-hook)
  12716. (let ((org-show-hierarchy-above t)
  12717. (org-show-following-heading t)
  12718. (org-show-siblings (if siblings t org-show-siblings)))
  12719. (org-show-context nil))
  12720. (when (equal siblings '(16))
  12721. (save-excursion
  12722. (when (org-up-heading-safe)
  12723. (org-show-subtree)
  12724. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12725. (defun org-highlight-new-match (beg end)
  12726. "Highlight from BEG to END and mark the highlight is an occur headline."
  12727. (let ((ov (make-overlay beg end)))
  12728. (overlay-put ov 'face 'secondary-selection)
  12729. (overlay-put ov 'org-type 'org-occur)
  12730. (push ov org-occur-highlights)))
  12731. (defun org-remove-occur-highlights (&optional beg end noremove)
  12732. "Remove the occur highlights from the buffer.
  12733. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12734. from the `before-change-functions' in the current buffer."
  12735. (interactive)
  12736. (unless org-inhibit-highlight-removal
  12737. (mapc 'delete-overlay org-occur-highlights)
  12738. (setq org-occur-highlights nil)
  12739. (setq org-occur-parameters nil)
  12740. (unless noremove
  12741. (remove-hook 'before-change-functions
  12742. 'org-remove-occur-highlights 'local))))
  12743. ;;;; Priorities
  12744. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12745. "Regular expression matching the priority indicator.")
  12746. (defvar org-remove-priority-next-time nil)
  12747. (defun org-priority-up ()
  12748. "Increase the priority of the current item."
  12749. (interactive)
  12750. (org-priority 'up))
  12751. (defun org-priority-down ()
  12752. "Decrease the priority of the current item."
  12753. (interactive)
  12754. (org-priority 'down))
  12755. (defun org-priority (&optional action show)
  12756. "Change the priority of an item.
  12757. ACTION can be `set', `up', `down', or a character."
  12758. (interactive "P")
  12759. (if (equal action '(4))
  12760. (org-show-priority)
  12761. (unless org-enable-priority-commands
  12762. (user-error "Priority commands are disabled"))
  12763. (setq action (or action 'set))
  12764. (let (current new news have remove)
  12765. (save-excursion
  12766. (org-back-to-heading t)
  12767. (if (looking-at org-priority-regexp)
  12768. (setq current (string-to-char (match-string 2))
  12769. have t))
  12770. (cond
  12771. ((eq action 'remove)
  12772. (setq remove t new ?\ ))
  12773. ((or (eq action 'set)
  12774. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12775. (if (not (eq action 'set))
  12776. (setq new action)
  12777. (message "Priority %c-%c, SPC to remove: "
  12778. org-highest-priority org-lowest-priority)
  12779. (save-match-data
  12780. (setq new (read-char-exclusive))))
  12781. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12782. (= (upcase org-lowest-priority) org-lowest-priority))
  12783. (setq new (upcase new)))
  12784. (cond ((equal new ?\ ) (setq remove t))
  12785. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12786. (user-error "Priority must be between `%c' and `%c'"
  12787. org-highest-priority org-lowest-priority))))
  12788. ((eq action 'up)
  12789. (setq new (if have
  12790. (1- current) ; normal cycling
  12791. ;; last priority was empty
  12792. (if (eq last-command this-command)
  12793. org-lowest-priority ; wrap around empty to lowest
  12794. ;; default
  12795. (if org-priority-start-cycle-with-default
  12796. org-default-priority
  12797. (1- org-default-priority))))))
  12798. ((eq action 'down)
  12799. (setq new (if have
  12800. (1+ current) ; normal cycling
  12801. ;; last priority was empty
  12802. (if (eq last-command this-command)
  12803. org-highest-priority ; wrap around empty to highest
  12804. ;; default
  12805. (if org-priority-start-cycle-with-default
  12806. org-default-priority
  12807. (1+ org-default-priority))))))
  12808. (t (user-error "Invalid action")))
  12809. (if (or (< (upcase new) org-highest-priority)
  12810. (> (upcase new) org-lowest-priority))
  12811. (if (and (memq action '(up down))
  12812. (not have) (not (eq last-command this-command)))
  12813. ;; `new' is from default priority
  12814. (error
  12815. "The default can not be set, see `org-default-priority' why")
  12816. ;; normal cycling: `new' is beyond highest/lowest priority
  12817. ;; and is wrapped around to the empty priority
  12818. (setq remove t)))
  12819. (setq news (format "%c" new))
  12820. (if have
  12821. (if remove
  12822. (replace-match "" t t nil 1)
  12823. (replace-match news t t nil 2))
  12824. (if remove
  12825. (user-error "No priority cookie found in line")
  12826. (let ((case-fold-search nil))
  12827. (looking-at org-todo-line-regexp))
  12828. (if (match-end 2)
  12829. (progn
  12830. (goto-char (match-end 2))
  12831. (insert " [#" news "]"))
  12832. (goto-char (match-beginning 3))
  12833. (insert "[#" news "] "))))
  12834. (org-set-tags nil 'align))
  12835. (if remove
  12836. (message "Priority removed")
  12837. (message "Priority of current item set to %s" news)))))
  12838. (defun org-show-priority ()
  12839. "Show the priority of the current item.
  12840. This priority is composed of the main priority given with the [#A] cookies,
  12841. and by additional input from the age of a schedules or deadline entry."
  12842. (interactive)
  12843. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12844. (org-get-at-bol 'priority)
  12845. (save-excursion
  12846. (save-match-data
  12847. (beginning-of-line)
  12848. (and (looking-at org-heading-regexp)
  12849. (org-get-priority (match-string 0))))))))
  12850. (message "Priority is %d" (if pri pri -1000))))
  12851. (defun org-get-priority (s)
  12852. "Find priority cookie and return priority."
  12853. (save-match-data
  12854. (if (functionp org-get-priority-function)
  12855. (funcall org-get-priority-function)
  12856. (if (not (string-match org-priority-regexp s))
  12857. (* 1000 (- org-lowest-priority org-default-priority))
  12858. (* 1000 (- org-lowest-priority
  12859. (string-to-char (match-string 2 s))))))))
  12860. ;;;; Tags
  12861. (defvar org-agenda-archives-mode)
  12862. (defvar org-map-continue-from nil
  12863. "Position from where mapping should continue.
  12864. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12865. (defvar org-scanner-tags nil
  12866. "The current tag list while the tags scanner is running.")
  12867. (defvar org-trust-scanner-tags nil
  12868. "Should `org-get-tags-at' use the tags for the scanner.
  12869. This is for internal dynamical scoping only.
  12870. When this is non-nil, the function `org-get-tags-at' will return the value
  12871. of `org-scanner-tags' instead of building the list by itself. This
  12872. can lead to large speed-ups when the tags scanner is used in a file with
  12873. many entries, and when the list of tags is retrieved, for example to
  12874. obtain a list of properties. Building the tags list for each entry in such
  12875. a file becomes an N^2 operation - but with this variable set, it scales
  12876. as N.")
  12877. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12878. "Scan headline tags with inheritance and produce output ACTION.
  12879. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12880. or `agenda' to produce an entry list for an agenda view. It can also be
  12881. a Lisp form or a function that should be called at each matched headline, in
  12882. this case the return value is a list of all return values from these calls.
  12883. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12884. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12885. only lines with a not-done TODO keyword are included in the output.
  12886. This should be the same variable that was scoped into
  12887. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12888. START-LEVEL can be a string with asterisks, reducing the scope to
  12889. headlines matching this string."
  12890. (require 'org-agenda)
  12891. (let* ((re (concat "^"
  12892. (if start-level
  12893. ;; Get the correct level to match
  12894. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12895. org-outline-regexp)
  12896. " *\\(\\<\\("
  12897. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12898. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12899. (props (list 'face 'default
  12900. 'done-face 'org-agenda-done
  12901. 'undone-face 'default
  12902. 'mouse-face 'highlight
  12903. 'org-not-done-regexp org-not-done-regexp
  12904. 'org-todo-regexp org-todo-regexp
  12905. 'org-complex-heading-regexp org-complex-heading-regexp
  12906. 'help-echo
  12907. (format "mouse-2 or RET jump to org file %s"
  12908. (abbreviate-file-name
  12909. (or (buffer-file-name (buffer-base-buffer))
  12910. (buffer-name (buffer-base-buffer)))))))
  12911. (org-map-continue-from nil)
  12912. lspos tags tags-list
  12913. (tags-alist (list (cons 0 org-file-tags)))
  12914. (llast 0) rtn rtn1 level category i txt
  12915. todo marker entry priority
  12916. ts-date ts-date-type ts-date-pair)
  12917. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12918. (setq action (list 'lambda nil action)))
  12919. (save-excursion
  12920. (goto-char (point-min))
  12921. (when (eq action 'sparse-tree)
  12922. (org-overview)
  12923. (org-remove-occur-highlights))
  12924. (while (let (case-fold-search)
  12925. (re-search-forward re nil t))
  12926. (setq org-map-continue-from nil)
  12927. (catch :skip
  12928. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12929. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12930. (goto-char (setq lspos (match-beginning 0)))
  12931. (setq level (org-reduced-level (org-outline-level))
  12932. category (org-get-category))
  12933. (when (eq action 'agenda)
  12934. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12935. ts-date (car ts-date-pair)
  12936. ts-date-type (cdr ts-date-pair)))
  12937. (setq i llast llast level)
  12938. ;; remove tag lists from same and sublevels
  12939. (while (>= i level)
  12940. (when (setq entry (assoc i tags-alist))
  12941. (setq tags-alist (delete entry tags-alist)))
  12942. (setq i (1- i)))
  12943. ;; add the next tags
  12944. (when tags
  12945. (setq tags (org-split-string tags ":")
  12946. tags-alist
  12947. (cons (cons level tags) tags-alist)))
  12948. ;; compile tags for current headline
  12949. (setq tags-list
  12950. (if org-use-tag-inheritance
  12951. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12952. tags)
  12953. org-scanner-tags tags-list)
  12954. (when org-use-tag-inheritance
  12955. (setcdr (car tags-alist)
  12956. (mapcar (lambda (x)
  12957. (setq x (copy-sequence x))
  12958. (org-add-prop-inherited x))
  12959. (cdar tags-alist))))
  12960. (when (and tags org-use-tag-inheritance
  12961. (or (not (eq t org-use-tag-inheritance))
  12962. org-tags-exclude-from-inheritance))
  12963. ;; selective inheritance, remove uninherited ones
  12964. (setcdr (car tags-alist)
  12965. (org-remove-uninherited-tags (cdar tags-alist))))
  12966. (when (and
  12967. ;; eval matcher only when the todo condition is OK
  12968. (and (or (not todo-only) (member todo org-not-done-keywords))
  12969. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12970. (eval matcher)))
  12971. ;; Call the skipper, but return t if it does not skip,
  12972. ;; so that the `and' form continues evaluating
  12973. (progn
  12974. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12975. t)
  12976. ;; Check if timestamps are deselecting this entry
  12977. (or (not todo-only)
  12978. (and (member todo org-not-done-keywords)
  12979. (or (not org-agenda-tags-todo-honor-ignore-options)
  12980. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12981. ;; select this headline
  12982. (cond
  12983. ((eq action 'sparse-tree)
  12984. (and org-highlight-sparse-tree-matches
  12985. (org-get-heading) (match-end 0)
  12986. (org-highlight-new-match
  12987. (match-beginning 1) (match-end 1)))
  12988. (org-show-context 'tags-tree))
  12989. ((eq action 'agenda)
  12990. (setq txt (org-agenda-format-item
  12991. ""
  12992. (concat
  12993. (if (eq org-tags-match-list-sublevels 'indented)
  12994. (make-string (1- level) ?.) "")
  12995. (org-get-heading))
  12996. level category
  12997. tags-list)
  12998. priority (org-get-priority txt))
  12999. (goto-char lspos)
  13000. (setq marker (org-agenda-new-marker))
  13001. (org-add-props txt props
  13002. 'org-marker marker 'org-hd-marker marker 'org-category category
  13003. 'todo-state todo
  13004. 'ts-date ts-date
  13005. 'priority priority
  13006. 'type (concat "tagsmatch" ts-date-type))
  13007. (push txt rtn))
  13008. ((functionp action)
  13009. (setq org-map-continue-from nil)
  13010. (save-excursion
  13011. (setq rtn1 (funcall action))
  13012. (push rtn1 rtn)))
  13013. (t (user-error "Invalid action")))
  13014. ;; if we are to skip sublevels, jump to end of subtree
  13015. (unless org-tags-match-list-sublevels
  13016. (org-end-of-subtree t)
  13017. (backward-char 1))))
  13018. ;; Get the correct position from where to continue
  13019. (if org-map-continue-from
  13020. (goto-char org-map-continue-from)
  13021. (and (= (point) lspos) (end-of-line 1)))))
  13022. (when (and (eq action 'sparse-tree)
  13023. (not org-sparse-tree-open-archived-trees))
  13024. (org-hide-archived-subtrees (point-min) (point-max)))
  13025. (nreverse rtn)))
  13026. (defun org-remove-uninherited-tags (tags)
  13027. "Remove all tags that are not inherited from the list TAGS."
  13028. (cond
  13029. ((eq org-use-tag-inheritance t)
  13030. (if org-tags-exclude-from-inheritance
  13031. (org-delete-all org-tags-exclude-from-inheritance tags)
  13032. tags))
  13033. ((not org-use-tag-inheritance) nil)
  13034. ((stringp org-use-tag-inheritance)
  13035. (delq nil (mapcar
  13036. (lambda (x)
  13037. (if (and (string-match org-use-tag-inheritance x)
  13038. (not (member x org-tags-exclude-from-inheritance)))
  13039. x nil))
  13040. tags)))
  13041. ((listp org-use-tag-inheritance)
  13042. (delq nil (mapcar
  13043. (lambda (x)
  13044. (if (member x org-use-tag-inheritance) x nil))
  13045. tags)))))
  13046. (defun org-match-sparse-tree (&optional todo-only match)
  13047. "Create a sparse tree according to tags string MATCH.
  13048. MATCH can contain positive and negative selection of tags, like
  13049. \"+WORK+URGENT-WITHBOSS\".
  13050. If optional argument TODO-ONLY is non-nil, only select lines that are
  13051. also TODO lines."
  13052. (interactive "P")
  13053. (org-agenda-prepare-buffers (list (current-buffer)))
  13054. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13055. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13056. (defvar org-cached-props nil)
  13057. (defun org-cached-entry-get (pom property)
  13058. (if (or (eq t org-use-property-inheritance)
  13059. (and (stringp org-use-property-inheritance)
  13060. (let ((case-fold-search t))
  13061. (org-string-match-p org-use-property-inheritance property)))
  13062. (and (listp org-use-property-inheritance)
  13063. (member-ignore-case property org-use-property-inheritance)))
  13064. ;; Caching is not possible, check it directly.
  13065. (org-entry-get pom property 'inherit)
  13066. ;; Get all properties, so we can do complicated checks easily.
  13067. (cdr (assoc-string property
  13068. (or org-cached-props
  13069. (setq org-cached-props (org-entry-properties pom)))
  13070. t))))
  13071. (defun org-global-tags-completion-table (&optional files)
  13072. "Return the list of all tags in all agenda buffer/files.
  13073. Optional FILES argument is a list of files which can be used
  13074. instead of the agenda files."
  13075. (save-excursion
  13076. (org-uniquify
  13077. (delq nil
  13078. (apply 'append
  13079. (mapcar
  13080. (lambda (file)
  13081. (set-buffer (find-file-noselect file))
  13082. (append (org-get-buffer-tags)
  13083. (mapcar (lambda (x) (if (stringp (car-safe x))
  13084. (list (car-safe x)) nil))
  13085. org-tag-alist)))
  13086. (if (and files (car files))
  13087. files
  13088. (org-agenda-files))))))))
  13089. (defun org-make-tags-matcher (match)
  13090. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13091. The variable `todo-only' is scoped dynamically into this function.
  13092. It will be set to t if the matcher restricts matching to TODO entries,
  13093. otherwise will not be touched.
  13094. Returns a cons of the selection string MATCH and the constructed
  13095. lisp form implementing the matcher. The matcher is to be evaluated
  13096. at an Org entry, with point on the headline, and returns t if the
  13097. entry matches the selection string MATCH. The returned lisp form
  13098. references two variables with information about the entry, which
  13099. must be bound around the form's evaluation: todo, the TODO keyword
  13100. at the entry (or nil of none); and tags-list, the list of all tags
  13101. at the entry including inherited ones. Additionally, the category
  13102. of the entry (if any) must be specified as the text property
  13103. 'org-category on the headline.
  13104. See also `org-scan-tags'.
  13105. "
  13106. (declare (special todo-only))
  13107. (unless (boundp 'todo-only)
  13108. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13109. (unless match
  13110. ;; Get a new match request, with completion against the global
  13111. ;; tags table and the local tags in current buffer
  13112. (let ((org-last-tags-completion-table
  13113. (org-uniquify
  13114. (delq nil (append (org-get-buffer-tags)
  13115. (org-global-tags-completion-table))))))
  13116. (setq match (org-completing-read-no-i
  13117. "Match: " 'org-tags-completion-function nil nil nil
  13118. 'org-tags-history))))
  13119. ;; Parse the string and create a lisp form
  13120. (let ((match0 match)
  13121. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13122. minus tag mm
  13123. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13124. orterms term orlist re-p str-p level-p level-op time-p
  13125. prop-p pn pv po gv rest (start 0) (ss 0))
  13126. ;; Expand group tags
  13127. (setq match (org-tags-expand match))
  13128. ;; Check if there is a TODO part of this match, which would be the
  13129. ;; part after a "/". TO make sure that this slash is not part of
  13130. ;; a property value to be matched against, we also check that there
  13131. ;; is no " after that slash.
  13132. ;; First, find the last slash
  13133. (while (string-match "/+" match ss)
  13134. (setq start (match-beginning 0) ss (match-end 0)))
  13135. (if (and (string-match "/+" match start)
  13136. (not (save-match-data (string-match "\"" match start))))
  13137. ;; match contains also a todo-matching request
  13138. (progn
  13139. (setq tagsmatch (substring match 0 (match-beginning 0))
  13140. todomatch (substring match (match-end 0)))
  13141. (if (string-match "^!" todomatch)
  13142. (setq todo-only t todomatch (substring todomatch 1)))
  13143. (if (string-match "^\\s-*$" todomatch)
  13144. (setq todomatch nil)))
  13145. ;; only matching tags
  13146. (setq tagsmatch match todomatch nil))
  13147. ;; Make the tags matcher
  13148. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13149. (setq tagsmatcher t)
  13150. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13151. (while (setq term (pop orterms))
  13152. (while (and (equal (substring term -1) "\\") orterms)
  13153. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13154. (while (string-match re term)
  13155. (setq rest (substring term (match-end 0))
  13156. minus (and (match-end 1)
  13157. (equal (match-string 1 term) "-"))
  13158. tag (save-match-data (replace-regexp-in-string
  13159. "\\\\-" "-"
  13160. (match-string 2 term)))
  13161. re-p (equal (string-to-char tag) ?{)
  13162. level-p (match-end 4)
  13163. prop-p (match-end 5)
  13164. mm (cond
  13165. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13166. (level-p
  13167. (setq level-op (org-op-to-function (match-string 3 term)))
  13168. `(,level-op level ,(string-to-number
  13169. (match-string 4 term))))
  13170. (prop-p
  13171. (setq pn (match-string 5 term)
  13172. po (match-string 6 term)
  13173. pv (match-string 7 term)
  13174. re-p (equal (string-to-char pv) ?{)
  13175. str-p (equal (string-to-char pv) ?\")
  13176. time-p (save-match-data
  13177. (string-match "^\"[[<].*[]>]\"$" pv))
  13178. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13179. (if time-p (setq pv (org-matcher-time pv)))
  13180. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13181. (cond
  13182. ((equal pn "CATEGORY")
  13183. (setq gv '(get-text-property (point) 'org-category)))
  13184. ((equal pn "TODO")
  13185. (setq gv 'todo))
  13186. (t
  13187. (setq gv `(org-cached-entry-get nil ,pn))))
  13188. (if re-p
  13189. (if (eq po 'org<>)
  13190. `(not (string-match ,pv (or ,gv "")))
  13191. `(string-match ,pv (or ,gv "")))
  13192. (if str-p
  13193. `(,po (or ,gv "") ,pv)
  13194. `(,po (string-to-number (or ,gv ""))
  13195. ,(string-to-number pv) ))))
  13196. (t `(member ,tag tags-list)))
  13197. mm (if minus (list 'not mm) mm)
  13198. term rest)
  13199. (push mm tagsmatcher))
  13200. (push (if (> (length tagsmatcher) 1)
  13201. (cons 'and tagsmatcher)
  13202. (car tagsmatcher))
  13203. orlist)
  13204. (setq tagsmatcher nil))
  13205. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13206. (setq tagsmatcher
  13207. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13208. ;; Make the todo matcher
  13209. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13210. (setq todomatcher t)
  13211. (setq orterms (org-split-string todomatch "|") orlist nil)
  13212. (while (setq term (pop orterms))
  13213. (while (string-match re term)
  13214. (setq minus (and (match-end 1)
  13215. (equal (match-string 1 term) "-"))
  13216. kwd (match-string 2 term)
  13217. re-p (equal (string-to-char kwd) ?{)
  13218. term (substring term (match-end 0))
  13219. mm (if re-p
  13220. `(string-match ,(substring kwd 1 -1) todo)
  13221. (list 'equal 'todo kwd))
  13222. mm (if minus (list 'not mm) mm))
  13223. (push mm todomatcher))
  13224. (push (if (> (length todomatcher) 1)
  13225. (cons 'and todomatcher)
  13226. (car todomatcher))
  13227. orlist)
  13228. (setq todomatcher nil))
  13229. (setq todomatcher (if (> (length orlist) 1)
  13230. (cons 'or orlist) (car orlist))))
  13231. ;; Return the string and lisp forms of the matcher
  13232. (setq matcher (if todomatcher
  13233. (list 'and tagsmatcher todomatcher)
  13234. tagsmatcher))
  13235. (when todo-only
  13236. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13237. matcher)))
  13238. (cons match0 matcher)))
  13239. (defun org-tags-expand (match &optional single-as-list downcased)
  13240. "Expand group tags in MATCH.
  13241. This replaces every group tag in MATCH with a regexp tag search.
  13242. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13243. will be replaced like this:
  13244. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13245. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13246. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13247. Replacing by a regexp preserves the structure of the match.
  13248. E.g., this expansion
  13249. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13250. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13251. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13252. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13253. assumed to be a single group tag, and the function will return
  13254. the list of tags in this group.
  13255. When DOWNCASE is non-nil, expand downcased TAGS."
  13256. (if org-group-tags
  13257. (let* ((case-fold-search t)
  13258. (stable org-mode-syntax-table)
  13259. (tal (or org-tag-groups-alist-for-agenda
  13260. org-tag-groups-alist))
  13261. (tal (if downcased
  13262. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13263. (tml (mapcar 'car tal))
  13264. (rtnmatch match) rpl)
  13265. ;; @ and _ are allowed as word-components in tags
  13266. (modify-syntax-entry ?@ "w" stable)
  13267. (modify-syntax-entry ?_ "w" stable)
  13268. (while (and tml
  13269. (with-syntax-table stable
  13270. (string-match
  13271. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13272. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13273. (let* ((dir (match-string 1 rtnmatch))
  13274. (tag (match-string 2 rtnmatch))
  13275. (tag (if downcased (downcase tag) tag)))
  13276. (setq tml (delete tag tml))
  13277. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13278. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13279. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13280. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13281. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13282. (if single-as-list
  13283. (or (reverse rpl) (list rtnmatch))
  13284. rtnmatch))
  13285. (if single-as-list (list (if downcased (downcase match) match))
  13286. match)))
  13287. (defun org-op-to-function (op &optional stringp)
  13288. "Turn an operator into the appropriate function."
  13289. (setq op
  13290. (cond
  13291. ((equal op "<" ) '(< string< org-time<))
  13292. ((equal op ">" ) '(> org-string> org-time>))
  13293. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13294. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13295. ((member op '("=" "==")) '(= string= org-time=))
  13296. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13297. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13298. (defun org<> (a b) (not (= a b)))
  13299. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13300. (defun org-string>= (a b) (not (string< a b)))
  13301. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13302. (defun org-string<> (a b) (not (string= a b)))
  13303. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13304. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13305. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13306. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13307. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13308. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13309. (defun org-2ft (s)
  13310. "Convert S to a floating point time.
  13311. If S is already a number, just return it. If it is a string, parse
  13312. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13313. (cond
  13314. ((numberp s) s)
  13315. ((stringp s)
  13316. (condition-case nil
  13317. (float-time (apply 'encode-time (org-parse-time-string s)))
  13318. (error 0.)))
  13319. (t 0.)))
  13320. (defun org-time-today ()
  13321. "Time in seconds today at 0:00.
  13322. Returns the float number of seconds since the beginning of the
  13323. epoch to the beginning of today (00:00)."
  13324. (float-time (apply 'encode-time
  13325. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13326. (defun org-matcher-time (s)
  13327. "Interpret a time comparison value."
  13328. (save-match-data
  13329. (cond
  13330. ((string= s "<now>") (float-time))
  13331. ((string= s "<today>") (org-time-today))
  13332. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13333. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13334. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13335. (+ (org-time-today)
  13336. (* (string-to-number (match-string 1 s))
  13337. (cdr (assoc (match-string 2 s)
  13338. '(("d" . 86400.0) ("w" . 604800.0)
  13339. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13340. (t (org-2ft s)))))
  13341. (defun org-match-any-p (re list)
  13342. "Does re match any element of list?"
  13343. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13344. (delq nil list))
  13345. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13346. (defvar org-tags-overlay (make-overlay 1 1))
  13347. (org-detach-overlay org-tags-overlay)
  13348. (defun org-get-local-tags-at (&optional pos)
  13349. "Get a list of tags defined in the current headline."
  13350. (org-get-tags-at pos 'local))
  13351. (defun org-get-local-tags ()
  13352. "Get a list of tags defined in the current headline."
  13353. (org-get-tags-at nil 'local))
  13354. (defun org-get-tags-at (&optional pos local)
  13355. "Get a list of all headline tags applicable at POS.
  13356. POS defaults to point. If tags are inherited, the list contains
  13357. the targets in the same sequence as the headlines appear, i.e.
  13358. the tags of the current headline come last.
  13359. When LOCAL is non-nil, only return tags from the current headline,
  13360. ignore inherited ones."
  13361. (interactive)
  13362. (if (and org-trust-scanner-tags
  13363. (or (not pos) (equal pos (point)))
  13364. (not local))
  13365. org-scanner-tags
  13366. (let (tags ltags lastpos parent)
  13367. (save-excursion
  13368. (save-restriction
  13369. (widen)
  13370. (goto-char (or pos (point)))
  13371. (save-match-data
  13372. (catch 'done
  13373. (condition-case nil
  13374. (progn
  13375. (org-back-to-heading t)
  13376. (while (not (equal lastpos (point)))
  13377. (setq lastpos (point))
  13378. (when (looking-at
  13379. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13380. (setq ltags (org-split-string
  13381. (org-match-string-no-properties 1) ":"))
  13382. (when parent
  13383. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13384. (setq tags (append
  13385. (if parent
  13386. (org-remove-uninherited-tags ltags)
  13387. ltags)
  13388. tags)))
  13389. (or org-use-tag-inheritance (throw 'done t))
  13390. (if local (throw 'done t))
  13391. (or (org-up-heading-safe) (error nil))
  13392. (setq parent t)))
  13393. (error nil)))))
  13394. (if local
  13395. tags
  13396. (reverse (delete-dups
  13397. (reverse (append
  13398. (org-remove-uninherited-tags
  13399. org-file-tags) tags)))))))))
  13400. (defun org-add-prop-inherited (s)
  13401. (add-text-properties 0 (length s) '(inherited t) s)
  13402. s)
  13403. (defun org-toggle-tag (tag &optional onoff)
  13404. "Toggle the tag TAG for the current line.
  13405. If ONOFF is `on' or `off', don't toggle but set to this state."
  13406. (let (res current)
  13407. (save-excursion
  13408. (org-back-to-heading t)
  13409. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13410. (point-at-eol) t)
  13411. (progn
  13412. (setq current (match-string 1))
  13413. (replace-match ""))
  13414. (setq current ""))
  13415. (setq current (nreverse (org-split-string current ":")))
  13416. (cond
  13417. ((eq onoff 'on)
  13418. (setq res t)
  13419. (or (member tag current) (push tag current)))
  13420. ((eq onoff 'off)
  13421. (or (not (member tag current)) (setq current (delete tag current))))
  13422. (t (if (member tag current)
  13423. (setq current (delete tag current))
  13424. (setq res t)
  13425. (push tag current))))
  13426. (end-of-line 1)
  13427. (if current
  13428. (progn
  13429. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13430. (org-set-tags nil t))
  13431. (delete-horizontal-space))
  13432. (run-hooks 'org-after-tags-change-hook))
  13433. res))
  13434. (defun org-align-tags-here (to-col)
  13435. ;; Assumes that this is a headline
  13436. "Align tags on the current headline to TO-COL."
  13437. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13438. (beginning-of-line 1)
  13439. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13440. (< pos (match-beginning 2)))
  13441. (progn
  13442. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13443. (goto-char (match-beginning 1))
  13444. (insert " ")
  13445. (delete-region (point) (1+ (match-beginning 2)))
  13446. (setq ncol (max (current-column)
  13447. (1+ col)
  13448. (if (> to-col 0)
  13449. to-col
  13450. (- (abs to-col) tags-l))))
  13451. (setq p (point))
  13452. (insert (make-string (- ncol (current-column)) ?\ ))
  13453. (setq ncol (current-column))
  13454. (when indent-tabs-mode (tabify p (point-at-eol)))
  13455. (org-move-to-column (min ncol col)))
  13456. (goto-char pos))))
  13457. (defun org-set-tags-command (&optional arg just-align)
  13458. "Call the set-tags command for the current entry."
  13459. (interactive "P")
  13460. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13461. (org-set-tags arg just-align)
  13462. (save-excursion
  13463. (unless (and (org-region-active-p)
  13464. org-loop-over-headlines-in-active-region)
  13465. (org-back-to-heading t))
  13466. (org-set-tags arg just-align))))
  13467. (defun org-set-tags-to (data)
  13468. "Set the tags of the current entry to DATA, replacing the current tags.
  13469. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13470. If DATA is nil or the empty string, any tags will be removed."
  13471. (interactive "sTags: ")
  13472. (setq data
  13473. (cond
  13474. ((eq data nil) "")
  13475. ((equal data "") "")
  13476. ((stringp data)
  13477. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13478. ":"))
  13479. ((listp data)
  13480. (concat ":" (mapconcat 'identity data ":") ":"))))
  13481. (when data
  13482. (save-excursion
  13483. (org-back-to-heading t)
  13484. (when (looking-at org-complex-heading-regexp)
  13485. (if (match-end 5)
  13486. (progn
  13487. (goto-char (match-beginning 5))
  13488. (insert data)
  13489. (delete-region (point) (point-at-eol))
  13490. (org-set-tags nil 'align))
  13491. (goto-char (point-at-eol))
  13492. (insert " " data)
  13493. (org-set-tags nil 'align)))
  13494. (beginning-of-line 1)
  13495. (if (looking-at ".*?\\([ \t]+\\)$")
  13496. (delete-region (match-beginning 1) (match-end 1))))))
  13497. (defun org-align-all-tags ()
  13498. "Align the tags i all headings."
  13499. (interactive)
  13500. (save-excursion
  13501. (or (ignore-errors (org-back-to-heading t))
  13502. (outline-next-heading))
  13503. (if (org-at-heading-p)
  13504. (org-set-tags t)
  13505. (message "No headings"))))
  13506. (defvar org-indent-indentation-per-level)
  13507. (defun org-set-tags (&optional arg just-align)
  13508. "Set the tags for the current headline.
  13509. With prefix ARG, realign all tags in headings in the current buffer.
  13510. When JUST-ALIGN is non-nil, only align tags."
  13511. (interactive "P")
  13512. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13513. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13514. 'region-start-level 'region))
  13515. org-loop-over-headlines-in-active-region)
  13516. (org-map-entries
  13517. ;; We don't use ARG and JUST-ALIGN here because these args
  13518. ;; are not useful when looping over headlines.
  13519. `(org-set-tags)
  13520. org-loop-over-headlines-in-active-region
  13521. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13522. (let* ((re org-outline-regexp-bol)
  13523. (current (unless arg (org-get-tags-string)))
  13524. (col (current-column))
  13525. (org-setting-tags t)
  13526. table current-tags inherited-tags ; computed below when needed
  13527. tags p0 c0 c1 rpl di tc level)
  13528. (if arg
  13529. (save-excursion
  13530. (goto-char (point-min))
  13531. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13532. (while (re-search-forward re nil t)
  13533. (org-set-tags nil t)
  13534. (end-of-line 1)))
  13535. (message "All tags realigned to column %d" org-tags-column))
  13536. (if just-align
  13537. (setq tags current)
  13538. ;; Get a new set of tags from the user
  13539. (save-excursion
  13540. (setq table (append org-tag-persistent-alist
  13541. (or org-tag-alist (org-get-buffer-tags))
  13542. (and
  13543. org-complete-tags-always-offer-all-agenda-tags
  13544. (org-global-tags-completion-table
  13545. (org-agenda-files))))
  13546. org-last-tags-completion-table table
  13547. current-tags (org-split-string current ":")
  13548. inherited-tags (nreverse
  13549. (nthcdr (length current-tags)
  13550. (nreverse (org-get-tags-at))))
  13551. tags
  13552. (if (or (eq t org-use-fast-tag-selection)
  13553. (and org-use-fast-tag-selection
  13554. (delq nil (mapcar 'cdr table))))
  13555. (org-fast-tag-selection
  13556. current-tags inherited-tags table
  13557. (if org-fast-tag-selection-include-todo
  13558. org-todo-key-alist))
  13559. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13560. (org-trim
  13561. (org-icompleting-read "Tags: "
  13562. 'org-tags-completion-function
  13563. nil nil current 'org-tags-history))))))
  13564. (while (string-match "[-+&]+" tags)
  13565. ;; No boolean logic, just a list
  13566. (setq tags (replace-match ":" t t tags))))
  13567. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13568. (if org-tags-sort-function
  13569. (setq tags (mapconcat 'identity
  13570. (sort (org-split-string
  13571. tags (org-re "[^[:alnum:]_@#%]+"))
  13572. org-tags-sort-function) ":")))
  13573. (if (string-match "\\`[\t ]*\\'" tags)
  13574. (setq tags "")
  13575. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13576. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13577. ;; Insert new tags at the correct column
  13578. (beginning-of-line 1)
  13579. (setq level (or (and (looking-at org-outline-regexp)
  13580. (- (match-end 0) (point) 1))
  13581. 1))
  13582. (cond
  13583. ((and (equal current "") (equal tags "")))
  13584. ((re-search-forward
  13585. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13586. (point-at-eol) t)
  13587. (if (equal tags "")
  13588. (setq rpl "")
  13589. (goto-char (match-beginning 0))
  13590. (setq c0 (current-column)
  13591. ;; compute offset for the case of org-indent-mode active
  13592. di (if (org-bound-and-true-p org-indent-mode)
  13593. (* (1- org-indent-indentation-per-level) (1- level))
  13594. 0)
  13595. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13596. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13597. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13598. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13599. (replace-match rpl t t)
  13600. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13601. tags)
  13602. (t (error "Tags alignment failed")))
  13603. (org-move-to-column col)
  13604. (unless just-align
  13605. (run-hooks 'org-after-tags-change-hook))))))
  13606. (defun org-change-tag-in-region (beg end tag off)
  13607. "Add or remove TAG for each entry in the region.
  13608. This works in the agenda, and also in an org-mode buffer."
  13609. (interactive
  13610. (list (region-beginning) (region-end)
  13611. (let ((org-last-tags-completion-table
  13612. (if (derived-mode-p 'org-mode)
  13613. (org-uniquify
  13614. (delq nil (append (org-get-buffer-tags)
  13615. (org-global-tags-completion-table))))
  13616. (org-global-tags-completion-table))))
  13617. (org-icompleting-read
  13618. "Tag: " 'org-tags-completion-function nil nil nil
  13619. 'org-tags-history))
  13620. (progn
  13621. (message "[s]et or [r]emove? ")
  13622. (equal (read-char-exclusive) ?r))))
  13623. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13624. (let ((agendap (equal major-mode 'org-agenda-mode))
  13625. l1 l2 m buf pos newhead (cnt 0))
  13626. (goto-char end)
  13627. (setq l2 (1- (org-current-line)))
  13628. (goto-char beg)
  13629. (setq l1 (org-current-line))
  13630. (loop for l from l1 to l2 do
  13631. (org-goto-line l)
  13632. (setq m (get-text-property (point) 'org-hd-marker))
  13633. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13634. (and agendap m))
  13635. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13636. pos (if agendap m (point)))
  13637. (with-current-buffer buf
  13638. (save-excursion
  13639. (save-restriction
  13640. (goto-char pos)
  13641. (setq cnt (1+ cnt))
  13642. (org-toggle-tag tag (if off 'off 'on))
  13643. (setq newhead (org-get-heading)))))
  13644. (and agendap (org-agenda-change-all-lines newhead m))))
  13645. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13646. (defun org-tags-completion-function (string predicate &optional flag)
  13647. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13648. (confirm (lambda (x) (stringp (car x)))))
  13649. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13650. (setq s1 (match-string 1 string)
  13651. s2 (match-string 2 string))
  13652. (setq s1 "" s2 string))
  13653. (cond
  13654. ((eq flag nil)
  13655. ;; try completion
  13656. (setq rtn (try-completion s2 ctable confirm))
  13657. (if (stringp rtn)
  13658. (setq rtn
  13659. (concat s1 s2 (substring rtn (length s2))
  13660. (if (and org-add-colon-after-tag-completion
  13661. (assoc rtn ctable))
  13662. ":" ""))))
  13663. rtn)
  13664. ((eq flag t)
  13665. ;; all-completions
  13666. (all-completions s2 ctable confirm))
  13667. ((eq flag 'lambda)
  13668. ;; exact match?
  13669. (assoc s2 ctable)))))
  13670. (defun org-fast-tag-insert (kwd tags face &optional end)
  13671. "Insert KDW, and the TAGS, the latter with face FACE.
  13672. Also insert END."
  13673. (insert (format "%-12s" (concat kwd ":"))
  13674. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13675. (or end "")))
  13676. (defun org-fast-tag-show-exit (flag)
  13677. (save-excursion
  13678. (org-goto-line 3)
  13679. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13680. (replace-match ""))
  13681. (when flag
  13682. (end-of-line 1)
  13683. (org-move-to-column (- (window-width) 19) t)
  13684. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13685. (defun org-set-current-tags-overlay (current prefix)
  13686. "Add an overlay to CURRENT tag with PREFIX."
  13687. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13688. (if (featurep 'xemacs)
  13689. (org-overlay-display org-tags-overlay (concat prefix s)
  13690. 'secondary-selection)
  13691. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13692. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13693. (defvar org-last-tag-selection-key nil)
  13694. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13695. "Fast tag selection with single keys.
  13696. CURRENT is the current list of tags in the headline, INHERITED is the
  13697. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13698. possibly with grouping information. TODO-TABLE is a similar table with
  13699. TODO keywords, should these have keys assigned to them.
  13700. If the keys are nil, a-z are automatically assigned.
  13701. Returns the new tags string, or nil to not change the current settings."
  13702. (let* ((fulltable (append table todo-table))
  13703. (maxlen (apply 'max (mapcar
  13704. (lambda (x)
  13705. (if (stringp (car x)) (string-width (car x)) 0))
  13706. fulltable)))
  13707. (buf (current-buffer))
  13708. (expert (eq org-fast-tag-selection-single-key 'expert))
  13709. (buffer-tags nil)
  13710. (fwidth (+ maxlen 3 1 3))
  13711. (ncol (/ (- (window-width) 4) fwidth))
  13712. (i-face 'org-done)
  13713. (c-face 'org-todo)
  13714. tg cnt e c char c1 c2 ntable tbl rtn
  13715. ov-start ov-end ov-prefix
  13716. (exit-after-next org-fast-tag-selection-single-key)
  13717. (done-keywords org-done-keywords)
  13718. groups ingroup)
  13719. (save-excursion
  13720. (beginning-of-line 1)
  13721. (if (looking-at
  13722. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13723. (setq ov-start (match-beginning 1)
  13724. ov-end (match-end 1)
  13725. ov-prefix "")
  13726. (setq ov-start (1- (point-at-eol))
  13727. ov-end (1+ ov-start))
  13728. (skip-chars-forward "^\n\r")
  13729. (setq ov-prefix
  13730. (concat
  13731. (buffer-substring (1- (point)) (point))
  13732. (if (> (current-column) org-tags-column)
  13733. " "
  13734. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13735. (move-overlay org-tags-overlay ov-start ov-end)
  13736. (save-window-excursion
  13737. (if expert
  13738. (set-buffer (get-buffer-create " *Org tags*"))
  13739. (delete-other-windows)
  13740. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13741. (org-switch-to-buffer-other-window " *Org tags*"))
  13742. (erase-buffer)
  13743. (org-set-local 'org-done-keywords done-keywords)
  13744. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13745. (org-fast-tag-insert "Current" current c-face "\n\n")
  13746. (org-fast-tag-show-exit exit-after-next)
  13747. (org-set-current-tags-overlay current ov-prefix)
  13748. (setq tbl fulltable char ?a cnt 0)
  13749. (while (setq e (pop tbl))
  13750. (cond
  13751. ((equal (car e) :startgroup)
  13752. (push '() groups) (setq ingroup t)
  13753. (when (not (= cnt 0))
  13754. (setq cnt 0)
  13755. (insert "\n"))
  13756. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13757. ((equal (car e) :endgroup)
  13758. (setq ingroup nil cnt 0)
  13759. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13760. ((equal e '(:newline))
  13761. (when (not (= cnt 0))
  13762. (setq cnt 0)
  13763. (insert "\n")
  13764. (setq e (car tbl))
  13765. (while (equal (car tbl) '(:newline))
  13766. (insert "\n")
  13767. (setq tbl (cdr tbl)))))
  13768. ((equal e '(:grouptags)) nil)
  13769. (t
  13770. (setq tg (copy-sequence (car e)) c2 nil)
  13771. (if (cdr e)
  13772. (setq c (cdr e))
  13773. ;; automatically assign a character.
  13774. (setq c1 (string-to-char
  13775. (downcase (substring
  13776. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13777. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13778. (while (or (rassoc char ntable) (rassoc char table))
  13779. (setq char (1+ char)))
  13780. (setq c2 c1))
  13781. (setq c (or c2 char)))
  13782. (if ingroup (push tg (car groups)))
  13783. (setq tg (org-add-props tg nil 'face
  13784. (cond
  13785. ((not (assoc tg table))
  13786. (org-get-todo-face tg))
  13787. ((member tg current) c-face)
  13788. ((member tg inherited) i-face))))
  13789. (if (equal (caar tbl) :grouptags)
  13790. (org-add-props tg nil 'face 'org-tag-group))
  13791. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13792. (insert "[" c "] " tg (make-string
  13793. (- fwidth 4 (length tg)) ?\ ))
  13794. (push (cons tg c) ntable)
  13795. (when (= (setq cnt (1+ cnt)) ncol)
  13796. (insert "\n")
  13797. (if ingroup (insert " "))
  13798. (setq cnt 0)))))
  13799. (setq ntable (nreverse ntable))
  13800. (insert "\n")
  13801. (goto-char (point-min))
  13802. (if (not expert) (org-fit-window-to-buffer))
  13803. (setq rtn
  13804. (catch 'exit
  13805. (while t
  13806. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13807. (if (not groups) "no " "")
  13808. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13809. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13810. (setq org-last-tag-selection-key c)
  13811. (cond
  13812. ((= c ?\r) (throw 'exit t))
  13813. ((= c ?!)
  13814. (setq groups (not groups))
  13815. (goto-char (point-min))
  13816. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13817. ((= c ?\C-c)
  13818. (if (not expert)
  13819. (org-fast-tag-show-exit
  13820. (setq exit-after-next (not exit-after-next)))
  13821. (setq expert nil)
  13822. (delete-other-windows)
  13823. (set-window-buffer (split-window-vertically) " *Org tags*")
  13824. (org-switch-to-buffer-other-window " *Org tags*")
  13825. (org-fit-window-to-buffer)))
  13826. ((or (= c ?\C-g)
  13827. (and (= c ?q) (not (rassoc c ntable))))
  13828. (org-detach-overlay org-tags-overlay)
  13829. (setq quit-flag t))
  13830. ((= c ?\ )
  13831. (setq current nil)
  13832. (if exit-after-next (setq exit-after-next 'now)))
  13833. ((= c ?\t)
  13834. (condition-case nil
  13835. (setq tg (org-icompleting-read
  13836. "Tag: "
  13837. (or buffer-tags
  13838. (with-current-buffer buf
  13839. (org-get-buffer-tags)))))
  13840. (quit (setq tg "")))
  13841. (when (string-match "\\S-" tg)
  13842. (add-to-list 'buffer-tags (list tg))
  13843. (if (member tg current)
  13844. (setq current (delete tg current))
  13845. (push tg current)))
  13846. (if exit-after-next (setq exit-after-next 'now)))
  13847. ((setq e (rassoc c todo-table) tg (car e))
  13848. (with-current-buffer buf
  13849. (save-excursion (org-todo tg)))
  13850. (if exit-after-next (setq exit-after-next 'now)))
  13851. ((setq e (rassoc c ntable) tg (car e))
  13852. (if (member tg current)
  13853. (setq current (delete tg current))
  13854. (loop for g in groups do
  13855. (if (member tg g)
  13856. (mapc (lambda (x)
  13857. (setq current (delete x current)))
  13858. g)))
  13859. (push tg current))
  13860. (if exit-after-next (setq exit-after-next 'now))))
  13861. ;; Create a sorted list
  13862. (setq current
  13863. (sort current
  13864. (lambda (a b)
  13865. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13866. (if (eq exit-after-next 'now) (throw 'exit t))
  13867. (goto-char (point-min))
  13868. (beginning-of-line 2)
  13869. (delete-region (point) (point-at-eol))
  13870. (org-fast-tag-insert "Current" current c-face)
  13871. (org-set-current-tags-overlay current ov-prefix)
  13872. (while (re-search-forward
  13873. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13874. (setq tg (match-string 1))
  13875. (add-text-properties
  13876. (match-beginning 1) (match-end 1)
  13877. (list 'face
  13878. (cond
  13879. ((member tg current) c-face)
  13880. ((member tg inherited) i-face)
  13881. (t (get-text-property (match-beginning 1) 'face))))))
  13882. (goto-char (point-min)))))
  13883. (org-detach-overlay org-tags-overlay)
  13884. (if rtn
  13885. (mapconcat 'identity current ":")
  13886. nil))))
  13887. (defun org-get-tags-string ()
  13888. "Get the TAGS string in the current headline."
  13889. (unless (org-at-heading-p t)
  13890. (user-error "Not on a heading"))
  13891. (save-excursion
  13892. (beginning-of-line 1)
  13893. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13894. (org-match-string-no-properties 1)
  13895. "")))
  13896. (defun org-get-tags ()
  13897. "Get the list of tags specified in the current headline."
  13898. (org-split-string (org-get-tags-string) ":"))
  13899. (defun org-get-buffer-tags ()
  13900. "Get a table of all tags used in the buffer, for completion."
  13901. (org-with-wide-buffer
  13902. (goto-char (point-min))
  13903. (let ((tag-re (concat org-outline-regexp-bol
  13904. "\\(?:.*?[ \t]\\)?"
  13905. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13906. tags)
  13907. (while (re-search-forward tag-re nil t)
  13908. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13909. (push tag tags)))
  13910. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13911. ;;;; The mapping API
  13912. (defun org-map-entries (func &optional match scope &rest skip)
  13913. "Call FUNC at each headline selected by MATCH in SCOPE.
  13914. FUNC is a function or a lisp form. The function will be called without
  13915. arguments, with the cursor positioned at the beginning of the headline.
  13916. The return values of all calls to the function will be collected and
  13917. returned as a list.
  13918. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13919. does not need to preserve point. After evaluation, the cursor will be
  13920. moved to the end of the line (presumably of the headline of the
  13921. processed entry) and search continues from there. Under some
  13922. circumstances, this may not produce the wanted results. For example,
  13923. if you have removed (e.g. archived) the current (sub)tree it could
  13924. mean that the next entry will be skipped entirely. In such cases, you
  13925. can specify the position from where search should continue by making
  13926. FUNC set the variable `org-map-continue-from' to the desired buffer
  13927. position.
  13928. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13929. Only headlines that are matched by this query will be considered during
  13930. the iteration. When MATCH is nil or t, all headlines will be
  13931. visited by the iteration.
  13932. SCOPE determines the scope of this command. It can be any of:
  13933. nil The current buffer, respecting the restriction if any
  13934. tree The subtree started with the entry at point
  13935. region The entries within the active region, if any
  13936. region-start-level
  13937. The entries within the active region, but only those at
  13938. the same level than the first one.
  13939. file The current buffer, without restriction
  13940. file-with-archives
  13941. The current buffer, and any archives associated with it
  13942. agenda All agenda files
  13943. agenda-with-archives
  13944. All agenda files with any archive files associated with them
  13945. \(file1 file2 ...)
  13946. If this is a list, all files in the list will be scanned
  13947. The remaining args are treated as settings for the skipping facilities of
  13948. the scanner. The following items can be given here:
  13949. archive skip trees with the archive tag
  13950. comment skip trees with the COMMENT keyword
  13951. function or Emacs Lisp form:
  13952. will be used as value for `org-agenda-skip-function', so
  13953. whenever the function returns a position, FUNC will not be
  13954. called for that entry and search will continue from the
  13955. position returned
  13956. If your function needs to retrieve the tags including inherited tags
  13957. at the *current* entry, you can use the value of the variable
  13958. `org-scanner-tags' which will be much faster than getting the value
  13959. with `org-get-tags-at'. If your function gets properties with
  13960. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13961. to t around the call to `org-entry-properties' to get the same speedup.
  13962. Note that if your function moves around to retrieve tags and properties at
  13963. a *different* entry, you cannot use these techniques."
  13964. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13965. (not (org-region-active-p)))
  13966. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13967. (org-agenda-skip-archived-trees (memq 'archive skip))
  13968. (org-agenda-skip-comment-trees (memq 'comment skip))
  13969. (org-agenda-skip-function
  13970. (car (org-delete-all '(comment archive) skip)))
  13971. (org-tags-match-list-sublevels t)
  13972. (start-level (eq scope 'region-start-level))
  13973. matcher file res
  13974. org-todo-keywords-for-agenda
  13975. org-done-keywords-for-agenda
  13976. org-todo-keyword-alist-for-agenda
  13977. org-tag-alist-for-agenda
  13978. todo-only)
  13979. (cond
  13980. ((eq match t) (setq matcher t))
  13981. ((eq match nil) (setq matcher t))
  13982. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13983. (save-excursion
  13984. (save-restriction
  13985. (cond ((eq scope 'tree)
  13986. (org-back-to-heading t)
  13987. (org-narrow-to-subtree)
  13988. (setq scope nil))
  13989. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13990. (org-region-active-p))
  13991. ;; If needed, set start-level to a string like "2"
  13992. (when start-level
  13993. (save-excursion
  13994. (goto-char (region-beginning))
  13995. (unless (org-at-heading-p) (outline-next-heading))
  13996. (setq start-level (org-current-level))))
  13997. (narrow-to-region (region-beginning)
  13998. (save-excursion
  13999. (goto-char (region-end))
  14000. (unless (and (bolp) (org-at-heading-p))
  14001. (outline-next-heading))
  14002. (point)))
  14003. (setq scope nil)))
  14004. (if (not scope)
  14005. (progn
  14006. (org-agenda-prepare-buffers
  14007. (list (buffer-file-name (current-buffer))))
  14008. (setq res (org-scan-tags func matcher todo-only start-level)))
  14009. ;; Get the right scope
  14010. (cond
  14011. ((and scope (listp scope) (symbolp (car scope)))
  14012. (setq scope (eval scope)))
  14013. ((eq scope 'agenda)
  14014. (setq scope (org-agenda-files t)))
  14015. ((eq scope 'agenda-with-archives)
  14016. (setq scope (org-agenda-files t))
  14017. (setq scope (org-add-archive-files scope)))
  14018. ((eq scope 'file)
  14019. (setq scope (list (buffer-file-name))))
  14020. ((eq scope 'file-with-archives)
  14021. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14022. (org-agenda-prepare-buffers scope)
  14023. (while (setq file (pop scope))
  14024. (with-current-buffer (org-find-base-buffer-visiting file)
  14025. (save-excursion
  14026. (save-restriction
  14027. (widen)
  14028. (goto-char (point-min))
  14029. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14030. res)))
  14031. ;;; Properties API
  14032. (defconst org-special-properties
  14033. '("ALLTAGS" "BLOCKED" "CATEGORY" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE"
  14034. "FILE" "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA"
  14035. "TODO")
  14036. "The special properties valid in Org mode.
  14037. These are properties that are not defined in the property drawer,
  14038. but in some other way.")
  14039. (defconst org-default-properties
  14040. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14041. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14042. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14043. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14044. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14045. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14046. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14047. "Some properties that are used by Org mode for various purposes.
  14048. Being in this list makes sure that they are offered for completion.")
  14049. (defun org--update-property-plist (key val props)
  14050. "Associate KEY to VAL in alist PROPS.
  14051. Modifications are made by side-effect. Return new alist."
  14052. (let* ((appending (string= (substring key -1) "+"))
  14053. (key (if appending (substring key 0 -1) key))
  14054. (old (assoc-string key props t)))
  14055. (if (not old) (cons (cons key val) props)
  14056. (setcdr old (if appending (concat (cdr old) " " val) val))
  14057. props)))
  14058. (defun org-get-property-block (&optional beg force)
  14059. "Return the (beg . end) range of the body of the property drawer.
  14060. BEG is the beginning of the current subtree, or of the part
  14061. before the first headline. If it is not given, it will be found.
  14062. If the drawer does not exist, create it if FORCE is non-nil, or
  14063. return nil."
  14064. (org-with-wide-buffer
  14065. (when beg (goto-char beg))
  14066. (unless (org-before-first-heading-p)
  14067. (let ((beg (cond (beg)
  14068. ((or (not (featurep 'org-inlinetask))
  14069. (org-inlinetask-in-task-p))
  14070. (org-back-to-heading t))
  14071. (t (org-with-limited-levels (org-back-to-heading t))))))
  14072. (forward-line)
  14073. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14074. (cond ((looking-at org-property-drawer-re)
  14075. (forward-line)
  14076. (cons (point) (progn (goto-char (match-end 0))
  14077. (line-beginning-position))))
  14078. (force
  14079. (goto-char beg)
  14080. (org-insert-property-drawer)
  14081. (let ((pos (save-excursion (search-forward ":END:")
  14082. (line-beginning-position))))
  14083. (cons pos pos))))))))
  14084. (defun org-at-property-p ()
  14085. "Non-nil when point is inside a property drawer.
  14086. See `org-property-re' for match data, if applicable."
  14087. (save-excursion
  14088. (beginning-of-line)
  14089. (and (looking-at org-property-re)
  14090. (let ((property-drawer (save-match-data (org-get-property-block))))
  14091. (and property-drawer (< (point) (cdr property-drawer)))))))
  14092. (defun org-property-action ()
  14093. "Do an action on properties."
  14094. (interactive)
  14095. (unless (org-at-property-p) (user-error "Not at a property"))
  14096. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14097. (let ((c (read-char-exclusive)))
  14098. (case c
  14099. (?s (call-interactively #'org-set-property))
  14100. (?d (call-interactively #'org-delete-property))
  14101. (?D (call-interactively #'org-delete-property-globally))
  14102. (?c (call-interactively #'org-compute-property-at-point))
  14103. (otherwise (user-error "No such property action %c" c)))))
  14104. (defun org-inc-effort ()
  14105. "Increment the value of the effort property in the current entry."
  14106. (interactive)
  14107. (org-set-effort nil t))
  14108. (defvar org-clock-effort) ; Defined in org-clock.el.
  14109. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14110. (defun org-set-effort (&optional value increment)
  14111. "Set the effort property of the current entry.
  14112. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14113. 10th allowed value.
  14114. When INCREMENT is non-nil, set the property to the next allowed value."
  14115. (interactive "P")
  14116. (if (equal value 0) (setq value 10))
  14117. (let* ((completion-ignore-case t)
  14118. (prop org-effort-property)
  14119. (cur (org-entry-get nil prop))
  14120. (allowed (org-property-get-allowed-values nil prop 'table))
  14121. (existing (mapcar 'list (org-property-values prop)))
  14122. (heading (nth 4 (org-heading-components)))
  14123. rpl
  14124. (val (cond
  14125. ((stringp value) value)
  14126. ((and allowed (integerp value))
  14127. (or (car (nth (1- value) allowed))
  14128. (car (org-last allowed))))
  14129. ((and allowed increment)
  14130. (or (caadr (member (list cur) allowed))
  14131. (user-error "Allowed effort values are not set")))
  14132. (allowed
  14133. (message "Select 1-9,0, [RET%s]: %s"
  14134. (if cur (concat "=" cur) "")
  14135. (mapconcat 'car allowed " "))
  14136. (setq rpl (read-char-exclusive))
  14137. (if (equal rpl ?\r)
  14138. cur
  14139. (setq rpl (- rpl ?0))
  14140. (if (equal rpl 0) (setq rpl 10))
  14141. (if (and (> rpl 0) (<= rpl (length allowed)))
  14142. (car (nth (1- rpl) allowed))
  14143. (org-completing-read "Effort: " allowed nil))))
  14144. (t
  14145. (let (org-completion-use-ido org-completion-use-iswitchb)
  14146. (org-completing-read
  14147. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14148. (concat "[" cur "]") "")
  14149. ": ")
  14150. existing nil nil "" nil cur))))))
  14151. (unless (equal (org-entry-get nil prop) val)
  14152. (org-entry-put nil prop val))
  14153. (org-refresh-property
  14154. '((effort . identity)
  14155. (effort-minutes . org-duration-string-to-minutes))
  14156. val)
  14157. (when (string= heading org-clock-current-task)
  14158. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14159. (org-clock-update-mode-line))
  14160. (message "%s is now %s" prop val)))
  14161. (defun org-entry-properties (&optional pom which)
  14162. "Get all properties of the current entry.
  14163. When POM is a buffer position, get all properties from the entry
  14164. there instead.
  14165. This includes the TODO keyword, the tags, time strings for
  14166. deadline, scheduled, and clocking, and any additional properties
  14167. defined in the entry.
  14168. If WHICH is nil or `all', get all properties. If WHICH is
  14169. `special' or `standard', only get that subclass. If WHICH is
  14170. a string, only get that property.
  14171. Return value is an alist. Keys are properties, as upcased
  14172. strings."
  14173. (org-with-point-at pom
  14174. (when (and (derived-mode-p 'org-mode)
  14175. (ignore-errors (org-back-to-heading t)))
  14176. (catch 'exit
  14177. (let* ((beg (point))
  14178. (specific (and (stringp which) (upcase which)))
  14179. (which (cond ((not specific) which)
  14180. ((member specific org-special-properties) 'special)
  14181. (t 'standard)))
  14182. props)
  14183. ;; Get the special properties, like TODO and TAGS.
  14184. (when (memq which '(nil all special))
  14185. (when (or (not specific) (string= specific "CLOCKSUM"))
  14186. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14187. (when clocksum
  14188. (push (cons "CLOCKSUM"
  14189. (org-columns-number-to-string
  14190. (/ (float clocksum) 60.) 'add_times))
  14191. props)))
  14192. (when specific (throw 'exit props)))
  14193. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14194. (let ((clocksumt (get-text-property (point)
  14195. :org-clock-minutes-today)))
  14196. (when clocksumt
  14197. (push (cons "CLOCKSUM_T"
  14198. (org-columns-number-to-string
  14199. (/ (float clocksumt) 60.) 'add_times))
  14200. props)))
  14201. (when specific (throw 'exit props)))
  14202. (when (or (not specific) (string= specific "ITEM"))
  14203. (when (looking-at org-complex-heading-regexp)
  14204. (push (cons "ITEM"
  14205. (concat
  14206. (org-match-string-no-properties 1)
  14207. (let ((title (org-match-string-no-properties 4)))
  14208. (when (org-string-nw-p title)
  14209. (concat " " (org-remove-tabs title))))))
  14210. props))
  14211. (when specific (throw 'exit props)))
  14212. (when (or (not specific) (string= specific "TODO"))
  14213. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14214. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14215. (when specific (throw 'exit props)))
  14216. (when (or (not specific) (string= specific "PRIORITY"))
  14217. (when (looking-at org-priority-regexp)
  14218. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14219. (when specific (throw 'exit props)))
  14220. (when (or (not specific) (string= specific "FILE"))
  14221. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14222. props)
  14223. (when specific (throw 'exit props)))
  14224. (when (or (not specific) (string= specific "TAGS"))
  14225. (let ((value (org-string-nw-p (org-get-tags-string))))
  14226. (when value (push (cons "TAGS" value) props)))
  14227. (when specific (throw 'exit props)))
  14228. (when (or (not specific) (string= specific "ALLTAGS"))
  14229. (let ((value (org-get-tags-at)))
  14230. (when value
  14231. (push (cons "ALLTAGS"
  14232. (format ":%s:" (mapconcat #'identity value ":")))
  14233. props)))
  14234. (when specific (throw 'exit props)))
  14235. (when (or (not specific) (string= specific "BLOCKED"))
  14236. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14237. (when specific (throw 'exit props)))
  14238. (when (or (not specific)
  14239. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14240. (forward-line)
  14241. (when (org-looking-at-p org-planning-line-re)
  14242. (end-of-line)
  14243. (let ((bol (line-beginning-position))
  14244. ;; Backward compatibility: time keywords used to
  14245. ;; be configurable (before 8.3). Make sure we
  14246. ;; get the correct keyword.
  14247. (key-assoc `(("CLOSED" . ,org-closed-string)
  14248. ("DEADLINE" . ,org-deadline-string)
  14249. ("SCHEDULED" . ,org-scheduled-string))))
  14250. (dolist (pair (if specific (list (assoc specific key-assoc))
  14251. key-assoc))
  14252. (save-excursion
  14253. (when (search-backward (cdr pair) bol t)
  14254. (goto-char (match-end 0))
  14255. (skip-chars-forward " \t")
  14256. (and (looking-at org-ts-regexp-both)
  14257. (push (cons (car pair)
  14258. (org-match-string-no-properties 0))
  14259. props)))))))
  14260. (when specific (throw 'exit props)))
  14261. (when (or (not specific)
  14262. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14263. (let ((find-ts
  14264. (lambda (end ts)
  14265. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14266. (assoc "TIMESTAMP_IA" ts))
  14267. org-ts-regexp
  14268. org-ts-regexp-both)))
  14269. (catch 'next
  14270. (while (re-search-forward regexp end t)
  14271. (backward-char)
  14272. (let ((object (org-element-context)))
  14273. ;; Accept to match timestamps in node
  14274. ;; properties, too.
  14275. (when (memq (org-element-type object)
  14276. '(node-property timestamp))
  14277. (let ((type
  14278. (org-element-property :type object)))
  14279. (cond
  14280. ((and (memq type '(active active-range))
  14281. (not (equal specific "TIMESTAMP_IA")))
  14282. (unless (assoc "TIMESTAMP" ts)
  14283. (push (cons "TIMESTAMP"
  14284. (org-element-property
  14285. :raw-value object))
  14286. ts)
  14287. (when specific (throw 'exit ts))))
  14288. ((and (memq type '(inactive inactive-range))
  14289. (not (string= specific "TIMESTAMP")))
  14290. (unless (assoc "TIMESTAMP_IA" ts)
  14291. (push (cons "TIMESTAMP_IA"
  14292. (org-element-property
  14293. :raw-value object))
  14294. ts)
  14295. (when specific (throw 'exit ts))))))
  14296. ;; Both timestamp types are found,
  14297. ;; move to next part.
  14298. (when (= (length ts) 2) (throw 'next ts)))))
  14299. ts)))))
  14300. (goto-char beg)
  14301. ;; First look for timestamps within headline.
  14302. (let ((ts (funcall find-ts (line-end-position) nil)))
  14303. (if (= (length ts) 2) (setq props (nconc ts props))
  14304. (forward-line)
  14305. ;; Then find timestamps in the section, skipping
  14306. ;; planning line.
  14307. (when (org-looking-at-p org-planning-line-re)
  14308. (forward-line))
  14309. (let ((end (save-excursion (outline-next-heading))))
  14310. (setq props (nconc (funcall find-ts end ts) props))))))))
  14311. ;; Get the standard properties, like :PROP:.
  14312. (when (memq which '(nil all standard))
  14313. ;; If we are looking after a specific property, delegate
  14314. ;; to `org-entry-get', which is faster. However, make an
  14315. ;; exception for "CATEGORY", since it can be also set
  14316. ;; through keywords (i.e. #+CATEGORY).
  14317. (if (and specific (not (equal specific "CATEGORY")))
  14318. (let ((value (org-entry-get beg specific nil t)))
  14319. (throw 'exit (and value (list (cons specific value)))))
  14320. (let ((range (org-get-property-block beg)))
  14321. (when range
  14322. (let ((end (cdr range)) seen-base)
  14323. (goto-char (car range))
  14324. ;; Unlike to `org--update-property-plist', we
  14325. ;; handle the case where base values is found
  14326. ;; after its extension. We also forbid standard
  14327. ;; properties to be named as special properties.
  14328. (while (re-search-forward org-property-re end t)
  14329. (let* ((key (upcase (org-match-string-no-properties 2)))
  14330. (extendp (org-string-match-p "\\+\\'" key))
  14331. (key-base (if extendp (substring key 0 -1) key))
  14332. (value (org-match-string-no-properties 3)))
  14333. (cond
  14334. ((member-ignore-case key-base org-special-properties))
  14335. (extendp
  14336. (setq props
  14337. (org--update-property-plist key value props)))
  14338. ((member key seen-base))
  14339. (t (push key seen-base)
  14340. (let ((p (assoc-string key props t)))
  14341. (if p (setcdr p (concat value " " (cdr p)))
  14342. (push (cons key value) props))))))))))))
  14343. (unless (assoc "CATEGORY" props)
  14344. (push (cons "CATEGORY" (org-get-category beg)) props)
  14345. (when (string= specific "CATEGORY") (throw 'exit props)))
  14346. ;; Return value.
  14347. (append (get-text-property beg 'org-summaries) props))))))
  14348. (defun org-entry-get (pom property &optional inherit literal-nil)
  14349. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14350. If INHERIT is non-nil and the entry does not have the property,
  14351. then also check higher levels of the hierarchy. If INHERIT is
  14352. the symbol `selective', use inheritance only if the setting in
  14353. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14354. If the property is present but empty, the return value is the
  14355. empty string. If the property is not present at all, nil is
  14356. returned. In any other case, return the value as a string.
  14357. Search is case-insensitive.
  14358. If LITERAL-NIL is set, return the string value \"nil\" as
  14359. a string, do not interpret it as the list atom nil. This is used
  14360. for inheritance when a \"nil\" value can supersede a non-nil
  14361. value higher up the hierarchy."
  14362. (org-with-point-at pom
  14363. (cond
  14364. ((and inherit
  14365. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14366. (org-entry-get-with-inheritance property literal-nil))
  14367. ((member-ignore-case property org-special-properties)
  14368. ;; We need a special property. Use `org-entry-properties' to
  14369. ;; retrieve it, but specify the wanted property.
  14370. (cdr (assoc-string property (org-entry-properties nil property))))
  14371. (t
  14372. (let ((range (org-get-property-block)))
  14373. (when range
  14374. (let* ((case-fold-search t)
  14375. (end (cdr range))
  14376. (props
  14377. (let ((global
  14378. (or (assoc-string property org-file-properties t)
  14379. (assoc-string property org-global-properties t)
  14380. (assoc-string
  14381. property org-global-properties-fixed t))))
  14382. ;; Make sure to not re-use GLOBAL as
  14383. ;; `org--update-property-plist' would alter it by
  14384. ;; side-effect.
  14385. (and global (list (cons property (cdr global))))))
  14386. (find-value
  14387. (lambda (key)
  14388. (when (re-search-forward (org-re-property key nil t) end t)
  14389. (setq props
  14390. (org--update-property-plist
  14391. key (org-match-string-no-properties 3) props))))))
  14392. (goto-char (car range))
  14393. ;; Find base value.
  14394. (save-excursion (funcall find-value property))
  14395. ;; Find additional values.
  14396. (let ((property+ (concat property "+")))
  14397. (while (funcall find-value property+)))
  14398. ;; Return final value.
  14399. (let ((val (cdr (assoc-string property props t))))
  14400. (if literal-nil val (org-not-nil val))))))))))
  14401. (defun org-property-or-variable-value (var &optional inherit)
  14402. "Check if there is a property fixing the value of VAR.
  14403. If yes, return this value. If not, return the current value of the variable."
  14404. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14405. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14406. (read prop)
  14407. (symbol-value var))))
  14408. (defun org-entry-delete (pom property)
  14409. "Delete the property PROPERTY from entry at point-or-marker POM."
  14410. (unless (member property org-special-properties)
  14411. (org-with-point-at pom
  14412. (let ((range (org-get-property-block)))
  14413. (when range
  14414. (let ((begin (car range))
  14415. (end (copy-marker (cdr range))))
  14416. (goto-char begin)
  14417. (when (re-search-forward (org-re-property property nil t) end t)
  14418. (delete-region (match-beginning 0) (line-beginning-position 2))
  14419. ;; If drawer is empty, remove it altogether.
  14420. (when (= begin end)
  14421. (delete-region (line-beginning-position 0)
  14422. (line-beginning-position 2)))
  14423. (set-marker end nil))))))))
  14424. ;; Multi-values properties are properties that contain multiple values
  14425. ;; These values are assumed to be single words, separated by whitespace.
  14426. (defun org-entry-add-to-multivalued-property (pom property value)
  14427. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14428. (let* ((old (org-entry-get pom property))
  14429. (values (and old (org-split-string old "[ \t]"))))
  14430. (setq value (org-entry-protect-space value))
  14431. (unless (member value values)
  14432. (setq values (append values (list value)))
  14433. (org-entry-put pom property
  14434. (mapconcat 'identity values " ")))))
  14435. (defun org-entry-remove-from-multivalued-property (pom property value)
  14436. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14437. (let* ((old (org-entry-get pom property))
  14438. (values (and old (org-split-string old "[ \t]"))))
  14439. (setq value (org-entry-protect-space value))
  14440. (when (member value values)
  14441. (setq values (delete value values))
  14442. (org-entry-put pom property
  14443. (mapconcat 'identity values " ")))))
  14444. (defun org-entry-member-in-multivalued-property (pom property value)
  14445. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14446. (let* ((old (org-entry-get pom property))
  14447. (values (and old (org-split-string old "[ \t]"))))
  14448. (setq value (org-entry-protect-space value))
  14449. (member value values)))
  14450. (defun org-entry-get-multivalued-property (pom property)
  14451. "Return a list of values in a multivalued property."
  14452. (let* ((value (org-entry-get pom property))
  14453. (values (and value (org-split-string value "[ \t]"))))
  14454. (mapcar 'org-entry-restore-space values)))
  14455. (defun org-entry-put-multivalued-property (pom property &rest values)
  14456. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14457. VALUES should be a list of strings. Spaces will be protected."
  14458. (org-entry-put pom property
  14459. (mapconcat 'org-entry-protect-space values " "))
  14460. (let* ((value (org-entry-get pom property))
  14461. (values (and value (org-split-string value "[ \t]"))))
  14462. (mapcar 'org-entry-restore-space values)))
  14463. (defun org-entry-protect-space (s)
  14464. "Protect spaces and newline in string S."
  14465. (while (string-match " " s)
  14466. (setq s (replace-match "%20" t t s)))
  14467. (while (string-match "\n" s)
  14468. (setq s (replace-match "%0A" t t s)))
  14469. s)
  14470. (defun org-entry-restore-space (s)
  14471. "Restore spaces and newline in string S."
  14472. (while (string-match "%20" s)
  14473. (setq s (replace-match " " t t s)))
  14474. (while (string-match "%0A" s)
  14475. (setq s (replace-match "\n" t t s)))
  14476. s)
  14477. (defvar org-entry-property-inherited-from (make-marker)
  14478. "Marker pointing to the entry from where a property was inherited.
  14479. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14480. location of the entry where the inheritance search matched. If there was
  14481. no match, the marker will point nowhere.
  14482. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14483. is set.")
  14484. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14485. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14486. The search will stop at the first ancestor which has the property defined.
  14487. If the value found is \"nil\", return nil to show that the property
  14488. should be considered as undefined (this is the meaning of nil here).
  14489. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14490. (move-marker org-entry-property-inherited-from nil)
  14491. (let (value)
  14492. (org-with-wide-buffer
  14493. (catch 'exit
  14494. (while t
  14495. (when (setq value (org-entry-get nil property nil literal-nil))
  14496. (org-back-to-heading t)
  14497. (move-marker org-entry-property-inherited-from (point))
  14498. (throw 'exit nil))
  14499. (or (org-up-heading-safe) (throw 'exit nil)))))
  14500. (unless value
  14501. (setq value
  14502. (cdr (or (assoc-string property org-file-properties t)
  14503. (assoc-string property org-global-properties t)
  14504. (assoc-string property org-global-properties-fixed t)))))
  14505. (if literal-nil value (org-not-nil value))))
  14506. (defvar org-property-changed-functions nil
  14507. "Hook called when the value of a property has changed.
  14508. Each hook function should accept two arguments, the name of the property
  14509. and the new value.")
  14510. (defun org-entry-put (pom property value)
  14511. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14512. If the value is `nil', it is converted to the empty string. If
  14513. it is not a string, an error is raised.
  14514. PROPERTY can be any regular property (see
  14515. `org-special-properties'). It can also be \"TODO\",
  14516. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14517. For the last two properties, VALUE may have any of the special
  14518. values \"earlier\" and \"later\". The function then increases or
  14519. decreases scheduled or deadline date by one day."
  14520. (cond ((null value) (setq value ""))
  14521. ((not (stringp value)) (error "Properties values should be strings")))
  14522. (org-with-point-at pom
  14523. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14524. (org-back-to-heading t)
  14525. (org-with-limited-levels (org-back-to-heading t)))
  14526. (let ((beg (point)))
  14527. (cond
  14528. ((equal property "TODO")
  14529. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14530. ((not (member value org-todo-keywords-1))
  14531. (user-error "\"%s\" is not a valid TODO state" value)))
  14532. (org-todo value)
  14533. (org-set-tags nil 'align))
  14534. ((equal property "PRIORITY")
  14535. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14536. (org-set-tags nil 'align))
  14537. ((equal property "SCHEDULED")
  14538. (forward-line)
  14539. (if (and (org-looking-at-p org-planning-line-re)
  14540. (re-search-forward
  14541. org-scheduled-time-regexp (line-end-position) t))
  14542. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14543. ((string= value "later") (org-timestamp-change 1 'day))
  14544. ((string= value "") (org-schedule '(4)))
  14545. (t (org-schedule nil value)))
  14546. (if (member value '("earlier" "later" ""))
  14547. (call-interactively #'org-schedule)
  14548. (org-schedule nil value))))
  14549. ((equal property "DEADLINE")
  14550. (forward-line)
  14551. (if (and (org-looking-at-p org-planning-line-re)
  14552. (re-search-forward
  14553. org-deadline-time-regexp (line-end-position) t))
  14554. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14555. ((string= value "later") (org-timestamp-change 1 'day))
  14556. ((string= value "") (org-deadline '(4)))
  14557. (t (org-deadline nil value)))
  14558. (if (member value '("earlier" "later" ""))
  14559. (call-interactively #'org-deadline)
  14560. (org-deadline nil value))))
  14561. ((member property org-special-properties)
  14562. (error "The %s property cannot be set with `org-entry-put'" property))
  14563. (t
  14564. (let* ((range (org-get-property-block beg 'force))
  14565. (end (cdr range))
  14566. (case-fold-search t))
  14567. (goto-char (car range))
  14568. (if (re-search-forward (org-re-property property nil t) end t)
  14569. (progn (delete-region (match-beginning 0) (match-end 0))
  14570. (goto-char (match-beginning 0)))
  14571. (goto-char end)
  14572. (insert "\n")
  14573. (backward-char))
  14574. (insert ":" property ":")
  14575. (when value (insert " " value))
  14576. (org-indent-line)))))
  14577. (run-hook-with-args 'org-property-changed-functions property value)))
  14578. (defun org-buffer-property-keys (&optional specials defaults columns)
  14579. "Get all property keys in the current buffer.
  14580. When SPECIALS is non-nil, also list the special properties that
  14581. reflect things like tags and TODO state.
  14582. When DEFAULTS is non-nil, also include properties that has
  14583. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14584. DESCRIPTION, LOCATION, and LOGGING and others.
  14585. When COLUMNS in non-nil, also include property names given in
  14586. COLUMN formats in the current buffer."
  14587. (let ((case-fold-search t)
  14588. (props (append
  14589. (and specials org-special-properties)
  14590. (and defaults (cons org-effort-property org-default-properties))
  14591. nil)))
  14592. (org-with-wide-buffer
  14593. (goto-char (point-min))
  14594. (while (re-search-forward org-property-start-re nil t)
  14595. (let ((range (org-get-property-block)))
  14596. (catch 'skip
  14597. (unless range
  14598. (when (and (not (org-before-first-heading-p))
  14599. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14600. (line-beginning-position))))
  14601. (org-get-property-block nil t))
  14602. (throw 'skip nil))
  14603. (goto-char (car range))
  14604. (let ((begin (car range))
  14605. (end (cdr range)))
  14606. ;; Make sure that found property block is not located
  14607. ;; before current point, as it would generate an infloop.
  14608. ;; It can happen, for example, in the following
  14609. ;; situation:
  14610. ;;
  14611. ;; * Headline
  14612. ;; :PROPERTIES:
  14613. ;; ...
  14614. ;; :END:
  14615. ;; *************** Inlinetask
  14616. ;; #+BEGIN_EXAMPLE
  14617. ;; :PROPERTIES:
  14618. ;; #+END_EXAMPLE
  14619. ;;
  14620. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14621. (while (< (point) end)
  14622. (let ((p (progn (looking-at org-property-re)
  14623. (org-match-string-no-properties 2))))
  14624. ;; Only add true property name, not extension symbol.
  14625. (add-to-list 'props
  14626. (if (not (org-string-match-p "\\+\\'" p)) p
  14627. (substring p 0 -1))))
  14628. (forward-line))))
  14629. (outline-next-heading)))
  14630. (when columns
  14631. (goto-char (point-min))
  14632. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14633. (let ((element (org-element-at-point)))
  14634. (when (memq (org-element-type element) '(keyword node-property))
  14635. (let ((value (org-element-property :value element))
  14636. (start 0))
  14637. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14638. (setq start (match-end 0))
  14639. (let ((p (org-match-string-no-properties 1 value)))
  14640. (unless (member-ignore-case p org-special-properties)
  14641. (add-to-list 'props p))))))))))
  14642. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14643. (defun org-property-values (key)
  14644. "List all non-nil values of property KEY in current buffer."
  14645. (org-with-wide-buffer
  14646. (goto-char (point-min))
  14647. (let ((case-fold-search t)
  14648. (re (org-re-property key))
  14649. values)
  14650. (while (re-search-forward re nil t)
  14651. (add-to-list 'values (org-entry-get (point) key)))
  14652. values)))
  14653. (defun org-insert-property-drawer ()
  14654. "Insert a property drawer into the current entry."
  14655. (org-with-wide-buffer
  14656. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14657. (org-back-to-heading t)
  14658. (org-with-limited-levels (org-back-to-heading t)))
  14659. (forward-line)
  14660. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14661. (unless (org-looking-at-p org-property-drawer-re)
  14662. (let ((inhibit-read-only t))
  14663. (unless (bolp) (insert "\n"))
  14664. (let ((begin (point)))
  14665. (insert ":PROPERTIES:\n:END:\n")
  14666. (org-indent-region begin (point)))))))
  14667. (defun org-insert-drawer (&optional arg drawer)
  14668. "Insert a drawer at point.
  14669. When optional argument ARG is non-nil, insert a property drawer.
  14670. Optional argument DRAWER, when non-nil, is a string representing
  14671. drawer's name. Otherwise, the user is prompted for a name.
  14672. If a region is active, insert the drawer around that region
  14673. instead.
  14674. Point is left between drawer's boundaries."
  14675. (interactive "P")
  14676. (let* ((drawer (if arg "PROPERTIES"
  14677. (or drawer (read-from-minibuffer "Drawer: ")))))
  14678. (cond
  14679. ;; With C-u, fall back on `org-insert-property-drawer'
  14680. (arg (org-insert-property-drawer))
  14681. ;; Check validity of suggested drawer's name.
  14682. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14683. (user-error "Invalid drawer name"))
  14684. ;; With an active region, insert a drawer at point.
  14685. ((not (org-region-active-p))
  14686. (progn
  14687. (unless (bolp) (insert "\n"))
  14688. (insert (format ":%s:\n\n:END:\n" drawer))
  14689. (forward-line -2)))
  14690. ;; Otherwise, insert the drawer at point
  14691. (t
  14692. (let ((rbeg (region-beginning))
  14693. (rend (copy-marker (region-end))))
  14694. (unwind-protect
  14695. (progn
  14696. (goto-char rbeg)
  14697. (beginning-of-line)
  14698. (when (save-excursion
  14699. (re-search-forward org-outline-regexp-bol rend t))
  14700. (user-error "Drawers cannot contain headlines"))
  14701. ;; Position point at the beginning of the first
  14702. ;; non-blank line in region. Insert drawer's opening
  14703. ;; there, then indent it.
  14704. (org-skip-whitespace)
  14705. (beginning-of-line)
  14706. (insert ":" drawer ":\n")
  14707. (forward-line -1)
  14708. (indent-for-tab-command)
  14709. ;; Move point to the beginning of the first blank line
  14710. ;; after the last non-blank line in region. Insert
  14711. ;; drawer's closing, then indent it.
  14712. (goto-char rend)
  14713. (skip-chars-backward " \r\t\n")
  14714. (insert "\n:END:")
  14715. (deactivate-mark t)
  14716. (indent-for-tab-command)
  14717. (unless (eolp) (insert "\n")))
  14718. ;; Clear marker, whatever the outcome of insertion is.
  14719. (set-marker rend nil)))))))
  14720. (defvar org-property-set-functions-alist nil
  14721. "Property set function alist.
  14722. Each entry should have the following format:
  14723. (PROPERTY . READ-FUNCTION)
  14724. The read function will be called with the same argument as
  14725. `org-completing-read'.")
  14726. (defun org-set-property-function (property)
  14727. "Get the function that should be used to set PROPERTY.
  14728. This is computed according to `org-property-set-functions-alist'."
  14729. (or (cdr (assoc property org-property-set-functions-alist))
  14730. 'org-completing-read))
  14731. (defun org-read-property-value (property)
  14732. "Read PROPERTY value from user."
  14733. (let* ((completion-ignore-case t)
  14734. (allowed (org-property-get-allowed-values nil property 'table))
  14735. (cur (org-entry-get nil property))
  14736. (prompt (concat property " value"
  14737. (if (and cur (string-match "\\S-" cur))
  14738. (concat " [" cur "]") "") ": "))
  14739. (set-function (org-set-property-function property))
  14740. (val (if allowed
  14741. (funcall set-function prompt allowed nil
  14742. (not (get-text-property 0 'org-unrestricted
  14743. (caar allowed))))
  14744. (let (org-completion-use-ido org-completion-use-iswitchb)
  14745. (funcall set-function prompt
  14746. (mapcar 'list (org-property-values property))
  14747. nil nil "" nil cur)))))
  14748. (org-trim val)))
  14749. (defvar org-last-set-property nil)
  14750. (defvar org-last-set-property-value nil)
  14751. (defun org-read-property-name ()
  14752. "Read a property name."
  14753. (let* ((completion-ignore-case t)
  14754. (keys (org-buffer-property-keys nil t t))
  14755. (default-prop (or (save-excursion
  14756. (save-match-data
  14757. (beginning-of-line)
  14758. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14759. (null (string= (match-string 1) "END"))
  14760. (match-string 1))))
  14761. org-last-set-property))
  14762. (property (org-icompleting-read
  14763. (concat "Property"
  14764. (if default-prop (concat " [" default-prop "]") "")
  14765. ": ")
  14766. (mapcar 'list keys)
  14767. nil nil nil nil
  14768. default-prop)))
  14769. (if (member property keys)
  14770. property
  14771. (or (cdr (assoc (downcase property)
  14772. (mapcar (lambda (x) (cons (downcase x) x))
  14773. keys)))
  14774. property))))
  14775. (defun org-set-property-and-value (use-last)
  14776. "Allow to set [PROPERTY]: [value] direction from prompt.
  14777. When use-default, don't even ask, just use the last
  14778. \"[PROPERTY]: [value]\" string from the history."
  14779. (interactive "P")
  14780. (let* ((completion-ignore-case t)
  14781. (pv (or (and use-last org-last-set-property-value)
  14782. (org-completing-read
  14783. "Enter a \"[Property]: [value]\" pair: "
  14784. nil nil nil nil nil
  14785. org-last-set-property-value)))
  14786. prop val)
  14787. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14788. (setq prop (match-string 1 pv)
  14789. val (match-string 2 pv))
  14790. (org-set-property prop val))))
  14791. (defun org-set-property (property value)
  14792. "In the current entry, set PROPERTY to VALUE.
  14793. When called interactively, this will prompt for a property name, offering
  14794. completion on existing and default properties. And then it will prompt
  14795. for a value, offering completion either on allowed values (via an inherited
  14796. xxx_ALL property) or on existing values in other instances of this property
  14797. in the current file."
  14798. (interactive (list nil nil))
  14799. (let* ((property (or property (org-read-property-name)))
  14800. (value (or value (org-read-property-value property)))
  14801. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14802. (setq org-last-set-property property)
  14803. (setq org-last-set-property-value (concat property ": " value))
  14804. ;; Possibly postprocess the inserted value:
  14805. (when fn (setq value (funcall fn value)))
  14806. (unless (equal (org-entry-get nil property) value)
  14807. (org-entry-put nil property value))))
  14808. (defun org-delete-property (property)
  14809. "In the current entry, delete PROPERTY."
  14810. (interactive
  14811. (let* ((completion-ignore-case t)
  14812. (cat (org-entry-get (point) "CATEGORY"))
  14813. (props0 (org-entry-properties nil 'standard))
  14814. (props (if cat props0
  14815. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14816. (prop (if (< 1 (length props))
  14817. (org-icompleting-read "Property: " props nil t)
  14818. (caar props))))
  14819. (list prop)))
  14820. (if (not property)
  14821. (message "No property to delete in this entry")
  14822. (org-entry-delete nil property)
  14823. (message "Property \"%s\" deleted" property)))
  14824. (defun org-delete-property-globally (property)
  14825. "Remove PROPERTY globally, from all entries."
  14826. (interactive
  14827. (let* ((completion-ignore-case t)
  14828. (prop (org-icompleting-read
  14829. "Globally remove property: "
  14830. (mapcar 'list (org-buffer-property-keys)))))
  14831. (list prop)))
  14832. (save-excursion
  14833. (save-restriction
  14834. (widen)
  14835. (goto-char (point-min))
  14836. (let ((cnt 0))
  14837. (while (re-search-forward
  14838. (org-re-property property)
  14839. nil t)
  14840. (setq cnt (1+ cnt))
  14841. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14842. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14843. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14844. (defun org-compute-property-at-point ()
  14845. "Compute the property at point.
  14846. This looks for an enclosing column format, extracts the operator and
  14847. then applies it to the property in the column format's scope."
  14848. (interactive)
  14849. (unless (org-at-property-p)
  14850. (user-error "Not at a property"))
  14851. (let ((prop (org-match-string-no-properties 2)))
  14852. (org-columns-get-format-and-top-level)
  14853. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14854. (user-error "No operator defined for property %s" prop))
  14855. (org-columns-compute prop)))
  14856. (defvar org-property-allowed-value-functions nil
  14857. "Hook for functions supplying allowed values for a specific property.
  14858. The functions must take a single argument, the name of the property, and
  14859. return a flat list of allowed values. If \":ETC\" is one of
  14860. the values, this means that these values are intended as defaults for
  14861. completion, but that other values should be allowed too.
  14862. The functions must return nil if they are not responsible for this
  14863. property.")
  14864. (defun org-property-get-allowed-values (pom property &optional table)
  14865. "Get allowed values for the property PROPERTY.
  14866. When TABLE is non-nil, return an alist that can directly be used for
  14867. completion."
  14868. (let (vals)
  14869. (cond
  14870. ((equal property "TODO")
  14871. (setq vals (org-with-point-at pom
  14872. (append org-todo-keywords-1 '("")))))
  14873. ((equal property "PRIORITY")
  14874. (let ((n org-lowest-priority))
  14875. (while (>= n org-highest-priority)
  14876. (push (char-to-string n) vals)
  14877. (setq n (1- n)))))
  14878. ((member property org-special-properties))
  14879. ((setq vals (run-hook-with-args-until-success
  14880. 'org-property-allowed-value-functions property)))
  14881. (t
  14882. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14883. (when (and vals (string-match "\\S-" vals))
  14884. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14885. (setq vals (mapcar (lambda (x)
  14886. (cond ((stringp x) x)
  14887. ((numberp x) (number-to-string x))
  14888. ((symbolp x) (symbol-name x))
  14889. (t "???")))
  14890. vals)))))
  14891. (when (member ":ETC" vals)
  14892. (setq vals (remove ":ETC" vals))
  14893. (org-add-props (car vals) '(org-unrestricted t)))
  14894. (if table (mapcar 'list vals) vals)))
  14895. (defun org-property-previous-allowed-value (&optional previous)
  14896. "Switch to the next allowed value for this property."
  14897. (interactive)
  14898. (org-property-next-allowed-value t))
  14899. (defun org-property-next-allowed-value (&optional previous)
  14900. "Switch to the next allowed value for this property."
  14901. (interactive)
  14902. (unless (org-at-property-p)
  14903. (user-error "Not at a property"))
  14904. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14905. (key (match-string 2))
  14906. (value (match-string 3))
  14907. (allowed (or (org-property-get-allowed-values (point) key)
  14908. (and (member value '("[ ]" "[-]" "[X]"))
  14909. '("[ ]" "[X]"))))
  14910. (heading (save-match-data (nth 4 (org-heading-components))))
  14911. nval)
  14912. (unless allowed
  14913. (user-error "Allowed values for this property have not been defined"))
  14914. (if previous (setq allowed (reverse allowed)))
  14915. (if (member value allowed)
  14916. (setq nval (car (cdr (member value allowed)))))
  14917. (setq nval (or nval (car allowed)))
  14918. (if (equal nval value)
  14919. (user-error "Only one allowed value for this property"))
  14920. (org-at-property-p)
  14921. (replace-match (concat " :" key ": " nval) t t)
  14922. (org-indent-line)
  14923. (beginning-of-line 1)
  14924. (skip-chars-forward " \t")
  14925. (when (equal prop org-effort-property)
  14926. (org-refresh-property
  14927. '((effort . identity)
  14928. (effort-minutes . org-duration-string-to-minutes))
  14929. nval)
  14930. (when (string= org-clock-current-task heading)
  14931. (setq org-clock-effort nval)
  14932. (org-clock-update-mode-line)))
  14933. (run-hook-with-args 'org-property-changed-functions key nval)))
  14934. (defun org-find-olp (path &optional this-buffer)
  14935. "Return a marker pointing to the entry at outline path OLP.
  14936. If anything goes wrong, throw an error.
  14937. You can wrap this call to catch the error like this:
  14938. (condition-case msg
  14939. (org-mobile-locate-entry (match-string 4))
  14940. (error (nth 1 msg)))
  14941. The return value will then be either a string with the error message,
  14942. or a marker if everything is OK.
  14943. If THIS-BUFFER is set, the outline path does not contain a file,
  14944. only headings."
  14945. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14946. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14947. (level 1)
  14948. (lmin 1)
  14949. (lmax 1)
  14950. limit re end found pos heading cnt flevel)
  14951. (unless buffer (error "File not found :%s" file))
  14952. (with-current-buffer buffer
  14953. (save-excursion
  14954. (save-restriction
  14955. (widen)
  14956. (setq limit (point-max))
  14957. (goto-char (point-min))
  14958. (while (setq heading (pop path))
  14959. (setq re (format org-complex-heading-regexp-format
  14960. (regexp-quote heading)))
  14961. (setq cnt 0 pos (point))
  14962. (while (re-search-forward re end t)
  14963. (setq level (- (match-end 1) (match-beginning 1)))
  14964. (if (and (>= level lmin) (<= level lmax))
  14965. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14966. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14967. lmax heading))
  14968. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14969. lmax heading))
  14970. (goto-char found)
  14971. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14972. (setq end (save-excursion (org-end-of-subtree t t))))
  14973. (when (org-at-heading-p)
  14974. (point-marker)))))))
  14975. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14976. "Find node HEADING in BUFFER.
  14977. Return a marker to the heading if it was found, or nil if not.
  14978. If POS-ONLY is set, return just the position instead of a marker.
  14979. The heading text must match exact, but it may have a TODO keyword,
  14980. a priority cookie and tags in the standard locations."
  14981. (with-current-buffer (or buffer (current-buffer))
  14982. (save-excursion
  14983. (save-restriction
  14984. (widen)
  14985. (goto-char (point-min))
  14986. (let (case-fold-search)
  14987. (if (re-search-forward
  14988. (format org-complex-heading-regexp-format
  14989. (regexp-quote heading)) nil t)
  14990. (if pos-only
  14991. (match-beginning 0)
  14992. (move-marker (make-marker) (match-beginning 0)))))))))
  14993. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14994. "Find Org node headline HEADING in all .org files in directory DIR.
  14995. When the target headline is found, return a marker to this location."
  14996. (let ((files (directory-files (or dir default-directory)
  14997. t "\\`[^.#].*\\.org\\'"))
  14998. file visiting m buffer)
  14999. (catch 'found
  15000. (while (setq file (pop files))
  15001. (message "trying %s" file)
  15002. (setq visiting (org-find-base-buffer-visiting file))
  15003. (setq buffer (or visiting (find-file-noselect file)))
  15004. (setq m (org-find-exact-headline-in-buffer
  15005. heading buffer))
  15006. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15007. (and m (throw 'found m))))))
  15008. (defun org-find-entry-with-id (ident)
  15009. "Locate the entry that contains the ID property with exact value IDENT.
  15010. IDENT can be a string, a symbol or a number, this function will search for
  15011. the string representation of it.
  15012. Return the position where this entry starts, or nil if there is no such entry."
  15013. (interactive "sID: ")
  15014. (let ((id (cond
  15015. ((stringp ident) ident)
  15016. ((symbol-name ident) (symbol-name ident))
  15017. ((numberp ident) (number-to-string ident))
  15018. (t (error "IDENT %s must be a string, symbol or number" ident))))
  15019. (case-fold-search nil))
  15020. (save-excursion
  15021. (save-restriction
  15022. (widen)
  15023. (goto-char (point-min))
  15024. (when (re-search-forward
  15025. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  15026. nil t)
  15027. (org-back-to-heading t)
  15028. (point))))))
  15029. ;;;; Timestamps
  15030. (defvar org-last-changed-timestamp nil)
  15031. (defvar org-last-inserted-timestamp nil
  15032. "The last time stamp inserted with `org-insert-time-stamp'.")
  15033. (defvar org-ts-what) ; dynamically scoped parameter
  15034. (defun org-time-stamp (arg &optional inactive)
  15035. "Prompt for a date/time and insert a time stamp.
  15036. If the user specifies a time like HH:MM or if this command is
  15037. called with at least one prefix argument, the time stamp contains
  15038. the date and the time. Otherwise, only the date is included.
  15039. All parts of a date not specified by the user are filled in from
  15040. the timestamp at point, if any, or the current date/time
  15041. otherwise.
  15042. If there is already a timestamp at the cursor, it is replaced.
  15043. With two universal prefix arguments, insert an active timestamp
  15044. with the current time without prompting the user.
  15045. When called from lisp, the timestamp is inactive if INACTIVE is
  15046. non-nil."
  15047. (interactive "P")
  15048. (let* ((ts
  15049. (cond ((org-at-date-range-p t)
  15050. (save-excursion
  15051. (goto-char (match-beginning 0))
  15052. (looking-at (if inactive org-ts-regexp-both org-ts-regexp)))
  15053. (match-string 0))
  15054. ((org-at-timestamp-p t) (match-string 0))))
  15055. ;; Default time is either the timestamp at point or today.
  15056. ;; When entering a range, only the range start is considered.
  15057. (default-time (if (not ts) (current-time)
  15058. (apply #'encode-time (org-parse-time-string ts))))
  15059. (default-input (and ts (org-get-compact-tod ts)))
  15060. (repeater (and ts
  15061. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15062. (match-string 0 ts)))
  15063. (time
  15064. (and (if (equal arg '(16)) (current-time)
  15065. ;; Preserve `this-command' and `last-command'.
  15066. (let ((this-command this-command)
  15067. (last-command last-command))
  15068. (org-read-date
  15069. arg 'totime nil nil default-time default-input
  15070. inactive)))))
  15071. org-time-was-given org-end-time-was-given)
  15072. (cond
  15073. ((and ts
  15074. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15075. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15076. (insert "--")
  15077. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15078. (ts
  15079. ;; Make sure we're on a timestamp. When in the middle of a date
  15080. ;; range, move arbitrarily to range end.
  15081. (unless (org-at-timestamp-p t)
  15082. (skip-chars-forward "-")
  15083. (org-at-timestamp-p t))
  15084. (replace-match "")
  15085. (setq org-last-changed-timestamp
  15086. (org-insert-time-stamp
  15087. time (or org-time-was-given arg)
  15088. inactive nil nil (list org-end-time-was-given)))
  15089. (when repeater
  15090. (backward-char)
  15091. (insert " " repeater)
  15092. (setq org-last-changed-timestamp
  15093. (concat (substring org-last-inserted-timestamp 0 -1)
  15094. " " repeater ">")))
  15095. (message "Timestamp updated"))
  15096. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15097. (t (org-insert-time-stamp
  15098. time (or org-time-was-given arg) inactive nil nil
  15099. (list org-end-time-was-given))))))
  15100. ;; FIXME: can we use this for something else, like computing time differences?
  15101. (defun org-get-compact-tod (s)
  15102. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15103. (let* ((t1 (match-string 1 s))
  15104. (h1 (string-to-number (match-string 2 s)))
  15105. (m1 (string-to-number (match-string 3 s)))
  15106. (t2 (and (match-end 4) (match-string 5 s)))
  15107. (h2 (and t2 (string-to-number (match-string 6 s))))
  15108. (m2 (and t2 (string-to-number (match-string 7 s))))
  15109. dh dm)
  15110. (if (not t2)
  15111. t1
  15112. (setq dh (- h2 h1) dm (- m2 m1))
  15113. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15114. (concat t1 "+" (number-to-string dh)
  15115. (and (/= 0 dm) (format ":%02d" dm)))))))
  15116. (defun org-time-stamp-inactive (&optional arg)
  15117. "Insert an inactive time stamp.
  15118. An inactive time stamp is enclosed in square brackets instead of angle
  15119. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15120. does not link to the calendar and cannot be changed with the S-cursor keys.
  15121. So these are more for recording a certain time/date."
  15122. (interactive "P")
  15123. (org-time-stamp arg 'inactive))
  15124. (defvar org-date-ovl (make-overlay 1 1))
  15125. (overlay-put org-date-ovl 'face 'org-date-selected)
  15126. (org-detach-overlay org-date-ovl)
  15127. (defvar org-ans1) ; dynamically scoped parameter
  15128. (defvar org-ans2) ; dynamically scoped parameter
  15129. (defvar org-plain-time-of-day-regexp) ; defined below
  15130. (defvar org-overriding-default-time nil) ; dynamically scoped
  15131. (defvar org-read-date-overlay nil)
  15132. (defvar org-dcst nil) ; dynamically scoped
  15133. (defvar org-read-date-history nil)
  15134. (defvar org-read-date-final-answer nil)
  15135. (defvar org-read-date-analyze-futurep nil)
  15136. (defvar org-read-date-analyze-forced-year nil)
  15137. (defvar org-read-date-inactive)
  15138. (defvar org-read-date-minibuffer-local-map
  15139. (let* ((map (make-sparse-keymap)))
  15140. (set-keymap-parent map minibuffer-local-map)
  15141. (org-defkey map (kbd ".")
  15142. (lambda () (interactive)
  15143. ;; Are we at the beginning of the prompt?
  15144. (if (looking-back "^[^:]+: ")
  15145. (org-eval-in-calendar '(calendar-goto-today))
  15146. (insert "."))))
  15147. (org-defkey map (kbd "C-.")
  15148. (lambda () (interactive)
  15149. (org-eval-in-calendar '(calendar-goto-today))))
  15150. (org-defkey map [(meta shift left)]
  15151. (lambda () (interactive)
  15152. (org-eval-in-calendar '(calendar-backward-month 1))))
  15153. (org-defkey map [(meta shift right)]
  15154. (lambda () (interactive)
  15155. (org-eval-in-calendar '(calendar-forward-month 1))))
  15156. (org-defkey map [(meta shift up)]
  15157. (lambda () (interactive)
  15158. (org-eval-in-calendar '(calendar-backward-year 1))))
  15159. (org-defkey map [(meta shift down)]
  15160. (lambda () (interactive)
  15161. (org-eval-in-calendar '(calendar-forward-year 1))))
  15162. (org-defkey map [?\e (shift left)]
  15163. (lambda () (interactive)
  15164. (org-eval-in-calendar '(calendar-backward-month 1))))
  15165. (org-defkey map [?\e (shift right)]
  15166. (lambda () (interactive)
  15167. (org-eval-in-calendar '(calendar-forward-month 1))))
  15168. (org-defkey map [?\e (shift up)]
  15169. (lambda () (interactive)
  15170. (org-eval-in-calendar '(calendar-backward-year 1))))
  15171. (org-defkey map [?\e (shift down)]
  15172. (lambda () (interactive)
  15173. (org-eval-in-calendar '(calendar-forward-year 1))))
  15174. (org-defkey map [(shift up)]
  15175. (lambda () (interactive)
  15176. (org-eval-in-calendar '(calendar-backward-week 1))))
  15177. (org-defkey map [(shift down)]
  15178. (lambda () (interactive)
  15179. (org-eval-in-calendar '(calendar-forward-week 1))))
  15180. (org-defkey map [(shift left)]
  15181. (lambda () (interactive)
  15182. (org-eval-in-calendar '(calendar-backward-day 1))))
  15183. (org-defkey map [(shift right)]
  15184. (lambda () (interactive)
  15185. (org-eval-in-calendar '(calendar-forward-day 1))))
  15186. (org-defkey map "!"
  15187. (lambda () (interactive)
  15188. (org-eval-in-calendar '(diary-view-entries))
  15189. (message "")))
  15190. (org-defkey map ">"
  15191. (lambda () (interactive)
  15192. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15193. (org-defkey map "<"
  15194. (lambda () (interactive)
  15195. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15196. (org-defkey map "\C-v"
  15197. (lambda () (interactive)
  15198. (org-eval-in-calendar
  15199. '(calendar-scroll-left-three-months 1))))
  15200. (org-defkey map "\M-v"
  15201. (lambda () (interactive)
  15202. (org-eval-in-calendar
  15203. '(calendar-scroll-right-three-months 1))))
  15204. map)
  15205. "Keymap for minibuffer commands when using `org-read-date'.")
  15206. (defvar org-def)
  15207. (defvar org-defdecode)
  15208. (defvar org-with-time)
  15209. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15210. default-time default-input inactive)
  15211. "Read a date, possibly a time, and make things smooth for the user.
  15212. The prompt will suggest to enter an ISO date, but you can also enter anything
  15213. which will at least partially be understood by `parse-time-string'.
  15214. Unrecognized parts of the date will default to the current day, month, year,
  15215. hour and minute. If this command is called to replace a timestamp at point,
  15216. or to enter the second timestamp of a range, the default time is taken
  15217. from the existing stamp. Furthermore, the command prefers the future,
  15218. so if you are giving a date where the year is not given, and the day-month
  15219. combination is already past in the current year, it will assume you
  15220. mean next year. For details, see the manual. A few examples:
  15221. 3-2-5 --> 2003-02-05
  15222. feb 15 --> currentyear-02-15
  15223. 2/15 --> currentyear-02-15
  15224. sep 12 9 --> 2009-09-12
  15225. 12:45 --> today 12:45
  15226. 22 sept 0:34 --> currentyear-09-22 0:34
  15227. 12 --> currentyear-currentmonth-12
  15228. Fri --> nearest Friday after today
  15229. -Tue --> last Tuesday
  15230. etc.
  15231. Furthermore you can specify a relative date by giving, as the *first* thing
  15232. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15233. change in days weeks, months, years.
  15234. With a single plus or minus, the date is relative to today. With a double
  15235. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15236. +4d --> four days from today
  15237. +4 --> same as above
  15238. +2w --> two weeks from today
  15239. ++5 --> five days from default date
  15240. The function understands only English month and weekday abbreviations.
  15241. While prompting, a calendar is popped up - you can also select the
  15242. date with the mouse (button 1). The calendar shows a period of three
  15243. months. To scroll it to other months, use the keys `>' and `<'.
  15244. If you don't like the calendar, turn it off with
  15245. \(setq org-read-date-popup-calendar nil)
  15246. With optional argument TO-TIME, the date will immediately be converted
  15247. to an internal time.
  15248. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15249. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15250. still enter a time, and this function will inform the calling routine
  15251. about this change. The calling routine may then choose to change the
  15252. format used to insert the time stamp into the buffer to include the time.
  15253. With optional argument FROM-STRING, read from this string instead from
  15254. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15255. the time/date that is used for everything that is not specified by the
  15256. user."
  15257. (require 'parse-time)
  15258. (let* ((org-time-stamp-rounding-minutes
  15259. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15260. (org-dcst org-display-custom-times)
  15261. (ct (org-current-time))
  15262. (org-def (or org-overriding-default-time default-time ct))
  15263. (org-defdecode (decode-time org-def))
  15264. (dummy (progn
  15265. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15266. (setcar (nthcdr 2 org-defdecode) -1)
  15267. (setcar (nthcdr 1 org-defdecode) 59)
  15268. (setq org-def (apply 'encode-time org-defdecode)
  15269. org-defdecode (decode-time org-def)))))
  15270. (cur-frame (selected-frame))
  15271. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15272. (calendar-frame-setup nil)
  15273. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15274. (calendar-move-hook nil)
  15275. (calendar-view-diary-initially-flag nil)
  15276. (calendar-view-holidays-initially-flag nil)
  15277. (timestr (format-time-string
  15278. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15279. (prompt (concat (if prompt (concat prompt " ") "")
  15280. (format "Date+time [%s]: " timestr)))
  15281. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15282. (cond
  15283. (from-string (setq ans from-string))
  15284. (org-read-date-popup-calendar
  15285. (save-excursion
  15286. (save-window-excursion
  15287. (calendar)
  15288. (when (eq calendar-setup 'calendar-only)
  15289. (setq cal-frame
  15290. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15291. (select-frame cal-frame))
  15292. (org-eval-in-calendar '(setq cursor-type nil) t)
  15293. (unwind-protect
  15294. (progn
  15295. (calendar-forward-day (- (time-to-days org-def)
  15296. (calendar-absolute-from-gregorian
  15297. (calendar-current-date))))
  15298. (org-eval-in-calendar nil t)
  15299. (let* ((old-map (current-local-map))
  15300. (map (copy-keymap calendar-mode-map))
  15301. (minibuffer-local-map
  15302. (copy-keymap org-read-date-minibuffer-local-map)))
  15303. (org-defkey map (kbd "RET") 'org-calendar-select)
  15304. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15305. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15306. (unwind-protect
  15307. (progn
  15308. (use-local-map map)
  15309. (setq org-read-date-inactive inactive)
  15310. (add-hook 'post-command-hook 'org-read-date-display)
  15311. (setq org-ans0 (read-string prompt default-input
  15312. 'org-read-date-history nil))
  15313. ;; org-ans0: from prompt
  15314. ;; org-ans1: from mouse click
  15315. ;; org-ans2: from calendar motion
  15316. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15317. (remove-hook 'post-command-hook 'org-read-date-display)
  15318. (use-local-map old-map)
  15319. (when org-read-date-overlay
  15320. (delete-overlay org-read-date-overlay)
  15321. (setq org-read-date-overlay nil)))))
  15322. (bury-buffer "*Calendar*")
  15323. (when cal-frame
  15324. (delete-frame cal-frame)
  15325. (select-frame-set-input-focus cur-frame))))))
  15326. (t ; Naked prompt only
  15327. (unwind-protect
  15328. (setq ans (read-string prompt default-input
  15329. 'org-read-date-history timestr))
  15330. (when org-read-date-overlay
  15331. (delete-overlay org-read-date-overlay)
  15332. (setq org-read-date-overlay nil)))))
  15333. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15334. (when org-read-date-analyze-forced-year
  15335. (message "Year was forced into %s"
  15336. (if org-read-date-force-compatible-dates
  15337. "compatible range (1970-2037)"
  15338. "range representable on this machine"))
  15339. (ding))
  15340. ;; One round trip to get rid of 34th of August and stuff like that....
  15341. (setq final (decode-time (apply 'encode-time final)))
  15342. (setq org-read-date-final-answer ans)
  15343. (if to-time
  15344. (apply 'encode-time final)
  15345. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15346. (format "%04d-%02d-%02d %02d:%02d"
  15347. (nth 5 final) (nth 4 final) (nth 3 final)
  15348. (nth 2 final) (nth 1 final))
  15349. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15350. (defun org-read-date-display ()
  15351. "Display the current date prompt interpretation in the minibuffer."
  15352. (when org-read-date-display-live
  15353. (when org-read-date-overlay
  15354. (delete-overlay org-read-date-overlay))
  15355. (when (minibufferp (current-buffer))
  15356. (save-excursion
  15357. (end-of-line 1)
  15358. (while (not (equal (buffer-substring
  15359. (max (point-min) (- (point) 4)) (point))
  15360. " "))
  15361. (insert " ")))
  15362. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15363. " " (or org-ans1 org-ans2)))
  15364. (org-end-time-was-given nil)
  15365. (f (org-read-date-analyze ans org-def org-defdecode))
  15366. (fmts (if org-dcst
  15367. org-time-stamp-custom-formats
  15368. org-time-stamp-formats))
  15369. (fmt (if (or org-with-time
  15370. (and (boundp 'org-time-was-given) org-time-was-given))
  15371. (cdr fmts)
  15372. (car fmts)))
  15373. (txt (format-time-string fmt (apply 'encode-time f)))
  15374. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15375. (txt (concat "=> " txt)))
  15376. (when (and org-end-time-was-given
  15377. (string-match org-plain-time-of-day-regexp txt))
  15378. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15379. org-end-time-was-given
  15380. (substring txt (match-end 0)))))
  15381. (when org-read-date-analyze-futurep
  15382. (setq txt (concat txt " (=>F)")))
  15383. (setq org-read-date-overlay
  15384. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15385. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15386. (defun org-read-date-analyze (ans org-def org-defdecode)
  15387. "Analyze the combined answer of the date prompt."
  15388. ;; FIXME: cleanup and comment
  15389. (let ((nowdecode (decode-time (current-time)))
  15390. delta deltan deltaw deltadef year month day
  15391. hour minute second wday pm h2 m2 tl wday1
  15392. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15393. (setq org-read-date-analyze-futurep nil
  15394. org-read-date-analyze-forced-year nil)
  15395. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15396. (setq ans "+0"))
  15397. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15398. (setq ans (replace-match "" t t ans)
  15399. deltan (car delta)
  15400. deltaw (nth 1 delta)
  15401. deltadef (nth 2 delta)))
  15402. ;; Check if there is an iso week date in there. If yes, store the
  15403. ;; info and postpone interpreting it until the rest of the parsing
  15404. ;; is done.
  15405. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15406. (setq iso-year (if (match-end 1)
  15407. (org-small-year-to-year
  15408. (string-to-number (match-string 1 ans))))
  15409. iso-weekday (if (match-end 3)
  15410. (string-to-number (match-string 3 ans)))
  15411. iso-week (string-to-number (match-string 2 ans)))
  15412. (setq ans (replace-match "" t t ans)))
  15413. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15414. (when (string-match
  15415. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15416. (setq year (if (match-end 2)
  15417. (string-to-number (match-string 2 ans))
  15418. (progn (setq kill-year t)
  15419. (string-to-number (format-time-string "%Y"))))
  15420. month (string-to-number (match-string 3 ans))
  15421. day (string-to-number (match-string 4 ans)))
  15422. (if (< year 100) (setq year (+ 2000 year)))
  15423. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15424. t nil ans)))
  15425. ;; Help matching dotted european dates
  15426. (when (string-match
  15427. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15428. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15429. (setq kill-year t)
  15430. (string-to-number (format-time-string "%Y")))
  15431. day (string-to-number (match-string 1 ans))
  15432. month (string-to-number (match-string 2 ans))
  15433. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15434. t nil ans)))
  15435. ;; Help matching american dates, like 5/30 or 5/30/7
  15436. (when (string-match
  15437. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15438. (setq year (if (match-end 4)
  15439. (string-to-number (match-string 4 ans))
  15440. (progn (setq kill-year t)
  15441. (string-to-number (format-time-string "%Y"))))
  15442. month (string-to-number (match-string 1 ans))
  15443. day (string-to-number (match-string 2 ans)))
  15444. (if (< year 100) (setq year (+ 2000 year)))
  15445. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15446. t nil ans)))
  15447. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15448. ;; If there is a time with am/pm, and *no* time without it, we convert
  15449. ;; so that matching will be successful.
  15450. (loop for i from 1 to 2 do ; twice, for end time as well
  15451. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15452. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15453. (setq hour (string-to-number (match-string 1 ans))
  15454. minute (if (match-end 3)
  15455. (string-to-number (match-string 3 ans))
  15456. 0)
  15457. pm (equal ?p
  15458. (string-to-char (downcase (match-string 4 ans)))))
  15459. (if (and (= hour 12) (not pm))
  15460. (setq hour 0)
  15461. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15462. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15463. t t ans))))
  15464. ;; Check if a time range is given as a duration
  15465. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15466. (setq hour (string-to-number (match-string 1 ans))
  15467. h2 (+ hour (string-to-number (match-string 3 ans)))
  15468. minute (string-to-number (match-string 2 ans))
  15469. m2 (+ minute (if (match-end 5) (string-to-number
  15470. (match-string 5 ans))0)))
  15471. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15472. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15473. t t ans)))
  15474. ;; Check if there is a time range
  15475. (when (boundp 'org-end-time-was-given)
  15476. (setq org-time-was-given nil)
  15477. (when (and (string-match org-plain-time-of-day-regexp ans)
  15478. (match-end 8))
  15479. (setq org-end-time-was-given (match-string 8 ans))
  15480. (setq ans (concat (substring ans 0 (match-beginning 7))
  15481. (substring ans (match-end 7))))))
  15482. (setq tl (parse-time-string ans)
  15483. day (or (nth 3 tl) (nth 3 org-defdecode))
  15484. month
  15485. (cond ((nth 4 tl))
  15486. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15487. ;; Day was specified. Make sure DAY+MONTH
  15488. ;; combination happens in the future.
  15489. ((nth 3 tl)
  15490. (setq futurep t)
  15491. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15492. (nth 4 nowdecode)))
  15493. (t (nth 4 org-defdecode)))
  15494. year
  15495. (cond ((and (not kill-year) (nth 5 tl)))
  15496. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15497. ;; Month was guessed in the future and is at least
  15498. ;; equal to NOWDECODE's. Fix year accordingly.
  15499. (futurep
  15500. (if (or (> month (nth 4 nowdecode))
  15501. (>= day (nth 3 nowdecode)))
  15502. (nth 5 nowdecode)
  15503. (1+ (nth 5 nowdecode))))
  15504. ;; Month was specified. Make sure MONTH+YEAR
  15505. ;; combination happens in the future.
  15506. ((nth 4 tl)
  15507. (setq futurep t)
  15508. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15509. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15510. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15511. (t (nth 5 nowdecode))))
  15512. (t (nth 5 org-defdecode)))
  15513. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15514. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15515. second (or (nth 0 tl) 0)
  15516. wday (nth 6 tl))
  15517. (when (and (eq org-read-date-prefer-future 'time)
  15518. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15519. (equal day (nth 3 nowdecode))
  15520. (equal month (nth 4 nowdecode))
  15521. (equal year (nth 5 nowdecode))
  15522. (nth 2 tl)
  15523. (or (< (nth 2 tl) (nth 2 nowdecode))
  15524. (and (= (nth 2 tl) (nth 2 nowdecode))
  15525. (nth 1 tl)
  15526. (< (nth 1 tl) (nth 1 nowdecode)))))
  15527. (setq day (1+ day)
  15528. futurep t))
  15529. ;; Special date definitions below
  15530. (cond
  15531. (iso-week
  15532. ;; There was an iso week
  15533. (require 'cal-iso)
  15534. (setq futurep nil)
  15535. (setq year (or iso-year year)
  15536. day (or iso-weekday wday 1)
  15537. wday nil ; to make sure that the trigger below does not match
  15538. iso-date (calendar-gregorian-from-absolute
  15539. (calendar-iso-to-absolute
  15540. (list iso-week day year))))
  15541. ; FIXME: Should we also push ISO weeks into the future?
  15542. ; (when (and org-read-date-prefer-future
  15543. ; (not iso-year)
  15544. ; (< (calendar-absolute-from-gregorian iso-date)
  15545. ; (time-to-days (current-time))))
  15546. ; (setq year (1+ year)
  15547. ; iso-date (calendar-gregorian-from-absolute
  15548. ; (calendar-iso-to-absolute
  15549. ; (list iso-week day year)))))
  15550. (setq month (car iso-date)
  15551. year (nth 2 iso-date)
  15552. day (nth 1 iso-date)))
  15553. (deltan
  15554. (setq futurep nil)
  15555. (unless deltadef
  15556. (let ((now (decode-time (current-time))))
  15557. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15558. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15559. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15560. ((equal deltaw "m") (setq month (+ month deltan)))
  15561. ((equal deltaw "y") (setq year (+ year deltan)))))
  15562. ((and wday (not (nth 3 tl)))
  15563. ;; Weekday was given, but no day, so pick that day in the week
  15564. ;; on or after the derived date.
  15565. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15566. (unless (equal wday wday1)
  15567. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15568. (if (and (boundp 'org-time-was-given)
  15569. (nth 2 tl))
  15570. (setq org-time-was-given t))
  15571. (if (< year 100) (setq year (+ 2000 year)))
  15572. ;; Check of the date is representable
  15573. (if org-read-date-force-compatible-dates
  15574. (progn
  15575. (if (< year 1970)
  15576. (setq year 1970 org-read-date-analyze-forced-year t))
  15577. (if (> year 2037)
  15578. (setq year 2037 org-read-date-analyze-forced-year t)))
  15579. (condition-case nil
  15580. (ignore (encode-time second minute hour day month year))
  15581. (error
  15582. (setq year (nth 5 org-defdecode))
  15583. (setq org-read-date-analyze-forced-year t))))
  15584. (setq org-read-date-analyze-futurep futurep)
  15585. (list second minute hour day month year)))
  15586. (defvar parse-time-weekdays)
  15587. (defun org-read-date-get-relative (s today default)
  15588. "Check string S for special relative date string.
  15589. TODAY and DEFAULT are internal times, for today and for a default.
  15590. Return shift list (N what def-flag)
  15591. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15592. N is the number of WHATs to shift.
  15593. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15594. the DEFAULT date rather than TODAY."
  15595. (require 'parse-time)
  15596. (when (and
  15597. (string-match
  15598. (concat
  15599. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15600. "\\([0-9]+\\)?"
  15601. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15602. "\\([ \t]\\|$\\)") s)
  15603. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15604. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15605. (string-to-char (substring (match-string 1 s) -1))
  15606. ?+))
  15607. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15608. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15609. (what (if (match-end 3) (match-string 3 s) "d"))
  15610. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15611. (date (if rel default today))
  15612. (wday (nth 6 (decode-time date)))
  15613. delta)
  15614. (if wday1
  15615. (progn
  15616. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15617. (if (= delta 0) (setq delta 7))
  15618. (if (= dir ?-)
  15619. (progn
  15620. (setq delta (- delta 7))
  15621. (if (= delta 0) (setq delta -7))))
  15622. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15623. (list delta "d" rel))
  15624. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15625. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15626. "Turn a user-specified date into the internal representation.
  15627. The internal representation needed by the calendar is (month day year).
  15628. This is a wrapper to handle the brain-dead convention in calendar that
  15629. user function argument order change dependent on argument order."
  15630. (if (boundp 'calendar-date-style)
  15631. (cond
  15632. ((eq calendar-date-style 'american)
  15633. (list arg1 arg2 arg3))
  15634. ((eq calendar-date-style 'european)
  15635. (list arg2 arg1 arg3))
  15636. ((eq calendar-date-style 'iso)
  15637. (list arg2 arg3 arg1)))
  15638. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15639. (if (org-bound-and-true-p european-calendar-style)
  15640. (list arg2 arg1 arg3)
  15641. (list arg1 arg2 arg3)))))
  15642. (defun org-eval-in-calendar (form &optional keepdate)
  15643. "Eval FORM in the calendar window and return to current window.
  15644. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15645. otherwise stick to the current value of `org-ans2'."
  15646. (let ((sf (selected-frame))
  15647. (sw (selected-window)))
  15648. (select-window (get-buffer-window "*Calendar*" t))
  15649. (eval form)
  15650. (when (and (not keepdate) (calendar-cursor-to-date))
  15651. (let* ((date (calendar-cursor-to-date))
  15652. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15653. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15654. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15655. (select-window sw)
  15656. (org-select-frame-set-input-focus sf)))
  15657. (defun org-calendar-select ()
  15658. "Return to `org-read-date' with the date currently selected.
  15659. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15660. (interactive)
  15661. (when (calendar-cursor-to-date)
  15662. (let* ((date (calendar-cursor-to-date))
  15663. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15664. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15665. (if (active-minibuffer-window) (exit-minibuffer))))
  15666. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15667. "Insert a date stamp for the date given by the internal TIME.
  15668. See `format-time-string' for the format of TIME.
  15669. WITH-HM means use the stamp format that includes the time of the day.
  15670. INACTIVE means use square brackets instead of angular ones, so that the
  15671. stamp will not contribute to the agenda.
  15672. PRE and POST are optional strings to be inserted before and after the
  15673. stamp.
  15674. The command returns the inserted time stamp."
  15675. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15676. stamp)
  15677. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15678. (insert-before-markers (or pre ""))
  15679. (when (listp extra)
  15680. (setq extra (car extra))
  15681. (if (and (stringp extra)
  15682. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15683. (setq extra (format "-%02d:%02d"
  15684. (string-to-number (match-string 1 extra))
  15685. (string-to-number (match-string 2 extra))))
  15686. (setq extra nil)))
  15687. (when extra
  15688. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15689. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15690. (insert-before-markers (or post ""))
  15691. (setq org-last-inserted-timestamp stamp)))
  15692. (defun org-toggle-time-stamp-overlays ()
  15693. "Toggle the use of custom time stamp formats."
  15694. (interactive)
  15695. (setq org-display-custom-times (not org-display-custom-times))
  15696. (unless org-display-custom-times
  15697. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15698. (while (setq p (next-single-property-change p 'display))
  15699. (if (and (get-text-property p 'display)
  15700. (eq (get-text-property p 'face) 'org-date))
  15701. (remove-text-properties
  15702. p (setq p (next-single-property-change p 'display))
  15703. '(display t))))
  15704. (set-buffer-modified-p bmp)))
  15705. (if (featurep 'xemacs)
  15706. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15707. (org-restart-font-lock)
  15708. (setq org-table-may-need-update t)
  15709. (if org-display-custom-times
  15710. (message "Time stamps are overlaid with custom format")
  15711. (message "Time stamp overlays removed")))
  15712. (defun org-display-custom-time (beg end)
  15713. "Overlay modified time stamp format over timestamp between BEG and END."
  15714. (let* ((ts (buffer-substring beg end))
  15715. t1 w1 with-hm tf time str w2 (off 0))
  15716. (save-match-data
  15717. (setq t1 (org-parse-time-string ts t))
  15718. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15719. (setq off (- (match-end 0) (match-beginning 0)))))
  15720. (setq end (- end off))
  15721. (setq w1 (- end beg)
  15722. with-hm (and (nth 1 t1) (nth 2 t1))
  15723. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15724. time (org-fix-decoded-time t1)
  15725. str (org-add-props
  15726. (format-time-string
  15727. (substring tf 1 -1) (apply 'encode-time time))
  15728. nil 'mouse-face 'highlight)
  15729. w2 (length str))
  15730. (if (not (= w2 w1))
  15731. (add-text-properties (1+ beg) (+ 2 beg)
  15732. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15733. (if (featurep 'xemacs)
  15734. (progn
  15735. (put-text-property beg end 'invisible t)
  15736. (put-text-property beg end 'end-glyph (make-glyph str)))
  15737. (put-text-property beg end 'display str))))
  15738. (defun org-fix-decoded-time (time)
  15739. "Set 0 instead of nil for the first 6 elements of time.
  15740. Don't touch the rest."
  15741. (let ((n 0))
  15742. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15743. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15744. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15745. "Difference between TIMESTAMP-STRING and now in days.
  15746. If SECONDS is non-nil, return the difference in seconds."
  15747. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15748. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15749. (funcall fdiff (current-time)))))
  15750. (defun org-deadline-close (timestamp-string &optional ndays)
  15751. "Is the time in TIMESTAMP-STRING close to the current date?"
  15752. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15753. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15754. (not (org-entry-is-done-p))))
  15755. (defun org-get-wdays (ts &optional delay zero-delay)
  15756. "Get the deadline lead time appropriate for timestring TS.
  15757. When DELAY is non-nil, get the delay time for scheduled items
  15758. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15759. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15760. don't try to find the delay cookie in the scheduled timestamp."
  15761. (let ((tv (if delay org-scheduled-delay-days
  15762. org-deadline-warning-days)))
  15763. (cond
  15764. ((or (and delay (< tv 0))
  15765. (and delay zero-delay (<= tv 0))
  15766. (and (not delay) (<= tv 0)))
  15767. ;; Enforce this value no matter what
  15768. (- tv))
  15769. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15770. ;; lead time is specified.
  15771. (floor (* (string-to-number (match-string 1 ts))
  15772. (cdr (assoc (match-string 2 ts)
  15773. '(("d" . 1) ("w" . 7)
  15774. ("m" . 30.4) ("y" . 365.25)
  15775. ("h" . 0.041667)))))))
  15776. ;; go for the default.
  15777. (t tv))))
  15778. (defun org-calendar-select-mouse (ev)
  15779. "Return to `org-read-date' with the date currently selected.
  15780. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15781. (interactive "e")
  15782. (mouse-set-point ev)
  15783. (when (calendar-cursor-to-date)
  15784. (let* ((date (calendar-cursor-to-date))
  15785. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15786. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15787. (if (active-minibuffer-window) (exit-minibuffer))))
  15788. (defun org-check-deadlines (ndays)
  15789. "Check if there are any deadlines due or past due.
  15790. A deadline is considered due if it happens within `org-deadline-warning-days'
  15791. days from today's date. If the deadline appears in an entry marked DONE,
  15792. it is not shown. The prefix arg NDAYS can be used to test that many
  15793. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15794. (interactive "P")
  15795. (let* ((org-warn-days
  15796. (cond
  15797. ((equal ndays '(4)) 100000)
  15798. (ndays (prefix-numeric-value ndays))
  15799. (t (abs org-deadline-warning-days))))
  15800. (case-fold-search nil)
  15801. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15802. (callback
  15803. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15804. (message "%d deadlines past-due or due within %d days"
  15805. (org-occur regexp nil callback)
  15806. org-warn-days)))
  15807. (defsubst org-re-timestamp (type)
  15808. "Return a regexp for timestamp TYPE.
  15809. Allowed values for TYPE are:
  15810. all: all timestamps
  15811. active: only active timestamps (<...>)
  15812. inactive: only inactive timestamps ([...])
  15813. scheduled: only scheduled timestamps
  15814. deadline: only deadline timestamps
  15815. closed: only closed time-stamps
  15816. When TYPE is nil, fall back on returning a regexp that matches
  15817. both scheduled and deadline timestamps."
  15818. (case type
  15819. (all org-ts-regexp-both)
  15820. (active org-ts-regexp)
  15821. (inactive org-ts-regexp-inactive)
  15822. (scheduled org-scheduled-time-regexp)
  15823. (deadline org-deadline-time-regexp)
  15824. (closed org-closed-time-regexp)
  15825. (otherwise
  15826. (concat "\\<"
  15827. (regexp-opt (list org-deadline-string org-scheduled-string))
  15828. " *<\\([^>]+\\)>"))))
  15829. (defun org-check-before-date (date)
  15830. "Check if there are deadlines or scheduled entries before DATE."
  15831. (interactive (list (org-read-date)))
  15832. (let ((case-fold-search nil)
  15833. (regexp (org-re-timestamp org-ts-type))
  15834. (callback
  15835. `(lambda ()
  15836. (and ,(if (memq org-ts-type '(active inactive all))
  15837. '(eq (org-element-type (org-element-context) 'timestamp))
  15838. '(org-at-planning-p))
  15839. (time-less-p
  15840. (org-time-string-to-time (match-string 1))
  15841. (org-time-string-to-time date))))))
  15842. (message "%d entries before %s"
  15843. (org-occur regexp nil callback) date)))
  15844. (defun org-check-after-date (date)
  15845. "Check if there are deadlines or scheduled entries after DATE."
  15846. (interactive (list (org-read-date)))
  15847. (let ((case-fold-search nil)
  15848. (regexp (org-re-timestamp org-ts-type))
  15849. (callback
  15850. `(lambda ()
  15851. (and ,(if (memq org-ts-type '(active inactive all))
  15852. '(eq (org-element-type (org-element-context) 'timestamp))
  15853. '(org-at-planning-p))
  15854. (not (time-less-p
  15855. (org-time-string-to-time (match-string 1))
  15856. (org-time-string-to-time date)))))))
  15857. (message "%d entries after %s"
  15858. (org-occur regexp nil callback) date)))
  15859. (defun org-check-dates-range (start-date end-date)
  15860. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15861. (interactive (list (org-read-date nil nil nil "Range starts")
  15862. (org-read-date nil nil nil "Range end")))
  15863. (let ((case-fold-search nil)
  15864. (regexp (org-re-timestamp org-ts-type))
  15865. (callback
  15866. `(lambda ()
  15867. (let ((match (match-string 1)))
  15868. (and
  15869. ,(if (memq org-ts-type '(active inactive all))
  15870. '(eq (org-element-type (org-element-context) 'timestamp))
  15871. '(org-at-planning-p))
  15872. (not (time-less-p
  15873. (org-time-string-to-time match)
  15874. (org-time-string-to-time start-date)))
  15875. (time-less-p
  15876. (org-time-string-to-time match)
  15877. (org-time-string-to-time end-date)))))))
  15878. (message "%d entries between %s and %s"
  15879. (org-occur regexp nil callback) start-date end-date)))
  15880. (defun org-evaluate-time-range (&optional to-buffer)
  15881. "Evaluate a time range by computing the difference between start and end.
  15882. Normally the result is just printed in the echo area, but with prefix arg
  15883. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15884. If the time range is actually in a table, the result is inserted into the
  15885. next column.
  15886. For time difference computation, a year is assumed to be exactly 365
  15887. days in order to avoid rounding problems."
  15888. (interactive "P")
  15889. (or
  15890. (org-clock-update-time-maybe)
  15891. (save-excursion
  15892. (unless (org-at-date-range-p t)
  15893. (goto-char (point-at-bol))
  15894. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15895. (if (not (org-at-date-range-p t))
  15896. (user-error "Not at a time-stamp range, and none found in current line")))
  15897. (let* ((ts1 (match-string 1))
  15898. (ts2 (match-string 2))
  15899. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15900. (match-end (match-end 0))
  15901. (time1 (org-time-string-to-time ts1))
  15902. (time2 (org-time-string-to-time ts2))
  15903. (t1 (org-float-time time1))
  15904. (t2 (org-float-time time2))
  15905. (diff (abs (- t2 t1)))
  15906. (negative (< (- t2 t1) 0))
  15907. ;; (ys (floor (* 365 24 60 60)))
  15908. (ds (* 24 60 60))
  15909. (hs (* 60 60))
  15910. (fy "%dy %dd %02d:%02d")
  15911. (fy1 "%dy %dd")
  15912. (fd "%dd %02d:%02d")
  15913. (fd1 "%dd")
  15914. (fh "%02d:%02d")
  15915. y d h m align)
  15916. (if havetime
  15917. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15918. y 0
  15919. d (floor (/ diff ds)) diff (mod diff ds)
  15920. h (floor (/ diff hs)) diff (mod diff hs)
  15921. m (floor (/ diff 60)))
  15922. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15923. y 0
  15924. d (floor (+ (/ diff ds) 0.5))
  15925. h 0 m 0))
  15926. (if (not to-buffer)
  15927. (message "%s" (org-make-tdiff-string y d h m))
  15928. (if (org-at-table-p)
  15929. (progn
  15930. (goto-char match-end)
  15931. (setq align t)
  15932. (and (looking-at " *|") (goto-char (match-end 0))))
  15933. (goto-char match-end))
  15934. (if (looking-at
  15935. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15936. (replace-match ""))
  15937. (if negative (insert " -"))
  15938. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15939. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15940. (insert " " (format fh h m))))
  15941. (if align (org-table-align))
  15942. (message "Time difference inserted")))))
  15943. (defun org-make-tdiff-string (y d h m)
  15944. (let ((fmt "")
  15945. (l nil))
  15946. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15947. l (push y l)))
  15948. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15949. l (push d l)))
  15950. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15951. l (push h l)))
  15952. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15953. l (push m l)))
  15954. (apply 'format fmt (nreverse l))))
  15955. (defun org-time-string-to-time (s &optional buffer pos)
  15956. "Convert a timestamp string into internal time."
  15957. (condition-case errdata
  15958. (apply 'encode-time (org-parse-time-string s))
  15959. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15960. s (if (not (and buffer pos))
  15961. ""
  15962. (format " at %d in buffer `%s'" pos buffer))
  15963. (cdr errdata)))))
  15964. (defun org-time-string-to-seconds (s)
  15965. "Convert a timestamp string to a number of seconds."
  15966. (org-float-time (org-time-string-to-time s)))
  15967. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15968. "Convert a time stamp to an absolute day number.
  15969. If there is a specifier for a cyclic time stamp, get the closest
  15970. date to DAYNR.
  15971. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15972. The variable `date' is bound by the calendar when this is called."
  15973. (cond
  15974. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15975. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15976. daynr
  15977. (+ daynr 1000)))
  15978. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15979. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15980. (time-to-days (current-time))) (match-string 0 s)
  15981. prefer show-all))
  15982. (t (time-to-days
  15983. (condition-case errdata
  15984. (apply 'encode-time (org-parse-time-string s))
  15985. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15986. s (if (not (and buffer pos))
  15987. ""
  15988. (format " at %d in buffer `%s'" pos buffer))
  15989. (cdr errdata))))))))
  15990. (defun org-days-to-iso-week (days)
  15991. "Return the iso week number."
  15992. (require 'cal-iso)
  15993. (car (calendar-iso-from-absolute days)))
  15994. (defun org-small-year-to-year (year)
  15995. "Convert 2-digit years into 4-digit years.
  15996. YEAR is expanded into one of the 30 next years, if possible, or
  15997. into a past one. Any year larger than 99 is returned unchanged."
  15998. (if (>= year 100) year
  15999. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16000. (century (/ current 100))
  16001. (offset (- year (% current 100))))
  16002. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16003. ((> offset -70) (+ (* century 100) year))
  16004. (t (+ (* (1+ century) 100) year))))))
  16005. (defun org-time-from-absolute (d)
  16006. "Return the time corresponding to date D.
  16007. D may be an absolute day number, or a calendar-type list (month day year)."
  16008. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16009. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16010. (defun org-calendar-holiday ()
  16011. "List of holidays, for Diary display in Org-mode."
  16012. (require 'holidays)
  16013. (let ((hl (funcall
  16014. (if (fboundp 'calendar-check-holidays)
  16015. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16016. (if hl (mapconcat 'identity hl "; "))))
  16017. (defun org-diary-sexp-entry (sexp entry date)
  16018. "Process a SEXP diary ENTRY for DATE."
  16019. (require 'diary-lib)
  16020. (let ((result (if calendar-debug-sexp
  16021. (let ((stack-trace-on-error t))
  16022. (eval (car (read-from-string sexp))))
  16023. (condition-case nil
  16024. (eval (car (read-from-string sexp)))
  16025. (error
  16026. (beep)
  16027. (message "Bad sexp at line %d in %s: %s"
  16028. (org-current-line)
  16029. (buffer-file-name) sexp)
  16030. (sleep-for 2))))))
  16031. (cond ((stringp result) (split-string result "; "))
  16032. ((and (consp result)
  16033. (not (consp (cdr result)))
  16034. (stringp (cdr result))) (cdr result))
  16035. ((and (consp result)
  16036. (stringp (car result))) result)
  16037. (result entry))))
  16038. (defun org-diary-to-ical-string (frombuf)
  16039. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16040. This uses the icalendar.el library."
  16041. (let* ((tmpdir (if (featurep 'xemacs)
  16042. (temp-directory)
  16043. temporary-file-directory))
  16044. (tmpfile (make-temp-name
  16045. (expand-file-name "orgics" tmpdir)))
  16046. buf rtn b e)
  16047. (with-current-buffer frombuf
  16048. (icalendar-export-region (point-min) (point-max) tmpfile)
  16049. (setq buf (find-buffer-visiting tmpfile))
  16050. (set-buffer buf)
  16051. (goto-char (point-min))
  16052. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16053. (setq b (match-beginning 0)))
  16054. (goto-char (point-max))
  16055. (if (re-search-backward "^END:VEVENT" nil t)
  16056. (setq e (match-end 0)))
  16057. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16058. (kill-buffer buf)
  16059. (delete-file tmpfile)
  16060. rtn))
  16061. (defun org-closest-date (start current change prefer show-all)
  16062. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16063. When PREFER is `past', return a date that is either CURRENT or past.
  16064. When PREFER is `future', return a date that is either CURRENT or future.
  16065. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16066. ;; Make the proper lists from the dates
  16067. (catch 'exit
  16068. (let ((a1 '(("h" . hour)
  16069. ("d" . day)
  16070. ("w" . week)
  16071. ("m" . month)
  16072. ("y" . year)))
  16073. (shour (nth 2 (org-parse-time-string start)))
  16074. dn dw sday cday n1 n2 n0
  16075. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16076. (setq start (org-date-to-gregorian start)
  16077. current (org-date-to-gregorian
  16078. (if show-all
  16079. current
  16080. (time-to-days (current-time))))
  16081. sday (calendar-absolute-from-gregorian start)
  16082. cday (calendar-absolute-from-gregorian current))
  16083. (if (<= cday sday) (throw 'exit sday))
  16084. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16085. (setq dn (string-to-number (match-string 1 change))
  16086. dw (cdr (assoc (match-string 2 change) a1)))
  16087. (user-error "Invalid change specifier: %s" change))
  16088. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16089. (cond
  16090. ((eq dw 'hour)
  16091. (let ((missing-hours
  16092. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16093. dn)))
  16094. (setq n1 (if (zerop missing-hours) cday
  16095. (- cday (1+ (floor (/ missing-hours 24)))))
  16096. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16097. ((eq dw 'day)
  16098. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16099. n2 (+ n1 dn)))
  16100. ((eq dw 'year)
  16101. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16102. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16103. (setq date1 (list m d y1)
  16104. n1 (calendar-absolute-from-gregorian date1)
  16105. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16106. n2 (calendar-absolute-from-gregorian date2)))
  16107. ((eq dw 'month)
  16108. ;; approx number of month between the two dates
  16109. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16110. ;; How often does dn fit in there?
  16111. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16112. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16113. m (+ m nm)
  16114. ny (floor (/ m 12))
  16115. y (+ y ny)
  16116. m (- m (* ny 12)))
  16117. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16118. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16119. (setq m2 (+ m dn) y2 y)
  16120. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16121. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16122. (while (<= n2 cday)
  16123. (setq n1 n2 m m2 y y2)
  16124. (setq m2 (+ m dn) y2 y)
  16125. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16126. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16127. ;; Make sure n1 is the earlier date
  16128. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16129. (if show-all
  16130. (cond
  16131. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16132. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16133. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16134. (cond
  16135. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16136. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16137. (t (if (= cday n1) n1 n2)))))))
  16138. (defun org-date-to-gregorian (date)
  16139. "Turn any specification of DATE into a Gregorian date for the calendar."
  16140. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16141. ((and (listp date) (= (length date) 3)) date)
  16142. ((stringp date)
  16143. (setq date (org-parse-time-string date))
  16144. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16145. ((listp date)
  16146. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16147. (defun org-parse-time-string (s &optional nodefault)
  16148. "Parse the standard Org-mode time string.
  16149. This should be a lot faster than the normal `parse-time-string'.
  16150. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16151. hour and minute fields will be nil if not given."
  16152. (cond ((string-match org-ts-regexp0 s)
  16153. (list 0
  16154. (if (or (match-beginning 8) (not nodefault))
  16155. (string-to-number (or (match-string 8 s) "0")))
  16156. (if (or (match-beginning 7) (not nodefault))
  16157. (string-to-number (or (match-string 7 s) "0")))
  16158. (string-to-number (match-string 4 s))
  16159. (string-to-number (match-string 3 s))
  16160. (string-to-number (match-string 2 s))
  16161. nil nil nil))
  16162. ((string-match "^<[^>]+>$" s)
  16163. (decode-time (seconds-to-time (org-matcher-time s))))
  16164. (t (error "Not a standard Org-mode time string: %s" s))))
  16165. (defun org-timestamp-up (&optional arg)
  16166. "Increase the date item at the cursor by one.
  16167. If the cursor is on the year, change the year. If it is on the month,
  16168. the day or the time, change that.
  16169. With prefix ARG, change by that many units."
  16170. (interactive "p")
  16171. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16172. (defun org-timestamp-down (&optional arg)
  16173. "Decrease the date item at the cursor by one.
  16174. If the cursor is on the year, change the year. If it is on the month,
  16175. the day or the time, change that.
  16176. With prefix ARG, change by that many units."
  16177. (interactive "p")
  16178. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16179. (defun org-timestamp-up-day (&optional arg)
  16180. "Increase the date in the time stamp by one day.
  16181. With prefix ARG, change that many days."
  16182. (interactive "p")
  16183. (if (and (not (org-at-timestamp-p t))
  16184. (org-at-heading-p))
  16185. (org-todo 'up)
  16186. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16187. (defun org-timestamp-down-day (&optional arg)
  16188. "Decrease the date in the time stamp by one day.
  16189. With prefix ARG, change that many days."
  16190. (interactive "p")
  16191. (if (and (not (org-at-timestamp-p t))
  16192. (org-at-heading-p))
  16193. (org-todo 'down)
  16194. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16195. (defun org-at-timestamp-p (&optional inactive-ok)
  16196. "Determine if the cursor is in or at a timestamp."
  16197. (interactive)
  16198. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16199. (pos (point))
  16200. (ans (or (looking-at tsr)
  16201. (save-excursion
  16202. (skip-chars-backward "^[<\n\r\t")
  16203. (if (> (point) (point-min)) (backward-char 1))
  16204. (and (looking-at tsr)
  16205. (> (- (match-end 0) pos) -1))))))
  16206. (and ans
  16207. (boundp 'org-ts-what)
  16208. (setq org-ts-what
  16209. (cond
  16210. ((= pos (match-beginning 0)) 'bracket)
  16211. ;; Point is considered to be "on the bracket" whether
  16212. ;; it's really on it or right after it.
  16213. ((= pos (1- (match-end 0))) 'bracket)
  16214. ((= pos (match-end 0)) 'after)
  16215. ((org-pos-in-match-range pos 2) 'year)
  16216. ((org-pos-in-match-range pos 3) 'month)
  16217. ((org-pos-in-match-range pos 7) 'hour)
  16218. ((org-pos-in-match-range pos 8) 'minute)
  16219. ((or (org-pos-in-match-range pos 4)
  16220. (org-pos-in-match-range pos 5)) 'day)
  16221. ((and (> pos (or (match-end 8) (match-end 5)))
  16222. (< pos (match-end 0)))
  16223. (- pos (or (match-end 8) (match-end 5))))
  16224. (t 'day))))
  16225. ans))
  16226. (defun org-toggle-timestamp-type ()
  16227. "Toggle the type (<active> or [inactive]) of a time stamp."
  16228. (interactive)
  16229. (when (org-at-timestamp-p t)
  16230. (let ((beg (match-beginning 0)) (end (match-end 0))
  16231. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16232. (save-excursion
  16233. (goto-char beg)
  16234. (while (re-search-forward "[][<>]" end t)
  16235. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16236. t t)))
  16237. (message "Timestamp is now %sactive"
  16238. (if (equal (char-after beg) ?<) "" "in")))))
  16239. (defun org-at-clock-log-p nil
  16240. "Is the cursor on the clock log line?"
  16241. (save-excursion
  16242. (move-beginning-of-line 1)
  16243. (looking-at org-clock-line-re)))
  16244. (defvar org-clock-history) ; defined in org-clock.el
  16245. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16246. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16247. "Change the date in the time stamp at point.
  16248. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16249. `year', `minute', `second'. If WHAT is not given, the cursor position
  16250. in the timestamp determines what will be changed.
  16251. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16252. (let ((origin (point)) origin-cat
  16253. with-hm inactive
  16254. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16255. org-ts-what
  16256. extra rem
  16257. ts time time0 fixnext clrgx)
  16258. (if (not (org-at-timestamp-p t))
  16259. (user-error "Not at a timestamp"))
  16260. (if (and (not what) (eq org-ts-what 'bracket))
  16261. (org-toggle-timestamp-type)
  16262. ;; Point isn't on brackets. Remember the part of the time-stamp
  16263. ;; the point was in. Indeed, size of time-stamps may change,
  16264. ;; but point must be kept in the same category nonetheless.
  16265. (setq origin-cat org-ts-what)
  16266. (if (and (not what) (not (eq org-ts-what 'day))
  16267. org-display-custom-times
  16268. (get-text-property (point) 'display)
  16269. (not (get-text-property (1- (point)) 'display)))
  16270. (setq org-ts-what 'day))
  16271. (setq org-ts-what (or what org-ts-what)
  16272. inactive (= (char-after (match-beginning 0)) ?\[)
  16273. ts (match-string 0))
  16274. (replace-match "")
  16275. (when (string-match
  16276. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16277. ts)
  16278. (setq extra (match-string 1 ts))
  16279. (if suppress-tmp-delay
  16280. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16281. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16282. (setq with-hm t))
  16283. (setq time0 (org-parse-time-string ts))
  16284. (when (and updown
  16285. (eq org-ts-what 'minute)
  16286. (not current-prefix-arg))
  16287. ;; This looks like s-up and s-down. Change by one rounding step.
  16288. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16289. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16290. (setcar (cdr time0) (+ (nth 1 time0)
  16291. (if (> n 0) (- rem) (- dm rem))))))
  16292. (setq time
  16293. (encode-time (or (car time0) 0)
  16294. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16295. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16296. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16297. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16298. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16299. (nthcdr 6 time0)))
  16300. (when (and (member org-ts-what '(hour minute))
  16301. extra
  16302. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16303. (setq extra (org-modify-ts-extra
  16304. extra
  16305. (if (eq org-ts-what 'hour) 2 5)
  16306. n dm)))
  16307. (when (integerp org-ts-what)
  16308. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16309. (if (eq what 'calendar)
  16310. (let ((cal-date (org-get-date-from-calendar)))
  16311. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16312. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16313. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16314. (setcar time0 (or (car time0) 0))
  16315. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16316. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16317. (setq time (apply 'encode-time time0))))
  16318. ;; Insert the new time-stamp, and ensure point stays in the same
  16319. ;; category as before (i.e. not after the last position in that
  16320. ;; category).
  16321. (let ((pos (point)))
  16322. ;; Stay before inserted string. `save-excursion' is of no use.
  16323. (setq org-last-changed-timestamp
  16324. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16325. (goto-char pos))
  16326. (save-match-data
  16327. (looking-at org-ts-regexp3)
  16328. (goto-char (cond
  16329. ;; `day' category ends before `hour' if any, or at
  16330. ;; the end of the day name.
  16331. ((eq origin-cat 'day)
  16332. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16333. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16334. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16335. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16336. ;; `year' and `month' have both fixed size: point
  16337. ;; couldn't have moved into another part.
  16338. (t origin))))
  16339. ;; Update clock if on a CLOCK line.
  16340. (org-clock-update-time-maybe)
  16341. ;; Maybe adjust the closest clock in `org-clock-history'
  16342. (when org-clock-adjust-closest
  16343. (if (not (and (org-at-clock-log-p)
  16344. (< 1 (length (delq nil (mapcar 'marker-position
  16345. org-clock-history))))))
  16346. (message "No clock to adjust")
  16347. (cond ((save-excursion ; fix previous clock?
  16348. (re-search-backward org-ts-regexp0 nil t)
  16349. (org-looking-back (concat org-clock-string " \\[")))
  16350. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16351. ((save-excursion ; fix next clock?
  16352. (re-search-backward org-ts-regexp0 nil t)
  16353. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16354. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16355. (save-window-excursion
  16356. ;; Find closest clock to point, adjust the previous/next one in history
  16357. (let* ((p (save-excursion (org-back-to-heading t)))
  16358. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16359. (clfixnth
  16360. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16361. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16362. (if (not clfixpos)
  16363. (message "No clock to adjust")
  16364. (save-excursion
  16365. (org-goto-marker-or-bmk clfixpos)
  16366. (org-show-subtree)
  16367. (when (re-search-forward clrgx nil t)
  16368. (goto-char (match-beginning 1))
  16369. (let (org-clock-adjust-closest)
  16370. (org-timestamp-change n org-ts-what updown))
  16371. (message "Clock adjusted in %s for heading: %s"
  16372. (file-name-nondirectory (buffer-file-name))
  16373. (org-get-heading t t)))))))))
  16374. ;; Try to recenter the calendar window, if any.
  16375. (if (and org-calendar-follow-timestamp-change
  16376. (get-buffer-window "*Calendar*" t)
  16377. (memq org-ts-what '(day month year)))
  16378. (org-recenter-calendar (time-to-days time))))))
  16379. (defun org-modify-ts-extra (s pos n dm)
  16380. "Change the different parts of the lead-time and repeat fields in timestamp."
  16381. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16382. ng h m new rem)
  16383. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16384. (cond
  16385. ((or (org-pos-in-match-range pos 2)
  16386. (org-pos-in-match-range pos 3))
  16387. (setq m (string-to-number (match-string 3 s))
  16388. h (string-to-number (match-string 2 s)))
  16389. (if (org-pos-in-match-range pos 2)
  16390. (setq h (+ h n))
  16391. (setq n (* dm (org-no-warnings (signum n))))
  16392. (when (not (= 0 (setq rem (% m dm))))
  16393. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16394. (setq m (+ m n)))
  16395. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16396. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16397. (setq h (min 24 (max 0 h)))
  16398. (setq ng 1 new (format "-%02d:%02d" h m)))
  16399. ((org-pos-in-match-range pos 6)
  16400. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16401. ((org-pos-in-match-range pos 5)
  16402. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16403. ((org-pos-in-match-range pos 9)
  16404. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16405. ((org-pos-in-match-range pos 8)
  16406. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16407. (when ng
  16408. (setq s (concat
  16409. (substring s 0 (match-beginning ng))
  16410. new
  16411. (substring s (match-end ng))))))
  16412. s))
  16413. (defun org-recenter-calendar (date)
  16414. "If the calendar is visible, recenter it to DATE."
  16415. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16416. (when cwin
  16417. (let ((calendar-move-hook nil))
  16418. (with-selected-window cwin
  16419. (calendar-goto-date (if (listp date) date
  16420. (calendar-gregorian-from-absolute date))))))))
  16421. (defun org-goto-calendar (&optional arg)
  16422. "Go to the Emacs calendar at the current date.
  16423. If there is a time stamp in the current line, go to that date.
  16424. A prefix ARG can be used to force the current date."
  16425. (interactive "P")
  16426. (let ((tsr org-ts-regexp) diff
  16427. (calendar-move-hook nil)
  16428. (calendar-view-holidays-initially-flag nil)
  16429. (calendar-view-diary-initially-flag nil))
  16430. (if (or (org-at-timestamp-p)
  16431. (save-excursion
  16432. (beginning-of-line 1)
  16433. (looking-at (concat ".*" tsr))))
  16434. (let ((d1 (time-to-days (current-time)))
  16435. (d2 (time-to-days
  16436. (org-time-string-to-time (match-string 1)))))
  16437. (setq diff (- d2 d1))))
  16438. (calendar)
  16439. (calendar-goto-today)
  16440. (if (and diff (not arg)) (calendar-forward-day diff))))
  16441. (defun org-get-date-from-calendar ()
  16442. "Return a list (month day year) of date at point in calendar."
  16443. (with-current-buffer "*Calendar*"
  16444. (save-match-data
  16445. (calendar-cursor-to-date))))
  16446. (defun org-date-from-calendar ()
  16447. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16448. If there is already a time stamp at the cursor position, update it."
  16449. (interactive)
  16450. (if (org-at-timestamp-p t)
  16451. (org-timestamp-change 0 'calendar)
  16452. (let ((cal-date (org-get-date-from-calendar)))
  16453. (org-insert-time-stamp
  16454. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16455. (defcustom org-effort-durations
  16456. `(("min" . 1)
  16457. ("h" . 60)
  16458. ("d" . ,(* 60 8))
  16459. ("w" . ,(* 60 8 5))
  16460. ("m" . ,(* 60 8 5 4))
  16461. ("y" . ,(* 60 8 5 40)))
  16462. "Conversion factor to minutes for an effort modifier.
  16463. Each entry has the form (MODIFIER . MINUTES).
  16464. In an effort string, a number followed by MODIFIER is multiplied
  16465. by the specified number of MINUTES to obtain an effort in
  16466. minutes.
  16467. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16468. effort string \"2hours\" is equivalent to 120 minutes."
  16469. :group 'org-agenda
  16470. :version "25.1"
  16471. :package-version '(Org . "8.3")
  16472. :type '(alist :key-type (string :tag "Modifier")
  16473. :value-type (number :tag "Minutes")))
  16474. (defun org-minutes-to-clocksum-string (m)
  16475. "Format number of minutes as a clocksum string.
  16476. The format is determined by `org-time-clocksum-format',
  16477. `org-time-clocksum-use-fractional' and
  16478. `org-time-clocksum-fractional-format' and
  16479. `org-time-clocksum-use-effort-durations'."
  16480. (let ((clocksum "")
  16481. (m (round m)) ; Don't allow fractions of minutes
  16482. h d w mo y fmt n)
  16483. (setq h (if org-time-clocksum-use-effort-durations
  16484. (cdr (assoc "h" org-effort-durations)) 60)
  16485. d (if org-time-clocksum-use-effort-durations
  16486. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16487. w (if org-time-clocksum-use-effort-durations
  16488. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16489. mo (if org-time-clocksum-use-effort-durations
  16490. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16491. y (if org-time-clocksum-use-effort-durations
  16492. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16493. ;; fractional format
  16494. (if org-time-clocksum-use-fractional
  16495. (cond
  16496. ;; single format string
  16497. ((stringp org-time-clocksum-fractional-format)
  16498. (format org-time-clocksum-fractional-format (/ m (float h))))
  16499. ;; choice of fractional formats for different time units
  16500. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16501. (> (/ (truncate m) (* y d h)) 0))
  16502. (format fmt (/ m (* y d (float h)))))
  16503. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16504. (> (/ (truncate m) (* mo d h)) 0))
  16505. (format fmt (/ m (* mo d (float h)))))
  16506. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16507. (> (/ (truncate m) (* w d h)) 0))
  16508. (format fmt (/ m (* w d (float h)))))
  16509. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16510. (> (/ (truncate m) (* d h)) 0))
  16511. (format fmt (/ m (* d (float h)))))
  16512. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16513. (> (/ (truncate m) h) 0))
  16514. (format fmt (/ m (float h))))
  16515. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16516. (format fmt m))
  16517. ;; fall back to smallest time unit with a format
  16518. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16519. (format fmt (/ m (float h))))
  16520. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16521. (format fmt (/ m (* d (float h)))))
  16522. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16523. (format fmt (/ m (* w d (float h)))))
  16524. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16525. (format fmt (/ m (* mo d (float h)))))
  16526. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16527. (format fmt (/ m (* y d (float h))))))
  16528. ;; standard (non-fractional) format, with single format string
  16529. (if (stringp org-time-clocksum-format)
  16530. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16531. ;; separate formats components
  16532. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16533. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16534. (plist-get org-time-clocksum-format :require-years))
  16535. (setq clocksum (concat clocksum (format fmt n))
  16536. m (- m (* n y d h))))
  16537. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16538. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16539. (plist-get org-time-clocksum-format :require-months))
  16540. (setq clocksum (concat clocksum (format fmt n))
  16541. m (- m (* n mo d h))))
  16542. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16543. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16544. (plist-get org-time-clocksum-format :require-weeks))
  16545. (setq clocksum (concat clocksum (format fmt n))
  16546. m (- m (* n w d h))))
  16547. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16548. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16549. (plist-get org-time-clocksum-format :require-days))
  16550. (setq clocksum (concat clocksum (format fmt n))
  16551. m (- m (* n d h))))
  16552. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16553. (or (> (setq n (/ (truncate m) h)) 0)
  16554. (plist-get org-time-clocksum-format :require-hours))
  16555. (setq clocksum (concat clocksum (format fmt n))
  16556. m (- m (* n h))))
  16557. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16558. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16559. (setq clocksum (concat clocksum (format fmt m))))
  16560. ;; return formatted time duration
  16561. clocksum))))
  16562. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16563. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16564. "Org mode version 8.0")
  16565. (defun org-hours-to-clocksum-string (n)
  16566. (org-minutes-to-clocksum-string (* n 60)))
  16567. (defun org-hh:mm-string-to-minutes (s)
  16568. "Convert a string H:MM to a number of minutes.
  16569. If the string is just a number, interpret it as minutes.
  16570. In fact, the first hh:mm or number in the string will be taken,
  16571. there can be extra stuff in the string.
  16572. If no number is found, the return value is 0."
  16573. (cond
  16574. ((integerp s) s)
  16575. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16576. (+ (* (string-to-number (match-string 1 s)) 60)
  16577. (string-to-number (match-string 2 s))))
  16578. ((string-match "\\([0-9]+\\)" s)
  16579. (string-to-number (match-string 1 s)))
  16580. (t 0)))
  16581. (defcustom org-image-actual-width t
  16582. "Should we use the actual width of images when inlining them?
  16583. When set to `t', always use the image width.
  16584. When set to a number, use imagemagick (when available) to set
  16585. the image's width to this value.
  16586. When set to a number in a list, try to get the width from any
  16587. #+ATTR.* keyword if it matches a width specification like
  16588. #+ATTR_HTML: :width 300px
  16589. and fall back on that number if none is found.
  16590. When set to nil, try to get the width from an #+ATTR.* keyword
  16591. and fall back on the original width if none is found.
  16592. This requires Emacs >= 24.1, build with imagemagick support."
  16593. :group 'org-appearance
  16594. :version "24.4"
  16595. :package-version '(Org . "8.0")
  16596. :type '(choice
  16597. (const :tag "Use the image width" t)
  16598. (integer :tag "Use a number of pixels")
  16599. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16600. (const :tag "Use #+ATTR* or don't resize" nil)))
  16601. (defcustom org-agenda-inhibit-startup nil
  16602. "Inhibit startup when preparing agenda buffers.
  16603. When this variable is `t', the initialization of the Org agenda
  16604. buffers is inhibited: e.g. the visibility state is not set, the
  16605. tables are not re-aligned, etc."
  16606. :type 'boolean
  16607. :version "24.3"
  16608. :group 'org-agenda)
  16609. (define-obsolete-variable-alias
  16610. 'org-agenda-ignore-drawer-properties
  16611. 'org-agenda-ignore-properties "25.1")
  16612. (defcustom org-agenda-ignore-properties nil
  16613. "Avoid updating text properties when building the agenda.
  16614. Properties are used to prepare buffers for effort estimates,
  16615. appointments, statistics and subtree-local categories.
  16616. If you don't use these in the agenda, you can add them to this
  16617. list and agenda building will be a bit faster.
  16618. The value is a list, with zero or more of the symbols `effort', `appt',
  16619. `stats' or `category'."
  16620. :type '(set :greedy t
  16621. (const effort)
  16622. (const appt)
  16623. (const stats)
  16624. (const category))
  16625. :version "25.1"
  16626. :package-version '(Org . "8.3")
  16627. :group 'org-agenda)
  16628. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16629. "Convert a duration string S to minutes.
  16630. A bare number is interpreted as minutes, modifiers can be set by
  16631. customizing `org-effort-durations' (which see).
  16632. Entries containing a colon are interpreted as H:MM by
  16633. `org-hh:mm-string-to-minutes'."
  16634. (let ((result 0)
  16635. (re (concat "\\([0-9.]+\\) *\\("
  16636. (regexp-opt (mapcar 'car org-effort-durations))
  16637. "\\)")))
  16638. (while (string-match re s)
  16639. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16640. (string-to-number (match-string 1 s))))
  16641. (setq s (replace-match "" nil t s)))
  16642. (setq result (floor result))
  16643. (incf result (org-hh:mm-string-to-minutes s))
  16644. (if output-to-string (number-to-string result) result)))
  16645. ;;;; Files
  16646. (defun org-save-all-org-buffers ()
  16647. "Save all Org-mode buffers without user confirmation."
  16648. (interactive)
  16649. (message "Saving all Org-mode buffers...")
  16650. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16651. (when (featurep 'org-id) (org-id-locations-save))
  16652. (message "Saving all Org-mode buffers... done"))
  16653. (defun org-revert-all-org-buffers ()
  16654. "Revert all Org-mode buffers.
  16655. Prompt for confirmation when there are unsaved changes.
  16656. Be sure you know what you are doing before letting this function
  16657. overwrite your changes.
  16658. This function is useful in a setup where one tracks org files
  16659. with a version control system, to revert on one machine after pulling
  16660. changes from another. I believe the procedure must be like this:
  16661. 1. M-x org-save-all-org-buffers
  16662. 2. Pull changes from the other machine, resolve conflicts
  16663. 3. M-x org-revert-all-org-buffers"
  16664. (interactive)
  16665. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16666. (user-error "Abort"))
  16667. (save-excursion
  16668. (save-window-excursion
  16669. (mapc
  16670. (lambda (b)
  16671. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16672. (with-current-buffer b buffer-file-name))
  16673. (org-pop-to-buffer-same-window b)
  16674. (revert-buffer t 'no-confirm)))
  16675. (buffer-list))
  16676. (when (and (featurep 'org-id) org-id-track-globally)
  16677. (org-id-locations-load)))))
  16678. ;;;; Agenda files
  16679. ;;;###autoload
  16680. (defun org-switchb (&optional arg)
  16681. "Switch between Org buffers.
  16682. With one prefix argument, restrict available buffers to files.
  16683. With two prefix arguments, restrict available buffers to agenda files.
  16684. Defaults to `iswitchb' for buffer name completion.
  16685. Set `org-completion-use-ido' to make it use ido instead."
  16686. (interactive "P")
  16687. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16688. ((equal arg '(16)) (org-buffer-list 'agenda))
  16689. (t (org-buffer-list))))
  16690. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16691. (org-completion-use-ido org-completion-use-ido))
  16692. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16693. (setq org-completion-use-iswitchb t))
  16694. (org-pop-to-buffer-same-window
  16695. (org-icompleting-read "Org buffer: "
  16696. (mapcar 'list (mapcar 'buffer-name blist))
  16697. nil t))))
  16698. ;;; Define some older names previously used for this functionality
  16699. ;;;###autoload
  16700. (defalias 'org-ido-switchb 'org-switchb)
  16701. ;;;###autoload
  16702. (defalias 'org-iswitchb 'org-switchb)
  16703. (defun org-buffer-list (&optional predicate exclude-tmp)
  16704. "Return a list of Org buffers.
  16705. PREDICATE can be `export', `files' or `agenda'.
  16706. export restrict the list to Export buffers.
  16707. files restrict the list to buffers visiting Org files.
  16708. agenda restrict the list to buffers visiting agenda files.
  16709. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16710. (let* ((bfn nil)
  16711. (agenda-files (and (eq predicate 'agenda)
  16712. (mapcar 'file-truename (org-agenda-files t))))
  16713. (filter
  16714. (cond
  16715. ((eq predicate 'files)
  16716. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16717. ((eq predicate 'export)
  16718. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16719. ((eq predicate 'agenda)
  16720. (lambda (b)
  16721. (with-current-buffer b
  16722. (and (derived-mode-p 'org-mode)
  16723. (setq bfn (buffer-file-name b))
  16724. (member (file-truename bfn) agenda-files)))))
  16725. (t (lambda (b) (with-current-buffer b
  16726. (or (derived-mode-p 'org-mode)
  16727. (string-match "\*Org .*Export"
  16728. (buffer-name b)))))))))
  16729. (delq nil
  16730. (mapcar
  16731. (lambda(b)
  16732. (if (and (funcall filter b)
  16733. (or (not exclude-tmp)
  16734. (not (string-match "tmp" (buffer-name b)))))
  16735. b
  16736. nil))
  16737. (buffer-list)))))
  16738. (defun org-agenda-files (&optional unrestricted archives)
  16739. "Get the list of agenda files.
  16740. Optional UNRESTRICTED means return the full list even if a restriction
  16741. is currently in place.
  16742. When ARCHIVES is t, include all archive files that are really being
  16743. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16744. `org-agenda-archives-mode' is t."
  16745. (let ((files
  16746. (cond
  16747. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16748. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16749. ((listp org-agenda-files) org-agenda-files)
  16750. (t (error "Invalid value of `org-agenda-files'")))))
  16751. (setq files (apply 'append
  16752. (mapcar (lambda (f)
  16753. (if (file-directory-p f)
  16754. (directory-files
  16755. f t org-agenda-file-regexp)
  16756. (list f)))
  16757. files)))
  16758. (when org-agenda-skip-unavailable-files
  16759. (setq files (delq nil
  16760. (mapcar (function
  16761. (lambda (file)
  16762. (and (file-readable-p file) file)))
  16763. files))))
  16764. (when (or (eq archives t)
  16765. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16766. (setq files (org-add-archive-files files)))
  16767. files))
  16768. (defun org-agenda-file-p (&optional file)
  16769. "Return non-nil, if FILE is an agenda file.
  16770. If FILE is omitted, use the file associated with the current
  16771. buffer."
  16772. (let ((fname (or file (buffer-file-name))))
  16773. (and fname
  16774. (member (file-truename fname)
  16775. (mapcar #'file-truename (org-agenda-files t))))))
  16776. (defun org-edit-agenda-file-list ()
  16777. "Edit the list of agenda files.
  16778. Depending on setup, this either uses customize to edit the variable
  16779. `org-agenda-files', or it visits the file that is holding the list. In the
  16780. latter case, the buffer is set up in a way that saving it automatically kills
  16781. the buffer and restores the previous window configuration."
  16782. (interactive)
  16783. (if (stringp org-agenda-files)
  16784. (let ((cw (current-window-configuration)))
  16785. (find-file org-agenda-files)
  16786. (org-set-local 'org-window-configuration cw)
  16787. (org-add-hook 'after-save-hook
  16788. (lambda ()
  16789. (set-window-configuration
  16790. (prog1 org-window-configuration
  16791. (kill-buffer (current-buffer))))
  16792. (org-install-agenda-files-menu)
  16793. (message "New agenda file list installed"))
  16794. nil 'local)
  16795. (message "%s" (substitute-command-keys
  16796. "Edit list and finish with \\[save-buffer]")))
  16797. (customize-variable 'org-agenda-files)))
  16798. (defun org-store-new-agenda-file-list (list)
  16799. "Set new value for the agenda file list and save it correctly."
  16800. (if (stringp org-agenda-files)
  16801. (let ((fe (org-read-agenda-file-list t)) b u)
  16802. (while (setq b (find-buffer-visiting org-agenda-files))
  16803. (kill-buffer b))
  16804. (with-temp-file org-agenda-files
  16805. (insert
  16806. (mapconcat
  16807. (lambda (f) ;; Keep un-expanded entries.
  16808. (if (setq u (assoc f fe))
  16809. (cdr u)
  16810. f))
  16811. list "\n")
  16812. "\n")))
  16813. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16814. (org-insert-mode-line-in-empty-file nil))
  16815. (setq org-agenda-files list)
  16816. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16817. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16818. "Read the list of agenda files from a file.
  16819. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16820. filenames, used by `org-store-new-agenda-file-list' to write back
  16821. un-expanded file names."
  16822. (when (file-directory-p org-agenda-files)
  16823. (error "`org-agenda-files' cannot be a single directory"))
  16824. (when (stringp org-agenda-files)
  16825. (with-temp-buffer
  16826. (insert-file-contents org-agenda-files)
  16827. (mapcar
  16828. (lambda (f)
  16829. (let ((e (expand-file-name (substitute-in-file-name f)
  16830. org-directory)))
  16831. (if pair-with-expansion
  16832. (cons e f)
  16833. e)))
  16834. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16835. ;;;###autoload
  16836. (defun org-cycle-agenda-files ()
  16837. "Cycle through the files in `org-agenda-files'.
  16838. If the current buffer visits an agenda file, find the next one in the list.
  16839. If the current buffer does not, find the first agenda file."
  16840. (interactive)
  16841. (let* ((fs (org-agenda-files t))
  16842. (files (append fs (list (car fs))))
  16843. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16844. file)
  16845. (unless files (user-error "No agenda files"))
  16846. (catch 'exit
  16847. (while (setq file (pop files))
  16848. (if (equal (file-truename file) tcf)
  16849. (when (car files)
  16850. (find-file (car files))
  16851. (throw 'exit t))))
  16852. (find-file (car fs)))
  16853. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16854. (defun org-agenda-file-to-front (&optional to-end)
  16855. "Move/add the current file to the top of the agenda file list.
  16856. If the file is not present in the list, it is added to the front. If it is
  16857. present, it is moved there. With optional argument TO-END, add/move to the
  16858. end of the list."
  16859. (interactive "P")
  16860. (let ((org-agenda-skip-unavailable-files nil)
  16861. (file-alist (mapcar (lambda (x)
  16862. (cons (file-truename x) x))
  16863. (org-agenda-files t)))
  16864. (ctf (file-truename
  16865. (or buffer-file-name
  16866. (user-error "Please save the current buffer to a file"))))
  16867. x had)
  16868. (setq x (assoc ctf file-alist) had x)
  16869. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16870. (if to-end
  16871. (setq file-alist (append (delq x file-alist) (list x)))
  16872. (setq file-alist (cons x (delq x file-alist))))
  16873. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16874. (org-install-agenda-files-menu)
  16875. (message "File %s to %s of agenda file list"
  16876. (if had "moved" "added") (if to-end "end" "front"))))
  16877. (defun org-remove-file (&optional file)
  16878. "Remove current file from the list of files in variable `org-agenda-files'.
  16879. These are the files which are being checked for agenda entries.
  16880. Optional argument FILE means use this file instead of the current."
  16881. (interactive)
  16882. (let* ((org-agenda-skip-unavailable-files nil)
  16883. (file (or file buffer-file-name
  16884. (user-error "Current buffer does not visit a file")))
  16885. (true-file (file-truename file))
  16886. (afile (abbreviate-file-name file))
  16887. (files (delq nil (mapcar
  16888. (lambda (x)
  16889. (if (equal true-file
  16890. (file-truename x))
  16891. nil x))
  16892. (org-agenda-files t)))))
  16893. (if (not (= (length files) (length (org-agenda-files t))))
  16894. (progn
  16895. (org-store-new-agenda-file-list files)
  16896. (org-install-agenda-files-menu)
  16897. (message "Removed from Org Agenda list: %s" afile))
  16898. (message "File was not in list: %s (not removed)" afile))))
  16899. (defun org-file-menu-entry (file)
  16900. (vector file (list 'find-file file) t))
  16901. (defun org-check-agenda-file (file)
  16902. "Make sure FILE exists. If not, ask user what to do."
  16903. (when (not (file-exists-p file))
  16904. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16905. (abbreviate-file-name file))
  16906. (let ((r (downcase (read-char-exclusive))))
  16907. (cond
  16908. ((equal r ?r)
  16909. (org-remove-file file)
  16910. (throw 'nextfile t))
  16911. (t (user-error "Abort"))))))
  16912. (defun org-get-agenda-file-buffer (file)
  16913. "Get an agenda buffer visiting FILE.
  16914. If the buffer needs to be created, add it to the list of buffers
  16915. which might be released later."
  16916. (let ((buf (org-find-base-buffer-visiting file)))
  16917. (if buf
  16918. buf ; just return it
  16919. ;; Make a new buffer and remember it
  16920. (setq buf (find-file-noselect file))
  16921. (if buf (push buf org-agenda-new-buffers))
  16922. buf)))
  16923. (defun org-release-buffers (blist)
  16924. "Release all buffers in list, asking the user for confirmation when needed.
  16925. When a buffer is unmodified, it is just killed. When modified, it is saved
  16926. \(if the user agrees) and then killed."
  16927. (let (buf file)
  16928. (while (setq buf (pop blist))
  16929. (setq file (buffer-file-name buf))
  16930. (when (and (buffer-modified-p buf)
  16931. file
  16932. (y-or-n-p (format "Save file %s? " file)))
  16933. (with-current-buffer buf (save-buffer)))
  16934. (kill-buffer buf))))
  16935. (defun org-agenda-prepare-buffers (files)
  16936. "Create buffers for all agenda files, protect archived trees and comments."
  16937. (interactive)
  16938. (let ((pa '(:org-archived t))
  16939. (pc '(:org-comment t))
  16940. (pall '(:org-archived t :org-comment t))
  16941. (inhibit-read-only t)
  16942. (org-inhibit-startup org-agenda-inhibit-startup)
  16943. (rea (concat ":" org-archive-tag ":"))
  16944. file re pos)
  16945. (setq org-tag-alist-for-agenda nil
  16946. org-tag-groups-alist-for-agenda nil)
  16947. (save-excursion
  16948. (save-restriction
  16949. (while (setq file (pop files))
  16950. (catch 'nextfile
  16951. (if (bufferp file)
  16952. (set-buffer file)
  16953. (org-check-agenda-file file)
  16954. (set-buffer (org-get-agenda-file-buffer file)))
  16955. (widen)
  16956. (org-set-regexps-and-options 'tags-only)
  16957. (setq pos (point))
  16958. (or (memq 'category org-agenda-ignore-properties)
  16959. (org-refresh-category-properties))
  16960. (or (memq 'stats org-agenda-ignore-properties)
  16961. (org-refresh-stats-properties))
  16962. (or (memq 'effort org-agenda-ignore-properties)
  16963. (org-refresh-effort-properties))
  16964. (or (memq 'appt org-agenda-ignore-properties)
  16965. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16966. (setq org-todo-keywords-for-agenda
  16967. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16968. (setq org-done-keywords-for-agenda
  16969. (append org-done-keywords-for-agenda org-done-keywords))
  16970. (setq org-todo-keyword-alist-for-agenda
  16971. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16972. (setq org-tag-alist-for-agenda
  16973. (org-uniquify
  16974. (append org-tag-alist-for-agenda
  16975. org-tag-alist
  16976. org-tag-persistent-alist)))
  16977. (if org-group-tags
  16978. (setq org-tag-groups-alist-for-agenda
  16979. (org-uniquify-alist
  16980. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16981. (org-with-silent-modifications
  16982. (save-excursion
  16983. (remove-text-properties (point-min) (point-max) pall)
  16984. (when org-agenda-skip-archived-trees
  16985. (goto-char (point-min))
  16986. (while (re-search-forward rea nil t)
  16987. (if (org-at-heading-p t)
  16988. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16989. (goto-char (point-min))
  16990. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  16991. (while (re-search-forward re nil t)
  16992. (when (save-match-data (org-in-commented-heading-p t))
  16993. (add-text-properties
  16994. (match-beginning 0) (org-end-of-subtree t) pc)))))
  16995. (goto-char pos)))))
  16996. (setq org-todo-keywords-for-agenda
  16997. (org-uniquify org-todo-keywords-for-agenda))
  16998. (setq org-todo-keyword-alist-for-agenda
  16999. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17000. ;;;; CDLaTeX minor mode
  17001. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17002. "Keymap for the minor `org-cdlatex-mode'.")
  17003. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17004. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17005. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17006. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17007. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17008. (defvar org-cdlatex-texmathp-advice-is-done nil
  17009. "Flag remembering if we have applied the advice to texmathp already.")
  17010. (define-minor-mode org-cdlatex-mode
  17011. "Toggle the minor `org-cdlatex-mode'.
  17012. This mode supports entering LaTeX environment and math in LaTeX fragments
  17013. in Org-mode.
  17014. \\{org-cdlatex-mode-map}"
  17015. nil " OCDL" nil
  17016. (when org-cdlatex-mode
  17017. (require 'cdlatex)
  17018. (run-hooks 'cdlatex-mode-hook)
  17019. (cdlatex-compute-tables))
  17020. (unless org-cdlatex-texmathp-advice-is-done
  17021. (setq org-cdlatex-texmathp-advice-is-done t)
  17022. (defadvice texmathp (around org-math-always-on activate)
  17023. "Always return t in org-mode buffers.
  17024. This is because we want to insert math symbols without dollars even outside
  17025. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17026. an embedded LaTeX fragment, let texmathp do its job.
  17027. \\[org-cdlatex-mode-map]"
  17028. (interactive)
  17029. (let (p)
  17030. (cond
  17031. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17032. ((eq this-command 'cdlatex-math-symbol)
  17033. (setq ad-return-value t
  17034. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17035. (t
  17036. (let ((p (org-inside-LaTeX-fragment-p)))
  17037. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17038. (setq ad-return-value t
  17039. texmathp-why '("Org-mode embedded math" . 0))
  17040. (if p ad-do-it)))))))))
  17041. (defun turn-on-org-cdlatex ()
  17042. "Unconditionally turn on `org-cdlatex-mode'."
  17043. (org-cdlatex-mode 1))
  17044. (defun org-try-cdlatex-tab ()
  17045. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17046. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17047. - inside a LaTeX fragment, or
  17048. - after the first word in a line, where an abbreviation expansion could
  17049. insert a LaTeX environment."
  17050. (when org-cdlatex-mode
  17051. (cond
  17052. ;; Before any word on the line: No expansion possible.
  17053. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17054. ;; Just after first word on the line: Expand it. Make sure it
  17055. ;; cannot happen on headlines, though.
  17056. ((save-excursion
  17057. (skip-chars-backward "a-zA-Z0-9*")
  17058. (skip-chars-backward " \t")
  17059. (and (bolp) (not (org-at-heading-p))))
  17060. (cdlatex-tab) t)
  17061. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17062. (defun org-cdlatex-underscore-caret (&optional arg)
  17063. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17064. Revert to the normal definition outside of these fragments."
  17065. (interactive "P")
  17066. (if (org-inside-LaTeX-fragment-p)
  17067. (call-interactively 'cdlatex-sub-superscript)
  17068. (let (org-cdlatex-mode)
  17069. (call-interactively (key-binding (vector last-input-event))))))
  17070. (defun org-cdlatex-math-modify (&optional arg)
  17071. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17072. Revert to the normal definition outside of these fragments."
  17073. (interactive "P")
  17074. (if (org-inside-LaTeX-fragment-p)
  17075. (call-interactively 'cdlatex-math-modify)
  17076. (let (org-cdlatex-mode)
  17077. (call-interactively (key-binding (vector last-input-event))))))
  17078. (defun org-cdlatex-environment-indent (&optional environment item)
  17079. "Execute `cdlatex-environment' and indent the inserted environment."
  17080. (interactive)
  17081. (cdlatex-environment environment item)
  17082. (let ((element (org-element-at-point)))
  17083. (org-indent-region (org-element-property :begin element)
  17084. (org-element-property :end element))))
  17085. ;;;; LaTeX fragments
  17086. (defun org-inside-LaTeX-fragment-p ()
  17087. "Test if point is inside a LaTeX fragment.
  17088. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17089. sequence appearing also before point.
  17090. Even though the matchers for math are configurable, this function assumes
  17091. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17092. delimiters are skipped when they have been removed by customization.
  17093. The return value is nil, or a cons cell with the delimiter and the
  17094. position of this delimiter.
  17095. This function does a reasonably good job, but can locally be fooled by
  17096. for example currency specifications. For example it will assume being in
  17097. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17098. fragments that are properly closed, but during editing, we have to live
  17099. with the uncertainty caused by missing closing delimiters. This function
  17100. looks only before point, not after."
  17101. (catch 'exit
  17102. (let ((pos (point))
  17103. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17104. (lim (progn
  17105. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17106. (point)))
  17107. dd-on str (start 0) m re)
  17108. (goto-char pos)
  17109. (when dodollar
  17110. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17111. re (nth 1 (assoc "$" org-latex-regexps)))
  17112. (while (string-match re str start)
  17113. (cond
  17114. ((= (match-end 0) (length str))
  17115. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17116. ((= (match-end 0) (- (length str) 5))
  17117. (throw 'exit nil))
  17118. (t (setq start (match-end 0))))))
  17119. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17120. (goto-char pos)
  17121. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17122. (and (match-beginning 2) (throw 'exit nil))
  17123. ;; count $$
  17124. (while (re-search-backward "\\$\\$" lim t)
  17125. (setq dd-on (not dd-on)))
  17126. (goto-char pos)
  17127. (if dd-on (cons "$$" m))))))
  17128. (defun org-inside-latex-macro-p ()
  17129. "Is point inside a LaTeX macro or its arguments?"
  17130. (save-match-data
  17131. (org-in-regexp
  17132. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17133. (defvar org-latex-fragment-image-overlays nil
  17134. "List of overlays carrying the images of latex fragments.")
  17135. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17136. (defun org-remove-latex-fragment-image-overlays ()
  17137. "Remove all overlays with LaTeX fragment images in current buffer."
  17138. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  17139. (setq org-latex-fragment-image-overlays nil))
  17140. (define-obsolete-function-alias
  17141. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17142. (defun org-toggle-latex-fragment (&optional subtree)
  17143. "Preview the LaTeX fragment at point, or all locally or globally.
  17144. If the cursor is in a LaTeX fragment, create the image and overlay
  17145. it over the source code. If there is no fragment at point, display
  17146. all fragments in the current text, from one headline to the next. With
  17147. prefix SUBTREE, display all fragments in the current subtree. With a
  17148. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  17149. the cursor is before the first headline,
  17150. display all fragments in the buffer.
  17151. The images can be removed again with \\[org-toggle-latex-fragment]."
  17152. (interactive "P")
  17153. (unless (buffer-file-name (buffer-base-buffer))
  17154. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17155. (if org-latex-fragment-image-overlays
  17156. (progn (org-remove-latex-fragment-image-overlays)
  17157. (message "LaTeX fragments images removed"))
  17158. (when (display-graphic-p)
  17159. (org-remove-latex-fragment-image-overlays)
  17160. (org-with-wide-buffer
  17161. (let (beg end msg)
  17162. (cond
  17163. ((equal subtree '(16))
  17164. (setq beg (point-min) end (point-max)
  17165. msg "Creating images for buffer...%s"))
  17166. ((equal subtree '(4))
  17167. (org-back-to-heading)
  17168. (setq beg (point) end (org-end-of-subtree t)
  17169. msg "Creating images for subtree...%s"))
  17170. ((let ((context (org-element-context)))
  17171. (when (memq (org-element-type context)
  17172. '(latex-environment latex-fragment))
  17173. (setq beg (org-element-property :begin context)
  17174. end (org-element-property :end context)
  17175. msg "Creating image...%s"))))
  17176. ((org-before-first-heading-p)
  17177. (setq beg (point-min) end (point-max)
  17178. msg "Creating images for buffer...%s"))
  17179. (t
  17180. (org-back-to-heading)
  17181. (setq beg (point) end (progn (outline-next-heading) (point))
  17182. msg "Creating images for entry...%s")))
  17183. (message msg "")
  17184. (narrow-to-region beg end)
  17185. (goto-char beg)
  17186. (org-format-latex
  17187. (concat org-latex-preview-ltxpng-directory
  17188. (file-name-sans-extension
  17189. (file-name-nondirectory
  17190. (buffer-file-name (buffer-base-buffer)))))
  17191. default-directory 'overlays msg 'forbuffer
  17192. org-latex-create-formula-image-program)
  17193. (message msg "done. Use `C-c C-x C-l' to remove images."))))))
  17194. (defun org-format-latex
  17195. (prefix &optional dir overlays msg forbuffer processing-type)
  17196. "Replace LaTeX fragments with links to an image, and produce images.
  17197. Some of the options can be changed using the variable
  17198. `org-format-latex-options'."
  17199. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17200. (unless (eq processing-type 'verbatim)
  17201. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17202. (cnt 0)
  17203. checkdir-flag)
  17204. (goto-char (point-min))
  17205. (while (re-search-forward math-regexp nil t)
  17206. (unless (and overlays
  17207. (eq (get-char-property (point) 'org-overlay-type)
  17208. 'org-latex-overlay))
  17209. (let* ((context (org-element-context))
  17210. (type (org-element-type context)))
  17211. (when (memq type '(latex-environment latex-fragment))
  17212. (let ((block-type (eq type 'latex-environment))
  17213. (value (org-element-property :value context))
  17214. (beg (org-element-property :begin context))
  17215. (end (save-excursion
  17216. (goto-char (org-element-property :end context))
  17217. (skip-chars-backward " \r\t\n")
  17218. (point))))
  17219. (case processing-type
  17220. (mathjax
  17221. ;; Prepare for MathJax processing.
  17222. (if (eq (char-after beg) ?$)
  17223. (save-excursion
  17224. (delete-region beg end)
  17225. (insert "\\(" (substring value 1 -1) "\\)"))
  17226. (goto-char end)))
  17227. ((dvipng imagemagick)
  17228. ;; Process to an image.
  17229. (incf cnt)
  17230. (goto-char beg)
  17231. (let* ((face (face-at-point))
  17232. ;; Get the colors from the face at point.
  17233. (fg
  17234. (let ((color (plist-get org-format-latex-options
  17235. :foreground)))
  17236. (if (and forbuffer (eq color 'auto))
  17237. (face-attribute face :foreground nil 'default)
  17238. color)))
  17239. (bg
  17240. (let ((color (plist-get org-format-latex-options
  17241. :background)))
  17242. (if (and forbuffer (eq color 'auto))
  17243. (face-attribute face :background nil 'default)
  17244. color)))
  17245. (hash (sha1 (prin1-to-string
  17246. (list org-format-latex-header
  17247. org-latex-default-packages-alist
  17248. org-latex-packages-alist
  17249. org-format-latex-options
  17250. forbuffer value fg bg))))
  17251. (absprefix (expand-file-name prefix dir))
  17252. (linkfile (format "%s_%s.png" prefix hash))
  17253. (movefile (format "%s_%s.png" absprefix hash))
  17254. (sep (and block-type "\n\n"))
  17255. (link (concat sep "[[file:" linkfile "]]" sep))
  17256. (options
  17257. (org-combine-plists
  17258. org-format-latex-options
  17259. `(:foreground ,fg :background ,bg))))
  17260. (when msg (message msg cnt))
  17261. (unless checkdir-flag ; Ensure the directory exists.
  17262. (setq checkdir-flag t)
  17263. (let ((todir (file-name-directory absprefix)))
  17264. (unless (file-directory-p todir)
  17265. (make-directory todir t))))
  17266. (unless (file-exists-p movefile)
  17267. (org-create-formula-image
  17268. value movefile options forbuffer processing-type))
  17269. (if overlays
  17270. (progn
  17271. (dolist (o (overlays-in beg end))
  17272. (when (eq (overlay-get o 'org-overlay-type)
  17273. 'org-latex-overlay)
  17274. (delete-overlay o)))
  17275. (let ((ov (make-overlay beg end)))
  17276. (overlay-put ov
  17277. 'org-overlay-type
  17278. 'org-latex-overlay)
  17279. (if (featurep 'xemacs)
  17280. (progn
  17281. (overlay-put ov 'invisible t)
  17282. (overlay-put
  17283. ov 'end-glyph
  17284. (make-glyph
  17285. (vector 'png :file movefile))))
  17286. (overlay-put
  17287. ov 'display
  17288. (list 'image
  17289. :type 'png
  17290. :file movefile
  17291. :ascent 'center)))
  17292. (push ov org-latex-fragment-image-overlays))
  17293. (goto-char end))
  17294. (delete-region beg end)
  17295. (insert
  17296. (org-add-props link
  17297. (list 'org-latex-src
  17298. (replace-regexp-in-string "\"" "" value)
  17299. 'org-latex-src-embed-type
  17300. (if block-type 'paragraph 'character)))))))
  17301. (mathml
  17302. ;; Process to MathML.
  17303. (unless (org-format-latex-mathml-available-p)
  17304. (user-error "LaTeX to MathML converter not configured"))
  17305. (incf cnt)
  17306. (when msg (message msg cnt))
  17307. (goto-char beg)
  17308. (delete-region beg end)
  17309. (insert (org-format-latex-as-mathml
  17310. value block-type prefix dir)))
  17311. (otherwise
  17312. (error "Unknown conversion type %s for LaTeX fragments"
  17313. processing-type)))))))))))
  17314. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17315. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17316. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17317. sucessful, return the portion between \"<math...> </math>\"
  17318. elements otherwise return nil. When MATHML-FILE is specified,
  17319. write the results in to that file. When invoked as an
  17320. interactive command, prompt for LATEX-FRAG, with initial value
  17321. set to the current active region and echo the results for user
  17322. inspection."
  17323. (interactive (list (let ((frag (when (org-region-active-p)
  17324. (buffer-substring-no-properties
  17325. (region-beginning) (region-end)))))
  17326. (read-string "LaTeX Fragment: " frag nil frag))))
  17327. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17328. (let* ((tmp-in-file (file-relative-name
  17329. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17330. (ignore (write-region latex-frag nil tmp-in-file))
  17331. (tmp-out-file (file-relative-name
  17332. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17333. (cmd (format-spec
  17334. org-latex-to-mathml-convert-command
  17335. `((?j . ,(shell-quote-argument
  17336. (expand-file-name org-latex-to-mathml-jar-file)))
  17337. (?I . ,(shell-quote-argument tmp-in-file))
  17338. (?o . ,(shell-quote-argument tmp-out-file)))))
  17339. mathml shell-command-output)
  17340. (when (org-called-interactively-p 'any)
  17341. (unless (org-format-latex-mathml-available-p)
  17342. (user-error "LaTeX to MathML converter not configured")))
  17343. (message "Running %s" cmd)
  17344. (setq shell-command-output (shell-command-to-string cmd))
  17345. (setq mathml
  17346. (when (file-readable-p tmp-out-file)
  17347. (with-current-buffer (find-file-noselect tmp-out-file t)
  17348. (goto-char (point-min))
  17349. (when (re-search-forward
  17350. (concat
  17351. (regexp-quote
  17352. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17353. "\\(.\\|\n\\)*"
  17354. (regexp-quote "</math>")) nil t)
  17355. (prog1 (match-string 0) (kill-buffer))))))
  17356. (cond
  17357. (mathml
  17358. (setq mathml
  17359. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17360. (when mathml-file
  17361. (write-region mathml nil mathml-file))
  17362. (when (org-called-interactively-p 'any)
  17363. (message mathml)))
  17364. ((message "LaTeX to MathML conversion failed")
  17365. (message shell-command-output)))
  17366. (delete-file tmp-in-file)
  17367. (when (file-exists-p tmp-out-file)
  17368. (delete-file tmp-out-file))
  17369. mathml))
  17370. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17371. prefix &optional dir)
  17372. "Use `org-create-math-formula' but check local cache first."
  17373. (let* ((absprefix (expand-file-name prefix dir))
  17374. (print-length nil) (print-level nil)
  17375. (formula-id (concat
  17376. "formula-"
  17377. (sha1
  17378. (prin1-to-string
  17379. (list latex-frag
  17380. org-latex-to-mathml-convert-command)))))
  17381. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17382. (formula-cache-dir (file-name-directory formula-cache)))
  17383. (unless (file-directory-p formula-cache-dir)
  17384. (make-directory formula-cache-dir t))
  17385. (unless (file-exists-p formula-cache)
  17386. (org-create-math-formula latex-frag formula-cache))
  17387. (if (file-exists-p formula-cache)
  17388. ;; Successful conversion. Return the link to MathML file.
  17389. (org-add-props
  17390. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17391. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17392. 'org-latex-src-embed-type (if latex-frag-type
  17393. 'paragraph 'character)))
  17394. ;; Failed conversion. Return the LaTeX fragment verbatim
  17395. latex-frag)))
  17396. (defun org-create-formula-image (string tofile options buffer &optional type)
  17397. "Create an image from LaTeX source using dvipng or convert.
  17398. This function calls either `org-create-formula-image-with-dvipng'
  17399. or `org-create-formula-image-with-imagemagick' depending on the
  17400. value of `org-latex-create-formula-image-program' or on the value
  17401. of the optional TYPE variable.
  17402. Note: ultimately these two function should be combined as they
  17403. share a good deal of logic."
  17404. (org-check-external-command
  17405. "latex" "needed to convert LaTeX fragments to images")
  17406. (funcall
  17407. (case (or type org-latex-create-formula-image-program)
  17408. ('dvipng
  17409. (org-check-external-command
  17410. "dvipng" "needed to convert LaTeX fragments to images")
  17411. 'org-create-formula-image-with-dvipng)
  17412. ('imagemagick
  17413. (org-check-external-command
  17414. "convert" "you need to install imagemagick")
  17415. 'org-create-formula-image-with-imagemagick)
  17416. (t (error
  17417. "Invalid value of `org-latex-create-formula-image-program'")))
  17418. string tofile options buffer))
  17419. (declare-function org-export-get-backend "ox" (name))
  17420. (declare-function org-export--get-global-options "ox" (&optional backend))
  17421. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17422. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17423. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17424. (defun org-create-formula--latex-header ()
  17425. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17426. (let ((info (org-combine-plists (org-export--get-global-options
  17427. (org-export-get-backend 'latex))
  17428. (org-export--get-inbuffer-options
  17429. (org-export-get-backend 'latex)))))
  17430. (org-latex-guess-babel-language
  17431. (org-latex-guess-inputenc
  17432. (org-splice-latex-header
  17433. org-format-latex-header
  17434. org-latex-default-packages-alist
  17435. org-latex-packages-alist t
  17436. (plist-get info :latex-header)))
  17437. info)))
  17438. (defun org--get-display-dpi ()
  17439. "Get the DPI of the display.
  17440. Assumes that the display has the same pixel width in the
  17441. horizontal and vertical directions."
  17442. (if (display-graphic-p)
  17443. (round (/ (display-pixel-height)
  17444. (/ (display-mm-height) 25.4)))
  17445. (error "Attempt to calculate the dpi of a non-graphic display")))
  17446. ;; This function borrows from Ganesh Swami's latex2png.el
  17447. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17448. "This calls dvipng."
  17449. (require 'ox-latex)
  17450. (let* ((tmpdir (if (featurep 'xemacs)
  17451. (temp-directory)
  17452. temporary-file-directory))
  17453. (texfilebase (make-temp-name
  17454. (expand-file-name "orgtex" tmpdir)))
  17455. (texfile (concat texfilebase ".tex"))
  17456. (dvifile (concat texfilebase ".dvi"))
  17457. (pngfile (concat texfilebase ".png"))
  17458. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17459. ;; This assumes that the display has the same pixel width in
  17460. ;; the horizontal and vertical directions
  17461. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17462. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17463. "Black"))
  17464. (bg (or (plist-get options (if buffer :background :html-background))
  17465. "Transparent")))
  17466. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17467. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17468. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17469. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17470. (let ((latex-header (org-create-formula--latex-header)))
  17471. (with-temp-file texfile
  17472. (insert latex-header)
  17473. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17474. (let ((dir default-directory))
  17475. (ignore-errors
  17476. (cd tmpdir)
  17477. (call-process "latex" nil nil nil texfile))
  17478. (cd dir))
  17479. (if (not (file-exists-p dvifile))
  17480. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17481. (ignore-errors
  17482. (if (featurep 'xemacs)
  17483. (call-process "dvipng" nil nil nil
  17484. "-fg" fg "-bg" bg
  17485. "-T" "tight"
  17486. "-o" pngfile
  17487. dvifile)
  17488. (call-process "dvipng" nil nil nil
  17489. "-fg" fg "-bg" bg
  17490. "-D" dpi
  17491. ;;"-x" scale "-y" scale
  17492. "-T" "tight"
  17493. "-o" pngfile
  17494. dvifile)))
  17495. (if (not (file-exists-p pngfile))
  17496. (if org-format-latex-signal-error
  17497. (error "Failed to create png file from %s" texfile)
  17498. (message "Failed to create png file from %s" texfile)
  17499. nil)
  17500. ;; Use the requested file name and clean up
  17501. (copy-file pngfile tofile 'replace)
  17502. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17503. (if (file-exists-p (concat texfilebase e))
  17504. (delete-file (concat texfilebase e))))
  17505. pngfile))))
  17506. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17507. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17508. "This calls convert, which is included into imagemagick."
  17509. (require 'ox-latex)
  17510. (let* ((tmpdir (if (featurep 'xemacs)
  17511. (temp-directory)
  17512. temporary-file-directory))
  17513. (texfilebase (make-temp-name
  17514. (expand-file-name "orgtex" tmpdir)))
  17515. (texfile (concat texfilebase ".tex"))
  17516. (pdffile (concat texfilebase ".pdf"))
  17517. (pngfile (concat texfilebase ".png"))
  17518. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17519. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17520. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17521. "black"))
  17522. (bg (or (plist-get options (if buffer :background :html-background))
  17523. "white")))
  17524. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17525. (setq fg (org-latex-color-format fg)))
  17526. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17527. (setq bg (org-latex-color-format
  17528. (if (string= bg "Transparent") "white" bg))))
  17529. (let ((latex-header (org-create-formula--latex-header)))
  17530. (with-temp-file texfile
  17531. (insert latex-header)
  17532. (insert "\n\\begin{document}\n"
  17533. "\\definecolor{fg}{rgb}{" fg "}\n"
  17534. "\\definecolor{bg}{rgb}{" bg "}\n"
  17535. "\n\\pagecolor{bg}\n"
  17536. "\n{\\color{fg}\n"
  17537. string
  17538. "\n}\n"
  17539. "\n\\end{document}\n")))
  17540. (org-latex-compile texfile t)
  17541. (if (not (file-exists-p pdffile))
  17542. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17543. (ignore-errors
  17544. (if (featurep 'xemacs)
  17545. (call-process "convert" nil nil nil
  17546. "-density" "96"
  17547. "-trim"
  17548. "-antialias"
  17549. pdffile
  17550. "-quality" "100"
  17551. ;; "-sharpen" "0x1.0"
  17552. pngfile)
  17553. (call-process "convert" nil nil nil
  17554. "-density" dpi
  17555. "-trim"
  17556. "-antialias"
  17557. pdffile
  17558. "-quality" "100"
  17559. ;; "-sharpen" "0x1.0"
  17560. pngfile)))
  17561. (if (not (file-exists-p pngfile))
  17562. (if org-format-latex-signal-error
  17563. (error "Failed to create png file from %s" texfile)
  17564. (message "Failed to create png file from %s" texfile)
  17565. nil)
  17566. ;; Use the requested file name and clean up
  17567. (copy-file pngfile tofile 'replace)
  17568. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17569. (if (file-exists-p (concat texfilebase e))
  17570. (delete-file (concat texfilebase e))))
  17571. pngfile))))
  17572. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17573. "Fill a LaTeX header template TPL.
  17574. In the template, the following place holders will be recognized:
  17575. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17576. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17577. [PACKAGES] \\usepackage statements for PKG
  17578. [NO-PACKAGES] do not include PKG
  17579. [EXTRA] the string EXTRA
  17580. [NO-EXTRA] do not include EXTRA
  17581. For backward compatibility, if both the positive and the negative place
  17582. holder is missing, the positive one (without the \"NO-\") will be
  17583. assumed to be present at the end of the template.
  17584. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17585. EXTRA is a string.
  17586. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17587. (let (rpl (end ""))
  17588. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17589. (setq rpl (if (or (match-end 1) (not def-pkg))
  17590. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17591. tpl (replace-match rpl t t tpl))
  17592. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17593. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17594. (setq rpl (if (or (match-end 1) (not pkg))
  17595. "" (org-latex-packages-to-string pkg snippets-p t))
  17596. tpl (replace-match rpl t t tpl))
  17597. (if pkg (setq end
  17598. (concat end "\n"
  17599. (org-latex-packages-to-string pkg snippets-p)))))
  17600. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17601. (setq rpl (if (or (match-end 1) (not extra))
  17602. "" (concat extra "\n"))
  17603. tpl (replace-match rpl t t tpl))
  17604. (if (and extra (string-match "\\S-" extra))
  17605. (setq end (concat end "\n" extra))))
  17606. (if (string-match "\\S-" end)
  17607. (concat tpl "\n" end)
  17608. tpl)))
  17609. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17610. "Turn an alist of packages into a string with the \\usepackage macros."
  17611. (setq pkg (mapconcat (lambda(p)
  17612. (cond
  17613. ((stringp p) p)
  17614. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17615. (format "%% Package %s omitted" (cadr p)))
  17616. ((equal "" (car p))
  17617. (format "\\usepackage{%s}" (cadr p)))
  17618. (t
  17619. (format "\\usepackage[%s]{%s}"
  17620. (car p) (cadr p)))))
  17621. pkg
  17622. "\n"))
  17623. (if newline (concat pkg "\n") pkg))
  17624. (defun org-dvipng-color (attr)
  17625. "Return a RGB color specification for dvipng."
  17626. (apply 'format "rgb %s %s %s"
  17627. (mapcar 'org-normalize-color
  17628. (if (featurep 'xemacs)
  17629. (color-rgb-components
  17630. (face-property 'default
  17631. (cond ((eq attr :foreground) 'foreground)
  17632. ((eq attr :background) 'background))))
  17633. (color-values (face-attribute 'default attr nil))))))
  17634. (defun org-dvipng-color-format (color-name)
  17635. "Convert COLOR-NAME to a RGB color value for dvipng."
  17636. (apply 'format "rgb %s %s %s"
  17637. (mapcar 'org-normalize-color
  17638. (color-values color-name))))
  17639. (defun org-latex-color (attr)
  17640. "Return a RGB color for the LaTeX color package."
  17641. (apply 'format "%s,%s,%s"
  17642. (mapcar 'org-normalize-color
  17643. (if (featurep 'xemacs)
  17644. (color-rgb-components
  17645. (face-property 'default
  17646. (cond ((eq attr :foreground) 'foreground)
  17647. ((eq attr :background) 'background))))
  17648. (color-values (face-attribute 'default attr nil))))))
  17649. (defun org-latex-color-format (color-name)
  17650. "Convert COLOR-NAME to a RGB color value."
  17651. (apply 'format "%s,%s,%s"
  17652. (mapcar 'org-normalize-color
  17653. (color-values color-name))))
  17654. (defun org-normalize-color (value)
  17655. "Return string to be used as color value for an RGB component."
  17656. (format "%g" (/ value 65535.0)))
  17657. ;; Image display
  17658. (defvar org-inline-image-overlays nil)
  17659. (make-variable-buffer-local 'org-inline-image-overlays)
  17660. (defun org-toggle-inline-images (&optional include-linked)
  17661. "Toggle the display of inline images.
  17662. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17663. (interactive "P")
  17664. (if org-inline-image-overlays
  17665. (progn
  17666. (org-remove-inline-images)
  17667. (when (org-called-interactively-p 'interactive)
  17668. (message "Inline image display turned off")))
  17669. (org-display-inline-images include-linked)
  17670. (when (org-called-interactively-p 'interactive)
  17671. (message (if org-inline-image-overlays
  17672. (format "%d images displayed inline"
  17673. (length org-inline-image-overlays))
  17674. "No images to display inline")))))
  17675. (defun org-redisplay-inline-images ()
  17676. "Refresh the display of inline images."
  17677. (interactive)
  17678. (if (not org-inline-image-overlays)
  17679. (org-toggle-inline-images)
  17680. (org-toggle-inline-images)
  17681. (org-toggle-inline-images)))
  17682. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17683. "Display inline images.
  17684. An inline image is a link which follows either of these
  17685. conventions:
  17686. 1. Its path is a file with an extension matching return value
  17687. from `image-file-name-regexp' and it has no contents.
  17688. 2. Its description consists in a single link of the previous
  17689. type.
  17690. When optional argument INCLUDE-LINKED is non-nil, also links with
  17691. a text description part will be inlined. This can be nice for
  17692. a quick look at those images, but it does not reflect what
  17693. exported files will look like.
  17694. When optional argument REFRESH is non-nil, refresh existing
  17695. images between BEG and END. This will create new image displays
  17696. only if necessary. BEG and END default to the buffer
  17697. boundaries."
  17698. (interactive "P")
  17699. (when (display-graphic-p)
  17700. (unless refresh
  17701. (org-remove-inline-images)
  17702. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17703. (org-with-wide-buffer
  17704. (goto-char (or beg (point-min)))
  17705. (let ((case-fold-search t)
  17706. (file-extension-re (org-image-file-name-regexp)))
  17707. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17708. (let ((link (save-match-data (org-element-context))))
  17709. ;; Check if we're at an inline image.
  17710. (when (and (equal (org-element-property :type link) "file")
  17711. (or include-linked
  17712. (not (org-element-property :contents-begin link)))
  17713. (let ((parent (org-element-property :parent link)))
  17714. (or (not (eq (org-element-type parent) 'link))
  17715. (not (cdr (org-element-contents parent)))))
  17716. (org-string-match-p file-extension-re
  17717. (org-element-property :path link)))
  17718. (let ((file (expand-file-name (org-element-property :path link))))
  17719. (when (file-exists-p file)
  17720. (let ((width
  17721. ;; Apply `org-image-actual-width' specifications.
  17722. (cond
  17723. ((not (image-type-available-p 'imagemagick)) nil)
  17724. ((eq org-image-actual-width t) nil)
  17725. ((listp org-image-actual-width)
  17726. (or
  17727. ;; First try to find a width among
  17728. ;; attributes associated to the paragraph
  17729. ;; containing link.
  17730. (let ((paragraph
  17731. (let ((e link))
  17732. (while (and (setq e (org-element-property
  17733. :parent e))
  17734. (not (eq (org-element-type e)
  17735. 'paragraph))))
  17736. e)))
  17737. (when paragraph
  17738. (save-excursion
  17739. (goto-char (org-element-property :begin paragraph))
  17740. (when
  17741. (re-search-forward
  17742. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17743. (org-element-property
  17744. :post-affiliated paragraph)
  17745. t)
  17746. (string-to-number (match-string 1))))))
  17747. ;; Otherwise, fall-back to provided number.
  17748. (car org-image-actual-width)))
  17749. ((numberp org-image-actual-width)
  17750. org-image-actual-width)))
  17751. (old (get-char-property-and-overlay
  17752. (org-element-property :begin link)
  17753. 'org-image-overlay)))
  17754. (if (and (car-safe old) refresh)
  17755. (image-refresh (overlay-get (cdr old) 'display))
  17756. (let ((image (create-image file
  17757. (and width 'imagemagick)
  17758. nil
  17759. :width width)))
  17760. (when image
  17761. (let* ((link
  17762. ;; If inline image is the description
  17763. ;; of another link, be sure to
  17764. ;; consider the latter as the one to
  17765. ;; apply the overlay on.
  17766. (let ((parent
  17767. (org-element-property :parent link)))
  17768. (if (eq (org-element-type parent) 'link)
  17769. parent
  17770. link)))
  17771. (ov (make-overlay
  17772. (org-element-property :begin link)
  17773. (progn
  17774. (goto-char
  17775. (org-element-property :end link))
  17776. (skip-chars-backward " \t")
  17777. (point)))))
  17778. (overlay-put ov 'display image)
  17779. (overlay-put ov 'face 'default)
  17780. (overlay-put ov 'org-image-overlay t)
  17781. (overlay-put
  17782. ov 'modification-hooks
  17783. (list 'org-display-inline-remove-overlay))
  17784. (push ov org-inline-image-overlays)))))))))))))))
  17785. (define-obsolete-function-alias
  17786. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17787. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17788. "Remove inline-display overlay if a corresponding region is modified."
  17789. (let ((inhibit-modification-hooks t))
  17790. (when (and ov after)
  17791. (delete ov org-inline-image-overlays)
  17792. (delete-overlay ov))))
  17793. (defun org-remove-inline-images ()
  17794. "Remove inline display of images."
  17795. (interactive)
  17796. (mapc 'delete-overlay org-inline-image-overlays)
  17797. (setq org-inline-image-overlays nil))
  17798. ;;;; Key bindings
  17799. ;; Outline functions from `outline-mode-prefix-map'
  17800. ;; that can be remapped in Org:
  17801. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17802. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17803. (define-key org-mode-map [remap outline-forward-same-level]
  17804. 'org-forward-heading-same-level)
  17805. (define-key org-mode-map [remap outline-backward-same-level]
  17806. 'org-backward-heading-same-level)
  17807. (define-key org-mode-map [remap show-branches]
  17808. 'org-kill-note-or-show-branches)
  17809. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17810. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17811. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17812. (define-key org-mode-map [remap outline-next-visible-heading]
  17813. 'org-next-visible-heading)
  17814. (define-key org-mode-map [remap outline-previous-visible-heading]
  17815. 'org-previous-visible-heading)
  17816. ;; Outline functions from `outline-mode-prefix-map' that can not
  17817. ;; be remapped in Org:
  17818. ;; - the column "key binding" shows whether the Outline function is still
  17819. ;; available in Org mode on the same key that it has been bound to in
  17820. ;; Outline mode:
  17821. ;; - "overridden": key used for a different functionality in Org mode
  17822. ;; - else: key still bound to the same Outline function in Org mode
  17823. ;; | Outline function | key binding | Org replacement |
  17824. ;; |------------------------------------+-------------+--------------------------|
  17825. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  17826. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  17827. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17828. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17829. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17830. ;; | `show-entry' | overridden | no replacement |
  17831. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17832. ;; | `show-branches' | `C-c C-k' | still same function |
  17833. ;; | `show-subtree' | overridden | visibility cycling |
  17834. ;; | `show-all' | overridden | no replacement |
  17835. ;; | `hide-subtree' | overridden | visibility cycling |
  17836. ;; | `hide-body' | overridden | no replacement |
  17837. ;; | `hide-entry' | overridden | visibility cycling |
  17838. ;; | `hide-leaves' | overridden | no replacement |
  17839. ;; | `hide-sublevels' | overridden | no replacement |
  17840. ;; | `hide-other' | overridden | no replacement |
  17841. ;; Make `C-c C-x' a prefix key
  17842. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17843. ;; TAB key with modifiers
  17844. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17845. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17846. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17847. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17848. ;; The following line is necessary under Suse GNU/Linux
  17849. (unless (featurep 'xemacs)
  17850. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17851. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17852. (define-key org-mode-map [backtab] 'org-shifttab)
  17853. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17854. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17855. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17856. ;; Cursor keys with modifiers
  17857. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17858. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17859. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17860. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17861. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  17862. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  17863. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17864. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17865. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17866. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17867. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17868. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17869. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17870. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17871. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17872. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17873. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17874. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17875. ;; Babel keys
  17876. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17877. (mapc (lambda (pair)
  17878. (define-key org-babel-map (car pair) (cdr pair)))
  17879. org-babel-key-bindings)
  17880. ;;; Extra keys for tty access.
  17881. ;; We only set them when really needed because otherwise the
  17882. ;; menus don't show the simple keys
  17883. (when (or org-use-extra-keys
  17884. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17885. (not window-system))
  17886. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17887. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17888. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17889. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17890. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17891. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17892. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17893. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17894. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17895. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17896. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17897. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17898. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17899. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17900. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17901. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17902. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17903. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17904. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17905. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17906. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17907. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17908. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17909. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17910. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17911. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17912. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17913. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17914. ;; All the other keys
  17915. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17916. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17917. (if (boundp 'narrow-map)
  17918. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17919. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17920. (if (boundp 'narrow-map)
  17921. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17922. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17923. (if (boundp 'narrow-map)
  17924. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17925. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17926. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17927. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17928. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17929. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17930. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17931. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17932. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17933. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17934. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17935. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17936. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17937. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17938. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17939. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17940. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17941. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17942. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17943. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17944. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17945. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17946. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17947. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17948. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17949. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17950. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17951. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17952. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17953. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17954. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17955. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17956. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17957. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17958. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17959. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17960. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17961. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17962. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17963. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17964. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17965. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17966. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17967. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17968. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17969. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17970. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17971. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17972. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17973. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17974. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17975. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17976. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17977. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17978. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17979. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17980. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17981. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17982. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17983. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17984. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17985. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17986. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17987. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17988. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17989. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17990. (org-defkey org-mode-map "\C-m" 'org-return)
  17991. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17992. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17993. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17994. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17995. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17996. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17997. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17998. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  17999. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18000. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18001. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18002. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18003. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18004. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18005. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18006. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18007. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18008. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18009. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18010. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18011. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18012. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18013. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18014. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18015. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18016. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18017. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18018. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18019. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18020. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18021. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18022. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18023. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18024. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18025. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18026. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18027. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18028. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18029. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18030. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18031. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18032. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18033. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18034. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18035. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18036. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18037. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18038. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18039. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18040. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18041. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  18042. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18043. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18044. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18045. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18046. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18047. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18048. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18049. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18050. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18051. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18052. (when (featurep 'xemacs)
  18053. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18054. (defconst org-speed-commands-default
  18055. '(
  18056. ("Outline Navigation")
  18057. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18058. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18059. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18060. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18061. ("F" . org-next-block)
  18062. ("B" . org-previous-block)
  18063. ("u" . (org-speed-move-safe 'outline-up-heading))
  18064. ("j" . org-goto)
  18065. ("g" . (org-refile t))
  18066. ("Outline Visibility")
  18067. ("c" . org-cycle)
  18068. ("C" . org-shifttab)
  18069. (" " . org-display-outline-path)
  18070. ("s" . org-narrow-to-subtree)
  18071. ("=" . org-columns)
  18072. ("Outline Structure Editing")
  18073. ("U" . org-metaup)
  18074. ("D" . org-metadown)
  18075. ("r" . org-metaright)
  18076. ("l" . org-metaleft)
  18077. ("R" . org-shiftmetaright)
  18078. ("L" . org-shiftmetaleft)
  18079. ("i" . (progn (forward-char 1) (call-interactively
  18080. 'org-insert-heading-respect-content)))
  18081. ("^" . org-sort)
  18082. ("w" . org-refile)
  18083. ("a" . org-archive-subtree-default-with-confirmation)
  18084. ("@" . org-mark-subtree)
  18085. ("#" . org-toggle-comment)
  18086. ("Clock Commands")
  18087. ("I" . org-clock-in)
  18088. ("O" . org-clock-out)
  18089. ("Meta Data Editing")
  18090. ("t" . org-todo)
  18091. ("," . (org-priority))
  18092. ("0" . (org-priority ?\ ))
  18093. ("1" . (org-priority ?A))
  18094. ("2" . (org-priority ?B))
  18095. ("3" . (org-priority ?C))
  18096. (":" . org-set-tags-command)
  18097. ("e" . org-set-effort)
  18098. ("E" . org-inc-effort)
  18099. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18100. (org-entry-put (point) "APPT_WARNTIME" m)))
  18101. ("Agenda Views etc")
  18102. ("v" . org-agenda)
  18103. ("/" . org-sparse-tree)
  18104. ("Misc")
  18105. ("o" . org-open-at-point)
  18106. ("?" . org-speed-command-help)
  18107. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18108. (">" . (org-agenda-remove-restriction-lock))
  18109. )
  18110. "The default speed commands.")
  18111. (defun org-print-speed-command (e)
  18112. (if (> (length (car e)) 1)
  18113. (progn
  18114. (princ "\n")
  18115. (princ (car e))
  18116. (princ "\n")
  18117. (princ (make-string (length (car e)) ?-))
  18118. (princ "\n"))
  18119. (princ (car e))
  18120. (princ " ")
  18121. (if (symbolp (cdr e))
  18122. (princ (symbol-name (cdr e)))
  18123. (prin1 (cdr e)))
  18124. (princ "\n")))
  18125. (defun org-speed-command-help ()
  18126. "Show the available speed commands."
  18127. (interactive)
  18128. (if (not org-use-speed-commands)
  18129. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18130. (with-output-to-temp-buffer "*Help*"
  18131. (princ "User-defined Speed commands\n===========================\n")
  18132. (mapc 'org-print-speed-command org-speed-commands-user)
  18133. (princ "\n")
  18134. (princ "Built-in Speed commands\n=======================\n")
  18135. (mapc 'org-print-speed-command org-speed-commands-default))
  18136. (with-current-buffer "*Help*"
  18137. (setq truncate-lines t))))
  18138. (defun org-speed-move-safe (cmd)
  18139. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18140. If not, return to the original position and throw an error."
  18141. (interactive)
  18142. (let ((pos (point)))
  18143. (call-interactively cmd)
  18144. (unless (and (bolp) (org-at-heading-p))
  18145. (goto-char pos)
  18146. (error "Boundary reached while executing %s" cmd))))
  18147. (defvar org-self-insert-command-undo-counter 0)
  18148. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18149. (defvar org-speed-command nil)
  18150. (define-obsolete-function-alias
  18151. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18152. (defun org-speed-command-activate (keys)
  18153. "Hook for activating single-letter speed commands.
  18154. `org-speed-commands-default' specifies a minimal command set.
  18155. Use `org-speed-commands-user' for further customization."
  18156. (when (or (and (bolp) (looking-at org-outline-regexp))
  18157. (and (functionp org-use-speed-commands)
  18158. (funcall org-use-speed-commands)))
  18159. (cdr (assoc keys (append org-speed-commands-user
  18160. org-speed-commands-default)))))
  18161. (define-obsolete-function-alias
  18162. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18163. (defun org-babel-speed-command-activate (keys)
  18164. "Hook for activating single-letter code block commands."
  18165. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18166. (cdr (assoc keys org-babel-key-bindings))))
  18167. (defcustom org-speed-command-hook
  18168. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18169. "Hook for activating speed commands at strategic locations.
  18170. Hook functions are called in sequence until a valid handler is
  18171. found.
  18172. Each hook takes a single argument, a user-pressed command key
  18173. which is also a `self-insert-command' from the global map.
  18174. Within the hook, examine the cursor position and the command key
  18175. and return nil or a valid handler as appropriate. Handler could
  18176. be one of an interactive command, a function, or a form.
  18177. Set `org-use-speed-commands' to non-nil value to enable this
  18178. hook. The default setting is `org-speed-command-activate'."
  18179. :group 'org-structure
  18180. :version "24.1"
  18181. :type 'hook)
  18182. (defun org-self-insert-command (N)
  18183. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18184. If the cursor is in a table looking at whitespace, the whitespace is
  18185. overwritten, and the table is not marked as requiring realignment."
  18186. (interactive "p")
  18187. (org-check-before-invisible-edit 'insert)
  18188. (cond
  18189. ((and org-use-speed-commands
  18190. (let ((kv (this-command-keys-vector)))
  18191. (setq org-speed-command
  18192. (run-hook-with-args-until-success
  18193. 'org-speed-command-hook
  18194. (make-string 1 (aref kv (1- (length kv))))))))
  18195. (cond
  18196. ((commandp org-speed-command)
  18197. (setq this-command org-speed-command)
  18198. (call-interactively org-speed-command))
  18199. ((functionp org-speed-command)
  18200. (funcall org-speed-command))
  18201. ((and org-speed-command (listp org-speed-command))
  18202. (eval org-speed-command))
  18203. (t (let (org-use-speed-commands)
  18204. (call-interactively 'org-self-insert-command)))))
  18205. ((and
  18206. (org-table-p)
  18207. (progn
  18208. ;; check if we blank the field, and if that triggers align
  18209. (and (featurep 'org-table) org-table-auto-blank-field
  18210. (memq last-command
  18211. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18212. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18213. ;; got extra space, this field does not determine column width
  18214. (let (org-table-may-need-update) (org-table-blank-field))
  18215. ;; no extra space, this field may determine column width
  18216. (org-table-blank-field)))
  18217. t)
  18218. (eq N 1)
  18219. (looking-at "[^|\n]* |"))
  18220. (let (org-table-may-need-update)
  18221. (goto-char (1- (match-end 0)))
  18222. (backward-delete-char 1)
  18223. (goto-char (match-beginning 0))
  18224. (self-insert-command N)))
  18225. (t
  18226. (setq org-table-may-need-update t)
  18227. (self-insert-command N)
  18228. (org-fix-tags-on-the-fly)
  18229. (if org-self-insert-cluster-for-undo
  18230. (if (not (eq last-command 'org-self-insert-command))
  18231. (setq org-self-insert-command-undo-counter 1)
  18232. (if (>= org-self-insert-command-undo-counter 20)
  18233. (setq org-self-insert-command-undo-counter 1)
  18234. (and (> org-self-insert-command-undo-counter 0)
  18235. buffer-undo-list (listp buffer-undo-list)
  18236. (not (cadr buffer-undo-list)) ; remove nil entry
  18237. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18238. (setq org-self-insert-command-undo-counter
  18239. (1+ org-self-insert-command-undo-counter))))))))
  18240. (defun org-check-before-invisible-edit (kind)
  18241. "Check is editing if kind KIND would be dangerous with invisible text around.
  18242. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18243. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18244. (if (and org-catch-invisible-edits
  18245. (or (not (boundp 'visible-mode)) (not visible-mode))
  18246. (or (get-char-property (point) 'invisible)
  18247. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18248. ;; OK, we need to take a closer look
  18249. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18250. (invisible-before-point (if (bobp) nil (get-char-property
  18251. (1- (point)) 'invisible)))
  18252. (border-and-ok-direction
  18253. (or
  18254. ;; Check if we are acting predictably before invisible text
  18255. (and invisible-at-point (not invisible-before-point)
  18256. (memq kind '(insert delete-backward)))
  18257. ;; Check if we are acting predictably after invisible text
  18258. ;; This works not well, and I have turned it off. It seems
  18259. ;; better to always show and stop after invisible text.
  18260. ;; (and (not invisible-at-point) invisible-before-point
  18261. ;; (memq kind '(insert delete)))
  18262. )))
  18263. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18264. (memq invisible-before-point '(outline org-hide-block t)))
  18265. (if (eq org-catch-invisible-edits 'error)
  18266. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18267. (if (and org-custom-properties-overlays
  18268. (y-or-n-p "Display invisible properties in this buffer? "))
  18269. (org-toggle-custom-properties-visibility)
  18270. ;; Make the area visible
  18271. (save-excursion
  18272. (if invisible-before-point
  18273. (goto-char (previous-single-char-property-change
  18274. (point) 'invisible)))
  18275. (show-subtree))
  18276. (cond
  18277. ((eq org-catch-invisible-edits 'show)
  18278. ;; That's it, we do the edit after showing
  18279. (message
  18280. "Unfolding invisible region around point before editing")
  18281. (sit-for 1))
  18282. ((and (eq org-catch-invisible-edits 'smart)
  18283. border-and-ok-direction)
  18284. (message "Unfolding invisible region around point before editing"))
  18285. (t
  18286. ;; Don't do the edit, make the user repeat it in full visibility
  18287. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18288. (defun org-fix-tags-on-the-fly ()
  18289. (when (and (equal (char-after (point-at-bol)) ?*)
  18290. (org-at-heading-p))
  18291. (org-align-tags-here org-tags-column)))
  18292. (defun org-delete-backward-char (N)
  18293. "Like `delete-backward-char', insert whitespace at field end in tables.
  18294. When deleting backwards, in tables this function will insert whitespace in
  18295. front of the next \"|\" separator, to keep the table aligned. The table will
  18296. still be marked for re-alignment if the field did fill the entire column,
  18297. because, in this case the deletion might narrow the column."
  18298. (interactive "p")
  18299. (save-match-data
  18300. (org-check-before-invisible-edit 'delete-backward)
  18301. (if (and (org-table-p)
  18302. (eq N 1)
  18303. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18304. (looking-at ".*?|"))
  18305. (let ((pos (point))
  18306. (noalign (looking-at "[^|\n\r]* |"))
  18307. (c org-table-may-need-update))
  18308. (backward-delete-char N)
  18309. (if (not overwrite-mode)
  18310. (progn
  18311. (skip-chars-forward "^|")
  18312. (insert " ")
  18313. (goto-char (1- pos))))
  18314. ;; noalign: if there were two spaces at the end, this field
  18315. ;; does not determine the width of the column.
  18316. (if noalign (setq org-table-may-need-update c)))
  18317. (backward-delete-char N)
  18318. (org-fix-tags-on-the-fly))))
  18319. (defun org-delete-char (N)
  18320. "Like `delete-char', but insert whitespace at field end in tables.
  18321. When deleting characters, in tables this function will insert whitespace in
  18322. front of the next \"|\" separator, to keep the table aligned. The table will
  18323. still be marked for re-alignment if the field did fill the entire column,
  18324. because, in this case the deletion might narrow the column."
  18325. (interactive "p")
  18326. (save-match-data
  18327. (org-check-before-invisible-edit 'delete)
  18328. (if (and (org-table-p)
  18329. (not (bolp))
  18330. (not (= (char-after) ?|))
  18331. (eq N 1))
  18332. (if (looking-at ".*?|")
  18333. (let ((pos (point))
  18334. (noalign (looking-at "[^|\n\r]* |"))
  18335. (c org-table-may-need-update))
  18336. (replace-match
  18337. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18338. (goto-char pos)
  18339. ;; noalign: if there were two spaces at the end, this field
  18340. ;; does not determine the width of the column.
  18341. (if noalign (setq org-table-may-need-update c)))
  18342. (delete-char N))
  18343. (delete-char N)
  18344. (org-fix-tags-on-the-fly))))
  18345. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18346. (put 'org-self-insert-command 'delete-selection
  18347. (lambda ()
  18348. (not (run-hook-with-args-until-success
  18349. 'self-insert-uses-region-functions))))
  18350. (put 'orgtbl-self-insert-command 'delete-selection
  18351. (lambda ()
  18352. (not (run-hook-with-args-until-success
  18353. 'self-insert-uses-region-functions))))
  18354. (put 'org-delete-char 'delete-selection 'supersede)
  18355. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18356. (put 'org-yank 'delete-selection 'yank)
  18357. ;; Make `flyspell-mode' delay after some commands
  18358. (put 'org-self-insert-command 'flyspell-delayed t)
  18359. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18360. (put 'org-delete-char 'flyspell-delayed t)
  18361. (put 'org-delete-backward-char 'flyspell-delayed t)
  18362. ;; Make pabbrev-mode expand after org-mode commands
  18363. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18364. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18365. (defun org-remap (map &rest commands)
  18366. "In MAP, remap the functions given in COMMANDS.
  18367. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18368. (let (new old)
  18369. (while commands
  18370. (setq old (pop commands) new (pop commands))
  18371. (if (fboundp 'command-remapping)
  18372. (org-defkey map (vector 'remap old) new)
  18373. (substitute-key-definition old new map global-map)))))
  18374. (defun org-transpose-words ()
  18375. "Transpose words for Org.
  18376. This uses the `org-mode-transpose-word-syntax-table' syntax
  18377. table, which interprets characters in `org-emphasis-alist' as
  18378. word constituents."
  18379. (interactive)
  18380. (with-syntax-table org-mode-transpose-word-syntax-table
  18381. (call-interactively 'transpose-words)))
  18382. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18383. (when (eq org-enable-table-editor 'optimized)
  18384. ;; If the user wants maximum table support, we need to hijack
  18385. ;; some standard editing functions
  18386. (org-remap org-mode-map
  18387. 'self-insert-command 'org-self-insert-command
  18388. 'delete-char 'org-delete-char
  18389. 'delete-backward-char 'org-delete-backward-char)
  18390. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18391. (defvar org-ctrl-c-ctrl-c-hook nil
  18392. "Hook for functions attaching themselves to `C-c C-c'.
  18393. This can be used to add additional functionality to the C-c C-c
  18394. key which executes context-dependent commands. This hook is run
  18395. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18396. run after the last test.
  18397. Each function will be called with no arguments. The function
  18398. must check if the context is appropriate for it to act. If yes,
  18399. it should do its thing and then return a non-nil value. If the
  18400. context is wrong, just do nothing and return nil.")
  18401. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18402. "Hook for functions attaching themselves to `C-c C-c'.
  18403. This can be used to add additional functionality to the C-c C-c
  18404. key which executes context-dependent commands. This hook is run
  18405. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18406. before the first test.
  18407. Each function will be called with no arguments. The function
  18408. must check if the context is appropriate for it to act. If yes,
  18409. it should do its thing and then return a non-nil value. If the
  18410. context is wrong, just do nothing and return nil.")
  18411. (defvar org-tab-first-hook nil
  18412. "Hook for functions to attach themselves to TAB.
  18413. See `org-ctrl-c-ctrl-c-hook' for more information.
  18414. This hook runs as the first action when TAB is pressed, even before
  18415. `org-cycle' messes around with the `outline-regexp' to cater for
  18416. inline tasks and plain list item folding.
  18417. If any function in this hook returns t, any other actions that
  18418. would have been caused by TAB (such as table field motion or visibility
  18419. cycling) will not occur.")
  18420. (defvar org-tab-after-check-for-table-hook nil
  18421. "Hook for functions to attach themselves to TAB.
  18422. See `org-ctrl-c-ctrl-c-hook' for more information.
  18423. This hook runs after it has been established that the cursor is not in a
  18424. table, but before checking if the cursor is in a headline or if global cycling
  18425. should be done.
  18426. If any function in this hook returns t, not other actions like visibility
  18427. cycling will be done.")
  18428. (defvar org-tab-after-check-for-cycling-hook nil
  18429. "Hook for functions to attach themselves to TAB.
  18430. See `org-ctrl-c-ctrl-c-hook' for more information.
  18431. This hook runs after it has been established that not table field motion and
  18432. not visibility should be done because of current context. This is probably
  18433. the place where a package like yasnippets can hook in.")
  18434. (defvar org-tab-before-tab-emulation-hook nil
  18435. "Hook for functions to attach themselves to TAB.
  18436. See `org-ctrl-c-ctrl-c-hook' for more information.
  18437. This hook runs after every other options for TAB have been exhausted, but
  18438. before indentation and \t insertion takes place.")
  18439. (defvar org-metaleft-hook nil
  18440. "Hook for functions attaching themselves to `M-left'.
  18441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18442. (defvar org-metaright-hook nil
  18443. "Hook for functions attaching themselves to `M-right'.
  18444. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18445. (defvar org-metaup-hook nil
  18446. "Hook for functions attaching themselves to `M-up'.
  18447. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18448. (defvar org-metadown-hook nil
  18449. "Hook for functions attaching themselves to `M-down'.
  18450. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18451. (defvar org-shiftmetaleft-hook nil
  18452. "Hook for functions attaching themselves to `M-S-left'.
  18453. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18454. (defvar org-shiftmetaright-hook nil
  18455. "Hook for functions attaching themselves to `M-S-right'.
  18456. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18457. (defvar org-shiftmetaup-hook nil
  18458. "Hook for functions attaching themselves to `M-S-up'.
  18459. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18460. (defvar org-shiftmetadown-hook nil
  18461. "Hook for functions attaching themselves to `M-S-down'.
  18462. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18463. (defvar org-metareturn-hook nil
  18464. "Hook for functions attaching themselves to `M-RET'.
  18465. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18466. (defvar org-shiftup-hook nil
  18467. "Hook for functions attaching themselves to `S-up'.
  18468. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18469. (defvar org-shiftup-final-hook nil
  18470. "Hook for functions attaching themselves to `S-up'.
  18471. This one runs after all other options except shift-select have been excluded.
  18472. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18473. (defvar org-shiftdown-hook nil
  18474. "Hook for functions attaching themselves to `S-down'.
  18475. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18476. (defvar org-shiftdown-final-hook nil
  18477. "Hook for functions attaching themselves to `S-down'.
  18478. This one runs after all other options except shift-select have been excluded.
  18479. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18480. (defvar org-shiftleft-hook nil
  18481. "Hook for functions attaching themselves to `S-left'.
  18482. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18483. (defvar org-shiftleft-final-hook nil
  18484. "Hook for functions attaching themselves to `S-left'.
  18485. This one runs after all other options except shift-select have been excluded.
  18486. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18487. (defvar org-shiftright-hook nil
  18488. "Hook for functions attaching themselves to `S-right'.
  18489. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18490. (defvar org-shiftright-final-hook nil
  18491. "Hook for functions attaching themselves to `S-right'.
  18492. This one runs after all other options except shift-select have been excluded.
  18493. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18494. (defun org-modifier-cursor-error ()
  18495. "Throw an error, a modified cursor command was applied in wrong context."
  18496. (user-error "This command is active in special context like tables, headlines or items"))
  18497. (defun org-shiftselect-error ()
  18498. "Throw an error because Shift-Cursor command was applied in wrong context."
  18499. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18500. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18501. (user-error "This command works only in special context like headlines or timestamps")))
  18502. (defun org-call-for-shift-select (cmd)
  18503. (let ((this-command-keys-shift-translated t))
  18504. (call-interactively cmd)))
  18505. (defun org-shifttab (&optional arg)
  18506. "Global visibility cycling or move to previous table field.
  18507. Call `org-table-previous-field' within a table.
  18508. When ARG is nil, cycle globally through visibility states.
  18509. When ARG is a numeric prefix, show contents of this level."
  18510. (interactive "P")
  18511. (cond
  18512. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18513. ((integerp arg)
  18514. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18515. (message "Content view to level: %d" arg)
  18516. (org-content (prefix-numeric-value arg2))
  18517. (org-cycle-show-empty-lines t)
  18518. (setq org-cycle-global-status 'overview)))
  18519. (t (call-interactively 'org-global-cycle))))
  18520. (defun org-shiftmetaleft ()
  18521. "Promote subtree or delete table column.
  18522. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18523. `org-table-delete-column', depending on context. See the
  18524. individual commands for more information."
  18525. (interactive)
  18526. (cond
  18527. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18528. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18529. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18530. ((if (not (org-region-active-p)) (org-at-item-p)
  18531. (save-excursion (goto-char (region-beginning))
  18532. (org-at-item-p)))
  18533. (call-interactively 'org-outdent-item-tree))
  18534. (t (org-modifier-cursor-error))))
  18535. (defun org-shiftmetaright ()
  18536. "Demote subtree or insert table column.
  18537. Calls `org-demote-subtree', `org-indent-item-tree', or
  18538. `org-table-insert-column', depending on context. See the
  18539. individual commands for more information."
  18540. (interactive)
  18541. (cond
  18542. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18543. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18544. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18545. ((if (not (org-region-active-p)) (org-at-item-p)
  18546. (save-excursion (goto-char (region-beginning))
  18547. (org-at-item-p)))
  18548. (call-interactively 'org-indent-item-tree))
  18549. (t (org-modifier-cursor-error))))
  18550. (defun org-shiftmetaup (&optional arg)
  18551. "Drag the line at point up.
  18552. In a table, kill the current row.
  18553. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18554. but also adjust the previous clocked item in the clock history.
  18555. Everywhere else, drag the line at point up."
  18556. (interactive "P")
  18557. (cond
  18558. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18559. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18560. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18561. (call-interactively 'org-timestamp-up)))
  18562. (t (call-interactively 'org-drag-line-backward))))
  18563. (defun org-shiftmetadown (&optional arg)
  18564. "Drag the line at point down.
  18565. In a table, insert an empty row at the current line.
  18566. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18567. but also adjust the previous clocked item in the clock history.
  18568. Everywhere else, drag the line at point down."
  18569. (interactive "P")
  18570. (cond
  18571. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18572. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18573. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18574. (call-interactively 'org-timestamp-down)))
  18575. (t (call-interactively 'org-drag-line-forward))))
  18576. (defsubst org-hidden-tree-error ()
  18577. (user-error
  18578. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18579. (defun org-metaleft (&optional arg)
  18580. "Promote heading or move table column to left.
  18581. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18582. With no specific context, calls the Emacs default `backward-word'.
  18583. See the individual commands for more information."
  18584. (interactive "P")
  18585. (cond
  18586. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18587. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18588. ((org-with-limited-levels
  18589. (or (org-at-heading-p)
  18590. (and (org-region-active-p)
  18591. (save-excursion
  18592. (goto-char (region-beginning))
  18593. (org-at-heading-p)))))
  18594. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18595. (call-interactively 'org-do-promote))
  18596. ;; At an inline task.
  18597. ((org-at-heading-p)
  18598. (call-interactively 'org-inlinetask-promote))
  18599. ((or (org-at-item-p)
  18600. (and (org-region-active-p)
  18601. (save-excursion
  18602. (goto-char (region-beginning))
  18603. (org-at-item-p))))
  18604. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18605. (call-interactively 'org-outdent-item))
  18606. (t (call-interactively 'backward-word))))
  18607. (defun org-metaright (&optional arg)
  18608. "Demote a subtree, a list item or move table column to right.
  18609. In front of a drawer or a block keyword, indent it correctly.
  18610. With no specific context, calls the Emacs default `forward-word'.
  18611. See the individual commands for more information."
  18612. (interactive "P")
  18613. (cond
  18614. ((run-hook-with-args-until-success 'org-metaright-hook))
  18615. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18616. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18617. ((org-at-block-p) (call-interactively 'org-indent-block))
  18618. ((org-with-limited-levels
  18619. (or (org-at-heading-p)
  18620. (and (org-region-active-p)
  18621. (save-excursion
  18622. (goto-char (region-beginning))
  18623. (org-at-heading-p)))))
  18624. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18625. (call-interactively 'org-do-demote))
  18626. ;; At an inline task.
  18627. ((org-at-heading-p)
  18628. (call-interactively 'org-inlinetask-demote))
  18629. ((or (org-at-item-p)
  18630. (and (org-region-active-p)
  18631. (save-excursion
  18632. (goto-char (region-beginning))
  18633. (org-at-item-p))))
  18634. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18635. (call-interactively 'org-indent-item))
  18636. (t (call-interactively 'forward-word))))
  18637. (defun org-check-for-hidden (what)
  18638. "Check if there are hidden headlines/items in the current visual line.
  18639. WHAT can be either `headlines' or `items'. If the current line is
  18640. an outline or item heading and it has a folded subtree below it,
  18641. this function returns t, nil otherwise."
  18642. (let ((re (cond
  18643. ((eq what 'headlines) org-outline-regexp-bol)
  18644. ((eq what 'items) (org-item-beginning-re))
  18645. (t (error "This should not happen"))))
  18646. beg end)
  18647. (save-excursion
  18648. (catch 'exit
  18649. (unless (org-region-active-p)
  18650. (setq beg (point-at-bol))
  18651. (beginning-of-line 2)
  18652. (while (and (not (eobp)) ;; this is like `next-line'
  18653. (get-char-property (1- (point)) 'invisible))
  18654. (beginning-of-line 2))
  18655. (setq end (point))
  18656. (goto-char beg)
  18657. (goto-char (point-at-eol))
  18658. (setq end (max end (point)))
  18659. (while (re-search-forward re end t)
  18660. (if (get-char-property (match-beginning 0) 'invisible)
  18661. (throw 'exit t))))
  18662. nil))))
  18663. (defun org-metaup (&optional arg)
  18664. "Move subtree up or move table row up.
  18665. Calls `org-move-subtree-up' or `org-table-move-row' or
  18666. `org-move-item-up', depending on context. See the individual commands
  18667. for more information."
  18668. (interactive "P")
  18669. (cond
  18670. ((run-hook-with-args-until-success 'org-metaup-hook))
  18671. ((org-region-active-p)
  18672. (let* ((a (min (region-beginning) (region-end)))
  18673. (b (1- (max (region-beginning) (region-end))))
  18674. (c (save-excursion (goto-char a)
  18675. (move-beginning-of-line 0)))
  18676. (d (save-excursion (goto-char a)
  18677. (move-end-of-line 0) (point))))
  18678. (transpose-regions a b c d)
  18679. (goto-char c)))
  18680. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18681. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18682. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18683. (t (org-drag-element-backward))))
  18684. (defun org-metadown (&optional arg)
  18685. "Move subtree down or move table row down.
  18686. Calls `org-move-subtree-down' or `org-table-move-row' or
  18687. `org-move-item-down', depending on context. See the individual
  18688. commands for more information."
  18689. (interactive "P")
  18690. (cond
  18691. ((run-hook-with-args-until-success 'org-metadown-hook))
  18692. ((org-region-active-p)
  18693. (let* ((a (min (region-beginning) (region-end)))
  18694. (b (max (region-beginning) (region-end)))
  18695. (c (save-excursion (goto-char b)
  18696. (move-beginning-of-line 1)))
  18697. (d (save-excursion (goto-char b)
  18698. (move-end-of-line 1) (1+ (point)))))
  18699. (transpose-regions a b c d)
  18700. (goto-char d)))
  18701. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18702. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18703. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18704. (t (org-drag-element-forward))))
  18705. (defun org-shiftup (&optional arg)
  18706. "Increase item in timestamp or increase priority of current headline.
  18707. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18708. depending on context. See the individual commands for more information."
  18709. (interactive "P")
  18710. (cond
  18711. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18712. ((and org-support-shift-select (org-region-active-p))
  18713. (org-call-for-shift-select 'previous-line))
  18714. ((org-at-timestamp-p t)
  18715. (call-interactively (if org-edit-timestamp-down-means-later
  18716. 'org-timestamp-down 'org-timestamp-up)))
  18717. ((and (not (eq org-support-shift-select 'always))
  18718. org-enable-priority-commands
  18719. (org-at-heading-p))
  18720. (call-interactively 'org-priority-up))
  18721. ((and (not org-support-shift-select) (org-at-item-p))
  18722. (call-interactively 'org-previous-item))
  18723. ((org-clocktable-try-shift 'up arg))
  18724. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18725. (org-support-shift-select
  18726. (org-call-for-shift-select 'previous-line))
  18727. (t (org-shiftselect-error))))
  18728. (defun org-shiftdown (&optional arg)
  18729. "Decrease item in timestamp or decrease priority of current headline.
  18730. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18731. depending on context. See the individual commands for more information."
  18732. (interactive "P")
  18733. (cond
  18734. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18735. ((and org-support-shift-select (org-region-active-p))
  18736. (org-call-for-shift-select 'next-line))
  18737. ((org-at-timestamp-p t)
  18738. (call-interactively (if org-edit-timestamp-down-means-later
  18739. 'org-timestamp-up 'org-timestamp-down)))
  18740. ((and (not (eq org-support-shift-select 'always))
  18741. org-enable-priority-commands
  18742. (org-at-heading-p))
  18743. (call-interactively 'org-priority-down))
  18744. ((and (not org-support-shift-select) (org-at-item-p))
  18745. (call-interactively 'org-next-item))
  18746. ((org-clocktable-try-shift 'down arg))
  18747. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18748. (org-support-shift-select
  18749. (org-call-for-shift-select 'next-line))
  18750. (t (org-shiftselect-error))))
  18751. (defun org-shiftright (&optional arg)
  18752. "Cycle the thing at point or in the current line, depending on context.
  18753. Depending on context, this does one of the following:
  18754. - switch a timestamp at point one day into the future
  18755. - on a headline, switch to the next TODO keyword.
  18756. - on an item, switch entire list to the next bullet type
  18757. - on a property line, switch to the next allowed value
  18758. - on a clocktable definition line, move time block into the future"
  18759. (interactive "P")
  18760. (cond
  18761. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18762. ((and org-support-shift-select (org-region-active-p))
  18763. (org-call-for-shift-select 'forward-char))
  18764. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18765. ((and (not (eq org-support-shift-select 'always))
  18766. (org-at-heading-p))
  18767. (let ((org-inhibit-logging
  18768. (not org-treat-S-cursor-todo-selection-as-state-change))
  18769. (org-inhibit-blocking
  18770. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18771. (org-call-with-arg 'org-todo 'right)))
  18772. ((or (and org-support-shift-select
  18773. (not (eq org-support-shift-select 'always))
  18774. (org-at-item-bullet-p))
  18775. (and (not org-support-shift-select) (org-at-item-p)))
  18776. (org-call-with-arg 'org-cycle-list-bullet nil))
  18777. ((and (not (eq org-support-shift-select 'always))
  18778. (org-at-property-p))
  18779. (call-interactively 'org-property-next-allowed-value))
  18780. ((org-clocktable-try-shift 'right arg))
  18781. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18782. (org-support-shift-select
  18783. (org-call-for-shift-select 'forward-char))
  18784. (t (org-shiftselect-error))))
  18785. (defun org-shiftleft (&optional arg)
  18786. "Cycle the thing at point or in the current line, depending on context.
  18787. Depending on context, this does one of the following:
  18788. - switch a timestamp at point one day into the past
  18789. - on a headline, switch to the previous TODO keyword.
  18790. - on an item, switch entire list to the previous bullet type
  18791. - on a property line, switch to the previous allowed value
  18792. - on a clocktable definition line, move time block into the past"
  18793. (interactive "P")
  18794. (cond
  18795. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18796. ((and org-support-shift-select (org-region-active-p))
  18797. (org-call-for-shift-select 'backward-char))
  18798. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18799. ((and (not (eq org-support-shift-select 'always))
  18800. (org-at-heading-p))
  18801. (let ((org-inhibit-logging
  18802. (not org-treat-S-cursor-todo-selection-as-state-change))
  18803. (org-inhibit-blocking
  18804. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18805. (org-call-with-arg 'org-todo 'left)))
  18806. ((or (and org-support-shift-select
  18807. (not (eq org-support-shift-select 'always))
  18808. (org-at-item-bullet-p))
  18809. (and (not org-support-shift-select) (org-at-item-p)))
  18810. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18811. ((and (not (eq org-support-shift-select 'always))
  18812. (org-at-property-p))
  18813. (call-interactively 'org-property-previous-allowed-value))
  18814. ((org-clocktable-try-shift 'left arg))
  18815. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18816. (org-support-shift-select
  18817. (org-call-for-shift-select 'backward-char))
  18818. (t (org-shiftselect-error))))
  18819. (defun org-shiftcontrolright ()
  18820. "Switch to next TODO set."
  18821. (interactive)
  18822. (cond
  18823. ((and org-support-shift-select (org-region-active-p))
  18824. (org-call-for-shift-select 'forward-word))
  18825. ((and (not (eq org-support-shift-select 'always))
  18826. (org-at-heading-p))
  18827. (org-call-with-arg 'org-todo 'nextset))
  18828. (org-support-shift-select
  18829. (org-call-for-shift-select 'forward-word))
  18830. (t (org-shiftselect-error))))
  18831. (defun org-shiftcontrolleft ()
  18832. "Switch to previous TODO set."
  18833. (interactive)
  18834. (cond
  18835. ((and org-support-shift-select (org-region-active-p))
  18836. (org-call-for-shift-select 'backward-word))
  18837. ((and (not (eq org-support-shift-select 'always))
  18838. (org-at-heading-p))
  18839. (org-call-with-arg 'org-todo 'previousset))
  18840. (org-support-shift-select
  18841. (org-call-for-shift-select 'backward-word))
  18842. (t (org-shiftselect-error))))
  18843. (defun org-shiftcontrolup (&optional n)
  18844. "Change timestamps synchronously up in CLOCK log lines.
  18845. Optional argument N tells to change by that many units."
  18846. (interactive "P")
  18847. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18848. (let (org-support-shift-select)
  18849. (org-clock-timestamps-up n))
  18850. (user-error "Not at a clock log")))
  18851. (defun org-shiftcontroldown (&optional n)
  18852. "Change timestamps synchronously down in CLOCK log lines.
  18853. Optional argument N tells to change by that many units."
  18854. (interactive "P")
  18855. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18856. (let (org-support-shift-select)
  18857. (org-clock-timestamps-down n))
  18858. (user-error "Not at a clock log")))
  18859. (defun org-increase-number-at-point (&optional inc)
  18860. "Increment the number at point.
  18861. With an optional prefix numeric argument INC, increment using
  18862. this numeric value."
  18863. (interactive "p")
  18864. (if (not (number-at-point))
  18865. (user-error "Not on a number")
  18866. (unless inc (setq inc 1))
  18867. (let ((pos (point))
  18868. (beg (skip-chars-backward "-+^/*0-9eE."))
  18869. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  18870. (setq nap (buffer-substring-no-properties
  18871. (+ pos beg) (+ pos beg end)))
  18872. (delete-region (+ pos beg) (+ pos beg end))
  18873. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  18874. (when (org-at-table-p)
  18875. (org-table-align)
  18876. (org-table-end-of-field 1))))
  18877. (defun org-decrease-number-at-point (&optional inc)
  18878. "Decrement the number at point.
  18879. With an optional prefix numeric argument INC, decrement using
  18880. this numeric value."
  18881. (interactive "p")
  18882. (org-increase-number-at-point (- inc)))
  18883. (defun org-ctrl-c-ret ()
  18884. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18885. (interactive)
  18886. (cond
  18887. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18888. (t (call-interactively 'org-insert-heading))))
  18889. (defun org-find-visible ()
  18890. (let ((s (point)))
  18891. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18892. (get-char-property s 'invisible)))
  18893. s))
  18894. (defun org-find-invisible ()
  18895. (let ((s (point)))
  18896. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18897. (not (get-char-property s 'invisible))))
  18898. s))
  18899. (defun org-copy-visible (beg end)
  18900. "Copy the visible parts of the region."
  18901. (interactive "r")
  18902. (let (snippets s)
  18903. (save-excursion
  18904. (save-restriction
  18905. (narrow-to-region beg end)
  18906. (setq s (goto-char (point-min)))
  18907. (while (not (= (point) (point-max)))
  18908. (goto-char (org-find-invisible))
  18909. (push (buffer-substring s (point)) snippets)
  18910. (setq s (goto-char (org-find-visible))))))
  18911. (kill-new (apply 'concat (nreverse snippets)))))
  18912. (defun org-copy-special ()
  18913. "Copy region in table or copy current subtree.
  18914. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18915. See the individual commands for more information."
  18916. (interactive)
  18917. (call-interactively
  18918. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18919. (defun org-cut-special ()
  18920. "Cut region in table or cut current subtree.
  18921. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18922. See the individual commands for more information."
  18923. (interactive)
  18924. (call-interactively
  18925. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18926. (defun org-paste-special (arg)
  18927. "Paste rectangular region into table, or past subtree relative to level.
  18928. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18929. See the individual commands for more information."
  18930. (interactive "P")
  18931. (if (org-at-table-p)
  18932. (org-table-paste-rectangle)
  18933. (org-paste-subtree arg)))
  18934. (defsubst org-in-fixed-width-region-p ()
  18935. "Is point in a fixed-width region?"
  18936. (save-match-data
  18937. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18938. (defun org-edit-special (&optional arg)
  18939. "Call a special editor for the element at point.
  18940. When at a table, call the formula editor with `org-table-edit-formulas'.
  18941. When in a source code block, call `org-edit-src-code'.
  18942. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18943. When in an export block, call `org-edit-export-block'.
  18944. When at an #+INCLUDE keyword, visit the included file.
  18945. On a link, call `ffap' to visit the link at point.
  18946. Otherwise, return a user error."
  18947. (interactive "P")
  18948. (let ((element (org-element-at-point)))
  18949. (assert (not buffer-read-only) nil
  18950. "Buffer is read-only: %s" (buffer-name))
  18951. (case (org-element-type element)
  18952. (src-block
  18953. (if (not arg) (org-edit-src-code)
  18954. (let* ((info (org-babel-get-src-block-info))
  18955. (lang (nth 0 info))
  18956. (params (nth 2 info))
  18957. (session (cdr (assq :session params))))
  18958. (if (not session) (org-edit-src-code)
  18959. ;; At a src-block with a session and function called with
  18960. ;; an ARG: switch to the buffer related to the inferior
  18961. ;; process.
  18962. (switch-to-buffer
  18963. (funcall (intern (concat "org-babel-prep-session:" lang))
  18964. session params))))))
  18965. (keyword
  18966. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18967. (org-open-link-from-string
  18968. (format "[[%s]]"
  18969. (expand-file-name
  18970. (let ((value (org-element-property :value element)))
  18971. (cond ((not (org-string-nw-p value))
  18972. (user-error "No file to edit"))
  18973. ((string-match "\\`\"\\(.*?\\)\"" value)
  18974. (match-string 1 value))
  18975. ((string-match "\\`[^ \t\"]\\S-*" value)
  18976. (match-string 0 value))
  18977. (t (user-error "No valid file specified")))))))
  18978. (user-error "No special environment to edit here")))
  18979. (table
  18980. (if (eq (org-element-property :type element) 'table.el)
  18981. (org-edit-table.el)
  18982. (call-interactively 'org-table-edit-formulas)))
  18983. ;; Only Org tables contain `table-row' type elements.
  18984. (table-row (call-interactively 'org-table-edit-formulas))
  18985. ((example-block src-block) (org-edit-src-code))
  18986. (export-block (org-edit-export-block))
  18987. (fixed-width (org-edit-fixed-width-region))
  18988. (otherwise
  18989. ;; No notable element at point. Though, we may be at a link,
  18990. ;; which is an object. Thus, scan deeper.
  18991. (if (eq (org-element-type (org-element-context element)) 'link)
  18992. (call-interactively 'ffap)
  18993. (user-error "No special environment to edit here"))))))
  18994. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18995. (defun org-ctrl-c-ctrl-c (&optional arg)
  18996. "Set tags in headline, or update according to changed information at point.
  18997. This command does many different things, depending on context:
  18998. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18999. this is what we do.
  19000. - If the cursor is on a statistics cookie, update it.
  19001. - If the cursor is in a headline, prompt for tags and insert them
  19002. into the current line, aligned to `org-tags-column'. When called
  19003. with prefix arg, realign all tags in the current buffer.
  19004. - If the cursor is in one of the special #+KEYWORD lines, this
  19005. triggers scanning the buffer for these lines and updating the
  19006. information.
  19007. - If the cursor is inside a table, realign the table. This command
  19008. works even if the automatic table editor has been turned off.
  19009. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19010. the entire table.
  19011. - If the cursor is at a footnote reference or definition, jump to
  19012. the corresponding definition or references, respectively.
  19013. - If the cursor is a the beginning of a dynamic block, update it.
  19014. - If the current buffer is a capture buffer, close note and file it.
  19015. - If the cursor is on a <<<target>>>, update radio targets and
  19016. corresponding links in this buffer.
  19017. - If the cursor is on a numbered item in a plain list, renumber the
  19018. ordered list.
  19019. - If the cursor is on a checkbox, toggle it.
  19020. - If the cursor is on a code block, evaluate it. The variable
  19021. `org-confirm-babel-evaluate' can be used to control prompting
  19022. before code block evaluation, by default every code block
  19023. evaluation requires confirmation. Code block evaluation can be
  19024. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19025. (interactive "P")
  19026. (cond
  19027. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19028. org-occur-highlights)
  19029. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19030. (org-remove-occur-highlights)
  19031. (message "Temporary highlights/overlays removed from current buffer"))
  19032. ((and (local-variable-p 'org-finish-function (current-buffer))
  19033. (fboundp org-finish-function))
  19034. (funcall org-finish-function))
  19035. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19036. (t
  19037. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19038. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19039. (user-error "C-c C-c can do nothing useful at this location"))
  19040. (let* ((context (org-element-context))
  19041. (type (org-element-type context)))
  19042. (case type
  19043. ;; When at a link, act according to the parent instead.
  19044. (link (setq context (org-element-property :parent context))
  19045. (setq type (org-element-type context)))
  19046. ;; Unsupported object types: refer to the first supported
  19047. ;; element or object containing it.
  19048. ((bold code entity export-snippet inline-babel-call inline-src-block
  19049. italic latex-fragment line-break macro strike-through subscript
  19050. superscript underline verbatim)
  19051. (setq context
  19052. (org-element-lineage
  19053. context '(radio-target paragraph verse-block table-cell)))))
  19054. ;; For convenience: at the first line of a paragraph on the
  19055. ;; same line as an item, apply function on that item instead.
  19056. (when (eq type 'paragraph)
  19057. (let ((parent (org-element-property :parent context)))
  19058. (when (and (eq (org-element-type parent) 'item)
  19059. (= (line-beginning-position)
  19060. (org-element-property :begin parent)))
  19061. (setq context parent type 'item))))
  19062. ;; Act according to type of element or object at point.
  19063. (case type
  19064. (clock (org-clock-update-time-maybe))
  19065. (dynamic-block
  19066. (save-excursion
  19067. (goto-char (org-element-property :post-affiliated context))
  19068. (org-update-dblock)))
  19069. (footnote-definition
  19070. (goto-char (org-element-property :post-affiliated context))
  19071. (call-interactively 'org-footnote-action))
  19072. (footnote-reference (call-interactively 'org-footnote-action))
  19073. ((headline inlinetask)
  19074. (save-excursion (goto-char (org-element-property :begin context))
  19075. (call-interactively 'org-set-tags)))
  19076. (item
  19077. ;; At an item: a double C-u set checkbox to "[-]"
  19078. ;; unconditionally, whereas a single one will toggle its
  19079. ;; presence. Without an universal argument, if the item
  19080. ;; has a checkbox, toggle it. Otherwise repair the list.
  19081. (let* ((box (org-element-property :checkbox context))
  19082. (struct (org-element-property :structure context))
  19083. (old-struct (copy-tree struct))
  19084. (parents (org-list-parents-alist struct))
  19085. (prevs (org-list-prevs-alist struct))
  19086. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19087. (org-list-set-checkbox
  19088. (org-element-property :begin context) struct
  19089. (cond ((equal arg '(16)) "[-]")
  19090. ((and (not box) (equal arg '(4))) "[ ]")
  19091. ((or (not box) (equal arg '(4))) nil)
  19092. ((eq box 'on) "[ ]")
  19093. (t "[X]")))
  19094. ;; Mimic `org-list-write-struct' but with grabbing
  19095. ;; a return value from `org-list-struct-fix-box'.
  19096. (org-list-struct-fix-ind struct parents 2)
  19097. (org-list-struct-fix-item-end struct)
  19098. (org-list-struct-fix-bul struct prevs)
  19099. (org-list-struct-fix-ind struct parents)
  19100. (let ((block-item
  19101. (org-list-struct-fix-box struct parents prevs orderedp)))
  19102. (if (and box (equal struct old-struct))
  19103. (if (equal arg '(16))
  19104. (message "Checkboxes already reset")
  19105. (user-error "Cannot toggle this checkbox: %s"
  19106. (if (eq box 'on)
  19107. "all subitems checked"
  19108. "unchecked subitems")))
  19109. (org-list-struct-apply-struct struct old-struct)
  19110. (org-update-checkbox-count-maybe))
  19111. (when block-item
  19112. (message "Checkboxes were removed due to empty box at line %d"
  19113. (org-current-line block-item))))))
  19114. (keyword
  19115. (let ((org-inhibit-startup-visibility-stuff t)
  19116. (org-startup-align-all-tables nil))
  19117. (when (boundp 'org-table-coordinate-overlays)
  19118. (mapc 'delete-overlay org-table-coordinate-overlays)
  19119. (setq org-table-coordinate-overlays nil))
  19120. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19121. (message "Local setup has been refreshed"))
  19122. (plain-list
  19123. ;; At a plain list, with a double C-u argument, set
  19124. ;; checkboxes of each item to "[-]", whereas a single one
  19125. ;; will toggle their presence according to the state of the
  19126. ;; first item in the list. Without an argument, repair the
  19127. ;; list.
  19128. (let* ((begin (org-element-property :contents-begin context))
  19129. (beginm (move-marker (make-marker) begin))
  19130. (struct (org-element-property :structure context))
  19131. (old-struct (copy-tree struct))
  19132. (first-box (save-excursion
  19133. (goto-char begin)
  19134. (looking-at org-list-full-item-re)
  19135. (match-string-no-properties 3)))
  19136. (new-box (cond ((equal arg '(16)) "[-]")
  19137. ((equal arg '(4)) (unless first-box "[ ]"))
  19138. ((equal first-box "[X]") "[ ]")
  19139. (t "[X]"))))
  19140. (cond
  19141. (arg
  19142. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19143. (org-list-get-all-items
  19144. begin struct (org-list-prevs-alist struct))))
  19145. ((and first-box (eq (point) begin))
  19146. ;; For convenience, when point is at bol on the first
  19147. ;; item of the list and no argument is provided, simply
  19148. ;; toggle checkbox of that item, if any.
  19149. (org-list-set-checkbox begin struct new-box)))
  19150. (org-list-write-struct
  19151. struct (org-list-parents-alist struct) old-struct)
  19152. (org-update-checkbox-count-maybe)
  19153. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19154. ((property-drawer node-property)
  19155. (call-interactively 'org-property-action))
  19156. ((radio-target target)
  19157. (call-interactively 'org-update-radio-target-regexp))
  19158. (statistics-cookie
  19159. (call-interactively 'org-update-statistics-cookies))
  19160. ((table table-cell table-row)
  19161. ;; At a table, recalculate every field and align it. Also
  19162. ;; send the table if necessary. If the table has
  19163. ;; a `table.el' type, just give up. At a table row or
  19164. ;; cell, maybe recalculate line but always align table.
  19165. (if (eq (org-element-property :type context) 'table.el)
  19166. (message "Use C-c ' to edit table.el tables")
  19167. (let ((org-enable-table-editor t))
  19168. (if (or (eq type 'table)
  19169. ;; Check if point is at a TBLFM line.
  19170. (and (eq type 'table-row)
  19171. (= (point) (org-element-property :end context))))
  19172. (save-excursion
  19173. (if (org-at-TBLFM-p)
  19174. (progn (require 'org-table)
  19175. (org-table-calc-current-TBLFM))
  19176. (goto-char (org-element-property :contents-begin context))
  19177. (org-call-with-arg 'org-table-recalculate (or arg t))
  19178. (orgtbl-send-table 'maybe)))
  19179. (org-table-maybe-eval-formula)
  19180. (cond (arg (call-interactively 'org-table-recalculate))
  19181. ((org-table-maybe-recalculate-line))
  19182. (t (org-table-align)))))))
  19183. (timestamp (org-timestamp-change 0 'day))
  19184. (otherwise
  19185. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19186. (user-error
  19187. "C-c C-c can do nothing useful at this location")))))))))
  19188. (defun org-mode-restart ()
  19189. (interactive)
  19190. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19191. (funcall major-mode)
  19192. (hack-local-variables)
  19193. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19194. (org-indent-mode -1)))
  19195. (message "%s restarted" major-mode))
  19196. (defun org-kill-note-or-show-branches ()
  19197. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19198. (interactive)
  19199. (if (not org-finish-function)
  19200. (progn
  19201. (hide-subtree)
  19202. (call-interactively 'show-branches))
  19203. (let ((org-note-abort t))
  19204. (funcall org-finish-function))))
  19205. (defun org-open-line (n)
  19206. "Insert a new row in tables, call `open-line' elsewhere.
  19207. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19208. (interactive "*p")
  19209. (cond
  19210. ((not org-special-ctrl-o)
  19211. (open-line n))
  19212. ((org-at-table-p)
  19213. (org-table-insert-row))
  19214. (t
  19215. (open-line n))))
  19216. (defun org-return (&optional indent)
  19217. "Goto next table row or insert a newline.
  19218. Calls `org-table-next-row' or `newline', depending on context.
  19219. When optional INDENT argument is non-nil, call
  19220. `newline-and-indent' instead of `newline'.
  19221. When `org-return-follows-link' is non-nil and point is on
  19222. a timestamp or a link, call `org-open-at-point'. However, it
  19223. will not happen if point is in a table or on a \"dead\"
  19224. object (e.g., within a comment). In these case, you need to use
  19225. `org-open-at-point' directly."
  19226. (interactive)
  19227. (if (and (save-excursion
  19228. (beginning-of-line)
  19229. (looking-at org-todo-line-regexp))
  19230. (match-beginning 3)
  19231. (>= (point) (match-beginning 3)))
  19232. ;; Point is on headline tags. Do not break them: add a newline
  19233. ;; after the headline instead.
  19234. (progn (org-show-entry)
  19235. (end-of-line)
  19236. (if indent (newline-and-indent) (newline)))
  19237. (let* ((context (if org-return-follows-link (org-element-context)
  19238. (org-element-at-point)))
  19239. (type (org-element-type context)))
  19240. (cond
  19241. ;; In a table, call `org-table-next-row'.
  19242. ((or (and (eq type 'table)
  19243. (>= (point) (org-element-property :contents-begin context))
  19244. (< (point) (org-element-property :contents-end context)))
  19245. (org-element-lineage context '(table-row table-cell) t))
  19246. (org-table-justify-field-maybe)
  19247. (call-interactively #'org-table-next-row))
  19248. ;; On a link or a timestamp but not on white spaces after it,
  19249. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19250. ((and org-return-follows-link
  19251. (memq type '(link timestamp))
  19252. (< (point)
  19253. (save-excursion (goto-char (org-element-property :end context))
  19254. (skip-chars-backward " \t")
  19255. (point))))
  19256. (call-interactively #'org-open-at-point))
  19257. ;; In a list, make sure indenting keeps trailing text within.
  19258. ((and indent
  19259. (not (eolp))
  19260. (org-element-lineage context '(item)))
  19261. (let ((trailing-data
  19262. (delete-and-extract-region (point) (line-end-position))))
  19263. (newline-and-indent)
  19264. (save-excursion (insert trailing-data))))
  19265. (t (if indent (newline-and-indent) (newline)))))))
  19266. (defun org-return-indent ()
  19267. "Goto next table row or insert a newline and indent.
  19268. Calls `org-table-next-row' or `newline-and-indent', depending on
  19269. context. See the individual commands for more information."
  19270. (interactive)
  19271. (org-return t))
  19272. (defun org-ctrl-c-star ()
  19273. "Compute table, or change heading status of lines.
  19274. Calls `org-table-recalculate' or `org-toggle-heading',
  19275. depending on context."
  19276. (interactive)
  19277. (cond
  19278. ((org-at-table-p)
  19279. (call-interactively 'org-table-recalculate))
  19280. (t
  19281. ;; Convert all lines in region to list items
  19282. (call-interactively 'org-toggle-heading))))
  19283. (defun org-ctrl-c-minus ()
  19284. "Insert separator line in table or modify bullet status of line.
  19285. Also turns a plain line or a region of lines into list items.
  19286. Calls `org-table-insert-hline', `org-toggle-item', or
  19287. `org-cycle-list-bullet', depending on context."
  19288. (interactive)
  19289. (cond
  19290. ((org-at-table-p)
  19291. (call-interactively 'org-table-insert-hline))
  19292. ((org-region-active-p)
  19293. (call-interactively 'org-toggle-item))
  19294. ((org-in-item-p)
  19295. (call-interactively 'org-cycle-list-bullet))
  19296. (t
  19297. (call-interactively 'org-toggle-item))))
  19298. (defun org-toggle-item (arg)
  19299. "Convert headings or normal lines to items, items to normal lines.
  19300. If there is no active region, only the current line is considered.
  19301. If the first non blank line in the region is a headline, convert
  19302. all headlines to items, shifting text accordingly.
  19303. If it is an item, convert all items to normal lines.
  19304. If it is normal text, change region into a list of items.
  19305. With a prefix argument ARG, change the region in a single item."
  19306. (interactive "P")
  19307. (let ((shift-text
  19308. (function
  19309. ;; Shift text in current section to IND, from point to END.
  19310. ;; The function leaves point to END line.
  19311. (lambda (ind end)
  19312. (let ((min-i 1000) (end (copy-marker end)))
  19313. ;; First determine the minimum indentation (MIN-I) of
  19314. ;; the text.
  19315. (save-excursion
  19316. (catch 'exit
  19317. (while (< (point) end)
  19318. (let ((i (org-get-indentation)))
  19319. (cond
  19320. ;; Skip blank lines and inline tasks.
  19321. ((looking-at "^[ \t]*$"))
  19322. ((looking-at org-outline-regexp-bol))
  19323. ;; We can't find less than 0 indentation.
  19324. ((zerop i) (throw 'exit (setq min-i 0)))
  19325. ((< i min-i) (setq min-i i))))
  19326. (forward-line))))
  19327. ;; Then indent each line so that a line indented to
  19328. ;; MIN-I becomes indented to IND. Ignore blank lines
  19329. ;; and inline tasks in the process.
  19330. (let ((delta (- ind min-i)))
  19331. (while (< (point) end)
  19332. (unless (or (looking-at "^[ \t]*$")
  19333. (looking-at org-outline-regexp-bol))
  19334. (org-indent-line-to (+ (org-get-indentation) delta)))
  19335. (forward-line)))))))
  19336. (skip-blanks
  19337. (function
  19338. ;; Return beginning of first non-blank line, starting from
  19339. ;; line at POS.
  19340. (lambda (pos)
  19341. (save-excursion
  19342. (goto-char pos)
  19343. (skip-chars-forward " \r\t\n")
  19344. (point-at-bol)))))
  19345. beg end)
  19346. ;; Determine boundaries of changes.
  19347. (if (org-region-active-p)
  19348. (setq beg (funcall skip-blanks (region-beginning))
  19349. end (copy-marker (region-end)))
  19350. (setq beg (funcall skip-blanks (point-at-bol))
  19351. end (copy-marker (point-at-eol))))
  19352. ;; Depending on the starting line, choose an action on the text
  19353. ;; between BEG and END.
  19354. (org-with-limited-levels
  19355. (save-excursion
  19356. (goto-char beg)
  19357. (cond
  19358. ;; Case 1. Start at an item: de-itemize. Note that it only
  19359. ;; happens when a region is active: `org-ctrl-c-minus'
  19360. ;; would call `org-cycle-list-bullet' otherwise.
  19361. ((org-at-item-p)
  19362. (while (< (point) end)
  19363. (when (org-at-item-p)
  19364. (skip-chars-forward " \t")
  19365. (delete-region (point) (match-end 0)))
  19366. (forward-line)))
  19367. ;; Case 2. Start at an heading: convert to items.
  19368. ((org-at-heading-p)
  19369. (let* ((bul (org-list-bullet-string "-"))
  19370. (bul-len (length bul))
  19371. (done (org-entry-is-done-p))
  19372. (todo (org-entry-is-todo-p))
  19373. ;; Indentation of the first heading. It should be
  19374. ;; relative to the indentation of its parent, if any.
  19375. (start-ind (save-excursion
  19376. (cond
  19377. ((not org-adapt-indentation) 0)
  19378. ((not (outline-previous-heading)) 0)
  19379. (t (length (match-string 0))))))
  19380. ;; Level of first heading. Further headings will be
  19381. ;; compared to it to determine hierarchy in the list.
  19382. (ref-level (org-reduced-level (org-outline-level))))
  19383. (when (or done todo) (org-todo ""))
  19384. (while (< (point) end)
  19385. (let* ((level (org-reduced-level (org-outline-level)))
  19386. (delta (max 0 (- level ref-level))))
  19387. ;; If current headline is less indented than the first
  19388. ;; one, set it as reference, in order to preserve
  19389. ;; subtrees.
  19390. (when (< level ref-level) (setq ref-level level))
  19391. (replace-match bul t t)
  19392. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19393. (when (or done todo)
  19394. (let* ((struct (org-list-struct))
  19395. (old (copy-tree struct)))
  19396. (org-list-set-checkbox (line-beginning-position)
  19397. struct
  19398. (if done "[X]" "[ ]"))
  19399. (org-list-write-struct struct
  19400. (org-list-parents-alist struct)
  19401. old)))
  19402. ;; Ensure all text down to END (or SECTION-END) belongs
  19403. ;; to the newly created item.
  19404. (let ((section-end (save-excursion
  19405. (or (outline-next-heading) (point)))))
  19406. (forward-line)
  19407. (funcall shift-text
  19408. (+ start-ind (* (1+ delta) bul-len))
  19409. (min end section-end)))))))
  19410. ;; Case 3. Normal line with ARG: make the first line of region
  19411. ;; an item, and shift indentation of others lines to
  19412. ;; set them as item's body.
  19413. (arg (let* ((bul (org-list-bullet-string "-"))
  19414. (bul-len (length bul))
  19415. (ref-ind (org-get-indentation)))
  19416. (skip-chars-forward " \t")
  19417. (insert bul)
  19418. (forward-line)
  19419. (while (< (point) end)
  19420. ;; Ensure that lines less indented than first one
  19421. ;; still get included in item body.
  19422. (funcall shift-text
  19423. (+ ref-ind bul-len)
  19424. (min end (save-excursion (or (outline-next-heading)
  19425. (point)))))
  19426. (forward-line))))
  19427. ;; Case 4. Normal line without ARG: turn each non-item line
  19428. ;; into an item.
  19429. (t
  19430. (while (< (point) end)
  19431. (unless (or (org-at-heading-p) (org-at-item-p))
  19432. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19433. (replace-match
  19434. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19435. (forward-line))))))))
  19436. (defun org-toggle-heading (&optional nstars)
  19437. "Convert headings to normal text, or items or text to headings.
  19438. If there is no active region, only convert the current line.
  19439. With a \\[universal-argument] prefix, convert the whole list at
  19440. point into heading.
  19441. In a region:
  19442. - If the first non blank line is a headline, remove the stars
  19443. from all headlines in the region.
  19444. - If it is a normal line, turn each and every normal line (i.e.,
  19445. not an heading or an item) in the region into headings. If you
  19446. want to convert only the first line of this region, use one
  19447. universal prefix argument.
  19448. - If it is a plain list item, turn all plain list items into headings.
  19449. When converting a line into a heading, the number of stars is chosen
  19450. such that the lines become children of the current entry. However,
  19451. when a numeric prefix argument is given, its value determines the
  19452. number of stars to add."
  19453. (interactive "P")
  19454. (let ((skip-blanks
  19455. (function
  19456. ;; Return beginning of first non-blank line, starting from
  19457. ;; line at POS.
  19458. (lambda (pos)
  19459. (save-excursion
  19460. (goto-char pos)
  19461. (while (org-at-comment-p) (forward-line))
  19462. (skip-chars-forward " \r\t\n")
  19463. (point-at-bol)))))
  19464. beg end toggled)
  19465. ;; Determine boundaries of changes. If a universal prefix has
  19466. ;; been given, put the list in a region. If region ends at a bol,
  19467. ;; do not consider the last line to be in the region.
  19468. (when (and current-prefix-arg (org-at-item-p))
  19469. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19470. (org-mark-element))
  19471. (if (org-region-active-p)
  19472. (setq beg (funcall skip-blanks (region-beginning))
  19473. end (copy-marker (save-excursion
  19474. (goto-char (region-end))
  19475. (if (bolp) (point) (point-at-eol)))))
  19476. (setq beg (funcall skip-blanks (point-at-bol))
  19477. end (copy-marker (point-at-eol))))
  19478. ;; Ensure inline tasks don't count as headings.
  19479. (org-with-limited-levels
  19480. (save-excursion
  19481. (goto-char beg)
  19482. (cond
  19483. ;; Case 1. Started at an heading: de-star headings.
  19484. ((org-at-heading-p)
  19485. (while (< (point) end)
  19486. (when (org-at-heading-p t)
  19487. (looking-at org-outline-regexp) (replace-match "")
  19488. (setq toggled t))
  19489. (forward-line)))
  19490. ;; Case 2. Started at an item: change items into headlines.
  19491. ;; One star will be added by `org-list-to-subtree'.
  19492. ((org-at-item-p)
  19493. (while (< (point) end)
  19494. (when (org-at-item-p)
  19495. ;; Pay attention to cases when region ends before list.
  19496. (let* ((struct (org-list-struct))
  19497. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19498. (save-restriction
  19499. (narrow-to-region (point) list-end)
  19500. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19501. (setq toggled t))
  19502. (forward-line)))
  19503. ;; Case 3. Started at normal text: make every line an heading,
  19504. ;; skipping headlines and items.
  19505. (t (let* ((stars
  19506. (make-string
  19507. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19508. (add-stars
  19509. (cond (nstars "") ; stars from prefix only
  19510. ((equal stars "") "*") ; before first heading
  19511. (org-odd-levels-only "**") ; inside heading, odd
  19512. (t "*"))) ; inside heading, oddeven
  19513. (rpl (concat stars add-stars " "))
  19514. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19515. (while (< (point) (if (equal nstars '(4)) lend end))
  19516. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19517. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19518. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19519. (forward-line)))))))
  19520. (unless toggled (message "Cannot toggle heading from here"))))
  19521. (defun org-meta-return (&optional arg)
  19522. "Insert a new heading or wrap a region in a table.
  19523. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19524. on context. See the individual commands for more information."
  19525. (interactive "P")
  19526. (org-check-before-invisible-edit 'insert)
  19527. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19528. (let* ((element (org-element-at-point))
  19529. (type (org-element-type element)))
  19530. (when (eq type 'table-row)
  19531. (setq element (org-element-property :parent element))
  19532. (setq type 'table))
  19533. (if (and (eq type 'table)
  19534. (eq (org-element-property :type element) 'org)
  19535. (>= (point) (org-element-property :contents-begin element))
  19536. (< (point) (org-element-property :contents-end element)))
  19537. (call-interactively 'org-table-wrap-region)
  19538. (call-interactively 'org-insert-heading)))))
  19539. ;;; Menu entries
  19540. (defsubst org-in-subtree-not-table-p ()
  19541. "Are we in a subtree and not in a table?"
  19542. (and (not (org-before-first-heading-p))
  19543. (not (org-at-table-p))))
  19544. ;; Define the Org-mode menus
  19545. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19546. '("Tbl"
  19547. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19548. ["Next Field" org-cycle (org-at-table-p)]
  19549. ["Previous Field" org-shifttab (org-at-table-p)]
  19550. ["Next Row" org-return (org-at-table-p)]
  19551. "--"
  19552. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19553. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19554. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19555. "--"
  19556. ("Column"
  19557. ["Move Column Left" org-metaleft (org-at-table-p)]
  19558. ["Move Column Right" org-metaright (org-at-table-p)]
  19559. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19560. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19561. ("Row"
  19562. ["Move Row Up" org-metaup (org-at-table-p)]
  19563. ["Move Row Down" org-metadown (org-at-table-p)]
  19564. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19565. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19566. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19567. "--"
  19568. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19569. ("Rectangle"
  19570. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19571. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19572. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19573. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19574. "--"
  19575. ("Calculate"
  19576. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19577. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19578. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19579. "--"
  19580. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19581. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19582. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19583. "--"
  19584. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19585. "--"
  19586. ["Sum Column/Rectangle" org-table-sum
  19587. (or (org-at-table-p) (org-region-active-p))]
  19588. ["Which Column?" org-table-current-column (org-at-table-p)])
  19589. ["Debug Formulas"
  19590. org-table-toggle-formula-debugger
  19591. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19592. ["Show Col/Row Numbers"
  19593. org-table-toggle-coordinate-overlays
  19594. :style toggle
  19595. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19596. "--"
  19597. ["Create" org-table-create (and (not (org-at-table-p))
  19598. org-enable-table-editor)]
  19599. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19600. ["Import from File" org-table-import (not (org-at-table-p))]
  19601. ["Export to File" org-table-export (org-at-table-p)]
  19602. "--"
  19603. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19604. "--"
  19605. ("Plot"
  19606. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19607. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19608. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19609. '("Org"
  19610. ("Show/Hide"
  19611. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19612. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19613. ["Sparse Tree..." org-sparse-tree t]
  19614. ["Reveal Context" org-reveal t]
  19615. ["Show All" show-all t]
  19616. "--"
  19617. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19618. "--"
  19619. ["New Heading" org-insert-heading t]
  19620. ("Navigate Headings"
  19621. ["Up" outline-up-heading t]
  19622. ["Next" outline-next-visible-heading t]
  19623. ["Previous" outline-previous-visible-heading t]
  19624. ["Next Same Level" outline-forward-same-level t]
  19625. ["Previous Same Level" outline-backward-same-level t]
  19626. "--"
  19627. ["Jump" org-goto t])
  19628. ("Edit Structure"
  19629. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19630. "--"
  19631. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19632. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19633. "--"
  19634. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19635. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19636. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19637. "--"
  19638. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19639. "--"
  19640. ["Copy visible text" org-copy-visible t]
  19641. "--"
  19642. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19643. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19644. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19645. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19646. "--"
  19647. ["Sort Region/Children" org-sort t]
  19648. "--"
  19649. ["Convert to odd levels" org-convert-to-odd-levels t]
  19650. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19651. ("Editing"
  19652. ["Emphasis..." org-emphasize t]
  19653. ["Edit Source Example" org-edit-special t]
  19654. "--"
  19655. ["Footnote new/jump" org-footnote-action t]
  19656. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19657. ("Archive"
  19658. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19659. "--"
  19660. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19661. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19662. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19663. )
  19664. "--"
  19665. ("Hyperlinks"
  19666. ["Store Link (Global)" org-store-link t]
  19667. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19668. ["Insert Link" org-insert-link t]
  19669. ["Follow Link" org-open-at-point t]
  19670. "--"
  19671. ["Next link" org-next-link t]
  19672. ["Previous link" org-previous-link t]
  19673. "--"
  19674. ["Descriptive Links"
  19675. org-toggle-link-display
  19676. :style radio
  19677. :selected org-descriptive-links
  19678. ]
  19679. ["Literal Links"
  19680. org-toggle-link-display
  19681. :style radio
  19682. :selected (not org-descriptive-links)])
  19683. "--"
  19684. ("TODO Lists"
  19685. ["TODO/DONE/-" org-todo t]
  19686. ("Select keyword"
  19687. ["Next keyword" org-shiftright (org-at-heading-p)]
  19688. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19689. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19690. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19691. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19692. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19693. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19694. "--"
  19695. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19696. :selected org-enforce-todo-dependencies :style toggle :active t]
  19697. "Settings for tree at point"
  19698. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19699. :selected (org-entry-get nil "ORDERED")
  19700. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19701. ["Do Children parallel" org-toggle-ordered-property :style radio
  19702. :selected (not (org-entry-get nil "ORDERED"))
  19703. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19704. "--"
  19705. ["Set Priority" org-priority t]
  19706. ["Priority Up" org-shiftup t]
  19707. ["Priority Down" org-shiftdown t]
  19708. "--"
  19709. ["Get news from all feeds" org-feed-update-all t]
  19710. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19711. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19712. ("TAGS and Properties"
  19713. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19714. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19715. "--"
  19716. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19717. ["Column view of properties" org-columns t]
  19718. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19719. ("Dates and Scheduling"
  19720. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19721. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19722. ("Change Date"
  19723. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19724. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19725. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19726. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19727. ["Compute Time Range" org-evaluate-time-range t]
  19728. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19729. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19730. "--"
  19731. ["Custom time format" org-toggle-time-stamp-overlays
  19732. :style radio :selected org-display-custom-times]
  19733. "--"
  19734. ["Goto Calendar" org-goto-calendar t]
  19735. ["Date from Calendar" org-date-from-calendar t]
  19736. "--"
  19737. ["Start/Restart Timer" org-timer-start t]
  19738. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19739. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19740. ["Insert Timer String" org-timer t]
  19741. ["Insert Timer Item" org-timer-item t])
  19742. ("Logging work"
  19743. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19744. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19745. ["Clock out" org-clock-out t]
  19746. ["Clock cancel" org-clock-cancel t]
  19747. "--"
  19748. ["Mark as default task" org-clock-mark-default-task t]
  19749. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19750. ["Goto running clock" org-clock-goto t]
  19751. "--"
  19752. ["Display times" org-clock-display t]
  19753. ["Create clock table" org-clock-report t]
  19754. "--"
  19755. ["Record DONE time"
  19756. (progn (setq org-log-done (not org-log-done))
  19757. (message "Switching to %s will %s record a timestamp"
  19758. (car org-done-keywords)
  19759. (if org-log-done "automatically" "not")))
  19760. :style toggle :selected org-log-done])
  19761. "--"
  19762. ["Agenda Command..." org-agenda t]
  19763. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19764. ("File List for Agenda")
  19765. ("Special views current file"
  19766. ["TODO Tree" org-show-todo-tree t]
  19767. ["Check Deadlines" org-check-deadlines t]
  19768. ["Timeline" org-timeline t]
  19769. ["Tags/Property tree" org-match-sparse-tree t])
  19770. "--"
  19771. ["Export/Publish..." org-export-dispatch t]
  19772. ("LaTeX"
  19773. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19774. :selected org-cdlatex-mode]
  19775. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19776. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19777. ["Modify math symbol" org-cdlatex-math-modify
  19778. (org-inside-LaTeX-fragment-p)]
  19779. ["Insert citation" org-reftex-citation t]
  19780. "--"
  19781. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19782. "--"
  19783. ("MobileOrg"
  19784. ["Push Files and Views" org-mobile-push t]
  19785. ["Get Captured and Flagged" org-mobile-pull t]
  19786. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19787. "--"
  19788. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19789. "--"
  19790. ("Documentation"
  19791. ["Show Version" org-version t]
  19792. ["Info Documentation" org-info t])
  19793. ("Customize"
  19794. ["Browse Org Group" org-customize t]
  19795. "--"
  19796. ["Expand This Menu" org-create-customize-menu
  19797. (fboundp 'customize-menu-create)])
  19798. ["Send bug report" org-submit-bug-report t]
  19799. "--"
  19800. ("Refresh/Reload"
  19801. ["Refresh setup current buffer" org-mode-restart t]
  19802. ["Reload Org (after update)" org-reload t]
  19803. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19804. ))
  19805. (defun org-info (&optional node)
  19806. "Read documentation for Org-mode in the info system.
  19807. With optional NODE, go directly to that node."
  19808. (interactive)
  19809. (info (format "(org)%s" (or node ""))))
  19810. ;;;###autoload
  19811. (defun org-submit-bug-report ()
  19812. "Submit a bug report on Org-mode via mail.
  19813. Don't hesitate to report any problems or inaccurate documentation.
  19814. If you don't have setup sending mail from (X)Emacs, please copy the
  19815. output buffer into your mail program, as it gives us important
  19816. information about your Org-mode version and configuration."
  19817. (interactive)
  19818. (require 'reporter)
  19819. (org-load-modules-maybe)
  19820. (org-require-autoloaded-modules)
  19821. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19822. (reporter-submit-bug-report
  19823. "emacs-orgmode@gnu.org"
  19824. (org-version nil 'full)
  19825. (let (list)
  19826. (save-window-excursion
  19827. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19828. (delete-other-windows)
  19829. (erase-buffer)
  19830. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19831. We would like to add your full Org-mode and Outline configuration to the
  19832. bug report. This greatly simplifies the work of the maintainer and
  19833. other experts on the mailing list.
  19834. HOWEVER, some variables you have customized may contain private
  19835. information. The names of customers, colleagues, or friends, might
  19836. appear in the form of file names, tags, todo states, or search strings.
  19837. If you answer yes to the prompt, you might want to check and remove
  19838. such private information before sending the email.")
  19839. (add-text-properties (point-min) (point-max) '(face org-warning))
  19840. (when (yes-or-no-p "Include your Org-mode configuration ")
  19841. (mapatoms
  19842. (lambda (v)
  19843. (and (boundp v)
  19844. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19845. (or (and (symbol-value v)
  19846. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19847. (and
  19848. (get v 'custom-type) (get v 'standard-value)
  19849. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19850. (push v list)))))
  19851. (kill-buffer (get-buffer "*Warn about privacy*"))
  19852. list))
  19853. nil nil
  19854. "Remember to cover the basics, that is, what you expected to happen and
  19855. what in fact did happen. You don't know how to make a good report? See
  19856. http://orgmode.org/manual/Feedback.html#Feedback
  19857. Your bug report will be posted to the Org-mode mailing list.
  19858. ------------------------------------------------------------------------")
  19859. (save-excursion
  19860. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19861. (replace-match "\\1Bug: \\3 [\\2]")))))
  19862. (defun org-install-agenda-files-menu ()
  19863. (let ((bl (buffer-list)))
  19864. (save-excursion
  19865. (while bl
  19866. (set-buffer (pop bl))
  19867. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19868. (when (derived-mode-p 'org-mode)
  19869. (easy-menu-change
  19870. '("Org") "File List for Agenda"
  19871. (append
  19872. (list
  19873. ["Edit File List" (org-edit-agenda-file-list) t]
  19874. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19875. ["Remove Current File from List" org-remove-file t]
  19876. ["Cycle through agenda files" org-cycle-agenda-files t]
  19877. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19878. "--")
  19879. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19880. ;;;; Documentation
  19881. (defun org-require-autoloaded-modules ()
  19882. (interactive)
  19883. (mapc 'require
  19884. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19885. org-table org-timer)))
  19886. ;;;###autoload
  19887. (defun org-reload (&optional uncompiled)
  19888. "Reload all org lisp files.
  19889. With prefix arg UNCOMPILED, load the uncompiled versions."
  19890. (interactive "P")
  19891. (require 'loadhist)
  19892. (let* ((org-dir (org-find-library-dir "org"))
  19893. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19894. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19895. (remove-re (mapconcat 'identity
  19896. (mapcar (lambda (f) (concat "^" f "$"))
  19897. (list (if (featurep 'xemacs)
  19898. "org-colview"
  19899. "org-colview-xemacs")
  19900. "org" "org-loaddefs" "org-version"))
  19901. "\\|"))
  19902. (feats (delete-dups
  19903. (mapcar 'file-name-sans-extension
  19904. (mapcar 'file-name-nondirectory
  19905. (delq nil
  19906. (mapcar 'feature-file
  19907. features))))))
  19908. (lfeat (append
  19909. (sort
  19910. (setq feats
  19911. (delq nil (mapcar
  19912. (lambda (f)
  19913. (if (and (string-match feature-re f)
  19914. (not (string-match remove-re f)))
  19915. f nil))
  19916. feats)))
  19917. 'string-lessp)
  19918. (list "org-version" "org")))
  19919. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19920. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19921. load-uncore load-misses)
  19922. (setq load-misses
  19923. (delq 't
  19924. (mapcar (lambda (f)
  19925. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19926. (and (string= org-dir contrib-dir)
  19927. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19928. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19929. (add-to-list 'load-uncore f 'append)
  19930. 't)
  19931. f))
  19932. lfeat)))
  19933. (if load-uncore
  19934. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19935. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19936. (if load-misses
  19937. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19938. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19939. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19940. ;;;###autoload
  19941. (defun org-customize ()
  19942. "Call the customize function with org as argument."
  19943. (interactive)
  19944. (org-load-modules-maybe)
  19945. (org-require-autoloaded-modules)
  19946. (customize-browse 'org))
  19947. (defun org-create-customize-menu ()
  19948. "Create a full customization menu for Org-mode, insert it into the menu."
  19949. (interactive)
  19950. (org-load-modules-maybe)
  19951. (org-require-autoloaded-modules)
  19952. (if (fboundp 'customize-menu-create)
  19953. (progn
  19954. (easy-menu-change
  19955. '("Org") "Customize"
  19956. `(["Browse Org group" org-customize t]
  19957. "--"
  19958. ,(customize-menu-create 'org)
  19959. ["Set" Custom-set t]
  19960. ["Save" Custom-save t]
  19961. ["Reset to Current" Custom-reset-current t]
  19962. ["Reset to Saved" Custom-reset-saved t]
  19963. ["Reset to Standard Settings" Custom-reset-standard t]))
  19964. (message "\"Org\"-menu now contains full customization menu"))
  19965. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19966. ;;;; Miscellaneous stuff
  19967. ;;; Generally useful functions
  19968. (defsubst org-get-at-eol (property n)
  19969. "Get text property PROPERTY at the end of line less N characters."
  19970. (get-text-property (- (point-at-eol) n) property))
  19971. (defun org-find-text-property-in-string (prop s)
  19972. "Return the first non-nil value of property PROP in string S."
  19973. (or (get-text-property 0 prop s)
  19974. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19975. prop s)))
  19976. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19977. "Display the given MESSAGE as a warning."
  19978. (if (fboundp 'display-warning)
  19979. (display-warning 'org message
  19980. (if (featurep 'xemacs) 'warning :warning))
  19981. (let ((buf (get-buffer-create "*Org warnings*")))
  19982. (with-current-buffer buf
  19983. (goto-char (point-max))
  19984. (insert "Warning (Org): " message)
  19985. (unless (bolp)
  19986. (newline)))
  19987. (display-buffer buf)
  19988. (sit-for 0))))
  19989. (defun org-eval (form)
  19990. "Eval FORM and return result."
  19991. (condition-case error
  19992. (eval form)
  19993. (error (format "%%![Error: %s]" error))))
  19994. (defun org-in-clocktable-p ()
  19995. "Check if the cursor is in a clocktable."
  19996. (let ((pos (point)) start)
  19997. (save-excursion
  19998. (end-of-line 1)
  19999. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20000. (setq start (match-beginning 0))
  20001. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20002. (>= (match-end 0) pos)
  20003. start))))
  20004. (defun org-in-verbatim-emphasis ()
  20005. (save-match-data
  20006. (and (org-in-regexp org-emph-re 2)
  20007. (>= (point) (match-beginning 3))
  20008. (<= (point) (match-end 4))
  20009. (member (match-string 3) '("=" "~")))))
  20010. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20011. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20012. (if (and marker (marker-buffer marker)
  20013. (buffer-live-p (marker-buffer marker)))
  20014. (progn
  20015. (org-pop-to-buffer-same-window (marker-buffer marker))
  20016. (if (or (> marker (point-max)) (< marker (point-min)))
  20017. (widen))
  20018. (goto-char marker)
  20019. (org-show-context 'org-goto))
  20020. (if bookmark
  20021. (bookmark-jump bookmark)
  20022. (error "Cannot find location"))))
  20023. (defun org-quote-csv-field (s)
  20024. "Quote field for inclusion in CSV material."
  20025. (if (string-match "[\",]" s)
  20026. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20027. s))
  20028. (defun org-force-self-insert (N)
  20029. "Needed to enforce self-insert under remapping."
  20030. (interactive "p")
  20031. (self-insert-command N))
  20032. (defun org-string-width (s)
  20033. "Compute width of string, ignoring invisible characters.
  20034. This ignores character with invisibility property `org-link', and also
  20035. characters with property `org-cwidth', because these will become invisible
  20036. upon the next fontification round."
  20037. (let (b l)
  20038. (when (or (eq t buffer-invisibility-spec)
  20039. (assq 'org-link buffer-invisibility-spec))
  20040. (while (setq b (text-property-any 0 (length s)
  20041. 'invisible 'org-link s))
  20042. (setq s (concat (substring s 0 b)
  20043. (substring s (or (next-single-property-change
  20044. b 'invisible s) (length s)))))))
  20045. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20046. (setq s (concat (substring s 0 b)
  20047. (substring s (or (next-single-property-change
  20048. b 'org-cwidth s) (length s))))))
  20049. (setq l (string-width s) b -1)
  20050. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20051. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20052. l))
  20053. (defun org-shorten-string (s maxlength)
  20054. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20055. If the string is shorter or has length MAXLENGTH, just return the
  20056. original string. If it is longer, the functions finds a space in the
  20057. string, breaks this string off at that locations and adds three dots
  20058. as ellipsis. Including the ellipsis, the string will not be longer
  20059. than MAXLENGTH. If finding a good breaking point in the string does
  20060. not work, the string is just chopped off in the middle of a word
  20061. if necessary."
  20062. (if (<= (length s) maxlength)
  20063. s
  20064. (let* ((n (max (- maxlength 4) 1))
  20065. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20066. (if (string-match re s)
  20067. (concat (match-string 1 s) "...")
  20068. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20069. (defun org-get-indentation (&optional line)
  20070. "Get the indentation of the current line, interpreting tabs.
  20071. When LINE is given, assume it represents a line and compute its indentation."
  20072. (if line
  20073. (if (string-match "^ *" (org-remove-tabs line))
  20074. (match-end 0))
  20075. (save-excursion
  20076. (beginning-of-line 1)
  20077. (skip-chars-forward " \t")
  20078. (current-column))))
  20079. (defun org-get-string-indentation (s)
  20080. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20081. (let ((n -1) (i 0) (w tab-width) c)
  20082. (catch 'exit
  20083. (while (< (setq n (1+ n)) (length s))
  20084. (setq c (aref s n))
  20085. (cond ((= c ?\ ) (setq i (1+ i)))
  20086. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20087. (t (throw 'exit t)))))
  20088. i))
  20089. (defun org-remove-tabs (s &optional width)
  20090. "Replace tabulators in S with spaces.
  20091. Assumes that s is a single line, starting in column 0."
  20092. (setq width (or width tab-width))
  20093. (while (string-match "\t" s)
  20094. (setq s (replace-match
  20095. (make-string
  20096. (- (* width (/ (+ (match-beginning 0) width) width))
  20097. (match-beginning 0)) ?\ )
  20098. t t s)))
  20099. s)
  20100. (defun org-fix-indentation (line ind)
  20101. "Fix indentation in LINE.
  20102. IND is a cons cell with target and minimum indentation.
  20103. If the current indentation in LINE is smaller than the minimum,
  20104. leave it alone. If it is larger than ind, set it to the target."
  20105. (let* ((l (org-remove-tabs line))
  20106. (i (org-get-indentation l))
  20107. (i1 (car ind)) (i2 (cdr ind)))
  20108. (if (>= i i2) (setq l (substring line i2)))
  20109. (if (> i1 0)
  20110. (concat (make-string i1 ?\ ) l)
  20111. l)))
  20112. (defun org-remove-indentation (code &optional n)
  20113. "Remove the maximum common indentation from the lines in CODE.
  20114. N may optionally be the number of spaces to remove."
  20115. (with-temp-buffer
  20116. (insert code)
  20117. (org-do-remove-indentation n)
  20118. (buffer-string)))
  20119. (defun org-do-remove-indentation (&optional n)
  20120. "Remove the maximum common indentation from the buffer."
  20121. (untabify (point-min) (point-max))
  20122. (let ((min 10000) re)
  20123. (if n
  20124. (setq min n)
  20125. (goto-char (point-min))
  20126. (while (re-search-forward "^ *[^ \n]" nil t)
  20127. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20128. (unless (or (= min 0) (= min 10000))
  20129. (setq re (format "^ \\{%d\\}" min))
  20130. (goto-char (point-min))
  20131. (while (re-search-forward re nil t)
  20132. (replace-match "")
  20133. (end-of-line 1))
  20134. min)))
  20135. (defun org-fill-template (template alist)
  20136. "Find each %key of ALIST in TEMPLATE and replace it."
  20137. (let ((case-fold-search nil)
  20138. entry key value)
  20139. (setq alist (sort (copy-sequence alist)
  20140. (lambda (a b) (< (length (car a)) (length (car b))))))
  20141. (while (setq entry (pop alist))
  20142. (setq template
  20143. (replace-regexp-in-string
  20144. (concat "%" (regexp-quote (car entry)))
  20145. (or (cdr entry) "") template t t)))
  20146. template))
  20147. (defun org-base-buffer (buffer)
  20148. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20149. (if (not buffer)
  20150. buffer
  20151. (or (buffer-base-buffer buffer)
  20152. buffer)))
  20153. (defun org-wrap (string &optional width lines)
  20154. "Wrap string to either a number of lines, or a width in characters.
  20155. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20156. that costs. If there is a word longer than WIDTH, the text is actually
  20157. wrapped to the length of that word.
  20158. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20159. many lines, whatever width that takes.
  20160. The return value is a list of lines, without newlines at the end."
  20161. (let* ((words (org-split-string string "[ \t\n]+"))
  20162. (maxword (apply 'max (mapcar 'org-string-width words)))
  20163. w ll)
  20164. (cond (width
  20165. (org-do-wrap words (max maxword width)))
  20166. (lines
  20167. (setq w maxword)
  20168. (setq ll (org-do-wrap words maxword))
  20169. (if (<= (length ll) lines)
  20170. ll
  20171. (setq ll words)
  20172. (while (> (length ll) lines)
  20173. (setq w (1+ w))
  20174. (setq ll (org-do-wrap words w)))
  20175. ll))
  20176. (t (error "Cannot wrap this")))))
  20177. (defun org-do-wrap (words width)
  20178. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20179. (let (lines line)
  20180. (while words
  20181. (setq line (pop words))
  20182. (while (and words (< (+ (length line) (length (car words))) width))
  20183. (setq line (concat line " " (pop words))))
  20184. (setq lines (push line lines)))
  20185. (nreverse lines)))
  20186. (defun org-split-string (string &optional separators)
  20187. "Splits STRING into substrings at SEPARATORS.
  20188. No empty strings are returned if there are matches at the beginning
  20189. and end of string."
  20190. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20191. (start 0)
  20192. notfirst
  20193. (list nil))
  20194. (while (and (string-match rexp string
  20195. (if (and notfirst
  20196. (= start (match-beginning 0))
  20197. (< start (length string)))
  20198. (1+ start) start))
  20199. (< (match-beginning 0) (length string)))
  20200. (setq notfirst t)
  20201. (or (eq (match-beginning 0) 0)
  20202. (and (eq (match-beginning 0) (match-end 0))
  20203. (eq (match-beginning 0) start))
  20204. (setq list
  20205. (cons (substring string start (match-beginning 0))
  20206. list)))
  20207. (setq start (match-end 0)))
  20208. (or (eq start (length string))
  20209. (setq list
  20210. (cons (substring string start)
  20211. list)))
  20212. (nreverse list)))
  20213. (defun org-quote-vert (s)
  20214. "Replace \"|\" with \"\\vert\"."
  20215. (while (string-match "|" s)
  20216. (setq s (replace-match "\\vert" t t s)))
  20217. s)
  20218. (defun org-uuidgen-p (s)
  20219. "Is S an ID created by UUIDGEN?"
  20220. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20221. (defun org-in-src-block-p (&optional inside)
  20222. "Whether point is in a code source block.
  20223. When INSIDE is non-nil, don't consider we are within a src block
  20224. when point is at #+BEGIN_SRC or #+END_SRC."
  20225. (let ((case-fold-search t) ov)
  20226. (or (and (eq (get-char-property (point) 'src-block) t))
  20227. (and (not inside)
  20228. (save-match-data
  20229. (save-excursion
  20230. (beginning-of-line)
  20231. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20232. (defun org-context ()
  20233. "Return a list of contexts of the current cursor position.
  20234. If several contexts apply, all are returned.
  20235. Each context entry is a list with a symbol naming the context, and
  20236. two positions indicating start and end of the context. Possible
  20237. contexts are:
  20238. :headline anywhere in a headline
  20239. :headline-stars on the leading stars in a headline
  20240. :todo-keyword on a TODO keyword (including DONE) in a headline
  20241. :tags on the TAGS in a headline
  20242. :priority on the priority cookie in a headline
  20243. :item on the first line of a plain list item
  20244. :item-bullet on the bullet/number of a plain list item
  20245. :checkbox on the checkbox in a plain list item
  20246. :table in an org-mode table
  20247. :table-special on a special filed in a table
  20248. :table-table in a table.el table
  20249. :clocktable in a clocktable
  20250. :src-block in a source block
  20251. :link on a hyperlink
  20252. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20253. :target on a <<target>>
  20254. :radio-target on a <<<radio-target>>>
  20255. :latex-fragment on a LaTeX fragment
  20256. :latex-preview on a LaTeX fragment with overlaid preview image
  20257. This function expects the position to be visible because it uses font-lock
  20258. faces as a help to recognize the following contexts: :table-special, :link,
  20259. and :keyword."
  20260. (let* ((f (get-text-property (point) 'face))
  20261. (faces (if (listp f) f (list f)))
  20262. (case-fold-search t)
  20263. (p (point)) clist o)
  20264. ;; First the large context
  20265. (cond
  20266. ((org-at-heading-p t)
  20267. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20268. (when (progn
  20269. (beginning-of-line 1)
  20270. (looking-at org-todo-line-tags-regexp))
  20271. (push (org-point-in-group p 1 :headline-stars) clist)
  20272. (push (org-point-in-group p 2 :todo-keyword) clist)
  20273. (push (org-point-in-group p 4 :tags) clist))
  20274. (goto-char p)
  20275. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20276. (if (looking-at "\\[#[A-Z0-9]\\]")
  20277. (push (org-point-in-group p 0 :priority) clist)))
  20278. ((org-at-item-p)
  20279. (push (org-point-in-group p 2 :item-bullet) clist)
  20280. (push (list :item (point-at-bol)
  20281. (save-excursion (org-end-of-item) (point)))
  20282. clist)
  20283. (and (org-at-item-checkbox-p)
  20284. (push (org-point-in-group p 0 :checkbox) clist)))
  20285. ((org-at-table-p)
  20286. (push (list :table (org-table-begin) (org-table-end)) clist)
  20287. (if (memq 'org-formula faces)
  20288. (push (list :table-special
  20289. (previous-single-property-change p 'face)
  20290. (next-single-property-change p 'face)) clist)))
  20291. ((org-at-table-p 'any)
  20292. (push (list :table-table) clist)))
  20293. (goto-char p)
  20294. (let ((case-fold-search t))
  20295. ;; New the "medium" contexts: clocktables, source blocks
  20296. (cond ((org-in-clocktable-p)
  20297. (push (list :clocktable
  20298. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20299. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20300. (match-beginning 1))
  20301. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20302. (match-end 0))) clist))
  20303. ((org-in-src-block-p)
  20304. (push (list :src-block
  20305. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20306. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20307. (match-beginning 1))
  20308. (and (search-forward "#+END_SRC" nil t)
  20309. (match-beginning 0))) clist))))
  20310. (goto-char p)
  20311. ;; Now the small context
  20312. (cond
  20313. ((org-at-timestamp-p)
  20314. (push (org-point-in-group p 0 :timestamp) clist))
  20315. ((memq 'org-link faces)
  20316. (push (list :link
  20317. (previous-single-property-change p 'face)
  20318. (next-single-property-change p 'face)) clist))
  20319. ((memq 'org-special-keyword faces)
  20320. (push (list :keyword
  20321. (previous-single-property-change p 'face)
  20322. (next-single-property-change p 'face)) clist))
  20323. ((org-at-target-p)
  20324. (push (org-point-in-group p 0 :target) clist)
  20325. (goto-char (1- (match-beginning 0)))
  20326. (if (looking-at org-radio-target-regexp)
  20327. (push (org-point-in-group p 0 :radio-target) clist))
  20328. (goto-char p))
  20329. ((setq o (car (delq nil
  20330. (mapcar
  20331. (lambda (x)
  20332. (if (memq x org-latex-fragment-image-overlays) x))
  20333. (overlays-at (point))))))
  20334. (push (list :latex-fragment
  20335. (overlay-start o) (overlay-end o)) clist)
  20336. (push (list :latex-preview
  20337. (overlay-start o) (overlay-end o)) clist))
  20338. ((org-inside-LaTeX-fragment-p)
  20339. ;; FIXME: positions wrong.
  20340. (push (list :latex-fragment (point) (point)) clist)))
  20341. (setq clist (nreverse (delq nil clist)))
  20342. clist))
  20343. (defun org-in-regexp (re &optional nlines visually)
  20344. "Check if point is inside a match of RE.
  20345. Normally only the current line is checked, but you can include
  20346. NLINES extra lines after point into the search. If VISUALLY is
  20347. set, require that the cursor is not after the match but really
  20348. on, so that the block visually is on the match."
  20349. (catch 'exit
  20350. (let ((pos (point))
  20351. (eol (point-at-eol (+ 1 (or nlines 0))))
  20352. (inc (if visually 1 0)))
  20353. (save-excursion
  20354. (beginning-of-line (- 1 (or nlines 0)))
  20355. (while (re-search-forward re eol t)
  20356. (if (and (<= (match-beginning 0) pos)
  20357. (>= (+ inc (match-end 0)) pos))
  20358. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20359. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20360. "Org mode 8.3")
  20361. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20362. "Non-nil when point is between matches of START-RE and END-RE.
  20363. Also return a non-nil value when point is on one of the matches.
  20364. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20365. buffer positions. Default values are the positions of headlines
  20366. surrounding the point.
  20367. The functions returns a cons cell whose car (resp. cdr) is the
  20368. position before START-RE (resp. after END-RE)."
  20369. (save-match-data
  20370. (let ((pos (point))
  20371. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20372. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20373. beg end)
  20374. (save-excursion
  20375. ;; Point is on a block when on START-RE or if START-RE can be
  20376. ;; found before it...
  20377. (and (or (org-in-regexp start-re)
  20378. (re-search-backward start-re limit-up t))
  20379. (setq beg (match-beginning 0))
  20380. ;; ... and END-RE after it...
  20381. (goto-char (match-end 0))
  20382. (re-search-forward end-re limit-down t)
  20383. (> (setq end (match-end 0)) pos)
  20384. ;; ... without another START-RE in-between.
  20385. (goto-char (match-beginning 0))
  20386. (not (re-search-backward start-re (1+ beg) t))
  20387. ;; Return value.
  20388. (cons beg end))))))
  20389. (defun org-in-block-p (names)
  20390. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20391. NAMES is a list of strings containing names of blocks.
  20392. Return first block name matched, or nil. Beware that in case of
  20393. nested blocks, the returned name may not belong to the closest
  20394. block from point."
  20395. (save-match-data
  20396. (catch 'exit
  20397. (let ((case-fold-search t)
  20398. (lim-up (save-excursion (outline-previous-heading)))
  20399. (lim-down (save-excursion (outline-next-heading))))
  20400. (mapc (lambda (name)
  20401. (let ((n (regexp-quote name)))
  20402. (when (org-between-regexps-p
  20403. (concat "^[ \t]*#\\+begin_" n)
  20404. (concat "^[ \t]*#\\+end_" n)
  20405. lim-up lim-down)
  20406. (throw 'exit n))))
  20407. names))
  20408. nil)))
  20409. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20410. "Call `multi-occur' with buffers for all agenda files."
  20411. (interactive "sOrg-files matching: \np")
  20412. (let* ((files (org-agenda-files))
  20413. (tnames (mapcar 'file-truename files))
  20414. (extra org-agenda-text-search-extra-files)
  20415. f)
  20416. (when (eq (car extra) 'agenda-archives)
  20417. (setq extra (cdr extra))
  20418. (setq files (org-add-archive-files files)))
  20419. (while (setq f (pop extra))
  20420. (unless (member (file-truename f) tnames)
  20421. (add-to-list 'files f 'append)
  20422. (add-to-list 'tnames (file-truename f) 'append)))
  20423. (multi-occur
  20424. (mapcar (lambda (x)
  20425. (with-current-buffer
  20426. (or (get-file-buffer x) (find-file-noselect x))
  20427. (widen)
  20428. (current-buffer)))
  20429. files)
  20430. regexp)))
  20431. (if (boundp 'occur-mode-find-occurrence-hook)
  20432. ;; Emacs 23
  20433. (add-hook 'occur-mode-find-occurrence-hook
  20434. (lambda ()
  20435. (when (derived-mode-p 'org-mode)
  20436. (org-reveal))))
  20437. ;; Emacs 22
  20438. (defadvice occur-mode-goto-occurrence
  20439. (after org-occur-reveal activate)
  20440. (and (derived-mode-p 'org-mode) (org-reveal)))
  20441. (defadvice occur-mode-goto-occurrence-other-window
  20442. (after org-occur-reveal activate)
  20443. (and (derived-mode-p 'org-mode) (org-reveal)))
  20444. (defadvice occur-mode-display-occurrence
  20445. (after org-occur-reveal activate)
  20446. (when (derived-mode-p 'org-mode)
  20447. (let ((pos (occur-mode-find-occurrence)))
  20448. (with-current-buffer (marker-buffer pos)
  20449. (save-excursion
  20450. (goto-char pos)
  20451. (org-reveal)))))))
  20452. (defun org-occur-link-in-agenda-files ()
  20453. "Create a link and search for it in the agendas.
  20454. The link is not stored in `org-stored-links', it is just created
  20455. for the search purpose."
  20456. (interactive)
  20457. (let ((link (condition-case nil
  20458. (org-store-link nil)
  20459. (error "Unable to create a link to here"))))
  20460. (org-occur-in-agenda-files (regexp-quote link))))
  20461. (defun org-reverse-string (string)
  20462. "Return the reverse of STRING."
  20463. (apply 'string (reverse (string-to-list string))))
  20464. ;; defsubst org-uniquify must be defined before first use
  20465. (defun org-uniquify-alist (alist)
  20466. "Merge elements of ALIST with the same key.
  20467. For example, in this alist:
  20468. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20469. => '((a 1 3) (b 2))
  20470. merge (a 1) and (a 3) into (a 1 3).
  20471. The function returns the new ALIST."
  20472. (let (rtn)
  20473. (mapc
  20474. (lambda (e)
  20475. (let (n)
  20476. (if (not (assoc (car e) rtn))
  20477. (push e rtn)
  20478. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20479. (setq rtn (assq-delete-all (car e) rtn))
  20480. (push n rtn))))
  20481. alist)
  20482. rtn))
  20483. (defun org-delete-all (elts list)
  20484. "Remove all elements in ELTS from LIST."
  20485. (while elts
  20486. (setq list (delete (pop elts) list)))
  20487. list)
  20488. (defun org-count (cl-item cl-seq)
  20489. "Count the number of occurrences of ITEM in SEQ.
  20490. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20491. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20492. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20493. (while (< cl-start cl-end)
  20494. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20495. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20496. (setq cl-start (1+ cl-start)))
  20497. cl-count))
  20498. (defun org-remove-if (predicate seq)
  20499. "Remove everything from SEQ that fulfills PREDICATE."
  20500. (let (res e)
  20501. (while seq
  20502. (setq e (pop seq))
  20503. (if (not (funcall predicate e)) (push e res)))
  20504. (nreverse res)))
  20505. (defun org-remove-if-not (predicate seq)
  20506. "Remove everything from SEQ that does not fulfill PREDICATE."
  20507. (let (res e)
  20508. (while seq
  20509. (setq e (pop seq))
  20510. (if (funcall predicate e) (push e res)))
  20511. (nreverse res)))
  20512. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20513. "Reduce two-argument FUNCTION across SEQ.
  20514. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20515. \":initial-value\" removed."
  20516. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20517. (cadr (memq :initial-value cl-keys)))
  20518. (cl-seq (pop cl-seq))
  20519. (t (funcall cl-func)))))
  20520. (while cl-seq
  20521. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20522. cl-accum))
  20523. (defun org-every (pred seq)
  20524. "Return true if PREDICATE is true of every element of SEQ.
  20525. Adapted from `every' in cl.el."
  20526. (catch 'org-every
  20527. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20528. t))
  20529. (defun org-some (pred seq)
  20530. "Return true if PREDICATE is true of any element of SEQ.
  20531. Adapted from `some' in cl.el."
  20532. (catch 'org-some
  20533. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20534. nil))
  20535. (defun org-back-over-empty-lines ()
  20536. "Move backwards over whitespace, to the beginning of the first empty line.
  20537. Returns the number of empty lines passed."
  20538. (let ((pos (point)))
  20539. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20540. (skip-chars-backward " \t\n\r")
  20541. (unless (eobp)
  20542. (forward-line -1)))
  20543. (beginning-of-line 2)
  20544. (goto-char (min (point) pos))
  20545. (count-lines (point) pos)))
  20546. (defun org-skip-whitespace ()
  20547. (skip-chars-forward " \t\n\r"))
  20548. (defun org-point-in-group (point group &optional context)
  20549. "Check if POINT is in match-group GROUP.
  20550. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20551. match. If the match group does not exist or point is not inside it,
  20552. return nil."
  20553. (and (match-beginning group)
  20554. (>= point (match-beginning group))
  20555. (<= point (match-end group))
  20556. (if context
  20557. (list context (match-beginning group) (match-end group))
  20558. t)))
  20559. (defun org-switch-to-buffer-other-window (&rest args)
  20560. "Switch to buffer in a second window on the current frame.
  20561. In particular, do not allow pop-up frames.
  20562. Returns the newly created buffer."
  20563. (org-no-popups
  20564. (apply 'switch-to-buffer-other-window args)))
  20565. (defun org-combine-plists (&rest plists)
  20566. "Create a single property list from all plists in PLISTS.
  20567. The process starts by copying the first list, and then setting properties
  20568. from the other lists. Settings in the last list are the most significant
  20569. ones and overrule settings in the other lists."
  20570. (let ((rtn (copy-sequence (pop plists)))
  20571. p v ls)
  20572. (while plists
  20573. (setq ls (pop plists))
  20574. (while ls
  20575. (setq p (pop ls) v (pop ls))
  20576. (setq rtn (plist-put rtn p v))))
  20577. rtn))
  20578. (defun org-replace-escapes (string table)
  20579. "Replace %-escapes in STRING with values in TABLE.
  20580. TABLE is an association list with keys like \"%a\" and string values.
  20581. The sequences in STRING may contain normal field width and padding information,
  20582. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20583. so values can contain further %-escapes if they are define later in TABLE."
  20584. (let ((tbl (copy-alist table))
  20585. (case-fold-search nil)
  20586. (pchg 0)
  20587. e re rpl)
  20588. (while (setq e (pop tbl))
  20589. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20590. (when (and (cdr e) (string-match re (cdr e)))
  20591. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20592. (safe "SREF"))
  20593. (add-text-properties 0 3 (list 'sref sref) safe)
  20594. (setcdr e (replace-match safe t t (cdr e)))))
  20595. (while (string-match re string)
  20596. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20597. (cdr e)))
  20598. (setq string (replace-match rpl t t string))))
  20599. (while (setq pchg (next-property-change pchg string))
  20600. (let ((sref (get-text-property pchg 'sref string)))
  20601. (when (and sref (string-match "SREF" string pchg))
  20602. (setq string (replace-match sref t t string)))))
  20603. string))
  20604. (defun org-sublist (list start end)
  20605. "Return a section of LIST, from START to END.
  20606. Counting starts at 1."
  20607. (let (rtn (c start))
  20608. (setq list (nthcdr (1- start) list))
  20609. (while (and list (<= c end))
  20610. (push (pop list) rtn)
  20611. (setq c (1+ c)))
  20612. (nreverse rtn)))
  20613. (defun org-find-base-buffer-visiting (file)
  20614. "Like `find-buffer-visiting' but always return the base buffer and
  20615. not an indirect buffer."
  20616. (let ((buf (or (get-file-buffer file)
  20617. (find-buffer-visiting file))))
  20618. (if buf
  20619. (or (buffer-base-buffer buf) buf)
  20620. nil)))
  20621. (defun org-image-file-name-regexp (&optional extensions)
  20622. "Return regexp matching the file names of images.
  20623. If EXTENSIONS is given, only match these."
  20624. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20625. (image-file-name-regexp)
  20626. (let ((image-file-name-extensions
  20627. (or extensions
  20628. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20629. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20630. (concat "\\."
  20631. (regexp-opt (nconc (mapcar 'upcase
  20632. image-file-name-extensions)
  20633. image-file-name-extensions)
  20634. t)
  20635. "\\'"))))
  20636. (defun org-file-image-p (file &optional extensions)
  20637. "Return non-nil if FILE is an image."
  20638. (save-match-data
  20639. (string-match (org-image-file-name-regexp extensions) file)))
  20640. (defun org-get-cursor-date (&optional with-time)
  20641. "Return the date at cursor in as a time.
  20642. This works in the calendar and in the agenda, anywhere else it just
  20643. returns the current time.
  20644. If WITH-TIME is non-nil, returns the time of the event at point (in
  20645. the agenda) or the current time of the day."
  20646. (let (date day defd tp tm hod mod)
  20647. (when with-time
  20648. (setq tp (get-text-property (point) 'time))
  20649. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20650. (setq hod (string-to-number (match-string 1 tp))
  20651. mod (string-to-number (match-string 2 tp))))
  20652. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20653. mod (nth 1 (decode-time (current-time))))))
  20654. (cond
  20655. ((eq major-mode 'calendar-mode)
  20656. (setq date (calendar-cursor-to-date)
  20657. defd (encode-time 0 (or mod 0) (or hod 0)
  20658. (nth 1 date) (nth 0 date) (nth 2 date))))
  20659. ((eq major-mode 'org-agenda-mode)
  20660. (setq day (get-text-property (point) 'day))
  20661. (if day
  20662. (setq date (calendar-gregorian-from-absolute day)
  20663. defd (encode-time 0 (or mod 0) (or hod 0)
  20664. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20665. (or defd (current-time))))
  20666. (defun org-mark-subtree (&optional up)
  20667. "Mark the current subtree.
  20668. This puts point at the start of the current subtree, and mark at
  20669. the end. If a numeric prefix UP is given, move up into the
  20670. hierarchy of headlines by UP levels before marking the subtree."
  20671. (interactive "P")
  20672. (org-with-limited-levels
  20673. (cond ((org-at-heading-p) (beginning-of-line))
  20674. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20675. (t (outline-previous-visible-heading 1))))
  20676. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20677. (if (org-called-interactively-p 'any)
  20678. (call-interactively 'org-mark-element)
  20679. (org-mark-element)))
  20680. ;;; Indentation
  20681. (defun org--get-expected-indentation (element contentsp)
  20682. "Expected indentation column for current line, according to ELEMENT.
  20683. ELEMENT is an element containing point. CONTENTSP is non-nil
  20684. when indentation is to be computed according to contents of
  20685. ELEMENT."
  20686. (let ((type (org-element-type element))
  20687. (start (org-element-property :begin element)))
  20688. (org-with-wide-buffer
  20689. (cond
  20690. (contentsp
  20691. (case type
  20692. (footnote-definition 0)
  20693. ((headline inlinetask nil)
  20694. (if (not org-adapt-indentation) 0
  20695. (let ((level (org-current-level)))
  20696. (if level (1+ level) 0))))
  20697. (item
  20698. (org-list-item-body-column
  20699. (org-element-property :post-affiliated element)))
  20700. (plain-list
  20701. (save-excursion
  20702. (goto-char (org-element-property :post-affiliated element))
  20703. (org-get-indentation)))
  20704. (otherwise
  20705. (goto-char start)
  20706. (org-get-indentation))))
  20707. ((memq type '(headline inlinetask nil))
  20708. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20709. (org--get-expected-indentation element t)
  20710. 0))
  20711. ((eq type 'footnote-definition) 0)
  20712. ;; First paragraph of a footnote definition or an item.
  20713. ;; Indent like parent.
  20714. ((< (line-beginning-position) start)
  20715. (org--get-expected-indentation
  20716. (org-element-property :parent element) t))
  20717. ;; At first line: indent according to previous sibling, if any,
  20718. ;; ignoring footnote definitions and inline tasks, or parent's
  20719. ;; contents.
  20720. ((= (line-beginning-position) start)
  20721. (catch 'exit
  20722. (while t
  20723. (if (= (point-min) start) (throw 'exit 0)
  20724. (goto-char (1- start))
  20725. (let* ((previous (org-element-at-point))
  20726. (parent previous))
  20727. (while (and parent (<= (org-element-property :end parent) start))
  20728. (setq previous parent
  20729. parent (org-element-property :parent parent)))
  20730. (cond
  20731. ((not previous) (throw 'exit 0))
  20732. ((> (org-element-property :end previous) start)
  20733. (throw 'exit (org--get-expected-indentation previous t)))
  20734. ((memq (org-element-type previous)
  20735. '(footnote-definition inlinetask))
  20736. (setq start (org-element-property :begin previous)))
  20737. (t (goto-char (org-element-property :begin previous))
  20738. (throw 'exit
  20739. (if (bolp) (org-get-indentation)
  20740. ;; At first paragraph in an item or
  20741. ;; a footnote definition.
  20742. (org--get-expected-indentation
  20743. (org-element-property :parent previous) t))))))))))
  20744. ;; Otherwise, move to the first non-blank line above.
  20745. (t
  20746. (beginning-of-line)
  20747. (let ((pos (point)))
  20748. (skip-chars-backward " \r\t\n")
  20749. (cond
  20750. ;; Two blank lines end a footnote definition or a plain
  20751. ;; list. When we indent an empty line after them, the
  20752. ;; containing list or footnote definition is over, so it
  20753. ;; qualifies as a previous sibling. Therefore, we indent
  20754. ;; like its first line.
  20755. ((and (memq type '(footnote-definition plain-list))
  20756. (> (count-lines (point) pos) 2))
  20757. (goto-char start)
  20758. (org-get-indentation))
  20759. ;; Line above is the first one of a paragraph at the
  20760. ;; beginning of an item or a footnote definition. Indent
  20761. ;; like parent.
  20762. ((< (line-beginning-position) start)
  20763. (org--get-expected-indentation
  20764. (org-element-property :parent element) t))
  20765. ;; POS is after contents in a greater element. Indent like
  20766. ;; the beginning of the element.
  20767. ;;
  20768. ;; As a special case, if point is at the end of a footnote
  20769. ;; definition or an item, indent like the very last element
  20770. ;; within.
  20771. ((and (not (eq type 'paragraph))
  20772. (let ((cend (org-element-property :contents-end element)))
  20773. (and cend (<= cend pos))))
  20774. (if (memq type '(footnote-definition item plain-list))
  20775. (org--get-expected-indentation (org-element-at-point) nil)
  20776. (goto-char start)
  20777. (org-get-indentation)))
  20778. ;; In any other case, indent like the current line.
  20779. (t (org-get-indentation)))))))))
  20780. (defun org--align-node-property ()
  20781. "Align node property at point.
  20782. Alignment is done according to `org-property-format', which see."
  20783. (when (save-excursion
  20784. (beginning-of-line)
  20785. (looking-at org-property-re))
  20786. (replace-match
  20787. (concat (match-string 4)
  20788. (org-trim
  20789. (format org-property-format (match-string 1) (match-string 3))))
  20790. t t)))
  20791. (defun org-indent-line ()
  20792. "Indent line depending on context.
  20793. Indentation is done according to the following rules:
  20794. - Footnote definitions, headlines and inline tasks have to
  20795. start at column 0.
  20796. - On the very first line of an element, consider, in order, the
  20797. next rules until one matches:
  20798. 1. If there's a sibling element before, ignoring footnote
  20799. definitions and inline tasks, indent like its first line.
  20800. 2. If element has a parent, indent like its contents. More
  20801. precisely, if parent is an item, indent after the
  20802. description part, if any, or the bullet (see
  20803. `org-list-description-max-indent'). Else, indent like
  20804. parent's first line.
  20805. 3. Otherwise, indent relatively to current level, if
  20806. `org-adapt-indentation' is non-nil, or to left margin.
  20807. - On a blank line at the end of an element, indent according to
  20808. the type of the element. More precisely
  20809. 1. If element is a plain list, an item, or a footnote
  20810. definition, indent like the very last element within.
  20811. 2. If element is a paragraph, indent like its last non blank
  20812. line.
  20813. 3. Otherwise, indent like its very first line.
  20814. - In the code part of a source block, use language major mode
  20815. to indent current line if `org-src-tab-acts-natively' is
  20816. non-nil. If it is nil, do nothing.
  20817. - Otherwise, indent like the first non-blank line above.
  20818. The function doesn't indent an item as it could break the whole
  20819. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  20820. \\[org-shiftmetaright].
  20821. Also align node properties according to `org-property-format'."
  20822. (interactive)
  20823. (cond
  20824. (orgstruct-is-++
  20825. (let ((indent-line-function
  20826. (cadadr (assq 'indent-line-function org-fb-vars))))
  20827. (indent-according-to-mode)))
  20828. ((org-at-heading-p) 'noindent)
  20829. (t
  20830. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  20831. (type (org-element-type element)))
  20832. (cond ((and (memq type '(plain-list item))
  20833. (= (line-beginning-position)
  20834. (org-element-property :post-affiliated element)))
  20835. 'noindent)
  20836. ((and (eq type 'src-block)
  20837. org-src-tab-acts-natively
  20838. (> (line-beginning-position)
  20839. (org-element-property :post-affiliated element))
  20840. (< (line-beginning-position)
  20841. (org-with-wide-buffer
  20842. (goto-char (org-element-property :end element))
  20843. (skip-chars-backward " \r\t\n")
  20844. (line-beginning-position))))
  20845. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  20846. (t
  20847. (let ((column (org--get-expected-indentation element nil)))
  20848. ;; Preserve current column.
  20849. (if (<= (current-column) (current-indentation))
  20850. (org-indent-line-to column)
  20851. (save-excursion (org-indent-line-to column))))
  20852. ;; Align node property. Also preserve current column.
  20853. (when (eq type 'node-property)
  20854. (let ((column (current-column)))
  20855. (org--align-node-property)
  20856. (org-move-to-column column)))))))))
  20857. (defun org-indent-region (start end)
  20858. "Indent each non-blank line in the region.
  20859. Called from a program, START and END specify the region to
  20860. indent. The function will not indent contents of example blocks,
  20861. verse blocks and export blocks as leading white spaces are
  20862. assumed to be significant there."
  20863. (interactive "r")
  20864. (save-excursion
  20865. (goto-char start)
  20866. (skip-chars-forward " \r\t\n")
  20867. (unless (eobp) (beginning-of-line))
  20868. (let ((indent-to
  20869. (lambda (ind pos)
  20870. ;; Set IND as indentation for all lines between point and
  20871. ;; POS or END, whichever comes first. Blank lines are
  20872. ;; ignored. Leave point after POS once done.
  20873. (let ((limit (copy-marker (min end pos))))
  20874. (while (< (point) limit)
  20875. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  20876. (forward-line))
  20877. (set-marker limit nil))))
  20878. (end (copy-marker end)))
  20879. (while (< (point) end)
  20880. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  20881. (let* ((element (org-element-at-point))
  20882. (type (org-element-type element))
  20883. (element-end (copy-marker (org-element-property :end element)))
  20884. (ind (org--get-expected-indentation element nil)))
  20885. (cond
  20886. ((or (memq type '(paragraph table table-row))
  20887. (not (or (org-element-property :contents-begin element)
  20888. (memq type
  20889. '(example-block export-block src-block)))))
  20890. ;; Elements here are indented as a single block. Also
  20891. ;; align node properties.
  20892. (when (eq type 'node-property)
  20893. (org--align-node-property)
  20894. (beginning-of-line))
  20895. (funcall indent-to ind element-end))
  20896. (t
  20897. ;; Elements in this category consist of three parts:
  20898. ;; before the contents, the contents, and after the
  20899. ;; contents. The contents are treated specially,
  20900. ;; according to the element type, or not indented at
  20901. ;; all. Other parts are indented as a single block.
  20902. (let* ((post (copy-marker
  20903. (org-element-property :post-affiliated element)))
  20904. (cbeg
  20905. (copy-marker
  20906. (cond
  20907. ((not (org-element-property :contents-begin element))
  20908. ;; Fake contents for source blocks.
  20909. (org-with-wide-buffer
  20910. (goto-char post)
  20911. (forward-line)
  20912. (point)))
  20913. ((memq type '(footnote-definition item plain-list))
  20914. ;; Contents in these elements could start on
  20915. ;; the same line as the beginning of the
  20916. ;; element. Make sure we start indenting
  20917. ;; from the second line.
  20918. (org-with-wide-buffer
  20919. (goto-char post)
  20920. (end-of-line)
  20921. (skip-chars-forward " \r\t\n")
  20922. (if (eobp) (point) (line-beginning-position))))
  20923. (t (org-element-property :contents-begin element)))))
  20924. (cend (copy-marker
  20925. (or (org-element-property :contents-end element)
  20926. ;; Fake contents for source blocks.
  20927. (org-with-wide-buffer
  20928. (goto-char element-end)
  20929. (skip-chars-backward " \r\t\n")
  20930. (line-beginning-position)))
  20931. t)))
  20932. ;; Do not change items indentation individually as it
  20933. ;; might break the list as a whole. On the other
  20934. ;; hand, when at a plain list, indent it as a whole.
  20935. (cond ((eq type 'plain-list)
  20936. (let ((offset (- ind (org-get-indentation))))
  20937. (unless (zerop offset)
  20938. (indent-rigidly (org-element-property :begin element)
  20939. (org-element-property :end element)
  20940. offset))
  20941. (goto-char cbeg)))
  20942. ((eq type 'item) (goto-char cbeg))
  20943. (t (funcall indent-to ind cbeg)))
  20944. (when (< (point) end)
  20945. (case type
  20946. ((example-block export-block verse-block))
  20947. (src-block
  20948. ;; In a source block, indent source code
  20949. ;; according to language major mode, but only if
  20950. ;; `org-src-tab-acts-natively' is non-nil.
  20951. (when (and (< (point) end) org-src-tab-acts-natively)
  20952. (ignore-errors
  20953. (org-babel-do-in-edit-buffer
  20954. (indent-region (point-min) (point-max))))))
  20955. (t (org-indent-region (point) (min cend end))))
  20956. (goto-char (min cend end))
  20957. (when (< (point) end) (funcall indent-to ind element-end)))
  20958. (set-marker post nil)
  20959. (set-marker cbeg nil)
  20960. (set-marker cend nil))))
  20961. (set-marker element-end nil))))
  20962. (set-marker end nil))))
  20963. (defun org-indent-drawer ()
  20964. "Indent the drawer at point."
  20965. (interactive)
  20966. (unless (save-excursion
  20967. (beginning-of-line)
  20968. (org-looking-at-p org-drawer-regexp))
  20969. (user-error "Not at a drawer"))
  20970. (let ((element (org-element-at-point)))
  20971. (unless (memq (org-element-type element) '(drawer property-drawer))
  20972. (user-error "Not at a drawer"))
  20973. (org-with-wide-buffer
  20974. (org-indent-region (org-element-property :begin element)
  20975. (org-element-property :end element))))
  20976. (message "Drawer at point indented"))
  20977. (defun org-indent-block ()
  20978. "Indent the block at point."
  20979. (interactive)
  20980. (unless (save-excursion
  20981. (beginning-of-line)
  20982. (let ((case-fold-search t))
  20983. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  20984. (user-error "Not at a block"))
  20985. (let ((element (org-element-at-point)))
  20986. (unless (memq (org-element-type element)
  20987. '(comment-block center-block dynamic-block example-block
  20988. export-block quote-block special-block
  20989. src-block verse-block))
  20990. (user-error "Not at a block"))
  20991. (org-with-wide-buffer
  20992. (org-indent-region (org-element-property :begin element)
  20993. (org-element-property :end element))))
  20994. (message "Block at point indented"))
  20995. ;;; Filling
  20996. ;; We use our own fill-paragraph and auto-fill functions.
  20997. ;; `org-fill-paragraph' relies on adaptive filling and context
  20998. ;; checking. Appropriate `fill-prefix' is computed with
  20999. ;; `org-adaptive-fill-function'.
  21000. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21001. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21002. ;; `org-adaptive-fill-function' value. Internally,
  21003. ;; `org-comment-line-break-function' breaks the line.
  21004. ;; `org-setup-filling' installs filling and auto-filling related
  21005. ;; variables during `org-mode' initialization.
  21006. (defvar org-element-paragraph-separate) ; org-element.el
  21007. (defun org-setup-filling ()
  21008. (require 'org-element)
  21009. ;; Prevent auto-fill from inserting unwanted new items.
  21010. (when (boundp 'fill-nobreak-predicate)
  21011. (org-set-local
  21012. 'fill-nobreak-predicate
  21013. (org-uniquify
  21014. (append fill-nobreak-predicate
  21015. '(org-fill-line-break-nobreak-p
  21016. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21017. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21018. (org-set-local 'paragraph-start paragraph-ending)
  21019. (org-set-local 'paragraph-separate paragraph-ending))
  21020. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21021. (org-set-local 'auto-fill-inhibit-regexp nil)
  21022. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21023. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21024. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21025. (defun org-fill-line-break-nobreak-p ()
  21026. "Non-nil when a new line at point would create an Org line break."
  21027. (save-excursion
  21028. (skip-chars-backward "[ \t]")
  21029. (skip-chars-backward "\\\\")
  21030. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21031. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21032. "Non-nil when a new line at point would split a timestamp."
  21033. (and (org-at-timestamp-p t)
  21034. (not (looking-at org-ts-regexp-both))))
  21035. (declare-function message-in-body-p "message" ())
  21036. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21037. (defun org-adaptive-fill-function ()
  21038. "Compute a fill prefix for the current line.
  21039. Return fill prefix, as a string, or nil if current line isn't
  21040. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21041. matches in paragraphs or comments, use it."
  21042. (catch 'exit
  21043. (when (derived-mode-p 'message-mode)
  21044. (save-excursion
  21045. (beginning-of-line)
  21046. (cond ((or (not (message-in-body-p))
  21047. (looking-at orgtbl-line-start-regexp))
  21048. (throw 'exit nil))
  21049. ((looking-at message-cite-prefix-regexp)
  21050. (throw 'exit (match-string-no-properties 0)))
  21051. ((looking-at org-outline-regexp)
  21052. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21053. (org-with-wide-buffer
  21054. (unless (org-at-heading-p)
  21055. (let* ((p (line-beginning-position))
  21056. (element (save-excursion
  21057. (beginning-of-line)
  21058. (org-element-at-point)))
  21059. (type (org-element-type element))
  21060. (post-affiliated (org-element-property :post-affiliated element)))
  21061. (unless (< p post-affiliated)
  21062. (case type
  21063. (comment
  21064. (save-excursion
  21065. (beginning-of-line)
  21066. (looking-at "[ \t]*")
  21067. (concat (match-string 0) "# ")))
  21068. (footnote-definition "")
  21069. ((item plain-list)
  21070. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21071. (paragraph
  21072. ;; Fill prefix is usually the same as the current line,
  21073. ;; unless the paragraph is at the beginning of an item.
  21074. (let ((parent (org-element-property :parent element)))
  21075. (save-excursion
  21076. (beginning-of-line)
  21077. (cond ((eq (org-element-type parent) 'item)
  21078. (make-string (org-list-item-body-column
  21079. (org-element-property :begin parent))
  21080. ?\s))
  21081. ((and adaptive-fill-regexp
  21082. ;; Locally disable
  21083. ;; `adaptive-fill-function' to let
  21084. ;; `fill-context-prefix' handle
  21085. ;; `adaptive-fill-regexp' variable.
  21086. (let (adaptive-fill-function)
  21087. (fill-context-prefix
  21088. post-affiliated
  21089. (org-element-property :end element)))))
  21090. ((looking-at "[ \t]+") (match-string 0))
  21091. (t "")))))
  21092. (comment-block
  21093. ;; Only fill contents if P is within block boundaries.
  21094. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21095. (forward-line)
  21096. (point)))
  21097. (cend (save-excursion
  21098. (goto-char (org-element-property :end element))
  21099. (skip-chars-backward " \r\t\n")
  21100. (line-beginning-position))))
  21101. (when (and (>= p cbeg) (< p cend))
  21102. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21103. (match-string 0)
  21104. "")))))))))))
  21105. (declare-function message-goto-body "message" ())
  21106. (defvar message-cite-prefix-regexp) ; From message.el
  21107. (defun org-fill-paragraph (&optional justify)
  21108. "Fill element at point, when applicable.
  21109. This function only applies to comment blocks, comments, example
  21110. blocks and paragraphs. Also, as a special case, re-align table
  21111. when point is at one.
  21112. If JUSTIFY is non-nil (interactively, with prefix argument),
  21113. justify as well. If `sentence-end-double-space' is non-nil, then
  21114. period followed by one space does not end a sentence, so don't
  21115. break a line there. The variable `fill-column' controls the
  21116. width for filling.
  21117. For convenience, when point is at a plain list, an item or
  21118. a footnote definition, try to fill the first paragraph within."
  21119. (interactive)
  21120. (if (and (derived-mode-p 'message-mode)
  21121. (or (not (message-in-body-p))
  21122. (save-excursion (move-beginning-of-line 1)
  21123. (looking-at message-cite-prefix-regexp))))
  21124. ;; First ensure filling is correct in message-mode.
  21125. (let ((fill-paragraph-function
  21126. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21127. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21128. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21129. (paragraph-separate
  21130. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21131. (fill-paragraph nil))
  21132. (with-syntax-table org-mode-transpose-word-syntax-table
  21133. ;; Move to end of line in order to get the first paragraph
  21134. ;; within a plain list or a footnote definition.
  21135. (let ((element (save-excursion
  21136. (end-of-line)
  21137. (or (ignore-errors (org-element-at-point))
  21138. (user-error "An element cannot be parsed line %d"
  21139. (line-number-at-pos (point)))))))
  21140. ;; First check if point is in a blank line at the beginning of
  21141. ;; the buffer. In that case, ignore filling.
  21142. (case (org-element-type element)
  21143. ;; Use major mode filling function is src blocks.
  21144. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21145. ;; Align Org tables, leave table.el tables as-is.
  21146. (table-row (org-table-align) t)
  21147. (table
  21148. (when (eq (org-element-property :type element) 'org)
  21149. (save-excursion
  21150. (goto-char (org-element-property :post-affiliated element))
  21151. (org-table-align)))
  21152. t)
  21153. (paragraph
  21154. ;; Paragraphs may contain `line-break' type objects.
  21155. (let ((beg (max (point-min)
  21156. (org-element-property :contents-begin element)))
  21157. (end (min (point-max)
  21158. (org-element-property :contents-end element))))
  21159. ;; Do nothing if point is at an affiliated keyword.
  21160. (if (< (line-end-position) beg) t
  21161. (when (derived-mode-p 'message-mode)
  21162. ;; In `message-mode', do not fill following citation
  21163. ;; in current paragraph nor text before message body.
  21164. (let ((body-start (save-excursion (message-goto-body))))
  21165. (when body-start (setq beg (max body-start beg))))
  21166. (when (save-excursion
  21167. (re-search-forward
  21168. (concat "^" message-cite-prefix-regexp) end t))
  21169. (setq end (match-beginning 0))))
  21170. ;; Fill paragraph, taking line breaks into account.
  21171. (save-excursion
  21172. (goto-char beg)
  21173. (let ((cuts (list beg)))
  21174. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21175. (when (eq 'line-break
  21176. (org-element-type
  21177. (save-excursion (backward-char)
  21178. (org-element-context))))
  21179. (push (point) cuts)))
  21180. (dolist (c (delq end cuts))
  21181. (fill-region-as-paragraph c end justify)
  21182. (setq end c))))
  21183. t)))
  21184. ;; Contents of `comment-block' type elements should be
  21185. ;; filled as plain text, but only if point is within block
  21186. ;; markers.
  21187. (comment-block
  21188. (let* ((case-fold-search t)
  21189. (beg (save-excursion
  21190. (goto-char (org-element-property :begin element))
  21191. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21192. (forward-line)
  21193. (point)))
  21194. (end (save-excursion
  21195. (goto-char (org-element-property :end element))
  21196. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21197. (line-beginning-position))))
  21198. (if (or (< (point) beg) (> (point) end)) t
  21199. (fill-region-as-paragraph
  21200. (save-excursion (end-of-line)
  21201. (re-search-backward "^[ \t]*$" beg 'move)
  21202. (line-beginning-position))
  21203. (save-excursion (beginning-of-line)
  21204. (re-search-forward "^[ \t]*$" end 'move)
  21205. (line-beginning-position))
  21206. justify))))
  21207. ;; Fill comments.
  21208. (comment
  21209. (let ((begin (org-element-property :post-affiliated element))
  21210. (end (org-element-property :end element)))
  21211. (when (and (>= (point) begin) (<= (point) end))
  21212. (let ((begin (save-excursion
  21213. (end-of-line)
  21214. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21215. (progn (forward-line) (point))
  21216. begin)))
  21217. (end (save-excursion
  21218. (end-of-line)
  21219. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21220. (1- (line-beginning-position))
  21221. (skip-chars-backward " \r\t\n")
  21222. (line-end-position)))))
  21223. ;; Do not fill comments when at a blank line.
  21224. (when (> end begin)
  21225. (let ((fill-prefix
  21226. (save-excursion
  21227. (beginning-of-line)
  21228. (looking-at "[ \t]*#")
  21229. (let ((comment-prefix (match-string 0)))
  21230. (goto-char (match-end 0))
  21231. (if (looking-at adaptive-fill-regexp)
  21232. (concat comment-prefix (match-string 0))
  21233. (concat comment-prefix " "))))))
  21234. (save-excursion
  21235. (fill-region-as-paragraph begin end justify))))))
  21236. t))
  21237. ;; Ignore every other element.
  21238. (otherwise t))))))
  21239. (defun org-auto-fill-function ()
  21240. "Auto-fill function."
  21241. ;; Check if auto-filling is meaningful.
  21242. (let ((fc (current-fill-column)))
  21243. (when (and fc (> (current-column) fc))
  21244. (let* ((fill-prefix (org-adaptive-fill-function))
  21245. ;; Enforce empty fill prefix, if required. Otherwise, it
  21246. ;; will be computed again.
  21247. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21248. (when fill-prefix (do-auto-fill))))))
  21249. (defun org-comment-line-break-function (&optional soft)
  21250. "Break line at point and indent, continuing comment if within one.
  21251. The inserted newline is marked hard if variable
  21252. `use-hard-newlines' is true, unless optional argument SOFT is
  21253. non-nil."
  21254. (if soft (insert-and-inherit ?\n) (newline 1))
  21255. (save-excursion (forward-char -1) (delete-horizontal-space))
  21256. (delete-horizontal-space)
  21257. (indent-to-left-margin)
  21258. (insert-before-markers-and-inherit fill-prefix))
  21259. ;;; Fixed Width Areas
  21260. (defun org-toggle-fixed-width ()
  21261. "Toggle fixed-width markup.
  21262. Add or remove fixed-width markup on current line, whenever it
  21263. makes sense. Return an error otherwise.
  21264. If a region is active and if it contains only fixed-width areas
  21265. or blank lines, remove all fixed-width markup in it. If the
  21266. region contains anything else, convert all non-fixed-width lines
  21267. to fixed-width ones.
  21268. Blank lines at the end of the region are ignored unless the
  21269. region only contains such lines."
  21270. (interactive)
  21271. (if (not (org-region-active-p))
  21272. ;; No region:
  21273. ;;
  21274. ;; Remove fixed width marker only in a fixed-with element.
  21275. ;;
  21276. ;; Add fixed width maker in paragraphs, in blank lines after
  21277. ;; elements or at the beginning of a headline or an inlinetask,
  21278. ;; and before any one-line elements (e.g., a clock).
  21279. (progn
  21280. (beginning-of-line)
  21281. (let* ((element (org-element-at-point))
  21282. (type (org-element-type element)))
  21283. (cond
  21284. ((and (eq type 'fixed-width)
  21285. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21286. (replace-match
  21287. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21288. ((and (memq type '(babel-call clock comment diary-sexp headline
  21289. horizontal-rule keyword paragraph
  21290. planning))
  21291. (<= (org-element-property :post-affiliated element) (point)))
  21292. (skip-chars-forward " \t")
  21293. (insert ": "))
  21294. ((and (org-looking-at-p "[ \t]*$")
  21295. (or (eq type 'inlinetask)
  21296. (save-excursion
  21297. (skip-chars-forward " \r\t\n")
  21298. (<= (org-element-property :end element) (point)))))
  21299. (delete-region (point) (line-end-position))
  21300. (org-indent-line)
  21301. (insert ": "))
  21302. (t (user-error "Cannot insert a fixed-width line here")))))
  21303. ;; Region active.
  21304. (let* ((begin (save-excursion
  21305. (goto-char (region-beginning))
  21306. (line-beginning-position)))
  21307. (end (copy-marker
  21308. (save-excursion
  21309. (goto-char (region-end))
  21310. (unless (eolp) (beginning-of-line))
  21311. (if (save-excursion (re-search-backward "\\S-" begin t))
  21312. (progn (skip-chars-backward " \r\t\n") (point))
  21313. (point)))))
  21314. (all-fixed-width-p
  21315. (catch 'not-all-p
  21316. (save-excursion
  21317. (goto-char begin)
  21318. (skip-chars-forward " \r\t\n")
  21319. (when (eobp) (throw 'not-all-p nil))
  21320. (while (< (point) end)
  21321. (let ((element (org-element-at-point)))
  21322. (if (eq (org-element-type element) 'fixed-width)
  21323. (goto-char (org-element-property :end element))
  21324. (throw 'not-all-p nil))))
  21325. t))))
  21326. (if all-fixed-width-p
  21327. (save-excursion
  21328. (goto-char begin)
  21329. (while (< (point) end)
  21330. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21331. (replace-match
  21332. "" nil nil nil
  21333. (if (= (line-end-position) (match-end 0)) 0 1)))
  21334. (forward-line)))
  21335. (let ((min-ind (point-max)))
  21336. ;; Find minimum indentation across all lines.
  21337. (save-excursion
  21338. (goto-char begin)
  21339. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21340. (setq min-ind 0)
  21341. (catch 'zerop
  21342. (while (< (point) end)
  21343. (unless (org-looking-at-p "[ \t]*$")
  21344. (let ((ind (org-get-indentation)))
  21345. (setq min-ind (min min-ind ind))
  21346. (when (zerop ind) (throw 'zerop t))))
  21347. (forward-line)))))
  21348. ;; Loop over all lines and add fixed-width markup everywhere
  21349. ;; but in fixed-width lines.
  21350. (save-excursion
  21351. (goto-char begin)
  21352. (while (< (point) end)
  21353. (cond
  21354. ((org-at-heading-p)
  21355. (insert ": ")
  21356. (forward-line)
  21357. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21358. (insert ":")
  21359. (forward-line)))
  21360. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21361. (let* ((element (org-element-at-point))
  21362. (element-end (org-element-property :end element)))
  21363. (if (eq (org-element-type element) 'fixed-width)
  21364. (progn (goto-char element-end)
  21365. (skip-chars-backward " \r\t\n")
  21366. (forward-line))
  21367. (let ((limit (min end element-end)))
  21368. (while (< (point) limit)
  21369. (org-move-to-column min-ind t)
  21370. (insert ": ")
  21371. (forward-line))))))
  21372. (t
  21373. (org-move-to-column min-ind t)
  21374. (insert ": ")
  21375. (forward-line)))))))
  21376. (set-marker end nil))))
  21377. ;;; Comments
  21378. ;; Org comments syntax is quite complex. It requires the entire line
  21379. ;; to be just a comment. Also, even with the right syntax at the
  21380. ;; beginning of line, some some elements (i.e. verse-block or
  21381. ;; example-block) don't accept comments. Usual Emacs comment commands
  21382. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21383. ;; Org still relies on `comment-dwim', but cannot trust
  21384. ;; `comment-only-p'. So, `comment-region-function' and
  21385. ;; `uncomment-region-function' both point
  21386. ;; to`org-comment-or-uncomment-region'. Eventually,
  21387. ;; `org-insert-comment' takes care of insertion of comments at the
  21388. ;; beginning of line.
  21389. ;; `org-setup-comments-handling' install comments related variables
  21390. ;; during `org-mode' initialization.
  21391. (defun org-setup-comments-handling ()
  21392. (interactive)
  21393. (org-set-local 'comment-use-syntax nil)
  21394. (org-set-local 'comment-start "# ")
  21395. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21396. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21397. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21398. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21399. (defun org-insert-comment ()
  21400. "Insert an empty comment above current line.
  21401. If the line is empty, insert comment at its beginning. When
  21402. point is within a source block, comment according to the related
  21403. major mode."
  21404. (if (let ((element (org-element-at-point)))
  21405. (and (eq (org-element-type element) 'src-block)
  21406. (< (save-excursion
  21407. (goto-char (org-element-property :post-affiliated element))
  21408. (line-end-position))
  21409. (point))
  21410. (> (save-excursion
  21411. (goto-char (org-element-property :end element))
  21412. (skip-chars-backward " \r\t\n")
  21413. (line-beginning-position))
  21414. (point))))
  21415. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21416. (beginning-of-line)
  21417. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21418. (open-line 1))
  21419. (org-indent-line)
  21420. (insert "# ")))
  21421. (defvar comment-empty-lines) ; From newcomment.el.
  21422. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21423. "Comment or uncomment each non-blank line in the region.
  21424. Uncomment each non-blank line between BEG and END if it only
  21425. contains commented lines. Otherwise, comment them. If region is
  21426. strictly within a source block, use appropriate comment syntax."
  21427. (if (let ((element (org-element-at-point)))
  21428. (and (eq (org-element-type element) 'src-block)
  21429. (< (save-excursion
  21430. (goto-char (org-element-property :post-affiliated element))
  21431. (line-end-position))
  21432. beg)
  21433. (>= (save-excursion
  21434. (goto-char (org-element-property :end element))
  21435. (skip-chars-backward " \r\t\n")
  21436. (line-beginning-position))
  21437. end)))
  21438. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21439. (save-restriction
  21440. ;; Restrict region
  21441. (narrow-to-region (save-excursion (goto-char beg)
  21442. (skip-chars-forward " \r\t\n" end)
  21443. (line-beginning-position))
  21444. (save-excursion (goto-char end)
  21445. (skip-chars-backward " \r\t\n" beg)
  21446. (line-end-position)))
  21447. (let ((uncommentp
  21448. ;; UNCOMMENTP is non-nil when every non blank line between
  21449. ;; BEG and END is a comment.
  21450. (save-excursion
  21451. (goto-char (point-min))
  21452. (while (and (not (eobp))
  21453. (let ((element (org-element-at-point)))
  21454. (and (eq (org-element-type element) 'comment)
  21455. (goto-char (min (point-max)
  21456. (org-element-property
  21457. :end element)))))))
  21458. (eobp))))
  21459. (if uncommentp
  21460. ;; Only blank lines and comments in region: uncomment it.
  21461. (save-excursion
  21462. (goto-char (point-min))
  21463. (while (not (eobp))
  21464. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21465. (replace-match "" nil nil nil 1))
  21466. (forward-line)))
  21467. ;; Comment each line in region.
  21468. (let ((min-indent (point-max)))
  21469. ;; First find the minimum indentation across all lines.
  21470. (save-excursion
  21471. (goto-char (point-min))
  21472. (while (and (not (eobp)) (not (zerop min-indent)))
  21473. (unless (looking-at "[ \t]*$")
  21474. (setq min-indent (min min-indent (current-indentation))))
  21475. (forward-line)))
  21476. ;; Then loop over all lines.
  21477. (save-excursion
  21478. (goto-char (point-min))
  21479. (while (not (eobp))
  21480. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21481. ;; Don't get fooled by invisible text (e.g. link path)
  21482. ;; when moving to column MIN-INDENT.
  21483. (let ((buffer-invisibility-spec nil))
  21484. (org-move-to-column min-indent t))
  21485. (insert comment-start))
  21486. (forward-line)))))))))
  21487. (defun org-comment-dwim (arg)
  21488. "Call `comment-dwim' within a source edit buffer if needed."
  21489. (interactive "*P")
  21490. (if (org-in-src-block-p)
  21491. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21492. (call-interactively 'comment-dwim)))
  21493. ;;; Timestamps API
  21494. ;; This section contains tools to operate on timestamp objects, as
  21495. ;; returned by, e.g. `org-element-context'.
  21496. (defun org-timestamp-has-time-p (timestamp)
  21497. "Non-nil when TIMESTAMP has a time specified."
  21498. (org-element-property :hour-start timestamp))
  21499. (defun org-timestamp-format (timestamp format &optional end utc)
  21500. "Format a TIMESTAMP element into a string.
  21501. FORMAT is a format specifier to be passed to
  21502. `format-time-string'.
  21503. When optional argument END is non-nil, use end of date-range or
  21504. time-range, if possible.
  21505. When optional argument UTC is non-nil, time will be expressed as
  21506. Universal Time."
  21507. (format-time-string
  21508. format
  21509. (apply 'encode-time
  21510. (cons 0
  21511. (mapcar
  21512. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21513. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21514. '(:minute-start :hour-start :day-start :month-start
  21515. :year-start)))))
  21516. utc))
  21517. (defun org-timestamp-split-range (timestamp &optional end)
  21518. "Extract a timestamp object from a date or time range.
  21519. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21520. the end of the range. Otherwise, extract its start.
  21521. Return a new timestamp object."
  21522. (let ((type (org-element-property :type timestamp)))
  21523. (if (memq type '(active inactive diary)) timestamp
  21524. (let ((split-ts (org-element-copy timestamp)))
  21525. ;; Set new type.
  21526. (org-element-put-property
  21527. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21528. ;; Copy start properties over end properties if END is
  21529. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21530. (let ((p-alist '((:minute-start . :minute-end)
  21531. (:hour-start . :hour-end)
  21532. (:day-start . :day-end)
  21533. (:month-start . :month-end)
  21534. (:year-start . :year-end))))
  21535. (dolist (p-cell p-alist)
  21536. (org-element-put-property
  21537. split-ts
  21538. (funcall (if end #'car #'cdr) p-cell)
  21539. (org-element-property
  21540. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21541. ;; Eventually refresh `:raw-value'.
  21542. (org-element-put-property split-ts :raw-value nil)
  21543. (org-element-put-property
  21544. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21545. (defun org-timestamp-translate (timestamp &optional boundary)
  21546. "Translate TIMESTAMP object to custom format.
  21547. Format string is defined in `org-time-stamp-custom-formats',
  21548. which see.
  21549. When optional argument BOUNDARY is non-nil, it is either the
  21550. symbol `start' or `end'. In this case, only translate the
  21551. starting or ending part of TIMESTAMP if it is a date or time
  21552. range. Otherwise, translate both parts.
  21553. Return timestamp as-is if `org-display-custom-times' is nil or if
  21554. it has a `diary' type."
  21555. (let ((type (org-element-property :type timestamp)))
  21556. (if (or (not org-display-custom-times) (eq type 'diary))
  21557. (org-element-interpret-data timestamp)
  21558. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21559. org-time-stamp-custom-formats)))
  21560. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21561. (concat (org-timestamp-format timestamp fmt)
  21562. "--"
  21563. (org-timestamp-format timestamp fmt t))
  21564. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21565. ;;; Other stuff.
  21566. (defun org-reftex-citation ()
  21567. "Use reftex-citation to insert a citation into the buffer.
  21568. This looks for a line like
  21569. #+BIBLIOGRAPHY: foo plain option:-d
  21570. and derives from it that foo.bib is the bibliography file relevant
  21571. for this document. It then installs the necessary environment for RefTeX
  21572. to work in this buffer and calls `reftex-citation' to insert a citation
  21573. into the buffer.
  21574. Export of such citations to both LaTeX and HTML is handled by the contributed
  21575. package ox-bibtex by Taru Karttunen."
  21576. (interactive)
  21577. (let ((reftex-docstruct-symbol 'rds)
  21578. (reftex-cite-format "\\cite{%l}")
  21579. rds bib)
  21580. (save-excursion
  21581. (save-restriction
  21582. (widen)
  21583. (let ((case-fold-search t)
  21584. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21585. (if (not (save-excursion
  21586. (or (re-search-forward re nil t)
  21587. (re-search-backward re nil t))))
  21588. (user-error "No bibliography defined in file")
  21589. (setq bib (concat (match-string 1) ".bib")
  21590. rds (list (list 'bib bib)))))))
  21591. (call-interactively 'reftex-citation)))
  21592. ;;;; Functions extending outline functionality
  21593. (defun org-beginning-of-line (&optional arg)
  21594. "Go to the beginning of the current line. If that is invisible, continue
  21595. to a visible line beginning. This makes the function of C-a more intuitive.
  21596. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21597. first attempt, and only move to after the tags when the cursor is already
  21598. beyond the end of the headline."
  21599. (interactive "P")
  21600. (let ((pos (point))
  21601. (special (if (consp org-special-ctrl-a/e)
  21602. (car org-special-ctrl-a/e)
  21603. org-special-ctrl-a/e))
  21604. deactivate-mark refpos)
  21605. (if (org-bound-and-true-p visual-line-mode)
  21606. (beginning-of-visual-line 1)
  21607. (beginning-of-line 1))
  21608. (if (and arg (fboundp 'move-beginning-of-line))
  21609. (call-interactively 'move-beginning-of-line)
  21610. (if (bobp)
  21611. nil
  21612. (backward-char 1)
  21613. (if (org-truely-invisible-p)
  21614. (while (and (not (bobp)) (org-truely-invisible-p))
  21615. (backward-char 1)
  21616. (beginning-of-line 1))
  21617. (forward-char 1))))
  21618. (when special
  21619. (cond
  21620. ((and (looking-at org-complex-heading-regexp)
  21621. (= (char-after (match-end 1)) ?\ ))
  21622. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21623. (point-at-eol)))
  21624. (goto-char
  21625. (if (eq special t)
  21626. (cond ((> pos refpos) refpos)
  21627. ((= pos (point)) refpos)
  21628. (t (point)))
  21629. (cond ((> pos (point)) (point))
  21630. ((not (eq last-command this-command)) (point))
  21631. (t refpos)))))
  21632. ((org-at-item-p)
  21633. ;; Being at an item and not looking at an the item means point
  21634. ;; was previously moved to beginning of a visual line, which
  21635. ;; doesn't contain the item. Therefore, do nothing special,
  21636. ;; just stay here.
  21637. (when (looking-at org-list-full-item-re)
  21638. ;; Set special position at first white space character after
  21639. ;; bullet, and check-box, if any.
  21640. (let ((after-bullet
  21641. (let ((box (match-end 3)))
  21642. (if (not box) (match-end 1)
  21643. (let ((after (char-after box)))
  21644. (if (and after (= after ? )) (1+ box) box))))))
  21645. ;; Special case: Move point to special position when
  21646. ;; currently after it or at beginning of line.
  21647. (if (eq special t)
  21648. (when (or (> pos after-bullet) (= (point) pos))
  21649. (goto-char after-bullet))
  21650. ;; Reversed case: Move point to special position when
  21651. ;; point was already at beginning of line and command is
  21652. ;; repeated.
  21653. (when (and (= (point) pos) (eq last-command this-command))
  21654. (goto-char after-bullet))))))))
  21655. (org-no-warnings
  21656. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21657. (setq disable-point-adjustment
  21658. (or (not (invisible-p (point)))
  21659. (not (invisible-p (max (point-min) (1- (point))))))))
  21660. (defun org-end-of-line (&optional arg)
  21661. "Go to the end of the line.
  21662. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21663. tags on the first attempt, and only move to after the tags when
  21664. the cursor is already beyond the end of the headline."
  21665. (interactive "P")
  21666. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21667. org-special-ctrl-a/e))
  21668. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21669. 'end-of-visual-line)
  21670. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21671. (t 'end-of-line)))
  21672. deactivate-mark)
  21673. (if (or (not special) arg) (call-interactively move-fun)
  21674. (let* ((element (save-excursion (beginning-of-line)
  21675. (org-element-at-point)))
  21676. (type (org-element-type element)))
  21677. (cond
  21678. ((memq type '(headline inlinetask))
  21679. (let ((pos (point)))
  21680. (beginning-of-line 1)
  21681. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21682. (if (eq special t)
  21683. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21684. (goto-char (match-beginning 1))
  21685. (goto-char (match-end 0)))
  21686. (if (or (< pos (match-end 0))
  21687. (not (eq this-command last-command)))
  21688. (goto-char (match-end 0))
  21689. (goto-char (match-beginning 1))))
  21690. (call-interactively move-fun))))
  21691. ((outline-invisible-p (line-end-position))
  21692. ;; If element is hidden, `move-end-of-line' would put point
  21693. ;; after it. Use `end-of-line' to stay on current line.
  21694. (call-interactively 'end-of-line))
  21695. (t (call-interactively move-fun)))))
  21696. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21697. (setq disable-point-adjustment
  21698. (or (not (invisible-p (point)))
  21699. (not (invisible-p (max (point-min) (1- (point))))))))
  21700. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21701. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21702. (defun org-backward-sentence (&optional arg)
  21703. "Go to beginning of sentence, or beginning of table field.
  21704. This will call `backward-sentence' or `org-table-beginning-of-field',
  21705. depending on context."
  21706. (interactive "P")
  21707. (cond
  21708. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21709. (t (call-interactively 'backward-sentence))))
  21710. (defun org-forward-sentence (&optional arg)
  21711. "Go to end of sentence, or end of table field.
  21712. This will call `forward-sentence' or `org-table-end-of-field',
  21713. depending on context."
  21714. (interactive "P")
  21715. (cond
  21716. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21717. (t (call-interactively 'forward-sentence))))
  21718. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21719. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21720. (defun org-kill-line (&optional arg)
  21721. "Kill line, to tags or end of line."
  21722. (interactive "P")
  21723. (cond
  21724. ((or (not org-special-ctrl-k)
  21725. (bolp)
  21726. (not (org-at-heading-p)))
  21727. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21728. org-ctrl-k-protect-subtree)
  21729. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21730. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21731. (user-error "C-k aborted as it would kill a hidden subtree")))
  21732. (call-interactively
  21733. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21734. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21735. (kill-region (point) (match-beginning 1))
  21736. (org-set-tags nil t))
  21737. (t (kill-region (point) (point-at-eol)))))
  21738. (define-key org-mode-map "\C-k" 'org-kill-line)
  21739. (defun org-yank (&optional arg)
  21740. "Yank. If the kill is a subtree, treat it specially.
  21741. This command will look at the current kill and check if is a single
  21742. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21743. cursor is at the beginning of a line or after the stars of a currently
  21744. empty headline, then the yank is handled specially. How exactly depends
  21745. on the value of the following variables, both set by default.
  21746. `org-yank-folded-subtrees'
  21747. When set, the subtree(s) will be folded after insertion, but only
  21748. if doing so would now swallow text after the yanked text.
  21749. `org-yank-adjusted-subtrees'
  21750. When set, the subtree will be promoted or demoted in order to
  21751. fit into the local outline tree structure, which means that the
  21752. level will be adjusted so that it becomes the smaller one of the
  21753. two *visible* surrounding headings.
  21754. Any prefix to this command will cause `yank' to be called directly with
  21755. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21756. will just
  21757. plainly yank the text as it is.
  21758. \[1] The test checks if the first non-white line is a heading
  21759. and if there are no other headings with fewer stars."
  21760. (interactive "P")
  21761. (org-yank-generic 'yank arg))
  21762. (defun org-yank-generic (command arg)
  21763. "Perform some yank-like command.
  21764. This function implements the behavior described in the `org-yank'
  21765. documentation. However, it has been generalized to work for any
  21766. interactive command with similar behavior."
  21767. ;; pretend to be command COMMAND
  21768. (setq this-command command)
  21769. (if arg
  21770. (call-interactively command)
  21771. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21772. (and (org-kill-is-subtree-p)
  21773. (or (bolp)
  21774. (and (looking-at "[ \t]*$")
  21775. (string-match
  21776. "\\`\\*+\\'"
  21777. (buffer-substring (point-at-bol) (point)))))))
  21778. swallowp)
  21779. (cond
  21780. ((and subtreep org-yank-folded-subtrees)
  21781. (let ((beg (point))
  21782. end)
  21783. (if (and subtreep org-yank-adjusted-subtrees)
  21784. (org-paste-subtree nil nil 'for-yank)
  21785. (call-interactively command))
  21786. (setq end (point))
  21787. (goto-char beg)
  21788. (when (and (bolp) subtreep
  21789. (not (setq swallowp
  21790. (org-yank-folding-would-swallow-text beg end))))
  21791. (org-with-limited-levels
  21792. (or (looking-at org-outline-regexp)
  21793. (re-search-forward org-outline-regexp-bol end t))
  21794. (while (and (< (point) end) (looking-at org-outline-regexp))
  21795. (hide-subtree)
  21796. (org-cycle-show-empty-lines 'folded)
  21797. (condition-case nil
  21798. (outline-forward-same-level 1)
  21799. (error (goto-char end))))))
  21800. (when swallowp
  21801. (message
  21802. "Inserted text not folded because that would swallow text"))
  21803. (goto-char end)
  21804. (skip-chars-forward " \t\n\r")
  21805. (beginning-of-line 1)
  21806. (push-mark beg 'nomsg)))
  21807. ((and subtreep org-yank-adjusted-subtrees)
  21808. (let ((beg (point-at-bol)))
  21809. (org-paste-subtree nil nil 'for-yank)
  21810. (push-mark beg 'nomsg)))
  21811. (t
  21812. (call-interactively command))))))
  21813. (defun org-yank-folding-would-swallow-text (beg end)
  21814. "Would hide-subtree at BEG swallow any text after END?"
  21815. (let (level)
  21816. (org-with-limited-levels
  21817. (save-excursion
  21818. (goto-char beg)
  21819. (when (or (looking-at org-outline-regexp)
  21820. (re-search-forward org-outline-regexp-bol end t))
  21821. (setq level (org-outline-level)))
  21822. (goto-char end)
  21823. (skip-chars-forward " \t\r\n\v\f")
  21824. (if (or (eobp)
  21825. (and (bolp) (looking-at org-outline-regexp)
  21826. (<= (org-outline-level) level)))
  21827. nil ; Nothing would be swallowed
  21828. t))))) ; something would swallow
  21829. (define-key org-mode-map "\C-y" 'org-yank)
  21830. (defun org-truely-invisible-p ()
  21831. "Check if point is at a character currently not visible.
  21832. This version does not only check the character property, but also
  21833. `visible-mode'."
  21834. ;; Early versions of noutline don't have `outline-invisible-p'.
  21835. (if (org-bound-and-true-p visible-mode)
  21836. nil
  21837. (outline-invisible-p)))
  21838. (defun org-invisible-p2 ()
  21839. "Check if point is at a character currently not visible."
  21840. (save-excursion
  21841. (if (and (eolp) (not (bobp))) (backward-char 1))
  21842. ;; Early versions of noutline don't have `outline-invisible-p'.
  21843. (outline-invisible-p)))
  21844. (defun org-back-to-heading (&optional invisible-ok)
  21845. "Call `outline-back-to-heading', but provide a better error message."
  21846. (condition-case nil
  21847. (outline-back-to-heading invisible-ok)
  21848. (error (error "Before first headline at position %d in buffer %s"
  21849. (point) (current-buffer)))))
  21850. (defun org-before-first-heading-p ()
  21851. "Before first heading?"
  21852. (save-excursion
  21853. (end-of-line)
  21854. (null (re-search-backward org-outline-regexp-bol nil t))))
  21855. (defun org-at-heading-p (&optional ignored)
  21856. (outline-on-heading-p t))
  21857. ;; Compatibility alias with Org versions < 7.8.03
  21858. (defalias 'org-on-heading-p 'org-at-heading-p)
  21859. (defun org-in-commented-heading-p (&optional no-inheritance)
  21860. "Non-nil if point is under a commented heading.
  21861. This function also checks ancestors of the current headline,
  21862. unless optional argument NO-INHERITANCE is non-nil."
  21863. (cond
  21864. ((org-before-first-heading-p) nil)
  21865. ((let ((headline (nth 4 (org-heading-components))))
  21866. (and headline
  21867. (let ((case-fold-search nil))
  21868. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  21869. headline)))))
  21870. (no-inheritance nil)
  21871. (t
  21872. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  21873. (defun org-at-comment-p nil
  21874. "Is cursor in a commented line?"
  21875. (save-excursion
  21876. (save-match-data
  21877. (beginning-of-line)
  21878. (looking-at "^[ \t]*# "))))
  21879. (defun org-at-drawer-p nil
  21880. "Is cursor at a drawer keyword?"
  21881. (save-excursion
  21882. (move-beginning-of-line 1)
  21883. (looking-at org-drawer-regexp)))
  21884. (defun org-at-block-p nil
  21885. "Is cursor at a block keyword?"
  21886. (save-excursion
  21887. (move-beginning-of-line 1)
  21888. (looking-at org-block-regexp)))
  21889. (defun org-point-at-end-of-empty-headline ()
  21890. "If point is at the end of an empty headline, return t, else nil.
  21891. If the heading only contains a TODO keyword, it is still still considered
  21892. empty."
  21893. (and (looking-at "[ \t]*$")
  21894. (when org-todo-line-regexp
  21895. (save-excursion
  21896. (beginning-of-line 1)
  21897. (let ((case-fold-search nil))
  21898. (looking-at org-todo-line-regexp)
  21899. (string= (match-string 3) ""))))))
  21900. (defun org-at-heading-or-item-p ()
  21901. (or (org-at-heading-p) (org-at-item-p)))
  21902. (defun org-at-target-p ()
  21903. (or (org-in-regexp org-radio-target-regexp)
  21904. (org-in-regexp org-target-regexp)))
  21905. ;; Compatibility alias with Org versions < 7.8.03
  21906. (defalias 'org-on-target-p 'org-at-target-p)
  21907. (defun org-up-heading-all (arg)
  21908. "Move to the heading line of which the present line is a subheading.
  21909. This function considers both visible and invisible heading lines.
  21910. With argument, move up ARG levels."
  21911. (if (fboundp 'outline-up-heading-all)
  21912. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21913. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21914. (defun org-up-heading-safe ()
  21915. "Move to the heading line of which the present line is a subheading.
  21916. This version will not throw an error. It will return the level of the
  21917. headline found, or nil if no higher level is found.
  21918. Also, this function will be a lot faster than `outline-up-heading',
  21919. because it relies on stars being the outline starters. This can really
  21920. make a significant difference in outlines with very many siblings."
  21921. (when (ignore-errors (org-back-to-heading t))
  21922. (let ((level-up (1- (funcall outline-level))))
  21923. (and (> level-up 0)
  21924. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  21925. (funcall outline-level)))))
  21926. (defun org-first-sibling-p ()
  21927. "Is this heading the first child of its parents?"
  21928. (interactive)
  21929. (let ((re org-outline-regexp-bol)
  21930. level l)
  21931. (unless (org-at-heading-p t)
  21932. (user-error "Not at a heading"))
  21933. (setq level (funcall outline-level))
  21934. (save-excursion
  21935. (if (not (re-search-backward re nil t))
  21936. t
  21937. (setq l (funcall outline-level))
  21938. (< l level)))))
  21939. (defun org-goto-sibling (&optional previous)
  21940. "Goto the next sibling, even if it is invisible.
  21941. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21942. when a sibling was found. When none is found, return nil and don't
  21943. move point."
  21944. (let ((fun (if previous 're-search-backward 're-search-forward))
  21945. (pos (point))
  21946. (re org-outline-regexp-bol)
  21947. level l)
  21948. (when (ignore-errors (org-back-to-heading t))
  21949. (setq level (funcall outline-level))
  21950. (catch 'exit
  21951. (or previous (forward-char 1))
  21952. (while (funcall fun re nil t)
  21953. (setq l (funcall outline-level))
  21954. (when (< l level) (goto-char pos) (throw 'exit nil))
  21955. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21956. (goto-char pos)
  21957. nil))))
  21958. (defun org-show-siblings ()
  21959. "Show all siblings of the current headline."
  21960. (save-excursion
  21961. (while (org-goto-sibling) (org-flag-heading nil)))
  21962. (save-excursion
  21963. (while (org-goto-sibling 'previous)
  21964. (org-flag-heading nil))))
  21965. (defun org-goto-first-child ()
  21966. "Goto the first child, even if it is invisible.
  21967. Return t when a child was found. Otherwise don't move point and
  21968. return nil."
  21969. (let (level (pos (point)) (re org-outline-regexp-bol))
  21970. (when (ignore-errors (org-back-to-heading t))
  21971. (setq level (outline-level))
  21972. (forward-char 1)
  21973. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21974. (progn (goto-char (match-beginning 0)) t)
  21975. (goto-char pos) nil))))
  21976. (defun org-show-hidden-entry ()
  21977. "Show an entry where even the heading is hidden."
  21978. (save-excursion
  21979. (org-show-entry)))
  21980. (defun org-flag-heading (flag &optional entry)
  21981. "Flag the current heading. FLAG non-nil means make invisible.
  21982. When ENTRY is non-nil, show the entire entry."
  21983. (save-excursion
  21984. (org-back-to-heading t)
  21985. ;; Check if we should show the entire entry
  21986. (if entry
  21987. (progn
  21988. (org-show-entry)
  21989. (save-excursion
  21990. (and (outline-next-heading)
  21991. (org-flag-heading nil))))
  21992. (outline-flag-region (max (point-min) (1- (point)))
  21993. (save-excursion (outline-end-of-heading) (point))
  21994. flag))))
  21995. (defun org-get-next-sibling ()
  21996. "Move to next heading of the same level, and return point.
  21997. If there is no such heading, return nil.
  21998. This is like outline-next-sibling, but invisible headings are ok."
  21999. (let ((level (funcall outline-level)))
  22000. (outline-next-heading)
  22001. (while (and (not (eobp)) (> (funcall outline-level) level))
  22002. (outline-next-heading))
  22003. (if (or (eobp) (< (funcall outline-level) level))
  22004. nil
  22005. (point))))
  22006. (defun org-get-last-sibling ()
  22007. "Move to previous heading of the same level, and return point.
  22008. If there is no such heading, return nil."
  22009. (let ((opoint (point))
  22010. (level (funcall outline-level)))
  22011. (outline-previous-heading)
  22012. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22013. (while (and (> (funcall outline-level) level)
  22014. (not (bobp)))
  22015. (outline-previous-heading))
  22016. (if (< (funcall outline-level) level)
  22017. nil
  22018. (point)))))
  22019. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22020. "Goto to the end of a subtree."
  22021. ;; This contains an exact copy of the original function, but it uses
  22022. ;; `org-back-to-heading', to make it work also in invisible
  22023. ;; trees. And is uses an invisible-ok argument.
  22024. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22025. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22026. ;; with many children and grandchildren etc, this can be much faster
  22027. ;; than the outline version.
  22028. (org-back-to-heading invisible-ok)
  22029. (let ((first t)
  22030. (level (funcall outline-level)))
  22031. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22032. ;; A true heading (not a plain list item), in Org-mode
  22033. ;; This means we can easily find the end by looking
  22034. ;; only for the right number of stars. Using a regexp to do
  22035. ;; this is so much faster than using a Lisp loop.
  22036. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22037. (forward-char 1)
  22038. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22039. ;; something else, do it the slow way
  22040. (while (and (not (eobp))
  22041. (or first (> (funcall outline-level) level)))
  22042. (setq first nil)
  22043. (outline-next-heading)))
  22044. (unless to-heading
  22045. (if (memq (preceding-char) '(?\n ?\^M))
  22046. (progn
  22047. ;; Go to end of line before heading
  22048. (forward-char -1)
  22049. (if (memq (preceding-char) '(?\n ?\^M))
  22050. ;; leave blank line before heading
  22051. (forward-char -1))))))
  22052. (point))
  22053. (defun org-end-of-meta-data (&optional full)
  22054. "Skip planning line and properties drawer in current entry.
  22055. When optional argument FULL is non-nil, also skip empty lines,
  22056. clocking lines and regular drawers at the beginning of the
  22057. entry."
  22058. (org-back-to-heading t)
  22059. (forward-line)
  22060. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22061. (when (looking-at org-property-drawer-re)
  22062. (goto-char (match-end 0))
  22063. (forward-line))
  22064. (when (and full (not (org-at-heading-p)))
  22065. (catch 'exit
  22066. (let ((end (save-excursion (outline-next-heading) (point)))
  22067. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22068. (while (not (eobp))
  22069. (cond ((org-looking-at-p org-drawer-regexp)
  22070. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22071. (forward-line)
  22072. (throw 'exit t)))
  22073. ((org-looking-at-p re) (forward-line))
  22074. (t (throw 'exit t))))))))
  22075. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22076. "Move forward to the ARG'th subheading at same level as this one.
  22077. Stop at the first and last subheadings of a superior heading.
  22078. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22079. non-nil it will also look at invisible ones."
  22080. (interactive "p")
  22081. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22082. (if (and arg (< arg 0))
  22083. (goto-char (point-min))
  22084. (outline-next-heading))
  22085. (org-at-heading-p)
  22086. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22087. (f (if (and arg (< arg 0))
  22088. 're-search-backward
  22089. 're-search-forward))
  22090. (count (if arg (abs arg) 1))
  22091. (result (point)))
  22092. (while (and (prog1 (> count 0)
  22093. (forward-char (if (and arg (< arg 0)) -1 1)))
  22094. (funcall f org-outline-regexp-bol nil 'move))
  22095. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22096. (cond ((< l level) (setq count 0))
  22097. ((and (= l level)
  22098. (or invisible-ok
  22099. (progn
  22100. (goto-char (line-beginning-position))
  22101. (not (outline-invisible-p)))))
  22102. (setq count (1- count))
  22103. (when (eq l level)
  22104. (setq result (point)))))))
  22105. (goto-char result))
  22106. (beginning-of-line 1)))
  22107. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22108. "Move backward to the ARG'th subheading at same level as this one.
  22109. Stop at the first and last subheadings of a superior heading."
  22110. (interactive "p")
  22111. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22112. (defun org-next-visible-heading (arg)
  22113. "Move to the next visible heading.
  22114. This function wraps `outline-next-visible-heading' with
  22115. `org-with-limited-levels' in order to skip over inline tasks and
  22116. respect customization of `org-odd-levels-only'."
  22117. (interactive "p")
  22118. (org-with-limited-levels
  22119. (outline-next-visible-heading arg)))
  22120. (defun org-previous-visible-heading (arg)
  22121. "Move to the next visible heading.
  22122. This function wraps `outline-previous-visible-heading' with
  22123. `org-with-limited-levels' in order to skip over inline tasks and
  22124. respect customization of `org-odd-levels-only'."
  22125. (interactive "p")
  22126. (org-with-limited-levels
  22127. (outline-previous-visible-heading arg)))
  22128. (defun org-next-block (arg &optional backward block-regexp)
  22129. "Jump to the next block.
  22130. With a prefix argument ARG, jump forward ARG many source blocks.
  22131. When BACKWARD is non-nil, jump to the previous block.
  22132. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22133. (interactive "p")
  22134. (let ((re (or block-regexp org-block-regexp))
  22135. (re-search-fn (or (and backward 're-search-backward)
  22136. 're-search-forward)))
  22137. (if (looking-at re) (forward-char 1))
  22138. (condition-case nil
  22139. (funcall re-search-fn re nil nil arg)
  22140. (error (user-error "No %s code blocks"
  22141. (if backward "previous" "further" ))))
  22142. (goto-char (match-beginning 0)) (org-show-context)))
  22143. (defun org-previous-block (arg &optional block-regexp)
  22144. "Jump to the previous block.
  22145. With a prefix argument ARG, jump backward ARG many source blocks.
  22146. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22147. (interactive "p")
  22148. (org-next-block arg t block-regexp))
  22149. (defun org-forward-paragraph ()
  22150. "Move forward to beginning of next paragraph or equivalent.
  22151. The function moves point to the beginning of the next visible
  22152. structural element, which can be a paragraph, a table, a list
  22153. item, etc. It also provides some special moves for convenience:
  22154. - On an affiliated keyword, jump to the beginning of the
  22155. relative element.
  22156. - On an item or a footnote definition, move to the second
  22157. element inside, if any.
  22158. - On a table or a property drawer, jump after it.
  22159. - On a verse or source block, stop after blank lines."
  22160. (interactive)
  22161. (when (eobp) (user-error "Cannot move further down"))
  22162. (let* ((deactivate-mark nil)
  22163. (element (org-element-at-point))
  22164. (type (org-element-type element))
  22165. (post-affiliated (org-element-property :post-affiliated element))
  22166. (contents-begin (org-element-property :contents-begin element))
  22167. (contents-end (org-element-property :contents-end element))
  22168. (end (let ((end (org-element-property :end element)) (parent element))
  22169. (while (and (setq parent (org-element-property :parent parent))
  22170. (= (org-element-property :contents-end parent) end))
  22171. (setq end (org-element-property :end parent)))
  22172. end)))
  22173. (cond ((not element)
  22174. (skip-chars-forward " \r\t\n")
  22175. (or (eobp) (beginning-of-line)))
  22176. ;; On affiliated keywords, move to element's beginning.
  22177. ((< (point) post-affiliated)
  22178. (goto-char post-affiliated))
  22179. ;; At a table row, move to the end of the table. Similarly,
  22180. ;; at a node property, move to the end of the property
  22181. ;; drawer.
  22182. ((memq type '(node-property table-row))
  22183. (goto-char (org-element-property
  22184. :end (org-element-property :parent element))))
  22185. ((memq type '(property-drawer table)) (goto-char end))
  22186. ;; Consider blank lines as separators in verse and source
  22187. ;; blocks to ease editing.
  22188. ((memq type '(src-block verse-block))
  22189. (when (eq type 'src-block)
  22190. (setq contents-end
  22191. (save-excursion (goto-char end)
  22192. (skip-chars-backward " \r\t\n")
  22193. (line-beginning-position))))
  22194. (beginning-of-line)
  22195. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22196. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22197. (goto-char end)
  22198. (skip-chars-forward " \r\t\n")
  22199. (if (= (point) contents-end) (goto-char end)
  22200. (beginning-of-line))))
  22201. ;; With no contents, just skip element.
  22202. ((not contents-begin) (goto-char end))
  22203. ;; If contents are invisible, skip the element altogether.
  22204. ((outline-invisible-p (line-end-position))
  22205. (case type
  22206. (headline
  22207. (org-with-limited-levels (outline-next-visible-heading 1)))
  22208. ;; At a plain list, make sure we move to the next item
  22209. ;; instead of skipping the whole list.
  22210. (plain-list (forward-char)
  22211. (org-forward-paragraph))
  22212. (otherwise (goto-char end))))
  22213. ((>= (point) contents-end) (goto-char end))
  22214. ((>= (point) contents-begin)
  22215. ;; This can only happen on paragraphs and plain lists.
  22216. (case type
  22217. (paragraph (goto-char end))
  22218. ;; At a plain list, try to move to second element in
  22219. ;; first item, if possible.
  22220. (plain-list (end-of-line)
  22221. (org-forward-paragraph))))
  22222. ;; When contents start on the middle of a line (e.g. in
  22223. ;; items and footnote definitions), try to reach first
  22224. ;; element starting after current line.
  22225. ((> (line-end-position) contents-begin)
  22226. (end-of-line)
  22227. (org-forward-paragraph))
  22228. (t (goto-char contents-begin)))))
  22229. (defun org-backward-paragraph ()
  22230. "Move backward to start of previous paragraph or equivalent.
  22231. The function moves point to the beginning of the current
  22232. structural element, which can be a paragraph, a table, a list
  22233. item, etc., or to the beginning of the previous visible one if
  22234. point is already there. It also provides some special moves for
  22235. convenience:
  22236. - On an affiliated keyword, jump to the first one.
  22237. - On a table or a property drawer, move to its beginning.
  22238. - On a verse or source block, stop before blank lines."
  22239. (interactive)
  22240. (when (bobp) (user-error "Cannot move further up"))
  22241. (let* ((deactivate-mark nil)
  22242. (element (org-element-at-point))
  22243. (type (org-element-type element))
  22244. (contents-begin (org-element-property :contents-begin element))
  22245. (contents-end (org-element-property :contents-end element))
  22246. (post-affiliated (org-element-property :post-affiliated element))
  22247. (begin (org-element-property :begin element)))
  22248. (cond
  22249. ((not element) (goto-char (point-min)))
  22250. ((= (point) begin)
  22251. (backward-char)
  22252. (org-backward-paragraph))
  22253. ((<= (point) post-affiliated) (goto-char begin))
  22254. ((memq type '(node-property table-row))
  22255. (goto-char (org-element-property
  22256. :post-affiliated (org-element-property :parent element))))
  22257. ((memq type '(property-drawer table)) (goto-char begin))
  22258. ((memq type '(src-block verse-block))
  22259. (when (eq type 'src-block)
  22260. (setq contents-begin
  22261. (save-excursion (goto-char begin) (forward-line) (point))))
  22262. (if (= (point) contents-begin) (goto-char post-affiliated)
  22263. ;; Inside a verse block, see blank lines as paragraph
  22264. ;; separators.
  22265. (let ((origin (point)))
  22266. (skip-chars-backward " \r\t\n" contents-begin)
  22267. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22268. (skip-chars-forward " \r\t\n" origin)
  22269. (if (= (point) origin) (goto-char contents-begin)
  22270. (beginning-of-line))))))
  22271. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22272. ((eq type 'paragraph)
  22273. (goto-char contents-begin)
  22274. ;; When at first paragraph in an item or a footnote definition,
  22275. ;; move directly to beginning of line.
  22276. (let ((parent-contents
  22277. (org-element-property
  22278. :contents-begin (org-element-property :parent element))))
  22279. (when (and parent-contents (= parent-contents contents-begin))
  22280. (beginning-of-line))))
  22281. ;; At the end of a greater element, move to the beginning of the
  22282. ;; last element within.
  22283. ((>= (point) contents-end)
  22284. (goto-char (1- contents-end))
  22285. (org-backward-paragraph))
  22286. (t (goto-char (or post-affiliated begin))))
  22287. ;; Ensure we never leave point invisible.
  22288. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22289. (defun org-forward-element ()
  22290. "Move forward by one element.
  22291. Move to the next element at the same level, when possible."
  22292. (interactive)
  22293. (cond ((eobp) (user-error "Cannot move further down"))
  22294. ((org-with-limited-levels (org-at-heading-p))
  22295. (let ((origin (point)))
  22296. (goto-char (org-end-of-subtree nil t))
  22297. (unless (org-with-limited-levels (org-at-heading-p))
  22298. (goto-char origin)
  22299. (user-error "Cannot move further down"))))
  22300. (t
  22301. (let* ((elem (org-element-at-point))
  22302. (end (org-element-property :end elem))
  22303. (parent (org-element-property :parent elem)))
  22304. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22305. (goto-char (org-element-property :end parent)))
  22306. ((integer-or-marker-p end) (goto-char end))
  22307. (t (message "No element at point")))))))
  22308. (defun org-backward-element ()
  22309. "Move backward by one element.
  22310. Move to the previous element at the same level, when possible."
  22311. (interactive)
  22312. (cond ((bobp) (user-error "Cannot move further up"))
  22313. ((org-with-limited-levels (org-at-heading-p))
  22314. ;; At a headline, move to the previous one, if any, or stay
  22315. ;; here.
  22316. (let ((origin (point)))
  22317. (org-with-limited-levels (org-backward-heading-same-level 1))
  22318. ;; When current headline has no sibling above, move to its
  22319. ;; parent.
  22320. (when (= (point) origin)
  22321. (or (org-with-limited-levels (org-up-heading-safe))
  22322. (progn (goto-char origin)
  22323. (user-error "Cannot move further up"))))))
  22324. (t
  22325. (let* ((elem (org-element-at-point))
  22326. (beg (org-element-property :begin elem)))
  22327. (cond
  22328. ;; Move to beginning of current element if point isn't
  22329. ;; there already.
  22330. ((null beg) (message "No element at point"))
  22331. ((/= (point) beg) (goto-char beg))
  22332. (t (goto-char beg)
  22333. (skip-chars-backward " \r\t\n")
  22334. (unless (bobp)
  22335. (let ((prev (org-element-at-point)))
  22336. (goto-char (org-element-property :begin prev))
  22337. (while (and (setq prev (org-element-property :parent prev))
  22338. (<= (org-element-property :end prev) beg))
  22339. (goto-char (org-element-property :begin prev)))))))))))
  22340. (defun org-up-element ()
  22341. "Move to upper element."
  22342. (interactive)
  22343. (if (org-with-limited-levels (org-at-heading-p))
  22344. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22345. (let* ((elem (org-element-at-point))
  22346. (parent (org-element-property :parent elem)))
  22347. (if parent (goto-char (org-element-property :begin parent))
  22348. (if (org-with-limited-levels (org-before-first-heading-p))
  22349. (user-error "No surrounding element")
  22350. (org-with-limited-levels (org-back-to-heading)))))))
  22351. (defvar org-element-greater-elements)
  22352. (defun org-down-element ()
  22353. "Move to inner element."
  22354. (interactive)
  22355. (let ((element (org-element-at-point)))
  22356. (cond
  22357. ((memq (org-element-type element) '(plain-list table))
  22358. (goto-char (org-element-property :contents-begin element))
  22359. (forward-char))
  22360. ((memq (org-element-type element) org-element-greater-elements)
  22361. ;; If contents are hidden, first disclose them.
  22362. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22363. (goto-char (or (org-element-property :contents-begin element)
  22364. (user-error "No content for this element"))))
  22365. (t (user-error "No inner element")))))
  22366. (defun org-drag-element-backward ()
  22367. "Move backward element at point."
  22368. (interactive)
  22369. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22370. (let* ((elem (org-element-at-point))
  22371. (prev-elem
  22372. (save-excursion
  22373. (goto-char (org-element-property :begin elem))
  22374. (skip-chars-backward " \r\t\n")
  22375. (unless (bobp)
  22376. (let* ((beg (org-element-property :begin elem))
  22377. (prev (org-element-at-point))
  22378. (up prev))
  22379. (while (and (setq up (org-element-property :parent up))
  22380. (<= (org-element-property :end up) beg))
  22381. (setq prev up))
  22382. prev)))))
  22383. ;; Error out if no previous element or previous element is
  22384. ;; a parent of the current one.
  22385. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22386. (user-error "Cannot drag element backward")
  22387. (let ((pos (point)))
  22388. (org-element-swap-A-B prev-elem elem)
  22389. (goto-char (+ (org-element-property :begin prev-elem)
  22390. (- pos (org-element-property :begin elem)))))))))
  22391. (defun org-drag-element-forward ()
  22392. "Move forward element at point."
  22393. (interactive)
  22394. (let* ((pos (point))
  22395. (elem (org-element-at-point)))
  22396. (when (= (point-max) (org-element-property :end elem))
  22397. (user-error "Cannot drag element forward"))
  22398. (goto-char (org-element-property :end elem))
  22399. (let ((next-elem (org-element-at-point)))
  22400. (when (or (org-element-nested-p elem next-elem)
  22401. (and (eq (org-element-type next-elem) 'headline)
  22402. (not (eq (org-element-type elem) 'headline))))
  22403. (goto-char pos)
  22404. (user-error "Cannot drag element forward"))
  22405. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22406. ;; body's length (without final blanks) and by the length of
  22407. ;; blanks between ELEM and NEXT-ELEM.
  22408. (let ((size-next (- (save-excursion
  22409. (goto-char (org-element-property :end next-elem))
  22410. (skip-chars-backward " \r\t\n")
  22411. (forward-line)
  22412. ;; Small correction if buffer doesn't end
  22413. ;; with a newline character.
  22414. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22415. (org-element-property :begin next-elem)))
  22416. (size-blank (- (org-element-property :end elem)
  22417. (save-excursion
  22418. (goto-char (org-element-property :end elem))
  22419. (skip-chars-backward " \r\t\n")
  22420. (forward-line)
  22421. (point)))))
  22422. (org-element-swap-A-B elem next-elem)
  22423. (goto-char (+ pos size-next size-blank))))))
  22424. (defun org-drag-line-forward (arg)
  22425. "Drag the line at point ARG lines forward."
  22426. (interactive "p")
  22427. (dotimes (n (abs arg))
  22428. (let ((c (current-column)))
  22429. (if (< 0 arg)
  22430. (progn
  22431. (beginning-of-line 2)
  22432. (transpose-lines 1)
  22433. (beginning-of-line 0))
  22434. (transpose-lines 1)
  22435. (beginning-of-line -1))
  22436. (org-move-to-column c))))
  22437. (defun org-drag-line-backward (arg)
  22438. "Drag the line at point ARG lines backward."
  22439. (interactive "p")
  22440. (org-drag-line-forward (- arg)))
  22441. (defun org-mark-element ()
  22442. "Put point at beginning of this element, mark at end.
  22443. Interactively, if this command is repeated or (in Transient Mark
  22444. mode) if the mark is active, it marks the next element after the
  22445. ones already marked."
  22446. (interactive)
  22447. (let (deactivate-mark)
  22448. (if (and (org-called-interactively-p 'any)
  22449. (or (and (eq last-command this-command) (mark t))
  22450. (and transient-mark-mode mark-active)))
  22451. (set-mark
  22452. (save-excursion
  22453. (goto-char (mark))
  22454. (goto-char (org-element-property :end (org-element-at-point)))))
  22455. (let ((element (org-element-at-point)))
  22456. (end-of-line)
  22457. (push-mark (org-element-property :end element) t t)
  22458. (goto-char (org-element-property :begin element))))))
  22459. (defun org-narrow-to-element ()
  22460. "Narrow buffer to current element."
  22461. (interactive)
  22462. (let ((elem (org-element-at-point)))
  22463. (cond
  22464. ((eq (car elem) 'headline)
  22465. (narrow-to-region
  22466. (org-element-property :begin elem)
  22467. (org-element-property :end elem)))
  22468. ((memq (car elem) org-element-greater-elements)
  22469. (narrow-to-region
  22470. (org-element-property :contents-begin elem)
  22471. (org-element-property :contents-end elem)))
  22472. (t
  22473. (narrow-to-region
  22474. (org-element-property :begin elem)
  22475. (org-element-property :end elem))))))
  22476. (defun org-transpose-element ()
  22477. "Transpose current and previous elements, keeping blank lines between.
  22478. Point is moved after both elements."
  22479. (interactive)
  22480. (org-skip-whitespace)
  22481. (let ((end (org-element-property :end (org-element-at-point))))
  22482. (org-drag-element-backward)
  22483. (goto-char end)))
  22484. (defun org-unindent-buffer ()
  22485. "Un-indent the visible part of the buffer.
  22486. Relative indentation (between items, inside blocks, etc.) isn't
  22487. modified."
  22488. (interactive)
  22489. (unless (eq major-mode 'org-mode)
  22490. (user-error "Cannot un-indent a buffer not in Org mode"))
  22491. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22492. unindent-tree ; For byte-compiler.
  22493. (unindent-tree
  22494. (function
  22495. (lambda (contents)
  22496. (mapc
  22497. (lambda (element)
  22498. (if (memq (org-element-type element) '(headline section))
  22499. (funcall unindent-tree (org-element-contents element))
  22500. (save-excursion
  22501. (save-restriction
  22502. (narrow-to-region
  22503. (org-element-property :begin element)
  22504. (org-element-property :end element))
  22505. (org-do-remove-indentation)))))
  22506. (reverse contents))))))
  22507. (funcall unindent-tree (org-element-contents parse-tree))))
  22508. (defun org-show-subtree ()
  22509. "Show everything after this heading at deeper levels."
  22510. (interactive)
  22511. (outline-flag-region
  22512. (point)
  22513. (save-excursion
  22514. (org-end-of-subtree t t))
  22515. nil))
  22516. (defun org-show-entry ()
  22517. "Show the body directly following this heading.
  22518. Show the heading too, if it is currently invisible."
  22519. (interactive)
  22520. (save-excursion
  22521. (ignore-errors
  22522. (org-back-to-heading t)
  22523. (outline-flag-region
  22524. (max (point-min) (1- (point)))
  22525. (save-excursion
  22526. (if (re-search-forward
  22527. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22528. (match-beginning 1)
  22529. (point-max)))
  22530. nil)
  22531. (org-cycle-hide-drawers 'children))))
  22532. (defun org-make-options-regexp (kwds &optional extra)
  22533. "Make a regular expression for keyword lines.
  22534. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22535. when non-nil, is a regexp matching keywords names."
  22536. (concat "^[ \t]*#\\+\\("
  22537. (regexp-opt kwds)
  22538. (and extra (concat (and kwds "\\|") extra))
  22539. "\\):[ \t]*\\(.*\\)"))
  22540. ;; Make isearch reveal the necessary context
  22541. (defun org-isearch-end ()
  22542. "Reveal context after isearch exits."
  22543. (when isearch-success ; only if search was successful
  22544. (if (featurep 'xemacs)
  22545. ;; Under XEmacs, the hook is run in the correct place,
  22546. ;; we directly show the context.
  22547. (org-show-context 'isearch)
  22548. ;; In Emacs the hook runs *before* restoring the overlays.
  22549. ;; So we have to use a one-time post-command-hook to do this.
  22550. ;; (Emacs 22 has a special variable, see function `org-mode')
  22551. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22552. isearch-mode-end-hook-quit)
  22553. ;; Only when the isearch was not quitted.
  22554. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22555. 'append 'local)))))
  22556. (defun org-isearch-post-command ()
  22557. "Remove self from hook, and show context."
  22558. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22559. (org-show-context 'isearch))
  22560. ;;;; Integration with and fixes for other packages
  22561. ;;; Imenu support
  22562. (defvar org-imenu-markers nil
  22563. "All markers currently used by Imenu.")
  22564. (make-variable-buffer-local 'org-imenu-markers)
  22565. (defun org-imenu-new-marker (&optional pos)
  22566. "Return a new marker for use by Imenu, and remember the marker."
  22567. (let ((m (make-marker)))
  22568. (move-marker m (or pos (point)))
  22569. (push m org-imenu-markers)
  22570. m))
  22571. (defun org-imenu-get-tree ()
  22572. "Produce the index for Imenu."
  22573. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22574. (setq org-imenu-markers nil)
  22575. (let* ((n org-imenu-depth)
  22576. (re (concat "^" (org-get-limited-outline-regexp)))
  22577. (subs (make-vector (1+ n) nil))
  22578. (last-level 0)
  22579. m level head0 head)
  22580. (save-excursion
  22581. (save-restriction
  22582. (widen)
  22583. (goto-char (point-max))
  22584. (while (re-search-backward re nil t)
  22585. (setq level (org-reduced-level (funcall outline-level)))
  22586. (when (and (<= level n)
  22587. (looking-at org-complex-heading-regexp)
  22588. (setq head0 (org-match-string-no-properties 4)))
  22589. (setq head (org-link-display-format head0)
  22590. m (org-imenu-new-marker))
  22591. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22592. (if (>= level last-level)
  22593. (push (cons head m) (aref subs level))
  22594. (push (cons head (aref subs (1+ level))) (aref subs level))
  22595. (loop for i from (1+ level) to n do (aset subs i nil)))
  22596. (setq last-level level)))))
  22597. (aref subs 1)))
  22598. (eval-after-load "imenu"
  22599. '(progn
  22600. (add-hook 'imenu-after-jump-hook
  22601. (lambda ()
  22602. (if (derived-mode-p 'org-mode)
  22603. (org-show-context 'org-goto))))))
  22604. (defun org-link-display-format (link)
  22605. "Replace a link with its the description.
  22606. If there is no description, use the link target."
  22607. (save-match-data
  22608. (if (string-match org-bracket-link-analytic-regexp link)
  22609. (replace-match (if (match-end 5)
  22610. (match-string 5 link)
  22611. (concat (match-string 1 link)
  22612. (match-string 3 link)))
  22613. nil t link)
  22614. link)))
  22615. (defun org-toggle-link-display ()
  22616. "Toggle the literal or descriptive display of links."
  22617. (interactive)
  22618. (if org-descriptive-links
  22619. (progn (org-remove-from-invisibility-spec '(org-link))
  22620. (org-restart-font-lock)
  22621. (setq org-descriptive-links nil))
  22622. (progn (add-to-invisibility-spec '(org-link))
  22623. (org-restart-font-lock)
  22624. (setq org-descriptive-links t))))
  22625. ;; Speedbar support
  22626. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22627. "Overlay marking the agenda restriction line in speedbar.")
  22628. (overlay-put org-speedbar-restriction-lock-overlay
  22629. 'face 'org-agenda-restriction-lock)
  22630. (overlay-put org-speedbar-restriction-lock-overlay
  22631. 'help-echo "Agendas are currently limited to this item.")
  22632. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22633. (defun org-speedbar-set-agenda-restriction ()
  22634. "Restrict future agenda commands to the location at point in speedbar.
  22635. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22636. (interactive)
  22637. (require 'org-agenda)
  22638. (let (p m tp np dir txt)
  22639. (cond
  22640. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22641. 'org-imenu t))
  22642. (setq m (get-text-property p 'org-imenu-marker))
  22643. (with-current-buffer (marker-buffer m)
  22644. (goto-char m)
  22645. (org-agenda-set-restriction-lock 'subtree)))
  22646. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22647. 'speedbar-function 'speedbar-find-file))
  22648. (setq tp (previous-single-property-change
  22649. (1+ p) 'speedbar-function)
  22650. np (next-single-property-change
  22651. tp 'speedbar-function)
  22652. dir (speedbar-line-directory)
  22653. txt (buffer-substring-no-properties (or tp (point-min))
  22654. (or np (point-max))))
  22655. (with-current-buffer (find-file-noselect
  22656. (let ((default-directory dir))
  22657. (expand-file-name txt)))
  22658. (unless (derived-mode-p 'org-mode)
  22659. (user-error "Cannot restrict to non-Org-mode file"))
  22660. (org-agenda-set-restriction-lock 'file)))
  22661. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22662. (move-overlay org-speedbar-restriction-lock-overlay
  22663. (point-at-bol) (point-at-eol))
  22664. (setq current-prefix-arg nil)
  22665. (org-agenda-maybe-redo)))
  22666. (defvar speedbar-file-key-map)
  22667. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22668. (eval-after-load "speedbar"
  22669. '(progn
  22670. (speedbar-add-supported-extension ".org")
  22671. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22672. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22673. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22674. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22675. (add-hook 'speedbar-visiting-tag-hook
  22676. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22677. ;;; Fixes and Hacks for problems with other packages
  22678. (defun org--flyspell-object-check-p (element)
  22679. "Non-nil when Flyspell can check object at point.
  22680. ELEMENT is the element at point."
  22681. (let ((object (save-excursion
  22682. (when (org-looking-at-p "\\>") (backward-char))
  22683. (org-element-context element))))
  22684. (case (org-element-type object)
  22685. ;; Prevent checks in links due to keybinding conflict with
  22686. ;; Flyspell.
  22687. ((code entity export-snippet inline-babel-call
  22688. inline-src-block line-break latex-fragment link macro
  22689. statistics-cookie target timestamp verbatim)
  22690. nil)
  22691. (footnote-reference
  22692. ;; Only in inline footnotes, within the definition.
  22693. (and (eq (org-element-property :type object) 'inline)
  22694. (< (save-excursion
  22695. (goto-char (org-element-property :begin object))
  22696. (search-forward ":" nil t 2))
  22697. (point))))
  22698. (otherwise t))))
  22699. (defun org-mode-flyspell-verify ()
  22700. "Function used for `flyspell-generic-check-word-predicate'."
  22701. (if (org-at-heading-p)
  22702. ;; At a headline or an inlinetask, check title only. This is
  22703. ;; faster than relying on `org-element-at-point'.
  22704. (and (save-excursion (beginning-of-line)
  22705. (and (let ((case-fold-search t))
  22706. (not (looking-at "\\*+ END[ \t]*$")))
  22707. (looking-at org-complex-heading-regexp)))
  22708. (match-beginning 4)
  22709. (>= (point) (match-beginning 4))
  22710. (or (not (match-beginning 5))
  22711. (< (point) (match-beginning 5))))
  22712. (let* ((element (org-element-at-point))
  22713. (post-affiliated (org-element-property :post-affiliated element)))
  22714. (cond
  22715. ;; Ignore checks in all affiliated keywords but captions.
  22716. ((< (point) post-affiliated)
  22717. (and (save-excursion
  22718. (beginning-of-line)
  22719. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22720. (> (point) (match-end 0))
  22721. (org--flyspell-object-check-p element)))
  22722. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22723. ((let ((log (org-log-into-drawer)))
  22724. (and log
  22725. (let ((drawer (org-element-lineage element '(drawer))))
  22726. (and drawer
  22727. (eq (compare-strings
  22728. log nil nil
  22729. (org-element-property :drawer-name drawer) nil nil t)
  22730. t)))))
  22731. nil)
  22732. (t
  22733. (case (org-element-type element)
  22734. ((comment quote-section) t)
  22735. (comment-block
  22736. ;; Allow checks between block markers, not on them.
  22737. (and (> (line-beginning-position) post-affiliated)
  22738. (save-excursion
  22739. (end-of-line)
  22740. (skip-chars-forward " \r\t\n")
  22741. (< (point) (org-element-property :end element)))))
  22742. ;; Arbitrary list of keywords where checks are meaningful.
  22743. ;; Make sure point is on the value part of the element.
  22744. (keyword
  22745. (and (member (org-element-property :key element)
  22746. '("DESCRIPTION" "TITLE"))
  22747. (< (save-excursion
  22748. (beginning-of-line) (search-forward ":") (point))
  22749. (point))))
  22750. ;; Check is globally allowed in paragraphs verse blocks and
  22751. ;; table rows (after affiliated keywords) but some objects
  22752. ;; must not be affected.
  22753. ((paragraph table-row verse-block)
  22754. (let ((cbeg (org-element-property :contents-begin element))
  22755. (cend (org-element-property :contents-end element)))
  22756. (and cbeg (>= (point) cbeg) (< (point) cend)
  22757. (org--flyspell-object-check-p element))))))))))
  22758. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22759. (defun org-remove-flyspell-overlays-in (beg end)
  22760. "Remove flyspell overlays in region."
  22761. (and (org-bound-and-true-p flyspell-mode)
  22762. (fboundp 'flyspell-delete-region-overlays)
  22763. (flyspell-delete-region-overlays beg end)))
  22764. (defvar flyspell-delayed-commands)
  22765. (eval-after-load "flyspell"
  22766. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22767. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22768. (eval-after-load "bookmark"
  22769. '(if (boundp 'bookmark-after-jump-hook)
  22770. ;; We can use the hook
  22771. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22772. ;; Hook not available, use advice
  22773. (defadvice bookmark-jump (after org-make-visible activate)
  22774. "Make the position visible."
  22775. (org-bookmark-jump-unhide))))
  22776. ;; Make sure saveplace shows the location if it was hidden
  22777. (eval-after-load "saveplace"
  22778. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22779. "Make the position visible."
  22780. (org-bookmark-jump-unhide)))
  22781. ;; Make sure ecb shows the location if it was hidden
  22782. (eval-after-load "ecb"
  22783. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22784. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22785. (if (derived-mode-p 'org-mode)
  22786. (org-show-context))))
  22787. (defun org-bookmark-jump-unhide ()
  22788. "Unhide the current position, to show the bookmark location."
  22789. (and (derived-mode-p 'org-mode)
  22790. (or (outline-invisible-p)
  22791. (save-excursion (goto-char (max (point-min) (1- (point))))
  22792. (outline-invisible-p)))
  22793. (org-show-context 'bookmark-jump)))
  22794. (defun org-mark-jump-unhide ()
  22795. "Make the point visible with `org-show-context' after jumping to the mark."
  22796. (when (and (derived-mode-p 'org-mode)
  22797. (outline-invisible-p))
  22798. (org-show-context 'mark-goto)))
  22799. (eval-after-load "simple"
  22800. '(defadvice pop-to-mark-command (after org-make-visible activate)
  22801. "Make the point visible with `org-show-context'."
  22802. (org-mark-jump-unhide)))
  22803. (eval-after-load "simple"
  22804. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  22805. "Make the point visible with `org-show-context'."
  22806. (org-mark-jump-unhide)))
  22807. (eval-after-load "simple"
  22808. '(defadvice pop-global-mark (after org-make-visible activate)
  22809. "Make the point visible with `org-show-context'."
  22810. (org-mark-jump-unhide)))
  22811. ;; Make session.el ignore our circular variable
  22812. (defvar session-globals-exclude)
  22813. (eval-after-load "session"
  22814. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22815. ;;;; Finish up
  22816. (provide 'org)
  22817. (run-hooks 'org-load-hook)
  22818. ;;; org.el ends here